-
Notifications
You must be signed in to change notification settings - Fork 346
/
munkiimport
executable file
·613 lines (542 loc) · 24.6 KB
/
munkiimport
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#!/usr/bin/python
# encoding: utf-8
#
# Copyright 2010-2018 Greg Neagle.
#
# 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
#
# https://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.
"""
munkiimport
Created by Greg Neagle on 2010-09-29.
Assists with importing installer items into the munki repo
"""
# std lib imports
import optparse
import os
import subprocess
import sys
import tempfile
# our lib imports
from munkilib.cliutils import ConfigurationSaveError
from munkilib.cliutils import configure as _configure
from munkilib.cliutils import pref, path2url
from munkilib.cliutils import raw_input_with_default
from munkilib import info
from munkilib import dmgutils
from munkilib import munkihash
from munkilib import munkirepo
from munkilib import osutils
from munkilib import pkgutils
from munkilib import FoundationPlist
from munkilib.admin import makecatalogslib
from munkilib.admin import munkiimportlib
from munkilib.admin import pkginfolib
def make_dmg(pkgpath):
"""Wraps a non-flat package into a disk image.
Returns path to newly-created disk image."""
pkgname = os.path.basename(pkgpath)
print 'Making disk image containing %s...' % pkgname
diskimagename = os.path.splitext(pkgname)[0] + '.dmg'
diskimagepath = os.path.join(osutils.tmpdir(), diskimagename)
cmd = ['/usr/bin/hdiutil', 'create', '-fs', 'HFS+',
'-srcfolder', pkgpath, diskimagepath]
proc = subprocess.Popen(cmd, shell=False, bufsize=-1,
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
while True:
output = proc.stdout.readline()
if not output and (proc.poll() != None):
break
line = output.rstrip('\n').decode('UTF-8')
if len(line) > 0:
print line
sys.stdout.flush()
retcode = proc.poll()
if retcode:
print >> sys.stderr, 'Disk image creation failed.'
return ''
else:
print 'Disk image created at: %s' % diskimagepath
return diskimagepath
def edit_pkginfo_in_editor(pkginfo):
"""Opens pkginfo list in the user's chosen editor."""
editor = pref('editor')
if editor:
answer = raw_input('Edit pkginfo before upload? [y/n]: ')
if answer.lower().startswith('y'):
filedesc, filepath = tempfile.mkstemp()
# we just want the path; close the file descriptor
os.close(filedesc)
try:
FoundationPlist.writePlist(pkginfo, filepath)
except FoundationPlist.FoundationPlistException, err:
print >> sys.stderr, (
u'Could not save pkginfo to temp file: %s'
% unicode(err))
return pkginfo
if editor.endswith('.app'):
cmd = ['/usr/bin/open', '-a', editor, filepath]
else:
cmd = [editor, filepath]
try:
dummy_returncode = subprocess.check_call(cmd)
except (OSError, subprocess.CalledProcessError), err:
print >> sys.stderr, (
'Problem running editor %s: %s.' % (editor, err))
os.remove(filepath)
return pkginfo
else:
if editor.endswith('.app'):
# wait for user to finish with GUI editor.
answer = 'no'
while not answer.lower().startswith('y'):
answer = raw_input('Pkginfo editing complete? [y/n]: ')
try:
edited_pkginfo = FoundationPlist.readPlist(filepath)
except FoundationPlist.FoundationPlistException, err:
print >> sys.stderr, (
u'Problem reading edited pkginfo: %s' % unicode(err))
os.remove(filepath)
return pkginfo
os.remove(filepath)
return edited_pkginfo
return pkginfo
def prompt_for_subdirectory(repo, subdirectory):
"""Prompts the user for a subdirectory for the pkg and pkginfo"""
try:
pkgsinfo_list = munkiimportlib.list_items_of_kind(repo, 'pkgsinfo')
except munkirepo.RepoError, err:
raise munkiimportlib.RepoCopyError(
u'Unable to get list of current pkgsinfo: %s' % unicode(err))
# filter the list of pkgsinfo to a list of subdirectories
subdir_set = set()
for item in pkgsinfo_list:
subdir_set.add(os.path.dirname(item))
while True:
newdir = raw_input(
'Upload item to subdirectory path [%s]: ' % subdirectory)
if newdir:
destination_path = os.path.join('pkgsinfo', newdir)
if destination_path not in subdir_set:
answer = raw_input('Path %s doesn\'t exist. Create it? [y/n] '
% destination_path)
if answer.lower().startswith('y'):
break
else:
break
else:
return subdirectory
return newdir
def print_fn(text):
'''Wraps print in a function for make_catalogs'''
print text
def make_catalogs(repo, options):
"""Rebuild our catalogs"""
if not options.verbose:
print 'Rebuilding catalogs at %s...' % options.repo_url
output_fn = None
else:
output_fn = print_fn
errors = makecatalogslib.makecatalogs(repo, {}, output_fn=output_fn)
if errors:
print '\nThe following issues occurred while building catalogs:\n'
for error in errors:
print error
def cleanup_and_exit(exitcode):
"""Unmounts the repo if we mounted it, then exits"""
result = 0
# TODO: reimplement this
#if repo and repo.mounted and repo.WE_MOUNTED_THE_REPO:
# if not NOINTERACTIVE:
# answer = raw_input('Unmount the repo fileshare? [y/n] ')
# if answer.lower().startswith('y'):
# result = repo.unmount()
# else:
# result = repo.unmount()
# clean up tmpdir
osutils.cleanUpTmpDir()
exit(exitcode or result)
def configure():
"""Configures munkiimport for use"""
prompt_list = [
#('repo_path', 'Path to munki repo (example: /Volumes/repo)'),
('repo_url', 'Repo URL (example: afp://munki.example.com/repo)'),
('pkginfo_extension', 'pkginfo extension (Example: .plist)'),
('editor', 'pkginfo editor (examples: /usr/bin/vi or TextMate.app; '
'leave empty to not open an editor after import)'),
('default_catalog', 'Default catalog to use (example: testing)'),
('plugin', 'Repo access plugin (defaults to FileRepo)')
]
try:
_configure(prompt_list)
except ConfigurationSaveError:
pass
def main():
"""Main routine"""
usage = """usage: %prog [options] /path/to/installer_item
Imports an installer item into a munki repo.
Installer item can be a pkg, mpkg, dmg, mobileconfig, or app.
Bundle-style pkgs and apps are wrapped in a dmg file before upload.
Example:
munkiimport --subdirectory apps /path/to/installer_item
"""
parser = optparse.OptionParser(usage=usage)
parser.add_option('--configure', action='store_true',
help='Configure munkiimport with details about your '
'munki repo, preferred editor, and the like. Any '
'other options and arguments are ignored.')
parser.add_option('--subdirectory', default='',
help='When importing an installer item, item will be '
'uploaded to this subdirectory path in the repo '
'pkgs directory, and the pkginfo file will be '
'stored under this subdirectory under the pkgsinfo '
'directory.')
parser.add_option('--nointeractive', '-n', action='store_true',
help='No interactive prompts.')
parser.add_option('--repo_url', '--repo-url', default=pref('repo_url'),
help='Optional repo URL. If specified, overrides any '
'repo_url specified via --configure.')
parser.add_option('--plugin', default=pref('plugin'),
help='Optional plugin to connect to repo. If specified, '
'overrides any plugin specified via --configure.')
parser.add_option('--icon_path', '--icon-path', default='', type='string',
help='Path to an icns file for the package. Will be '
'converted to PNG and uploaded, and will overwrite '
'any existing PNG.')
parser.add_option('--extract_icon', '--extract-icon', action='store_true',
help='Attempt to extract and upload a product icon from '
'the installer item')
parser.add_option('--version', '-V', action='store_true',
help='Print the version of the munki tools and exit.')
parser.add_option('--verbose', '-v', action='store_true',
help='Print more output.')
# add a header for Pkginfo Options
pkginfo_options = optparse.OptionGroup(
parser, 'Pkginfo Options',
'The following options are shared with makepkginfo and affect the '
'generated pkginfo data.')
parser.add_option_group(pkginfo_options)
# now add all the shared makepkginfo options
pkginfolib.add_option_groups(parser)
sys.argv = [unicode(item, 'utf-8') for item in sys.argv]
options, arguments = parser.parse_args()
# there are a lot of valid combinations of option flags and arguments
# but if there are no option flags or arguments we should print usage
if len(sys.argv) == 1:
parser.print_usage()
exit(0)
if options.version:
print info.get_version()
exit(0)
if options.configure:
configure()
exit(0)
if not options.repo_url:
repo_path = pref('repo_path')
if repo_path:
options.repo_url = path2url(repo_path)
if not options.repo_url:
print >> sys.stderr, ('No repo URL found. Please run this tool with '
'the --configure option, or use the --repo-url '
'option.')
parser.print_help()
exit(-1)
if not options.plugin:
options.plugin = 'FileRepo'
if options.icon_path and not os.path.isfile(options.icon_path):
print >> sys.stderr, ('The specified icon file does not exist.')
exit(-1)
if (options.apple_update and len(arguments) > 0) or len(arguments) > 1:
parser.print_usage()
exit(0)
installer_item = None
if len(arguments):
installer_item = arguments[0]
uninstaller_item = options.uninstalleritem
is_applemetadata = options.apple_update
if not installer_item and not is_applemetadata:
cleanup_and_exit(-1)
if not is_applemetadata:
# Strip trailing '/' from installer_item
installer_item = installer_item.rstrip('/')
# Check if the item is a mount point for a disk image
if dmgutils.pathIsVolumeMountPoint(installer_item):
# Get the disk image path for the mount point
# and use that instead of the original item
installer_item = dmgutils.diskImageForMountPoint(installer_item)
if (not pkgutils.hasValidInstallerItemExt(installer_item) and
not pkgutils.isApplication(installer_item)):
print >> sys.stderr, (
'Unknown installer item type: "%s"' % installer_item)
exit(-1)
if not os.path.exists(installer_item):
print >> sys.stderr, '%s does not exist!' % installer_item
exit(-1)
try:
repo = munkirepo.connect(options.repo_url, options.plugin)
except munkirepo.RepoError, err:
print >> sys.stderr, (u'Could not connect to munki repo: %s'
% unicode(err))
exit(-1)
if not is_applemetadata:
if os.path.isdir(installer_item):
if pkgutils.hasValidDiskImageExt(installer_item):
# a directory named foo.dmg or foo.iso!
print >> sys.stderr, '%s is an unknown type.' % installer_item
cleanup_and_exit(-1)
else:
# we need to convert to dmg
dmg_path = make_dmg(installer_item)
if dmg_path:
installer_item = dmg_path
else:
print >> sys.stderr, (
'Could not convert %s to a disk image.'
% installer_item)
cleanup_and_exit(-1)
if uninstaller_item:
# Strip trailing '/' from uninstaller_item
uninstaller_item = uninstaller_item.rstrip('/')
if os.path.isdir(uninstaller_item):
if pkgutils.hasValidDiskImageExt(uninstaller_item):
# a directory named foo.dmg or foo.iso!
print >> sys.stderr, (
'%s is an unknown type.' % uninstaller_item)
cleanup_and_exit(-1)
else:
# we need to convert to dmg
dmg_path = make_dmg(uninstaller_item)
if dmg_path:
uninstaller_item = dmg_path
else:
print >> sys.stderr, (
'Could not convert %s to a disk image.'
% uninstaller_item)
cleanup_and_exit(-1)
options.uninstalleritem = uninstaller_item
# if catalog/catalogs have not been explictly specified via command-line,
# add our default catalog
if not options.catalog:
default_catalog = pref('default_catalog') or 'testing'
options.catalog = [default_catalog]
# make a pkginfo!
try:
pkginfo = pkginfolib.makepkginfo(installer_item, options)
except pkginfolib.PkgInfoGenerationError, err:
# makepkginfo returned an error
print >> sys.stderr, 'Getting package info failed.'
print >> sys.stderr, err
cleanup_and_exit(-1)
if not options.nointeractive:
# try to find existing pkginfo items that match this one
matchingpkginfo = munkiimportlib.find_matching_pkginfo(repo, pkginfo)
exactmatch = False
if matchingpkginfo:
if ('installer_item_hash' in matchingpkginfo and
matchingpkginfo['installer_item_hash'] ==
pkginfo.get('installer_item_hash')):
exactmatch = True
print ('***This item is identical to an existing item in '
'the repo***:')
else:
print 'This item is similar to an existing item in the repo:'
fields = (('Item name', 'name'),
('Display name', 'display_name'),
('Description', 'description'),
('Version', 'version'),
('Installer item path', 'installer_item_location'))
for (name, key) in fields:
print '%21s: %s' % (
name, matchingpkginfo.get(key, '').encode('UTF-8'))
print
if exactmatch:
answer = raw_input('Import this item anyway? [y/n] ')
if not answer.lower().startswith('y'):
cleanup_and_exit(0)
answer = raw_input('Use existing item as a template? [y/n] ')
if answer.lower().startswith('y'):
pkginfo['name'] = matchingpkginfo['name']
pkginfo['display_name'] = (
matchingpkginfo.get('display_name') or
pkginfo.get('display_name') or
matchingpkginfo['name'])
pkginfo['description'] = pkginfo.get('description') or \
matchingpkginfo.get('description', '')
if (options.subdirectory == '' and
matchingpkginfo.get('installer_item_location')):
options.subdirectory = os.path.dirname(
matchingpkginfo['installer_item_location'])
for key in ['blocking_applications',
'forced_install',
'forced_uninstall',
'unattended_install',
'unattended_uninstall',
'requires',
'update_for',
'category',
'developer',
'icon_name',
'unused_software_removal_info',
'localized_strings']:
if key in matchingpkginfo:
print 'Copying %s: %s' % (key, matchingpkginfo[key])
pkginfo[key] = matchingpkginfo[key]
# now let user do some basic editing
editfields = (('Item name', 'name', 'str'),
('Display name', 'display_name', 'str'),
('Description', 'description', 'str'),
('Version', 'version', 'str'),
('Category', 'category', 'str'),
('Developer', 'developer', 'str'),
('Unattended install', 'unattended_install', 'bool'),
('Unattended uninstall', 'unattended_uninstall', 'bool'),
)
for (name, key, kind) in editfields:
prompt = '%20s: ' % name
if kind == 'bool':
default = str(pkginfo.get(key, False))
else:
default = pkginfo.get(key, '').encode('UTF-8')
pkginfo[key] = raw_input_with_default(prompt, default)
if kind == 'bool':
value = pkginfo[key].lower().strip()
pkginfo[key] = value.startswith(('y', 't'))
# special handling for catalogs array
prompt = '%20s: ' % 'Catalogs'
default = ', '.join(pkginfo['catalogs'])
newvalue = raw_input_with_default(prompt, default)
pkginfo['catalogs'] = [item.strip()
for item in newvalue.split(',')]
if (not is_applemetadata and
not pkginfo.get(
'installer_type') in ['profile', 'startosinstall']):
if 'receipts' not in pkginfo and 'installs' not in pkginfo:
print >> sys.stderr, ('WARNING: There are no receipts and no '
'\'installs\' items for this installer '
'item. You will need to add at least '
'one item to the \'installs\' list.')
print
#for (name, key, kind) in editfields:
# if kind == 'bool':
# print '%20s: %s' % (name, pkginfo.get(key, False))
# else:
# print '%20s: %s' % (name, pkginfo.get(key, '').encode('UTF-8'))
#print '%20s: %s' % (
# 'Catalogs', ', '.join(pkginfo['catalogs']).encode('UTF-8'))
#print
answer = raw_input('Import this item? [y/n] ')
if not answer.lower().startswith('y'):
cleanup_and_exit(0)
if options.subdirectory == '':
if (not is_applemetadata and
isinstance(repo, munkirepo.FileRepo.FileRepo)):
repo_pkgs_path = os.path.join(repo.root, 'pkgs')
installer_item_abspath = os.path.abspath(installer_item)
if installer_item_abspath.startswith(repo_pkgs_path):
# special case: We're using a file repo and the item being
# "imported" is actually already in the repo -- we're just
# creating a pkginfo item and copying it to the repo.
# In this case, we want to use the same subdirectory for
# the pkginfo that corresponds to the one the pkg is
# already in.
# We aren't handling the case of alternate implementations
# FileRepo.
installer_item_dirpath = os.path.dirname(
installer_item_abspath)
options.subdirectory = installer_item_dirpath[
len(repo_pkgs_path)+1:]
options.subdirectory = prompt_for_subdirectory(
repo, options.subdirectory)
# if we have an icon, upload it
if options.icon_path:
try:
munkiimportlib.convert_and_install_icon(repo, pkginfo,
options.icon_path)
except munkiimportlib.RepoCopyError, err:
print >> sys.stderr, err
elif options.extract_icon:
pass
elif (not options.nointeractive and
not munkiimportlib.icon_exists_in_repo(repo, pkginfo) and
not is_applemetadata and
not pkginfo.get('installer_type') == 'profile'):
print 'No existing product icon found.'
answer = raw_input('Attempt to create a product icon? [y/n] ')
if answer.lower().startswith('y'):
print 'Attempting to extract and upload icon...'
options.extract_icon = True
if options.extract_icon:
try:
imported_paths = munkiimportlib.extract_and_copy_icon(
repo, installer_item, pkginfo)
if imported_paths:
print 'Imported %s.' % imported_paths
else:
print 'No icons found for import.'
except munkiimportlib.RepoCopyError, err:
print >> sys.stderr, err
# fix in case user accidentally starts subdirectory with a slash
if options.subdirectory.startswith('/'):
options.subdirectory = options.subdirectory[1:]
if not is_applemetadata:
try:
print 'Copying %s to repo...' % os.path.basename(installer_item)
uploaded_pkgpath = munkiimportlib.copy_item_to_repo(
repo, installer_item, pkginfo.get('version'),
options.subdirectory)
print 'Copied %s to %s.' % (os.path.basename(installer_item),
uploaded_pkgpath)
except munkiimportlib.RepoCopyError, errmsg:
print >> sys.stderr, errmsg
cleanup_and_exit(-1)
# adjust the installer_item_location to match
# the actual location and name
pkginfo['installer_item_location'] = uploaded_pkgpath.partition('/')[2]
if uninstaller_item:
try:
print 'Copying %s to repo...' % os.path.basename(
uninstaller_item)
uploaded_pkgpath = munkiimportlib.copy_item_to_repo(
repo, uninstaller_item, pkginfo.get('version'),
options.subdirectory)
print 'Copied %s to %s.' % (
os.path.basename(uninstaller_item), uploaded_pkgpath)
except munkiimportlib.RepoCopyError, errmsg:
print >> sys.stderr, errmsg
cleanup_and_exit(-1)
# adjust the uninstaller_item_location to match
# the actual location and name; update size and hash
pkginfo['uninstaller_item_location'] = (
uploaded_pkgpath.partition('/')[2])
itemsize = int(os.path.getsize(uninstaller_item))
itemhash = munkihash.getsha256hash(uninstaller_item)
pkginfo['uninstaller_item_size'] = int(itemsize/1024)
pkginfo['uninstaller_item_hash'] = itemhash
# add icon to pkginfo if in repository
munkiimportlib.add_icon_hash_to_pkginfo(pkginfo)
# installer_item upload was successful. so upload pkginfo to repo
if not options.nointeractive:
# possibly edit the pkginfo file in the user's editor
pkginfo = edit_pkginfo_in_editor(pkginfo)
try:
pkginfo_path = munkiimportlib.copy_pkginfo_to_repo(repo, pkginfo, options.subdirectory)
print 'Saved pkginfo to %s.' % pkginfo_path
except munkiimportlib.RepoCopyError, errmsg:
print >> sys.stderr, errmsg
cleanup_and_exit(-1)
if not options.nointeractive:
answer = raw_input('Rebuild catalogs? [y/n] ')
if answer.lower().startswith('y'):
make_catalogs(repo, options)
cleanup_and_exit(0)
if __name__ == '__main__':
main()