forked from grahampugh/jamf-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jamf_pkg_upload.py
executable file
·806 lines (711 loc) · 26.8 KB
/
jamf_pkg_upload.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
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
#!/usr/bin/env python3
"""
** Jamf Package Upload Script
by G Pugh
Developed from an idea posted at
https://www.jamf.com/jamf-nation/discussions/27869#responseChild166021
Incorporates a method for uploading packages using the web UI's method thanks to @mosen
Credentials can be supplied from the command line as arguments, or inputted, or
from an existing PLIST containing values for JSS_URL, API_USERNAME and API_PASSWORD,
for example an AutoPkg preferences file which has been configured for use with
JSSImporter: ~/Library/Preferences/com.github.autopkg
For usage, run jamf_pkg_upload.py --help
"""
import argparse
import getpass
import sys
import hashlib
import os
import json
import re
import math
import io
import six
import xml.etree.ElementTree as ElementTree
from zipfile import ZipFile, ZIP_DEFLATED
from time import sleep
from urllib.parse import quote
from xml.sax.saxutils import escape
from jamf_upload_lib import api_connect, api_get, nscurl, curl, smb_actions
if six.PY2:
input = raw_input # pylint: disable=E0602 # noqa: F821
from urlparse import urlparse # pylint: disable=F0401
from HTMLParser import HTMLParser # pylint: disable=F0401
html = HTMLParser()
else:
from urllib.parse import urlparse
import html
def check_local_pkg(mount_share, pkg_name, verbosity):
"""Check local DP or mounted share for existing package"""
path = "/Volumes{}".format(urlparse(mount_share).path)
if os.path.isdir(path):
existing_pkg_path = os.path.join(path, "Packages", pkg_name)
if os.path.isfile(existing_pkg_path):
return existing_pkg_path
else:
print("No existing package found")
if verbosity:
print("Expected path: {}".format(existing_pkg_path))
else:
print("Expected path not found!: {}".format(path))
def zip_pkg_path(path):
"""Add files from path to a zip file handle.
Args:
path (str): Path to folder to zip.
Returns:
(str) name of resulting zip file.
"""
zip_name = "{}.zip".format(path)
if os.path.exists(zip_name):
print("Package object is a bundle. Zipped version already exists.")
return zip_name
print("Package object is a bundle. Converting to zip...")
with ZipFile(zip_name, "w", ZIP_DEFLATED, allowZip64=True) as zip_handle:
for root, _, files in os.walk(path):
for member in files:
zip_handle.write(os.path.join(root, member))
print("Closing: {}".format(zip_name))
return zip_name
def check_pkg(pkg_name, jamf_url, enc_creds, verbosity):
"""check if a package with the same name exists in the repo
note that it is possible to have more than one with the same name
which could mess things up"""
url = "{}/JSSResource/packages/name/{}".format(jamf_url, quote(pkg_name))
r = curl.request("GET", url, enc_creds, verbosity)
if r.status_code == 200:
obj = json.loads(r.output)
try:
obj_id = str(obj["package"]["id"])
except KeyError:
obj_id = "-1"
else:
obj_id = "-1"
return obj_id
def post_pkg(pkg_name, pkg_path, jamf_url, enc_creds, obj_id, r_timeout, verbosity):
"""sends the package using requests"""
try:
import requests
except ImportError:
print(
"WARNING: could not import requests module. Use pip to install requests and try again."
)
sys.exit()
files = {"file": open(pkg_path, "rb")}
headers = {
"authorization": "Basic {}".format(enc_creds),
"content-type": "application/xml",
"DESTINATION": "0",
"OBJECT_ID": obj_id,
"FILE_TYPE": "0",
"FILE_NAME": pkg_name,
}
url = "{}/dbfileupload".format(jamf_url)
# look for existing session
headers_file = "/tmp/curl_headers_from_jamf_upload.txt"
try:
with open(headers_file, "r") as file:
headers = file.readlines()
existing_headers = [x.strip() for x in headers]
for header in existing_headers:
if "APBALANCEID" in header:
cookie = header.split()[1].rstrip(";")
if verbosity > 1:
print("Existing cookie found: {}".format(cookie))
cookies = cookie.split("=")
except IOError:
if verbosity > 1:
print("No existing cookie found - starting new session")
r = requests.post(
url, data=files, headers=headers, cookies=cookies, timeout=r_timeout
)
return r
def curl_pkg(pkg_name, pkg_path, jamf_url, enc_creds, obj_id, r_timeout, verbosity):
"""uploads the package using curl"""
url = "{}/dbfileupload".format(jamf_url)
additional_headers = [
"--header",
"DESTINATION: 0",
"--header",
"OBJECT_ID: {}".format(obj_id),
"--header",
"FILE_TYPE: 0",
"--header",
"FILE_NAME: {}".format(pkg_name),
"--connect-timeout",
str("60"),
"--max-time",
str(r_timeout),
]
r = curl.request("POST", url, enc_creds, verbosity, pkg_path, additional_headers)
if verbosity:
print("HTTP response: {}".format(r.status_code))
return r.output
def nscurl_pkg(pkg_name, pkg_path, jamf_url, enc_creds, obj_id, r_timeout, verbosity):
"""uploads the package using nscurl"""
url = "{}/dbfileupload".format(jamf_url)
additional_headers = [
"--header",
"DESTINATION: 0",
"--header",
"OBJECT_ID: {}".format(obj_id),
"--header",
"FILE_TYPE: 0",
"--header",
"FILE_NAME: {}".format(pkg_name),
"--payload-transmission-timeout",
str(r_timeout),
]
r = nscurl.request("POST", url, enc_creds, verbosity, pkg_path, additional_headers)
if verbosity:
print("HTTP response: {}".format(r.status_code))
return r.output
def update_pkg_metadata(
jamf_url, enc_creds, pkg_name, pkg_metadata, hash_value, verbosity, pkg_id=None
):
"""Update package metadata."""
if hash_value:
hash_type = "SHA_512"
else:
hash_type = "MD5"
# build the package record XML
pkg_data = (
"<package>"
+ f"<name>{pkg_name}</name>"
+ f"<filename>{pkg_name}</filename>"
+ f"<category>{escape(pkg_metadata['category'])}</category>"
+ f"<info>{escape(pkg_metadata['info'])}</info>"
+ f"<notes>{escape(pkg_metadata['notes'])}</notes>"
+ f"<priority>{pkg_metadata['priority']}</priority>"
+ f"<reboot_required>{pkg_metadata['reboot_required']}</reboot_required>"
+ f"<required_processor>{pkg_metadata['required_processor']}</required_processor>"
+ f"<os_requirements>{pkg_metadata['os_requirements']}</os_requirements>"
+ f"<hash_type>{hash_type}</hash_type>"
+ f"<hash_value>{hash_value}</hash_value>"
+ f"<send_notification>{pkg_metadata['send_notification']}</send_notification>"
+ "</package>"
)
# ideally we upload to the package ID but if we didn't get a good response
# we fall back to the package name
if pkg_id:
method = "PUT"
url = "{}/JSSResource/packages/id/{}".format(jamf_url, pkg_id)
else:
method = "POST"
url = "{}/JSSResource/packages/name/{}".format(jamf_url, pkg_name)
if verbosity > 2:
print("Package data:")
print(pkg_data)
count = 0
while True:
count += 1
if verbosity > 1:
print(f"Package metadata upload attempt {count}")
pkg_xml = curl.write_temp_file(pkg_data)
r = curl.request(method, url, enc_creds, verbosity, pkg_xml)
# check HTTP response
if curl.status_check(r, "Package", pkg_name) == "break":
break
if count > 5:
print("WARNING: Package metadata update did not succeed after 5 attempts")
print(f"HTTP POST Response Code: {r.status_code}")
print("ERROR: Package metadata upload failed ")
exit(-1)
sleep(30)
if verbosity:
api_get.get_headers(r)
# clean up temp files
if os.path.exists(pkg_xml):
os.remove(pkg_xml)
def login(
jamf_url, jamf_user, jamf_password, verbosity
): # type: (str, str, str, int) -> any
"""For creating a web UI Session, which is required to scrape JCDS information."""
import requests # pylint: disable=import-error
http = requests.Session()
r = http.post(jamf_url, data={"username": jamf_user, "password": jamf_password})
return r, http
def scrape_upload_token(session, jamf_url, verbosity):
"""Retrieve the packages page from the web UI session to scrape the JCDS endpoint
and data upload token for this session. Note that the JCDS endpoint varies by region."""
url = "{}/legacy/packages.html?id=-1&o=c".format(jamf_url)
r = session.get(url)
if six.PY2:
text = r.content
else:
text = r.text
if verbosity > 2:
print("huge amount of html follows")
print("------")
print(text)
print("------")
matches = re.search(r'data-base-url="([^"]*)"', text)
if matches is None:
print("WARNING: No JCDS distribution point URL was found")
print("- Are you sure that JCDS is your Primary distribution point?")
jcds_base_url_urlencoded = matches.group(1)
matches = re.search(r'data-upload-token="([^"]*)"', text)
if matches is None:
print("WARNING: No JCDS upload token was found")
print("- Are you sure that JCDS is your Primary distribution point?")
jcds_upload_token = matches.group(1)
matches = re.search(r'id="session-token" value="([^"]*)"', text)
if matches is None:
print("WARNING: No package upload session token was found")
session_token = matches.group(1)
jcds_base_url = html.unescape(jcds_base_url_urlencoded)
return jcds_base_url, jcds_upload_token, session_token
def post_pkg_chunks(
pkg_name,
pkg_path,
jcds_base_url,
jcds_upload_token,
obj_id,
jcds_chunk_mb,
verbosity=0,
):
"""sends the package in chunks"""
import requests # pylint: disable=import-error
jcds_chunk_size = int(jcds_chunk_mb) * 1048576 # 1mb is the default
file_size = os.stat(pkg_path).st_size
total_chunks = int(math.ceil(file_size / jcds_chunk_size))
resource = open(pkg_path, "rb")
headers = {"X-Auth-Token": jcds_upload_token, "content-type": "application/xml"}
http = requests.Session()
for chunk in range(0, total_chunks):
resource.seek(chunk * jcds_chunk_size)
chunk_data = resource.read(jcds_chunk_size)
chunk_reader = io.BytesIO(chunk_data)
chunk_url = "{}/{}/part?chunk={}&chunks={}".format(
jcds_base_url, html.escape(pkg_name), chunk, total_chunks
)
if verbosity > 1:
print("URL to post chunks: {}".format(chunk_url))
r = http.post(chunk_url, files={"file": chunk_reader}, headers=headers)
print("Uploaded chunk {} of {}".format(chunk + 1, total_chunks))
if verbosity > 2:
print(r)
resource.close()
def update_pkg_by_form(
session,
session_token,
jamf_url,
pkg_name,
pkg_path,
obj_id,
pkg_metadata,
verbosity=0,
):
"""save the package using the web form, which should force JCDS into pending state."""
# Create Package URL
url = "{}/legacy/packages.html?id={}&o=c".format(jamf_url, str(obj_id))
# TODO - not all metadata fields are represented here - need to find out if
# they can be added and if we can add the hash. Also if we could upload a manifest file.
r = session.post(
url,
data={
"session-token": session_token,
"lastTab": "General",
"lastSideTab": "null",
"lastSubTab": "null",
"lastSubTabSet": "null",
"name": pkg_name,
"categoryID": str(pkg_metadata["category_id"]),
"fileName": pkg_name,
"resetFIELD_MANIFEST_INPUT": "",
"info": pkg_metadata["info"],
"notes": pkg_metadata["notes"],
"priority": pkg_metadata["priority"],
"uninstall_disabled": "true",
"osRequirements": pkg_metadata["os_requirements"],
"requiredProcessor": pkg_metadata["required_processor"],
"switchWithPackageID": "-1",
"action": "Save",
},
)
if verbosity > 1:
print(r.content)
if r.status_code == 200:
print("Successfully created package")
query = urlparse(r.url).query
matches = re.search(r"id=([^&]*)", query)
if matches is None:
print("No package id in redirected url")
else:
pkg_id = matches.group(1)
print("Package ID: {}".format(pkg_id))
return pkg_id
else:
print("Package creation failed")
def get_args():
"""Parse any command line arguments"""
parser = argparse.ArgumentParser()
parser.add_argument("pkg", nargs="+", help="Full path to the package(s) to upload")
parser.add_argument(
"--replace", help="overwrite an existing uploaded package", action="store_true"
)
parser.add_argument(
"--nscurl", help="use curl instead of curl", action="store_true"
)
parser.add_argument(
"--requests", help="use requests instead of curl", action="store_true"
)
parser.add_argument(
"--direct",
help=(
"use direct upload to JCDS (experimental, will not work if JCDS is not "
"primary distribution point)"
),
action="store_true",
)
parser.add_argument("--url", default="", help="the Jamf Pro Server URL")
parser.add_argument(
"--user", default="", help="a user with the rights to upload a package"
)
parser.add_argument(
"--password",
default="",
help="password of the user with the rights to upload a package",
)
parser.add_argument(
"--smb_url",
default="",
help=(
"Path to an SMB FileShare Distribution Point, in the form "
"smb://server/mountpoint"
),
)
parser.add_argument(
"--smb_user",
default="",
help=(
"a user with the rights to upload a package to the SMB FileShare "
"Distribution Point"
),
)
parser.add_argument(
"--smb_pass",
default="",
help=(
"password of the user with the rights to upload a package to the SMB "
"FileShare Distribution Point"
),
)
parser.add_argument(
"--timeout",
type=int,
default="3600",
help="set timeout in seconds for HTTP request for problematic packages",
)
parser.add_argument("--chunksize", default="1", help="set chunk size in megabytes")
parser.add_argument(
"--prefs",
default="",
help=(
"full path to an AutoPkg prefs file containing "
"JSS URL, API_USERNAME and API_PASSWORD, "
"for example an AutoPkg preferences file which has been configured "
"for use with JSSImporter (~/Library/Preferences/com.github.autopkg.plist) "
"or a separate plist anywhere (e.g. ~/.com.company.jamf_upload.plist)"
),
)
parser.add_argument(
"-v",
"--verbose",
action="count",
default=0,
help="print verbose output headers",
)
# the following are for the package metadata
parser.add_argument(
"--category", default="", help="a category to assign to the package"
)
parser.add_argument(
"--info", default="", help="an info string to assign to the package"
)
parser.add_argument(
"--notes", default="", help="a notes string to assign to the package"
)
parser.add_argument(
"--reboot_required",
help="Set if the package requires a restart",
action="store_true",
)
parser.add_argument(
"--priority",
type=int,
choices=range(1, 21),
default=10,
help="a priority to assign to the package (default=10)",
)
parser.add_argument(
"--os_requirements",
default="",
help="an OS requirements string to assign to the package",
)
parser.add_argument(
"--required_processor",
default="None",
choices=["x86", "None"],
help="a required processor to assign to the package. Acceptable values are 'x86' or 'None'",
)
parser.add_argument(
"--send_notification",
help="set to send a notification when the package is installed",
action="store_true",
)
args = parser.parse_args()
if (
args.required_processor
and args.required_processor != "x86"
and args.required_processor != "None"
):
args.required_processor = "None"
if args.priority and args.priority < 1 or args.priority > 20:
parser.error("Acceptable priority range is 1-20")
return args
def sha512sum(filename):
"""calculate the SHA512 hash of the package
(see https://stackoverflow.com/a/44873382)"""
h = hashlib.sha512()
b = bytearray(128 * 1024)
mv = memoryview(b)
with open(filename, "rb", buffering=0) as f:
for n in iter(lambda: f.readinto(mv), 0):
h.update(mv[:n])
return h.hexdigest()
def main():
"""Do the main thing here"""
print("\n** Jamf package upload script")
print("** Uploads packages to Jamf Cloud or SMB Distribution Points.")
# parse the command line arguments
args = get_args()
verbosity = args.verbose
# create a dictionary of package metadata from the args
pkg_metadata = {
"category": args.category,
"info": args.info,
"notes": args.notes,
"reboot_required": args.reboot_required,
"priority": args.priority,
"os_requirements": args.os_requirements,
"required_processor": args.required_processor,
"send_notification": args.send_notification,
}
# grab values from a prefs file if supplied
(
jamf_url,
jamf_user,
jamf_password,
slack_webhook,
enc_creds,
) = api_connect.get_creds_from_args(args)
if args.prefs:
smb_url, smb_user, smb_pass = api_connect.get_smb_credentials(args.prefs)
else:
smb_url = ""
smb_user = ""
smb_pass = ""
# repeat for optional SMB share (but must supply a share path to invoke this)
if args.smb_url:
smb_url = args.smb_url
if args.smb_user:
smb_user = args.smb_user
if not smb_user:
smb_user = input(
"Enter a user with read/write permissions to {} : ".format(smb_url)
)
if args.smb_pass:
smb_pass = args.smb_pass
if not smb_pass:
if not smb_pass:
smb_pass = getpass.getpass(
"Enter the password for '{}' : ".format(smb_user)
)
# get HTTP request timeout
r_timeout = float(args.timeout)
if not args.pkg:
pkg = input("Enter the full path to the package to upload: ")
args.pkg = pkg
# establish a web login session which is reusable for scraping tokens
if args.direct:
r, login_session = login(jamf_url, jamf_user, jamf_password, verbosity)
if r.status_code != 200:
print("Failed to log in to the Jamf instance at: {}".format(jamf_url))
# get the id for a category if supplied
if args.category:
print("Checking ID for category '{}'".format(args.category))
# now get the session token
token = api_connect.get_uapi_token(jamf_url, enc_creds, verbosity)
category_id = api_get.get_uapi_obj_id_from_name(
jamf_url, "categories", args.category, token, verbosity
)
if not category_id:
print("WARNING: Category not found!")
category_id = "-1"
# add to the pkg_metadata dictionary
pkg_metadata["category_id"] = category_id
# now process the list of packages
for pkg_path in args.pkg:
pkg_name = os.path.basename(pkg_path)
# See if the package is non-flat (requires zipping prior to upload).
if os.path.isdir(pkg_path):
pkg_path = zip_pkg_path(pkg_path)
pkg_name += ".zip"
# calculate the SHA-512 hash of the package
sha512string = sha512sum(pkg_path)
# check for existing package
print("\nChecking '{}' on {}".format(pkg_name, jamf_url))
if verbosity:
print("Full path: {}".format(pkg_path))
replace_pkg = True if args.replace else False
obj_id = check_pkg(pkg_name, jamf_url, enc_creds, verbosity)
if obj_id != "-1":
print("Package '{}' already exists: ID {}".format(pkg_name, obj_id))
pkg_id = obj_id # assign pkg_id for smb runs - JCDS runs get it from the pkg upload
else:
pkg_id = ""
# post the package (won't run if the pkg exists and replace_pkg is False)
# process for SMB shares if defined
if args.smb_url:
# mount the share
smb_actions.mount_smb(args.smb_url, args.smb_user, args.smb_pass, verbosity)
# check for existing package
local_pkg = check_local_pkg(args.smb_url, pkg_name, verbosity)
if not local_pkg or replace_pkg:
# copy the file
smb_actions.copy_pkg(args.smb_url, pkg_path, pkg_name)
# unmount the share
smb_actions.umount_smb(args.smb_url)
# otherwise process for cloud DP
else:
if obj_id == "-1" or replace_pkg:
# JCDS direct upload method option
if args.direct:
jcds_url, jcds_token, session_token = scrape_upload_token(
login_session, jamf_url, verbosity
)
if jcds_url and jcds_token and session_token:
if verbosity:
print("JCDS URL: {}".format(jcds_url))
print("JCDS Upload token: {}".format(jcds_token))
print("Session token: {}".format(session_token))
# post the package as chunks
post_pkg_chunks(
pkg_name,
pkg_path,
jcds_url,
jcds_token,
obj_id,
args.chunksize,
verbosity,
)
# now create the package object and get the pkg ID
pkg_id = update_pkg_by_form(
login_session,
session_token,
jamf_url,
pkg_name,
pkg_path,
obj_id,
pkg_metadata,
verbosity,
)
# curl -> dbfileupload upload method option
elif args.nscurl:
r = nscurl_pkg(
pkg_name,
pkg_path,
jamf_url,
enc_creds,
obj_id,
r_timeout,
verbosity,
)
try:
pkg_id = ElementTree.fromstring(r).findtext("id")
if pkg_id:
print(
"\nPackage uploaded successfully, ID={}".format(pkg_id)
)
except ElementTree.ParseError:
print("Could not parse XML. Raw output:")
print(r.decode("ascii"))
else:
if verbosity:
if r:
print("\nResponse:\n")
print(r.decode("ascii"))
else:
print("No HTTP response")
# requests -> dbfileupload upload method option
elif args.requests:
r = post_pkg(
pkg_name,
pkg_path,
jamf_url,
enc_creds,
obj_id,
r_timeout,
verbosity,
)
# print result of the request
if r.status_code == 200 or r.status_code == 201:
pkg_id = ElementTree.fromstring(r.text).findtext("id")
print("\nPackage uploaded successfully, ID={}".format(pkg_id))
if verbosity:
print("HTTP POST Response Code: {}".format(r.status_code))
else:
print("\nHTTP POST Response Code: {}".format(r.status_code))
if verbosity:
api_get.get_headers(r)
# curl -> dbfileupload upload method option
else:
r = curl_pkg(
pkg_name,
pkg_path,
jamf_url,
enc_creds,
obj_id,
r_timeout,
verbosity,
)
try:
pkg_id = ElementTree.fromstring(r).findtext("id")
if pkg_id:
print(
"\nPackage uploaded successfully, ID={}".format(pkg_id)
)
except ElementTree.ParseError:
print("Could not parse XML. Raw output:")
print(r.decode("ascii"))
else:
if verbosity:
if r:
print("\nResponse:\n")
print(r.decode("ascii"))
else:
print("No HTTP response")
# now process the package metadata if a category is supplied,
# or if we are dealing with an SMB share
if not args.direct:
if pkg_id:
if verbosity:
print("Updating package metadata for {}".format(pkg_id))
update_pkg_metadata(
jamf_url,
enc_creds,
pkg_name,
pkg_metadata,
sha512string,
verbosity,
pkg_id,
)
else:
if verbosity:
print("Creating package metadata")
update_pkg_metadata(
jamf_url, enc_creds, pkg_name, pkg_metadata, sha512string, verbosity
)
print()
if __name__ == "__main__":
main()