Skip to content

Commit 6be04d3

Browse files
committed
Make dryrun more useful for debugging
This changes the `--dryrun` flag so that it runs all code, but doesn't update anything in jamf or snipeit. It also introduces a `--connection_test` flag, which takes over the functionality of the old dryrun flag. The main idea behind this is, that for debugging and development purposes it might be useful to run almost all code, but stop short of updating the environment.
1 parent ac50113 commit 6be04d3

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

jamf2snipe

+26-4
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ import datetime
6060
runtimeargs = argparse.ArgumentParser()
6161
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")
6262
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")
63-
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")
63+
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")
64+
runtimeargs.add_argument("--connection_test", help="This checks your config and tries to contact both the JAMFPro and Snipe-it instances.", action="store_true")
6465
runtimeargs.add_argument("-d", "--debug", help="Sets logging to include additional DEBUG messages.", action="store_true")
6566
runtimeargs.add_argument("--do_not_update_jamf", help="Does not update Jamf with the asset tags stored in Snipe.", action="store_false")
6667
runtimeargs.add_argument('--do_not_verify_ssl', help="Skips SSL verification for all requests. Helpful when you use self-signed certificate.", action="store_false")
@@ -89,8 +90,20 @@ elif user_args.debug:
8990
else:
9091
logging.basicConfig(level=logging.WARNING)
9192

93+
# Notify users if we're doing a connection test.
94+
if user_args.connection_test and user_args.dryrun:
95+
logging.error("You can't use --connection_test and --dryrun at the same time. Please choose one or the other.")
96+
raise SystemExit("Error: Invalid runtime arguments - Exiting.")
97+
if user_args.connection_test and user_args.force:
98+
logging.error("You can't use --connection_test and --force at the same time. Please choose one or the other.")
99+
raise SystemExit("Error: Invalid runtime arguments - Exiting.")
100+
if user_args.connection_test:
101+
print("Connection test: Starting jamf2snipe with a connection test where we'll try to contact both the JAMFPro and Snipe-it instances.")
102+
92103
# Notify users if we're doing a dry run.
93-
if user_args.dryrun:
104+
if user_args.dryrun and user_args.force:
105+
print("Running a dry run with force enabled. This will generate assets for debugging, but not update or sync anything.")
106+
elif user_args.dryrun:
94107
print("Dryrun: Starting jamf2snipe with a dry run where no assets will be updated.")
95108

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

396409
# Function to update the asset tag of computers in JAMF with an number passed from Snipe.
397410
def update_jamf_asset_tag(jamf_id, asset_tag):
411+
if user_args.dryrun:
412+
logging.debug("Would have updated JAMF asset id: {} with asset tag: {}".format(jamf_id, asset_tag))
413+
return True
398414
api_url = "{}/JSSResource/computers/id/{}".format(jamfpro_base, jamf_id)
399415
payload = """<?xml version="1.0" encoding="UTF-8"?><computer><general><id>{}</id><asset_tag>{}</asset_tag></general></computer>""".format(jamf_id, asset_tag)
400416
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))
@@ -418,6 +434,9 @@ def update_jamf_asset_tag(jamf_id, asset_tag):
418434

419435
# Function to update the asset tag of mobile devices in JAMF with an number passed from Snipe.
420436
def update_jamf_mobiledevice_asset_tag(jamf_id, asset_tag):
437+
if user_args.dryrun:
438+
logging.debug("Would have updated JAMF asset id: {} with asset tag: {}".format(jamf_id, asset_tag))
439+
return True
421440
api_url = "{}/JSSResource/mobiledevices/id/{}".format(jamfpro_base, jamf_id)
422441
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)
423442
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))
@@ -570,6 +589,9 @@ def create_snipe_asset(payload):
570589

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

739761
# After this point we start editing data, so quit if this is a dryrun
740-
if user_args.dryrun:
741-
raise SystemExit("Dryrun: Complete.")
762+
if user_args.connection_test:
763+
raise SystemExit("Connection Test: Complete.")
742764

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

0 commit comments

Comments
 (0)