forked from apple/ccs-caldavtester
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cdtdiagnose.py
executable file
·141 lines (117 loc) · 4.62 KB
/
cdtdiagnose.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python
#
##
# Copyright (c) 2013-2016 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
import os
import datetime
import shutil
import sys
import argparse
from subprocess import Popen, PIPE
server_root = "/Applications/Server.app/Contents/ServerRoot"
os.environ["PATH"] = "%s/usr/bin:%s" % (server_root, os.environ["PATH"])
library_root = "/Library/Server/Calendar and Contacts"
directory_node = "/LDAPv3/127.0.0.1"
def cmd(args, input=None, raiseOnFail=True):
if input:
p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
result = p.communicate(input)
else:
p = Popen(args, stdout=PIPE, stderr=PIPE, shell=True)
result = p.communicate()
if raiseOnFail and p.returncode:
raise RuntimeError(result[1])
return result[0], p.returncode
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Gather CalDAVTester diagnostics.',
)
parser.add_argument(
'-d', '--directory', action='store',
help='Destination directory for diagnostics archive'
)
args = parser.parse_args()
print "Running CDT diagnostics due to test failure."
log = []
def error(message, e):
log.append("CDT diagnostic: %s" % (message,))
log.append(str(e))
print "\n".join(log)
sys.exit(1)
now = datetime.datetime.now()
now = now.replace(microsecond=0)
dirname = "cdtdiagnose-%s" % (now.strftime("%Y%m%d-%H%M%S"),)
if args.directory is not None:
if not os.path.isdir(args.directory):
print "Specified target directory path is invalid, using default."
else:
dirname = os.path.join(args.directory, dirname)
print "Saving diagnostic archive to: {}".format(dirname,)
try:
os.mkdir(dirname)
except Exception as e:
error("Could not create archive directory: '%s'" % (dirname,), e)
# Copy CDT log file file
server_path = "cdt.txt"
archive_path = os.path.join(dirname, os.path.basename(server_path))
try:
shutil.copy(server_path, archive_path)
except Exception as e:
error("Could not copy cdt results file: '%s' to '%s'" % (server_path, archive_path,), e)
# Copy serverinfo file
server_path = "scripts/server/serverinfo-caldav.xml"
archive_path = os.path.join(dirname, os.path.basename(server_path))
try:
shutil.copy(server_path, archive_path)
except Exception as e:
error("Could not copy server info file: '%s' to '%s'" % (server_path, archive_path,), e)
# Get server logs
logs_path = os.path.join(library_root, "Logs")
archive_path = os.path.join(dirname, "logs")
try:
shutil.copytree(logs_path, archive_path)
except Exception as e:
error("Could not copy server logs: '%s' to '%s'" % (logs_path, archive_path,), e)
# Get server config files
server_path = os.path.join(server_root, "etc", "caldavd")
archive_path = os.path.join(dirname, "etc")
try:
shutil.copytree(server_path, archive_path)
except Exception as e:
error("Could not copy server conf: '%s' to '%s'" % (server_path, archive_path,), e)
server_path = library_root
archive_path = os.path.join(dirname, "Library")
try:
shutil.copytree(server_path, archive_path)
except Exception as e:
error("Could not copy library items: '%s' to '%s'" % (server_path, archive_path,), e)
# Dump OD data
try:
results = ["*** Users"]
results.extend(cmd("dscl %s -readall Users" % (directory_node,))[0].splitlines())
results.append("\n\n*** Groups")
results.extend(cmd("dscl %s -readall Groups" % (directory_node,))[0].splitlines())
results.append("")
with open(os.path.join(dirname, "dscl_dump.txt"), "w") as f:
f.write("\n".join(results))
except Exception as e:
error("Could not dump OD data.", e)
# Now archive the diagnostics data
try:
archive_name = shutil.make_archive(dirname, "gztar", dirname)
except Exception as e:
error("Could not make diagnostics archive.", e)
print "Saved diagnostics to '%s'" % (archive_name,)