Skip to content

Instantly share code, notes, and snippets.

@grahampugh
Created August 21, 2023 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grahampugh/7efd0417cf98f5412d1aedbc533b1fc1 to your computer and use it in GitHub Desktop.
Save grahampugh/7efd0417cf98f5412d1aedbc533b1fc1 to your computer and use it in GitHub Desktop.
Upload a package to Jamf's JCDS2
#!/usr/bin/env python3
"""
A script to upload a package to the Jamf Cloud Distribution Point S3 bucket (JCDS 2.0)
Requirements from pip
boto3
requests
"""
import boto3
from botocore.exceptions import ClientError
import os.path
import requests
from requests.exceptions import HTTPError
import sys
import threading
# REQUIRED AUTHENTICATION VARIABLES
jamfProUser = ""
jamfProPassword = ""
jamfProBaseURL = ""
# path to package - UPDATE AS APPROPRIATE
pkg_path = os.path.join(
"/Users/gpugh/sourcecode",
"erase-install/pkg/erase-install/build/erase-install-30.0.pkg",
)
class ProgressPercentage(object):
"""display upload progress"""
def __init__(self, filename):
self._filename = filename
self._size = float(os.path.getsize(filename))
self._seen_so_far = 0
self._lock = threading.Lock()
def __call__(self, bytes_amount):
# To simplify, assume this is hooked up to a single filename
with self._lock:
self._seen_so_far += bytes_amount
percentage = (self._seen_so_far / self._size) * 100
sys.stdout.write(
"\r%s %s / %s (%.2f%%)"
% (self._filename, self._seen_so_far, self._size, percentage)
)
sys.stdout.flush()
try:
pkg = os.path.basename(pkg_path)
response = requests.post(
jamfProBaseURL + "/api/v1/auth/token",
auth=(jamfProUser, jamfProPassword),
data="",
)
response.raise_for_status()
jsonResponse = response.json()
print("Retreived Token: ")
jamfProToken = jsonResponse["token"]
print(jamfProToken)
# Initiate Upload via Jamf Pro API
headers = {"Accept": "application/json", "Authorization": "Bearer " + jamfProToken}
print(headers)
response = requests.post(
jamfProBaseURL + "/api/v1/jcds/files", headers=headers, data=""
)
response.raise_for_status()
credentials = response.json()
print("Retreived Credentials Object: ")
print(credentials)
except HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
except Exception as err:
print(f"Other error occurred: {err}")
# Upload File To AWS S3
s3_client = boto3.client(
"s3",
aws_access_key_id=credentials["accessKeyID"],
aws_secret_access_key=credentials["secretAccessKey"],
aws_session_token=credentials["sessionToken"],
)
try:
response = s3_client.upload_file(
pkg_path,
credentials["bucketName"],
credentials["path"] + pkg,
Callback=ProgressPercentage(pkg_path),
)
print(response)
except ClientError as e:
print(f"Failure uploading to S3: {e}")
@cardinaldeville
Copy link

This works great except one small thing; Jamf is not aware of this package as it sits in JCDS.

Stealing from your shell script gets the package in its proper place and ready for use!

I just tacked this onto the bottom

# Tell Jamf Pro about the package
pkg_data = f"<package><name>{pkg}</name><filename>{pkg}</filename></package>"
try:
    headers = {"Content-Type": "application/xml", "Authorization": "Bearer " + jamfProToken}
    response = requests.post(
        jamfProBaseURL + "/JSSResource/packages/id/0", headers=headers, data=pkg_data
    )
    response.raise_for_status()
    print(response)
except HTTPError as http_err:
    print(f"HTTP error occurred: {http_err}")
except Exception as err:
    print(f"Other error occurred: {err}")

And this works wonders! I am working on this as a workflow that runs in AWS for getting large files into customer tenants with a faster, steadier pipe that most residential internet.

Thanks for the code!

@cardinaldeville
Copy link

Also, updated for API Clients. Because of the /JSSResource/packages/id/ is needed, the API client would also need access to Packages, as well as Jamf Content Distribution Server Files

#!/usr/bin/env python3

"""
A script to upload a package to the Jamf Cloud Distribution Point S3 bucket (JCDS 2.0)

Requirements from pip
boto3
requests

Updated from @grahampugh's original script to use API Clients, instead of user accounts

"""

import boto3
from botocore.exceptions import ClientError
import os.path
import requests
from requests.exceptions import HTTPError
import sys
import threading


# REQUIRED AUTHENTICATION VARIABLES
JAMF_PRO_CLIENT_ID = "client_id"
JAMF_PRO_CLIENT_SECRET = "client_secret"
JAMF_URL = "https://jss.jamfcloud.com"

# path to package - UPDATE AS APPROPRIATE
pkg_path = os.path.join(
    "/home/ubuntu/jcds",
    "InstallAssistant-14.3-23D56.pkg",
)


class ProgressPercentage(object):
    """display upload progress"""

    def __init__(self, filename):
        self._filename = filename
        self._size = float(os.path.getsize(filename))
        self._seen_so_far = 0
        self._lock = threading.Lock()

    def __call__(self, bytes_amount):
        # To simplify, assume this is hooked up to a single filename
        with self._lock:
            self._seen_so_far += bytes_amount
            percentage = (self._seen_so_far / self._size) * 100
            sys.stdout.write(
                "\r%s  %s / %s  (%.2f%%)"
                % (self._filename, self._seen_so_far, self._size, percentage)
            )
            sys.stdout.flush()


try:
    pkg = os.path.basename(pkg_path)

    # Get Jamf Pro API Token using API Clients
    URL = JAMF_URL + "/api/oauth/token"
    headers = {"Content-Type": "application/x-www-form-urlencoded"}
    data = {
        "grant_type": "client_credentials",
        "client_id": JAMF_PRO_CLIENT_ID,
        "client_secret": JAMF_PRO_CLIENT_SECRET,
    }
    response = requests.post(URL, headers=headers, data=data)
    
    response.raise_for_status()
    jsonResponse = response.json()
    print("Retreived Token: ")
    jamfProToken = jsonResponse["access_token"]
    print(jamfProToken)

    # Initiate Upload via Jamf Pro API
    headers = {"Accept": "application/json", "Authorization": "Bearer " + jamfProToken}

    print(headers)

    response = requests.post(
        JAMF_URL + "/api/v1/jcds/files", headers=headers, data=""
    )
    response.raise_for_status()

    credentials = response.json()
    print("Retreived Credentials Object: ")
    print(credentials)


except HTTPError as http_err:
    print(f"HTTP error occurred: {http_err}")
except Exception as err:
    print(f"Other error occurred: {err}")

# Upload File To AWS S3
s3_client = boto3.client(
    "s3",
    region_name=credentials['region'],
    aws_access_key_id=credentials["accessKeyID"],
    aws_secret_access_key=credentials["secretAccessKey"],
    aws_session_token=credentials["sessionToken"],
)
try:
    response = s3_client.upload_file(
        pkg_path,
        credentials["bucketName"],
        credentials["path"] + pkg,
        Callback=ProgressPercentage(pkg_path),
    )
    print(response)
except ClientError as e:
    print(f"Failure uploading to S3: {e}")


# Tell Jamf Pro about the package
pkg_data = f"<package><name>{pkg}</name><filename>{pkg}</filename></package>"
try:
    headers = {"Content-Type": "application/xml", "Authorization": "Bearer " + jamfProToken}
    response = requests.post(
        JAMF_URL + "/JSSResource/packages/id/0", headers=headers, data=pkg_data
    )
    response.raise_for_status()
    # if response includes 201, then package was created, if 409, then package already exists
    if response.status_code == 201:
        print("Package created")
    if response.status_code == 409:
        print("Package already exists")
except HTTPError as http_err:
    print(f"HTTP error occurred: {http_err}")
except Exception as err:
    print(f"Other error occurred: {err}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment