Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make dryrun more useful for debugging #126

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions jamf2snipe
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ import datetime
runtimeargs = argparse.ArgumentParser()
runtimeargs.add_argument("-v", "--verbose", help="Sets the logging level to INFO and gives you a better idea of what the script is doing.", action="store_true")
runtimeargs.add_argument("--auto_incrementing", help="You can use this if you have auto-incrementing enabled in your snipe instance to utilize that instead of adding the Jamf ID for the asset tag.", action="store_true")
runtimeargs.add_argument("--dryrun", help="This checks your config and tries to contact both the JAMFPro and Snipe-it instances, but exits before updating or syncing any assets.", action="store_true")
runtimeargs.add_argument("--dryrun", help="This checks your config and tries to contact both the JAMFPro and Snipe-it instances, and will generate the assets for debugging, but not update or sync anything but exits before updating or syncing any assets.", action="store_true")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but not update or sync anything but exits before updating or syncing any assets.

sound repetitive. maybe end before the second "but"?

runtimeargs.add_argument("--connection_test", help="This checks your config and tries to contact both the JAMFPro and Snipe-it instances.", action="store_true")
runtimeargs.add_argument("-d", "--debug", help="Sets logging to include additional DEBUG messages.", action="store_true")
runtimeargs.add_argument("--do_not_update_jamf", help="Does not update Jamf with the asset tags stored in Snipe.", action="store_false")
runtimeargs.add_argument('--do_not_verify_ssl', help="Skips SSL verification for all requests. Helpful when you use self-signed certificate.", action="store_false")
Expand Down Expand Up @@ -89,8 +90,20 @@ elif user_args.debug:
else:
logging.basicConfig(level=logging.WARNING)

# Notify users if we're doing a connection test.
if user_args.connection_test and user_args.dryrun:
logging.error("You can't use --connection_test and --dryrun at the same time. Please choose one or the other.")
raise SystemExit("Error: Invalid runtime arguments - Exiting.")
if user_args.connection_test and user_args.force:
logging.error("You can't use --connection_test and --force at the same time. Please choose one or the other.")
raise SystemExit("Error: Invalid runtime arguments - Exiting.")
if user_args.connection_test:
print("Connection test: Starting jamf2snipe with a connection test where we'll try to contact both the JAMFPro and Snipe-it instances.")

# Notify users if we're doing a dry run.
if user_args.dryrun:
if user_args.dryrun and user_args.force:
print("Running a dry run with force enabled. This will generate assets for debugging, but not update or sync anything.")
elif user_args.dryrun:
print("Dryrun: Starting jamf2snipe with a dry run where no assets will be updated.")

# Find a valid settings.conf file.
Expand Down Expand Up @@ -395,6 +408,9 @@ def search_jamf_mobile(jamf_id):

# Function to update the asset tag of computers in JAMF with an number passed from Snipe.
def update_jamf_asset_tag(jamf_id, asset_tag):
if user_args.dryrun:
logging.debug("Would have updated JAMF asset id: {} with asset tag: {}".format(jamf_id, asset_tag))
return True
api_url = "{}/JSSResource/computers/id/{}".format(jamfpro_base, jamf_id)
payload = """<?xml version="1.0" encoding="UTF-8"?><computer><general><id>{}</id><asset_tag>{}</asset_tag></general></computer>""".format(jamf_id, asset_tag)
logging.debug('Making Get request against: {}\nPayload for the PUT request is: {}\nThe username, password, and headers can be found near the beginning of the output.'.format(api_url, payload))
Expand All @@ -418,6 +434,9 @@ def update_jamf_asset_tag(jamf_id, asset_tag):

# Function to update the asset tag of mobile devices in JAMF with an number passed from Snipe.
def update_jamf_mobiledevice_asset_tag(jamf_id, asset_tag):
if user_args.dryrun:
logging.debug("Would have updated JAMF asset id: {} with asset tag: {}".format(jamf_id, asset_tag))
return True
api_url = "{}/JSSResource/mobiledevices/id/{}".format(jamfpro_base, jamf_id)
payload = """<?xml version="1.0" encoding="UTF-8"?><mobile_device><general><id>{}</id><asset_tag>{}</asset_tag></general></mobile_device>""".format(jamf_id, asset_tag)
logging.debug('Making Get request against: {}\nPayload for the PUT request is: {}\nThe username, password, and headers can be found near the beginning of the output.'.format(api_url, payload))
Expand Down Expand Up @@ -570,6 +589,9 @@ def create_snipe_asset(payload):

# Function that updates a snipe asset with a JSON payload
def update_snipe_asset(snipe_id, payload):
if user_args.dryrun:
logging.debug("Dry run mode is enabled. We would have updated ID: {} with the following payload: {}".format(snipe_id, payload))
return True
api_url = '{}/api/v1/hardware/{}'.format(snipe_base, snipe_id)
logging.debug('The payload for the snipe update is: {}'.format(payload))
response = requests.patch(api_url, headers=snipeheaders, json=payload, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
Expand Down Expand Up @@ -737,8 +759,8 @@ else:
raise SystemExit("Unable to get JAMF Computers.")

# After this point we start editing data, so quit if this is a dryrun
if user_args.dryrun:
raise SystemExit("Dryrun: Complete.")
if user_args.connection_test:
raise SystemExit("Connection Test: Complete.")

# From this point on, we're editing data.
logging.info('Starting to Update Inventory')
Expand Down