-
Notifications
You must be signed in to change notification settings - Fork 339
/
munkiimport
executable file
·1128 lines (977 loc) · 43.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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# encoding: utf-8
#
# Copyright 2010-2016 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
"""
import sys
import os
#import readline
import subprocess
import time
from optparse import OptionParser, BadOptionError, AmbiguousOptionError
from munkilib import iconutils
from munkilib import munkicommon
from munkilib import FoundationPlist
class PassThroughOptionParser(OptionParser):
"""
An unknown option pass-through implementation of OptionParser.
When unknown arguments are encountered, bundle with largs and try again,
until rargs is depleted.
sys.exit(status) will still be called if a known argument is passed
incorrectly (e.g. missing arguments or bad argument types, etc.)
"""
def _process_args(self, largs, rargs, values):
while rargs:
try:
OptionParser._process_args(self, largs, rargs, values)
except (BadOptionError, AmbiguousOptionError), err:
largs.append(err.opt_str)
def format_epilog(self, formatter):
if not self.epilog:
self.epilog = ""
return self.epilog
def raw_input_with_default(prompt='', default=''):
'''Get input from user with a prompt and a suggested default value'''
if default:
prompt = '%s [%s]: ' % (prompt, default)
return raw_input(prompt).decode('UTF-8') or default.decode('UTF-8')
else:
# no default value, just call raw_input
return raw_input(prompt + ": ").decode('UTF-8')
def makeDMG(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(munkicommon.tmpdir(), diskimagename)
cmd = ['/usr/bin/hdiutil', 'create', '-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
print output.rstrip('\n').encode('UTF-8')
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 repoAvailable():
"""Checks the repo path for proper directory structure.
If the directories look wrong we probably don't have a
valid repo path. Returns True if things look OK."""
if not REPO_PATH:
print >> sys.stderr, 'No repo path specified.'
return False
if not os.path.exists(REPO_PATH):
mountRepoCLI()
if not os.path.exists(REPO_PATH):
return False
for subdir in ['catalogs', 'manifests', 'pkgs', 'pkgsinfo']:
if not os.path.exists(os.path.join(REPO_PATH, subdir)):
print >> sys.stderr, "%s is missing %s" % (REPO_PATH, subdir)
return False
# if we get this far, the repo path looks OK
return True
def mountRepoGUI():
"""Attempts to connect to the repo fileshare
Returns nothing whether we succeed or fail"""
if not REPO_PATH or not REPO_URL:
return
print 'Attempting to connect to munki repo...'
cmd = ['/usr/bin/open', REPO_URL]
dummy_retcode = subprocess.call(cmd)
for dummy_i in range(60):
# wait up to 60 seconds to connect to repo
if os.path.exists(REPO_PATH):
break
time.sleep(1)
def mountRepoCLI():
"""Attempts to connect to the repo fileshare"""
global WE_MOUNTED_THE_REPO
if os.path.exists(REPO_PATH):
return
os.mkdir(REPO_PATH)
print 'Attempting to mount fileshare %s:' % REPO_URL
if REPO_URL.startswith('afp:'):
cmd = ['/sbin/mount_afp', '-i', REPO_URL, REPO_PATH]
elif REPO_URL.startswith('smb:'):
cmd = ['/sbin/mount_smbfs', REPO_URL[4:], REPO_PATH]
elif REPO_URL.startswith('nfs://'):
cmd = ['/sbin/mount_nfs', REPO_URL[6:], REPO_PATH]
else:
print >> sys.stderr, 'Unsupported filesystem URL!'
return
retcode = subprocess.call(cmd)
if retcode:
os.rmdir(REPO_PATH)
else:
WE_MOUNTED_THE_REPO = True
def unmountRepoCLI():
"""Attempts to unmount the repo fileshare"""
if not os.path.exists(REPO_PATH):
return
cmd = ['/sbin/umount', REPO_PATH]
return subprocess.call(cmd)
class RepoCopyError(Exception):
"""Error copying installer item to repo"""
pass
def copyItemToRepo(itempath, vers, subdirectory=''):
"""Copies an item to the appropriate place in the repo.
If itempath is a path within the repo/pkgs directory, copies nothing.
Renames the item if an item already exists with that name.
Returns the relative path to the item."""
if not os.path.exists(REPO_PATH):
raise RepoCopyError('Could not connect to munki repo.')
destination_path = os.path.join(REPO_PATH, 'pkgs', subdirectory)
if not os.path.exists(destination_path):
try:
os.makedirs(destination_path)
except OSError, errmsg:
raise RepoCopyError('Could not create %s: %s'
% (destination_path, errmsg))
item_name = os.path.basename(itempath)
destination_path_name = os.path.join(destination_path, item_name)
if itempath == destination_path_name:
# we've been asked to 'import' a repo item.
# just return the relative path
return os.path.join(subdirectory, item_name)
if vers:
name, ext = os.path.splitext(item_name)
if not name.endswith(vers):
# add the version number to the end of the filename
item_name = '%s-%s%s' % (name, vers, ext)
destination_path_name = os.path.join(destination_path, item_name)
index = 0
name, ext = os.path.splitext(item_name)
while os.path.exists(destination_path_name):
print 'File %s already exists...' % destination_path_name
# try appending numbers until we have a unique name
index += 1
item_name = '%s__%s%s' % (name, index, ext)
destination_path_name = os.path.join(destination_path, item_name)
print 'Copying %s to %s...' % (os.path.basename(itempath),
destination_path_name)
cmd = ['/bin/cp', itempath, destination_path_name]
retcode = subprocess.call(cmd)
if retcode:
raise RepoCopyError('Unable to copy %s to %s'
% (itempath, destination_path_name))
else:
return os.path.join(subdirectory, item_name)
def getIconPath(pkginfo):
"""Return path for icon"""
icon_name = pkginfo.get('icon_name') or pkginfo['name']
if not os.path.splitext(icon_name)[1]:
icon_name += u'.png'
return os.path.join(REPO_PATH, u'icons', icon_name)
def iconExistsInRepo(pkginfo):
"""Returns True if there is an icon for this item in the repo"""
icon_path = getIconPath(pkginfo)
if os.path.exists(icon_path):
return True
return False
def addIconHashToPkginfo(pkginfo):
"""Adds the icon hash tp pkginfo if the icon exists in repo"""
icon_path = getIconPath(pkginfo)
if os.path.isfile(icon_path):
pkginfo['icon_hash'] = munkicommon.getsha256hash(icon_path)
def generate_png_from_copy_from_dmg_item(dmg_path, pkginfo):
'''Generates a product icon from a copy_from_dmg item
and uploads to the repo'''
mountpoints = munkicommon.mountdmg(dmg_path)
if mountpoints:
mountpoint = mountpoints[0]
apps = [item for item in pkginfo.get('items_to_copy', [])
if item.get('source_item', '').endswith('.app')]
if len(apps):
app_path = os.path.join(mountpoint, apps[0]['source_item'])
icon_path = iconutils.findIconForApp(app_path)
if icon_path:
convert_and_install_icon(pkginfo, icon_path)
else:
print 'No application icons found.'
else:
print 'No application icons found.'
munkicommon.unmountdmg(mountpoint)
def generate_pngs_from_installer_pkg(item_path, pkginfo):
'''Generates a product icon (or candidate icons) from
an installer pkg and uploads to the repo'''
icon_paths = []
mountpoint = None
pkg_path = None
if munkicommon.hasValidDiskImageExt(item_path):
dmg_path = item_path
mountpoints = munkicommon.mountdmg(dmg_path)
if mountpoints:
mountpoint = mountpoints[0]
if pkginfo.get('package_path'):
pkg_path = os.path.join(mountpoint, pkginfo['package_path'])
else:
# find first item that appears to be a pkg at the root
for fileitem in munkicommon.listdir(mountpoints[0]):
if munkicommon.hasValidPackageExt(fileitem):
pkg_path = os.path.join(mountpoint, fileitem)
break
elif munkicommon.hasValidPackageExt(item_path):
pkg_path = item_path
if pkg_path:
if os.path.isdir(pkg_path):
icon_paths = iconutils.extractAppIconsFromBundlePkg(pkg_path)
else:
icon_paths = iconutils.extractAppIconsFromFlatPkg(pkg_path)
if mountpoint:
munkicommon.unmountdmg(mountpoint)
if len(icon_paths) == 1:
convert_and_install_icon(pkginfo, icon_paths[0])
elif len(icon_paths) > 1:
index = 1
for icon_path in icon_paths:
convert_and_install_icon(pkginfo, icon_path, index=index)
index += 1
else:
print 'No application icons found.'
def convert_and_install_icon(pkginfo, icon_path, index=None):
destination_path = os.path.join(REPO_PATH, 'icons')
if not os.path.exists(destination_path):
try:
os.makedirs(destination_path)
except OSError, errmsg:
print >> sys.stderr, ('Could not create %s: %s' %
(destination_path, errmsg))
if index is not None:
destination_name = pkginfo['name'] + '_' + str(index)
else:
destination_name = pkginfo['name']
png_path = os.path.join(
destination_path, destination_name + u'.png')
result = iconutils.convertIconToPNG(icon_path, png_path)
if result:
print 'Created icon: %s' % png_path
else:
print >> sys.stderr, u'Error converting %s to png.' % icon_path
def copyIconToRepo(iconpath):
"""Saves a product icon to the repo"""
destination_path = os.path.join(REPO_PATH, 'icons')
if not os.path.exists(destination_path):
try:
os.makedirs(destination_path)
except OSError, errmsg:
raise RepoCopyError('Could not create %s: %s'
% (destination_path, errmsg))
icon_name = os.path.basename(iconpath)
destination_path_name = os.path.join(destination_path, icon_name)
if os.path.exists(destination_path_name):
# remove any existing icon in the repo
try:
os.unlink(destination_path_name)
except OSError, errmsg:
raise RepoCopyError('Could not remove existing %s'
% (destination_path_name))
print 'Copying %s to %s...' % (icon_name, destination_path_name)
cmd = ['/bin/cp', iconpath, destination_path_name]
retcode = subprocess.call(cmd)
if retcode:
raise RepoCopyError('Unable to copy %s to %s'
% (iconpath, destination_path_name))
def copyPkginfoToRepo(pkginfo, subdirectory=''):
"""Saves pkginfo to munki_repo_path/pkgsinfo/subdirectory"""
# less error checking because we copy the installer_item
# first and bail if it fails...
destination_path = os.path.join(REPO_PATH, 'pkgsinfo', subdirectory)
if not os.path.exists(destination_path):
try:
os.makedirs(destination_path)
except OSError, errmsg:
raise RepoCopyError('Could not create %s: %s'
% (destination_path, errmsg))
pkginfo_ext = pref('pkginfo_extension') or ''
if pkginfo_ext and not pkginfo_ext.startswith('.'):
pkginfo_ext = '.' + pkginfo_ext
pkginfo_name = '%s-%s%s' % (pkginfo['name'], pkginfo['version'],
pkginfo_ext)
pkginfo_path = os.path.join(destination_path, pkginfo_name)
index = 0
while os.path.exists(pkginfo_path):
index += 1
pkginfo_name = '%s-%s__%s%s' % (pkginfo['name'], pkginfo['version'],
index, pkginfo_ext)
pkginfo_path = os.path.join(destination_path, pkginfo_name)
print 'Saving pkginfo to %s...' % pkginfo_path
try:
FoundationPlist.writePlist(pkginfo, pkginfo_path)
except FoundationPlist.NSPropertyListWriteException, errmsg:
raise RepoCopyError(errmsg)
return pkginfo_path
def openPkginfoInEditor(pkginfo_path):
"""Opens pkginfo list in the user's chosen editor."""
editor = pref('editor')
if editor:
if editor.endswith('.app'):
cmd = ['/usr/bin/open', '-a', editor, pkginfo_path]
else:
cmd = [editor, pkginfo_path]
try:
dummy_returncode = subprocess.check_call(cmd)
except (OSError, subprocess.CalledProcessError), err:
print >> sys.stderr, (
'Problem running editor %s: %s.' % (editor, err))
def promptForSubdirectory(subdirectory):
"""Prompts the user for a subdirectory for the pkg and pkginfo"""
while True:
newdir = raw_input(
'Upload item to subdirectory path [%s]: ' % subdirectory)
if newdir:
if not repoAvailable():
raise RepoCopyError('Could not connect to munki repo.')
if APPLEMETADATA:
destination_path = os.path.join(REPO_PATH, 'pkgsinfo', newdir)
else:
destination_path = os.path.join(REPO_PATH, 'pkgs', newdir)
if not os.path.exists(destination_path):
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
class CatalogDBException(Exception):
'''Exception to throw if we can't make a pkginfo DB'''
pass
def makeCatalogDB():
"""Returns a dict we can use like a database"""
all_items_path = os.path.join(REPO_PATH, 'catalogs', 'all')
if not os.path.exists(all_items_path):
raise CatalogDBException
try:
catalogitems = FoundationPlist.readPlist(all_items_path)
except FoundationPlist.NSPropertyListSerializationException:
raise CatalogDBException
pkgid_table = {}
app_table = {}
installer_item_table = {}
hash_table = {}
profile_table = {}
itemindex = -1
for item in catalogitems:
itemindex = itemindex + 1
name = item.get('name', 'NO NAME')
vers = item.get('version', 'NO VERSION')
if name == 'NO NAME' or vers == 'NO VERSION':
munkicommon.display_warning('Bad pkginfo: %s' % item)
# add to hash table
if 'installer_item_hash' in item:
if not item['installer_item_hash'] in hash_table:
hash_table[item['installer_item_hash']] = []
hash_table[item['installer_item_hash']].append(itemindex)
# add to installer item table
if 'installer_item_location' in item:
installer_item_name = os.path.basename(
item['installer_item_location'])
(name, ext) = os.path.splitext(installer_item_name)
if '-' in name:
(name, vers) = munkicommon.nameAndVersion(name)
installer_item_name = name + ext
if not installer_item_name in installer_item_table:
installer_item_table[installer_item_name] = {}
if not vers in installer_item_table[installer_item_name]:
installer_item_table[installer_item_name][vers] = []
installer_item_table[installer_item_name][vers].append(itemindex)
# add to table of receipts
for receipt in item.get('receipts', []):
try:
if 'packageid' in receipt and 'version' in receipt:
pkgid = receipt['packageid']
pkgvers = receipt['version']
if not pkgid in pkgid_table:
pkgid_table[pkgid] = {}
if not pkgvers in pkgid_table[pkgid]:
pkgid_table[pkgid][pkgvers] = []
pkgid_table[pkgid][pkgvers].append(itemindex)
except TypeError:
munkicommon.display_warning(
'Bad receipt data for %s-%s: %s'
% (name, vers, receipt))
# add to table of installed applications
for install in item.get('installs', []):
try:
if install.get('type') == 'application':
if 'path' in install:
if not install['path'] in app_table:
app_table[install['path']] = {}
if not vers in app_table[install['path']]:
app_table[install['path']][vers] = []
app_table[install['path']][vers].append(itemindex)
except TypeError:
munkicommon.display_warning(
'Bad install data for %s-%s: %s'
% (name, vers, install))
# add to table of PayloadIdentifiers
if 'PayloadIdentifier' in item:
if not item['PayloadIdentifier'] in profile_table:
profile_table[item['PayloadIdentifier']] = {}
if not vers in profile_table[item['PayloadIdentifier']]:
profile_table[item['PayloadIdentifier']][vers] = []
profile_table[item['PayloadIdentifier']][vers].append(itemindex)
pkgdb = {}
pkgdb['hashes'] = hash_table
pkgdb['receipts'] = pkgid_table
pkgdb['applications'] = app_table
pkgdb['installer_items'] = installer_item_table
pkgdb['profiles'] = profile_table
pkgdb['items'] = catalogitems
return pkgdb
def findMatchingPkginfo(pkginfo):
"""Looks through repo catalogs looking for matching pkginfo
Returns a pkginfo dictionary, or an empty dict"""
def compare_version_keys(value_a, value_b):
"""Internal comparison function for use in sorting"""
return cmp(munkicommon.MunkiLooseVersion(value_b),
munkicommon.MunkiLooseVersion(value_a))
try:
catdb = makeCatalogDB()
except CatalogDBException:
return {}
if 'installer_item_hash' in pkginfo:
matchingindexes = catdb['hashes'].get(
pkginfo['installer_item_hash'])
if matchingindexes:
return catdb['items'][matchingindexes[0]]
if 'receipts' in pkginfo:
pkgids = [item['packageid']
for item in pkginfo['receipts']
if 'packageid' in item]
if pkgids:
possiblematches = catdb['receipts'].get(pkgids[0])
if possiblematches:
versionlist = possiblematches.keys()
versionlist.sort(compare_version_keys)
# go through possible matches, newest version first
for versionkey in versionlist:
testpkgindexes = possiblematches[versionkey]
for pkgindex in testpkgindexes:
testpkginfo = catdb['items'][pkgindex]
testpkgids = [item['packageid'] for item in
testpkginfo.get('receipts', [])
if 'packageid' in item]
if set(testpkgids) == set(pkgids):
return testpkginfo
if 'installs' in pkginfo:
applist = [item for item in pkginfo['installs']
if item['type'] == 'application'
and 'path' in item]
if applist:
app = applist[0]['path']
possiblematches = catdb['applications'].get(app)
if possiblematches:
versionlist = possiblematches.keys()
versionlist.sort(compare_version_keys)
indexes = catdb['applications'][app][versionlist[0]]
return catdb['items'][indexes[0]]
if 'PayloadIdentifier' in pkginfo:
identifier = pkginfo['PayloadIdentifier']
possiblematches = catdb['profiles'].get(identifier)
if possiblematches:
versionlist = possiblematches.keys()
versionlist.sort(compare_version_keys)
indexes = catdb['profiles'][identifier][versionlist[0]]
return catdb['items'][indexes[0]]
# no matches by receipts or installed applications,
# let's try to match based on installer_item_name
installer_item_name = os.path.basename(
pkginfo.get('installer_item_location', ''))
possiblematches = catdb['installer_items'].get(installer_item_name)
if possiblematches:
versionlist = possiblematches.keys()
versionlist.sort(compare_version_keys)
indexes = catdb['installer_items'][installer_item_name][versionlist[0]]
return catdb['items'][indexes[0]]
# if we get here, we found no matches
return {}
def makePkgInfo(options=None, test_mode=False):
"""Calls makepkginfo to generate the pkginfo for item_path."""
# first look for a makepkginfo in the same dir as us
mydir = os.path.dirname(os.path.abspath(__file__))
makepkginfo_path = os.path.join(mydir, 'makepkginfo')
if not os.path.exists(makepkginfo_path):
# didn't find it; assume the default install path
makepkginfo_path = '/usr/local/munki/makepkginfo'
if test_mode:
# prepend verification option if in test mode
options = ['--verify-options-only'] + options
# build makepkginfo command from discovered path and options
cmd = [makepkginfo_path] + options
proc = subprocess.Popen(cmd,
bufsize=-1, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(stdout, stderr) = proc.communicate()
if test_mode:
if proc.returncode == 2:
# option syntax error or unknown option
syntax_error = [error for error in stderr.splitlines()
if 'error' in error]
print >> sys.stderr, ('Option syntax error: %s' %
syntax_error[-1].split(': ', 2)[-1])
print >> sys.stderr, ('See \'%s --help\' for valid options that '
'can be used with munkiimport.'
% makepkginfo_path)
exit(-1)
elif proc.returncode:
# catch-all for any other error
if stderr:
print >> sys.stderr, stderr.rstrip('\n')
return {}
else:
return stdout.rstrip('\n')
if proc.returncode:
print >> sys.stderr, stderr.rstrip('\n')
return {}
if stderr:
# just warnings if returncode is 0
print >> sys.stderr, stderr.rstrip('\n')
return FoundationPlist.readPlistFromString(stdout)
def makeCatalogs():
"""Calls makecatalogs to rebuild our catalogs"""
# first look for a makecatalogs in the same dir as us
mydir = os.path.dirname(os.path.abspath(__file__))
makecatalogs_path = os.path.join(mydir, 'makecatalogs')
if not os.path.exists(makecatalogs_path):
# didn't find it; assume the default install path
makecatalogs_path = '/usr/local/munki/makecatalogs'
if not repoAvailable():
raise RepoCopyError('Could not connect to munki repo.')
if not VERBOSE:
print 'Rebuilding catalogs at %s...' % REPO_PATH
proc = subprocess.Popen([makecatalogs_path, REPO_PATH],
bufsize=-1, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
while True:
output = proc.stdout.readline()
if not output and (proc.poll() != None):
break
if VERBOSE:
print output.rstrip('\n').encode('UTF-8')
errors = proc.stderr.read()
if errors:
print '\nThe following errors occurred while building catalogs:\n'
print errors
def cleanupAndExit(exitcode):
"""Unmounts the repo if we mounted it, then exits"""
result = 0
if WE_MOUNTED_THE_REPO:
if not NOINTERACTIVE:
answer = raw_input('Unmount the repo fileshare? [y/n] ')
if answer.lower().startswith('y'):
result = unmountRepoCLI()
else:
result = unmountRepoCLI()
# clean up tmpdir
munkicommon.cleanUpTmpDir()
exit(exitcode or result)
def pref(prefname):
"""Returns a preference for prefname"""
try:
_prefs = FoundationPlist.readPlist(PREFSPATH)
except FoundationPlist.NSPropertyListSerializationException:
return None
if prefname in _prefs:
return _prefs[prefname]
else:
return None
def configure():
"""Configures munkiimport for use"""
_prefs = {}
for (key, prompt) in [
('repo_path', 'Path to munki repo (example: /Volumes/repo)'),
('repo_url',
'Repo fileshare URL (example: afp://munki.example.com/repo)'),
('pkginfo_extension', 'pkginfo extension (Example: .plist)'),
('editor',
'pkginfo editor (examples: /usr/bin/vi or TextMate.app)'),
('default_catalog', 'Default catalog to use (example: testing)')]:
_prefs[key] = raw_input_with_default('%15s' % prompt, pref(key))
try:
FoundationPlist.writePlist(_prefs, PREFSPATH)
except FoundationPlist.NSPropertyListWriteException:
print >> sys.stderr, 'Could not save configuration to %s' % PREFSPATH
PREFSNAME = 'com.googlecode.munki.munkiimport.plist'
PREFSPATH = os.path.expanduser(os.path.join('~/Library/Preferences',
PREFSNAME))
APPLEMETADATA = False
NOINTERACTIVE = False
WE_MOUNTED_THE_REPO = False
VERBOSE = False
REPO_PATH = ""
REPO_URL = ""
def main():
"""Main routine"""
global APPLEMETADATA
global NOINTERACTIVE
global VERBOSE
global REPO_PATH
global REPO_URL
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
"""
epilog = """\nExtended Options: (makepkginfo options)
In addition to the options described above, options used with
'makepkginfo' may also be specified to customize the resulting
pkginfo file.
Example:
munkiimport --subdirectory apps -c production --minimum_os_vers 10.6.8 /path/to/installer_item\n"""
parser = PassThroughOptionParser(usage=usage, epilog=epilog)
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. May cause a failure '
'if repo path is unavailable.')
parser.add_option('--repo_path', '--repo-path', default='',
help='Optional path to munki repo that takes precedence '
'over the default repo_path specified via '
'--configure.')
parser.add_option('--repo_url', '--repo-url', default='',
help='Optional repo fileshare URL that takes precedence '
'over the default repo_url specified via '
'--configure.')
parser.add_option('--icon_path', '--icon-path', default='', type='string',
help='Path to an icon file for the package. '
'Will overwrite an existing icon.')
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.')
sys.argv = [unicode(item, 'utf-8') for item in sys.argv]
options, arguments = parser.parse_args()
if options.version:
print munkicommon.get_version()
exit(0)
if options.configure:
configure()
exit(0)
NOINTERACTIVE = options.nointeractive
VERBOSE = options.verbose
REPO_PATH = pref('repo_path')
REPO_URL = pref('repo_url')
if options.repo_path:
if not os.path.exists(options.repo_path) and not options.repo_url:
print >> sys.stderr, (
'Munki repo path override provided but folder does not exist. '
'Please either provide --repo_url if you wish to connect to a'
'file share, or correct the path and try again.')
exit(-1)
REPO_PATH = options.repo_path
if options.repo_url:
REPO_URL = options.repo_url
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 len(arguments) == 0:
parser.print_usage()
exit(0)
# Verify that arguments, presumed to be for
# 'makepkginfo' are valid and return installer_item
return_dict = makePkgInfo(
options=arguments, test_mode=True)
try:
return_dict = FoundationPlist.readPlistFromString(return_dict)
except FoundationPlist.FoundationPlistException, err:
print >> sys.stderr, (
'Error getting info from makepkginfo: %s' % err)
cleanupAndExit(-1)
installer_item = return_dict.get('installeritem')
uninstaller_item = return_dict.get('uninstalleritem')
APPLEMETADATA = return_dict.get('installer_type') == 'apple_update_metadata'
if not installer_item and not APPLEMETADATA:
cleanupAndExit(-1)
if not APPLEMETADATA:
# Remove the installer_item from arguments
arguments.remove(installer_item)
# Strip trailing '/' from installer_item
installer_item = installer_item.rstrip('/')
# Check if the item is a mount point for a disk image
if munkicommon.pathIsVolumeMountPoint(installer_item):
# Get the disk image path for the mount point
# and use that instead of the original item
installer_item = munkicommon.diskImageForMountPoint(installer_item)
if not munkicommon.hasValidInstallerItemExt(installer_item) and \
not munkicommon.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)
if not REPO_PATH:
print >> sys.stderr, ('Path to munki repo has not been defined. '
'Run with --configure option to configure this '
'tool, or provide with --repo-path')
exit(-1)
if not repoAvailable():
print >> sys.stderr, ('Could not connect to munki repo. Check the '
'configuration and try again.')
exit(-1)
if not APPLEMETADATA:
if os.path.isdir(installer_item): # Start of indent
if munkicommon.hasValidDiskImageExt(installer_item):
# a directory named foo.dmg or foo.iso!
print >> sys.stderr, '%s is an unknown type.' % installer_item
cleanupAndExit(-1)
else:
# we need to convert to dmg
dmg_path = makeDMG(installer_item)
if dmg_path:
installer_item = dmg_path
else:
print >> sys.stderr, (
'Could not convert %s to a disk image.'
% installer_item)
cleanupAndExit(-1)
# append the installer_item to arguments which
# may have changed if bundle was wrapped into dmg
arguments.append(installer_item) # End of indent
if uninstaller_item:
if os.path.isdir(uninstaller_item):
if munkicommon.hasValidDiskImageExt(uninstaller_item):
# a directory named foo.dmg or foo.iso!
print >> sys.stderr, (
'%s is an unknown type.' % uninstaller_item)
cleanupAndExit(-1)
else:
# we need to convert to dmg
dmg_path = makeDMG(uninstaller_item)
if dmg_path:
uninstaller_item = dmg_path
else:
print >> sys.stderr, (
'Could not convert %s to a disk image.'
% uninstaller_item)
cleanupAndExit(-1)
# if catalog/catalogs have not been explictly specified via command-line,
# append our default catalog
if not '--catalog' in arguments and not '-c' in arguments:
default_catalog = pref('default_catalog') or 'testing'
arguments.extend(['--catalog', default_catalog])
pkginfo = makePkgInfo(arguments)
if not pkginfo:
# makepkginfo returned an error
print >> sys.stderr, 'Getting package info failed.'
cleanupAndExit(-1)
if not options.nointeractive:
# try to find existing pkginfo items that match this one
matchingpkginfo = findMatchingPkginfo(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'):
cleanupAndExit(0)
answer = raw_input('Use existing item as a template? [y/n] ')
if answer.lower().startswith('y'):
pkginfo['name'] = matchingpkginfo['name']
pkginfo['display_name'] = pkginfo.get('display_name') or \
matchingpkginfo.get('display_name',
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']:
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()
if value.startswith(('y', 't')):
pkginfo[key] = True