-
Notifications
You must be signed in to change notification settings - Fork 57
/
sync.py
executable file
·429 lines (389 loc) · 16.2 KB
/
sync.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/usr/bin/env python
# pylint: disable=missing-docstring,invalid-name
import warnings
import os
from os.path import dirname, join, realpath
import sys
import xml.etree.ElementTree as ET
import getpass
import argparse
import logging
import asyncio
import async_timeout
import aiohttp
import uvloop
logging.basicConfig(
level=logging.DEBUG,
format='%(levelname)7s: %(message)s',
stream=sys.stderr,
)
LOG = logging.getLogger('')
# The Jenkins file will contain a list of changes scripts and eas
# in $scripts and $eas.
# Use this variable to add a Slack emoji in front of each item if
# you use a post-build action for a Slack custom message
SLACK_EMOJI = ":white_check_mark: "
SUPPORTED_SCRIPT_EXTENSIONS = ('sh', 'py', 'pl', 'swift', 'rb')
SUPPORTED_EA_EXTENSIONS = ('sh', 'py', 'pl', 'swift', 'rb')
CATEGORIES = []
def check_for_changes():
"""Looks for files that were changed between the current commit and
the last commit so we don't upload everything on every run
--jenkins will utilize $GIT_PREVIOUS_COMMIT and $GIT_COMMIT
environmental variables
--update_all can be invoked to upload all scripts and
extension attributes
"""
# This line will work with the environmental variables in Jenkins
if args.jenkins:
git_changes = os.popen(
"git diff --name-only $GIT_PREVIOUS_COMMIT $GIT_COMMIT").read(
).split('\n')
# Compare the last two commits to determine the list of files that
# were changed
else:
git_commits = os.popen(
'git log -2 --pretty=oneline --pretty=format:"%h"').read().split(
'\n')
command = "git diff --name-only" + " " + \
git_commits[1] + " " + git_commits[0]
git_changes = os.popen(command).read().split('\n')
for i in git_changes:
if 'extension_attributes/' in i and i.split(
'/')[1] not in changed_ext_attrs:
changed_ext_attrs.append(i.split('/')[1])
for i in git_changes:
if 'scripts/' in i and i.split('/')[1] not in changed_scripts:
changed_scripts.append(i.split('/')[1])
def write_jenkins_file():
"""Write changed_ext_attrs and changed_scripts to jenkins file.
$eas will contains the changed extension attributes,
$scripts will contains the changed scripts
If there are no changes, the variable will be set to 'None'
"""
if changed_ext_attrs:
contents = "eas=" + "None"
else:
contents = "eas=" + SLACK_EMOJI + changed_ext_attrs[0] + '\\n' + '\\'
for changed_ext_attr in changed_ext_attrs[1:]:
contents = contents + '\n' + SLACK_EMOJI + \
changed_ext_attr + '\\n' + '\\'
if changed_scripts:
contents = contents.rstrip('\\') + '\n' + "scripts=" + "None"
else:
contents = contents.rstrip(
'\\'
) + '\n' + "scripts=" + SLACK_EMOJI + changed_scripts[0] + '\\n' + '\\'
for changed_script in changed_scripts[1:]:
contents = contents + '\n' + SLACK_EMOJI + \
changed_script + '\\n' + '\\'
with open('jenkins.properties', 'w') as f:
f.write(contents)
async def upload_extension_attributes(session, url, user, passwd, semaphore):
mypath = dirname(realpath(__file__))
if changed_ext_attrs and not args.update_all:
print('No Changes in Extension Attributes')
return
ext_attrs = [
f.name for f in os.scandir(join(mypath, 'extension_attributes'))
if f.is_dir() and f.name in changed_ext_attrs
]
if args.update_all:
print("Copying all extension attributes...")
ext_attrs = [
f.name for f in os.scandir(join(mypath, 'extension_attributes'))
if f.is_dir()
]
tasks = []
for ea in ext_attrs:
task = asyncio.ensure_future(
upload_extension_attribute(session, url, user, passwd, ea,
semaphore))
tasks.append(task)
await asyncio.gather(*tasks)
async def upload_extension_attribute(session, url, user, passwd, ext_attr,
semaphore):
mypath = dirname(realpath(__file__))
auth = aiohttp.BasicAuth(user, passwd)
headers = {'Accept': 'application/xml', 'Content-Type': 'application/xml'}
# Get the script files within the folder, we'll only use
# script_file[0] in case there are multiple files
script_file = [
f.name for f in os.scandir(join('extension_attributes', ext_attr))
if f.is_file() and f.name.split('.')[-1] in SUPPORTED_EA_EXTENSIONS
]
if script_file == []:
print('Warning: No script file found in extension_attributes/%s' %
ext_attr)
return # Need to skip if no script.
with open(
join(mypath, 'extension_attributes', ext_attr, script_file[0]),
'r') as f:
data = f.read()
async with semaphore:
with async_timeout.timeout(args.timeout):
template = await get_ea_template(session, url, user, passwd,
ext_attr)
async with session.get(
url + '/JSSResource/computerextensionattributes/name/' +
template.find('name').text,
auth=auth,
headers=headers) as resp:
template.find('input_type/script').text = data
if args.verbose:
print(ET.tostring(template))
print('response status initial get: ', resp.status)
if resp.status == 200:
put_url = url + '/JSSResource/computerextensionattributes/name/' + \
template.find('name').text
resp = await session.put(
put_url,
auth=auth,
data=ET.tostring(template),
headers=headers)
else:
post_url = url + '/JSSResource/computerextensionattributes/id/0'
resp = await session.post(
post_url,
auth=auth,
data=ET.tostring(template),
headers=headers)
if args.verbose:
print('response status: ', resp.status)
print('EA: ', ext_attr)
print('EA Name: ', template.find('name').text)
if resp.status in (201, 200):
print('Uploaded Extension Attribute: %s' % template.find('name').text)
else:
print('Error uploading script: %s' % template.find('name').text)
print('Error: %s' % resp.status)
return resp.status
async def get_ea_template(session, url, user, passwd, ext_attr):
auth = aiohttp.BasicAuth(user, passwd)
mypath = dirname(realpath(__file__))
xml_file = [
f.name for f in os.scandir(join('extension_attributes', ext_attr))
if f.is_file() and f.name.split('.')[-1] in 'xml'
]
try:
with open(
join(mypath, 'extension_attributes', ext_attr, xml_file[0]),
'r') as file:
template = ET.fromstring(file.read())
except IndexError:
with async_timeout.timeout(args.timeout):
headers = {
'Accept': 'application/xml',
'Content-Type': 'application/xml'
}
async with session.get(
url + '/JSSResource/computerextensionattributes/name/' +
ext_attr,
auth=auth,
headers=headers) as resp:
if resp.status == 200:
async with session.get(
url +
'/JSSResource/computerextensionattributes/name/' +
ext_attr,
auth=auth,
headers=headers) as response:
template = ET.fromstring(await response.text())
else:
template = ET.parse(join(mypath,
'templates/ea.xml')).getroot()
# name is mandatory, so we use the foldername if nothing is set in
# a template
if args.verbose:
print(ET.tostring(template))
if template.find('category') and template.find(
'category').text not in CATEGORIES:
ET.SubElement(template, 'category').text = 'None'
if args.verbose:
c = template.find('category').text
print(
f'''WARNING: Unable to find category {c} in the JSS,
setting to None'''
)
if template.find('name') is None:
ET.SubElement(template, 'name').text = ext_attr
elif not template.find('name').text or template.find(
'name').text is None:
template.find('name').text = ext_attr
return template
async def upload_scripts(session, url, user, passwd, semaphore):
mypath = dirname(realpath(__file__))
if changed_scripts and not args.update_all:
print('No Changes in Scripts')
scripts = [
f.name for f in os.scandir(join(mypath, 'scripts')) if f.is_dir()
and f.name in changed_scripts
]
if args.update_all:
print('Copying all scripts...')
scripts = [
f.name for f in os.scandir(join(mypath, 'scripts'))
if f.is_dir()
]
tasks = []
for script in scripts:
task = asyncio.ensure_future(
upload_script(session, url, user, passwd, script, semaphore))
tasks.append(task)
await asyncio.gather(*tasks)
async def upload_script(session, url, user, passwd, script, semaphore):
mypath = dirname(realpath(__file__))
auth = aiohttp.BasicAuth(user, passwd)
headers = {'Accept': 'application/xml', 'Content-Type': 'application/xml'}
script_file = [
f.name for f in os.scandir(join('scripts', script))
if f.is_file() and f.name.split('.')[-1] in SUPPORTED_SCRIPT_EXTENSIONS
]
if script_file == []:
print('Warning: No script file found in scripts/%s' % script)
return # Need to skip if no script.
with open(join(mypath, 'scripts', script, script_file[0]), 'r') as f:
data = f.read()
async with semaphore:
with async_timeout.timeout(args.timeout):
template = await get_script_template(session, url, user, passwd,
script)
async with session.get(
url + '/JSSResource/scripts/name/' +
template.find('name').text,
auth=auth,
headers=headers) as resp:
template.find('script_contents').text = data
if resp.status == 200:
put_url = url + '/JSSResource/scripts/name/' + \
template.find('name').text
resp = await session.put(
put_url,
auth=auth,
data=ET.tostring(template),
headers=headers)
else:
post_url = url + '/JSSResource/scripts/id/0'
resp = await session.post(
post_url,
auth=auth,
data=ET.tostring(template),
headers=headers)
if resp.status in (201, 200):
print('Uploaded script: %s' % template.find('name').text)
else:
print('Error uploading script: %s' % template.find('name').text)
print('Error: %s' % resp.status)
return resp.status
async def get_script_template(session, url, user, passwd, script):
auth = aiohttp.BasicAuth(user, passwd)
mypath = dirname(realpath(__file__))
xml_file = [
f.name for f in os.scandir(join('scripts', script))
if f.is_file() and f.name.split('.')[-1] in 'xml'
]
try:
with open(join(mypath, 'scripts', script, xml_file[0]), 'r') as file:
template = ET.fromstring(file.read())
except IndexError:
with async_timeout.timeout(args.timeout):
headers = {
'Accept': 'application/xml',
'Content-Type': 'application/xml'
}
async with session.get(
url + '/JSSResource/scripts/name/' + script,
auth=auth,
headers=headers) as resp:
if resp.status == 200:
async with session.get(
url + '/JSSResource/scripts/name/' + script,
auth=auth,
headers=headers) as response:
template = ET.fromstring(await response.text())
else:
template = ET.parse(join(
mypath, 'templates/script.xml')).getroot()
# name is mandatory, so we use the filename if nothing is set in a template
if args.verbose:
print(ET.tostring(template))
if template.find('category') is not None and template.find(
'category').text not in CATEGORIES:
c = template.find('category').text
template.remove(template.find('category'))
if args.verbose:
print(
f'''WARNING: Unable to find category "{c}" in the JSS,
setting to None'''
)
if template.find('name') is None:
ET.SubElement(template, 'name').text = script
elif not template.find('name').text or template.find(
'name').text is None:
template.find('name').text = script
return template
async def get_existing_categories(session, url, user, passwd, semaphore):
auth = aiohttp.BasicAuth(user, passwd)
headers = {
'Accept': 'application/xml',
'Content-Type': 'application/xml'
}
async with semaphore:
with async_timeout.timeout(args.timeout):
async with session.get(
url + '/JSSResource/categories',
auth=auth,
headers=headers) as resp:
if resp.status in (201, 200):
return [
c.find('name').text for c in [
e for e in ET.fromstring(await resp.text()).
findall('category')
]
]
return []
async def main():
# pylint: disable=global-statement
global CATEGORIES
semaphore = asyncio.BoundedSemaphore(args.limit)
async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(
connector=aiohttp.TCPConnector(
ssl=args.do_not_verify_ssl)) as session:
CATEGORIES = await get_existing_categories(
session, args.url, args.username, args.password, semaphore)
await upload_scripts(session, args.url, args.username,
args.password, semaphore)
await upload_extension_attributes(session, args.url, args.username,
args.password, semaphore)
if __name__ == '__main__':
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
parser = argparse.ArgumentParser(description='Sync repo with JamfPro')
parser.add_argument('--url')
parser.add_argument('--username')
parser.add_argument('--password')
parser.add_argument('--limit', type=int, default=25)
parser.add_argument('--timeout', type=int, default=60)
parser.add_argument('--verbose', action='store_true')
parser.add_argument('--do_not_verify_ssl', action='store_false')
parser.add_argument('--update_all', action='store_true')
parser.add_argument('--jenkins', action='store_true')
args = parser.parse_args()
changed_ext_attrs = []
changed_scripts = []
check_for_changes()
print('Changed Extention Attributes: ', changed_ext_attrs)
print('Changed Scripts: ', changed_scripts)
if args.jenkins:
write_jenkins_file()
# Ask for password if not supplied via command line args
if args.password:
password = args.password
else:
password = getpass.getpass()
loop = asyncio.get_event_loop()
if args.verbose:
loop.set_debug(True)
loop.slow_callback_duration = 0.001
warnings.simplefilter('always', ResourceWarning)
loop.run_until_complete(main())