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

Support environment variables, name sync for AppleDB and dry_run mode #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
settings.ini
.vscode/launch.json
35 changes: 17 additions & 18 deletions appleInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import colorama
from sys import exit

from config import get_config
from mosyle import Mosyle
from snipe import Snipe
from colorama import Fore
Expand All @@ -20,29 +21,27 @@
# Converts datetim/e to timestamp for Mosyle
ts = datetime.datetime.now().timestamp() - 200

# Set some Variables from the settings.conf:
config = configparser.ConfigParser()
config.read('settings.ini')

# This is the address, cname, or FQDN for your snipe-it instance.
snipe_url = config['snipe-it']['url']
apiKey = config['snipe-it']['apiKey']
defaultStatus = config['snipe-it']['defaultStatus']
apple_manufacturer_id = config['snipe-it']['manufacturer_id']
macos_category_id = config['snipe-it']['macos_category_id']
ios_category_id = config['snipe-it']['ios_category_id']
tvos_category_id = config['snipe-it']['tvos_category_id']
macos_fieldset_id = config['snipe-it']['macos_fieldset_id']
ios_fieldset_id = config['snipe-it']['ios_fieldset_id']
tvos_fieldset_id = config['snipe-it']['tvos_fieldset_id']
deviceTypes = config['mosyle']['deviceTypes'].split(',')
snipe_url = get_config('snipe-it','url')
apiKey = get_config('snipe-it','apiKey')
defaultStatus = get_config('snipe-it','defaultStatus')
apple_manufacturer_id = get_config('snipe-it','manufacturer_id')
macos_category_id = get_config('snipe-it','macos_category_id')
ios_category_id = get_config('snipe-it','ios_category_id')
tvos_category_id = get_config('snipe-it','tvos_category_id')
macos_fieldset_id = get_config('snipe-it','macos_fieldset_id')
ios_fieldset_id = get_config('snipe-it','ios_fieldset_id')
tvos_fieldset_id = get_config('snipe-it','tvos_fieldset_id')
deviceTypes = get_config('mosyle','deviceTypes').split(',')

snipe_rate_limit = int(config['snipe-it']['rate_limit'])
snipe_rate_limit = int(get_config('snipe-it','rate_limit'))

apple_image_check = config['snipe-it'].getboolean('apple_image_check')
apple_image_check = get_config('snipe-it', 'apple_image_check', as_boolean=True)
apple_friendly_name_check = get_config('snipe-it', 'apple_image_check', as_boolean=True)

dry_run = get_config('snipe-it', 'dryrun', as_boolean=True)
#setup the snipe-it api
snipe = Snipe(apiKey,snipe_url,apple_manufacturer_id,macos_category_id,ios_category_id,tvos_category_id,snipe_rate_limit, macos_fieldset_id, ios_fieldset_id, tvos_fieldset_id,apple_image_check)
snipe = Snipe(apiKey,snipe_url,apple_manufacturer_id,macos_category_id,ios_category_id,tvos_category_id,snipe_rate_limit, macos_fieldset_id, ios_fieldset_id, tvos_fieldset_id,apple_image_check, apple_friendly_name_check, dry_run=dry_run)


#get all models
Expand Down
16 changes: 16 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import configparser
import os

# Set some Variables from the settings.conf:
config = configparser.ConfigParser()
config.read('settings.ini')

def get_config(sys, key, as_boolean=False):
val = config[sys][key]
if val.startswith('ENV:'):
var = val.removeprefix('ENV:')
val = os.environ[var]
if as_boolean:
return val == 'True'
else:
return val
55 changes: 25 additions & 30 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Import all the things
import json
import datetime
import configparser
import colorama
from sys import exit

from config import get_config
from mosyle import Mosyle
from snipe import Snipe
from colorama import Fore
Expand All @@ -13,37 +13,37 @@
# Converts datetim/e to timestamp for Mosyle
ts = datetime.datetime.now().timestamp() - 200

# Set some Variables from the settings.conf:
config = configparser.ConfigParser()
config.read('settings.ini')

# This is the address, cname, or FQDN for your snipe-it instance.
snipe_url = config['snipe-it']['url']
apiKey = config['snipe-it']['apiKey']
defaultStatus = config['snipe-it']['defaultStatus']
apple_manufacturer_id = config['snipe-it']['manufacturer_id']
macos_category_id = config['snipe-it']['macos_category_id']
ios_category_id = config['snipe-it']['ios_category_id']
tvos_category_id = config['snipe-it']['tvos_category_id']
macos_fieldset_id = config['snipe-it']['macos_fieldset_id']
ios_fieldset_id = config['snipe-it']['ios_fieldset_id']
tvos_fieldset_id = config['snipe-it']['tvos_fieldset_id']
deviceTypes = config['mosyle']['deviceTypes'].split(',')
snipe_url = get_config('snipe-it','url')
apiKey = get_config('snipe-it','apiKey')
defaultStatus = get_config('snipe-it','defaultStatus')
apple_manufacturer_id = get_config('snipe-it','manufacturer_id')
macos_category_id = get_config('snipe-it','macos_category_id')
ios_category_id = get_config('snipe-it','ios_category_id')
tvos_category_id = get_config('snipe-it','tvos_category_id')
macos_fieldset_id = get_config('snipe-it','macos_fieldset_id')
ios_fieldset_id = get_config('snipe-it','ios_fieldset_id')
tvos_fieldset_id = get_config('snipe-it','tvos_fieldset_id')
deviceTypes = get_config('mosyle','deviceTypes').split(',')

snipe_rate_limit = int(config['snipe-it']['rate_limit'])
snipe_rate_limit = int(get_config('snipe-it','rate_limit'))

apple_image_check = config['snipe-it'].getboolean('apple_image_check')
apple_image_check = get_config('snipe-it', 'apple_image_check', as_boolean=True)
apple_friendly_name_check = get_config('snipe-it', 'apple_friendly_name_check', as_boolean=True)

dry_run = get_config('snipe-it', 'dryrun', as_boolean=True)



# Set the token for the Mosyle Api
mosyle = Mosyle(config['mosyle']['token'], config['mosyle']['url'], config['mosyle']['user'], config['mosyle']['password'])
mosyle = Mosyle(get_config('mosyle','token'), get_config('mosyle','url'), get_config('mosyle','user'), get_config('mosyle','password'), dry_run=dry_run)

# Set the call type for Mosyle
calltype = config['mosyle']['calltype']
calltype = get_config('mosyle','calltype')

#setup the snipe-it api
snipe = Snipe(apiKey,snipe_url,apple_manufacturer_id,macos_category_id,ios_category_id,tvos_category_id,snipe_rate_limit, macos_fieldset_id, ios_fieldset_id, tvos_fieldset_id,apple_image_check)
snipe = Snipe(apiKey,snipe_url,apple_manufacturer_id,macos_category_id,ios_category_id,tvos_category_id,snipe_rate_limit, macos_fieldset_id, ios_fieldset_id, tvos_fieldset_id,apple_image_check, apple_friendly_name_check, dry_run=dry_run)

for deviceType in deviceTypes:
# Get the list of devices from Mosyle based on the deviceType and call type
Expand All @@ -54,19 +54,14 @@
mosyle_response = mosyle.list(deviceType).json()

#print(mosyle_response)
if 'status' in mosyle_response:
if mosyle_response['status'] != "OK":
print('There was an issue with the Mosyle API. Stopping.', mosyle_response['message'])
exit();
if 'status' in mosyle_response and mosyle_response['status'] != "OK":
print('There was an issue with the Mosyle API. Stopping.', mosyle_response['message'])
exit();
if 'status' in mosyle_response['response'][0]:
print('There was an issue with the Mosyle API. Stopping script.')
print(mosyle_response['response'][0]['info'])
exit()





print('starting snipe')


Expand Down Expand Up @@ -122,7 +117,7 @@
devicePayload = snipe.buildPayloadFromMosyle(sn);

# If asset doesnt exist create and assign it
if asset['total'] == 0:
if "total" not in asset or asset['total'] == 0:
asset = snipe.createAsset(model, devicePayload).json()
if mosyle_user != None:
print('Assigning asset to SnipIT user based on Mosyle Assignment')
Expand Down Expand Up @@ -161,7 +156,7 @@
print('update the mosyle asset tag of device ', sn['serial_number'], 'to ', asset['rows'][0]['asset_tag'])
mosyle.setAssetTag(sn['serial_number'], asset['rows'][0]['asset_tag'])
else:
print('Mosyle already has an assest tag of: ', sn['asset_tag'])
print('Mosyle already has an asset tag of: ', sn['asset_tag'])

print('Finished with OS: ', deviceType)
print('')
16 changes: 10 additions & 6 deletions mosyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
class Mosyle:

# Create Mosyle instance
def __init__(self, key, url = "https://businessapi.mosyle.com/v1", user = "", password = ""):
def __init__(self, key, url = "https://businessapi.mosyle.com/v1", user = "", password = "", dry_run=False):
# Attribute the variable to the instance
self.url = url
self.request = requests.Session()
self.request.headers["accesstoken"] = key
#base64 encode username and password for basic auth
userpass = user + ':' + password
encoded_u = base64.b64encode(userpass.encode()).decode()
self.request.headers["Authorization"] = "Basic %s" % encoded_u
self.request.headers["content-type"] = "application/json"
loginResponse = self.request.post(self.url + "/login", json = {
"email" : user,
"password" : password
}).headers
self.request.headers["Authorization"] = loginResponse['Authorization']
self.dry_run = dry_run


# Create variables requests
Expand Down Expand Up @@ -61,4 +64,5 @@ def setAssetTag(self, serialnumber, tag):
"serialnumber": serialnumber,
"asset_tag": tag
}
return self.request.post(self.url + "/devices", json = params )
if not self.dry_run:
return self.request.post(self.url + "/devices", json = params )
14 changes: 9 additions & 5 deletions settings_example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
#url of the mosyle api (should be https://businessapi.mosyle.com/v1). It might be different for Mosyle Education, but I do not have that environment to confirm.
url = https://businessapi.mosyle.com/v1
#your api token from the mosyle intergation dashboard
token = token
token = ENV:MOSYLE_TOKEN
#a mosyle admin username
user = emailaddress
user = ENV:MOSYLE_EMAIL
#a mosyle admin password
password = password
password = ENV:MOSYLE_PASSWORD
#choose what device types you want to query. Types are: mac, ios, tvos and must be those exact strings. There should be no spaces between the commas and the types. Eg: mac,ios,tvos
deviceTypes = mac,ios,tvos
# Change the calltype for timestamp or all. Timestamp gets devices changed since the timestamp (not sure if this is working in my version of the script). All gets all devices.
calltype = all

[snipe-it]
#url of the snipe-it api (should end in /api/v1)
url = https://snipeit.example.com/api/v1
url = ENV:SNIPE_URL # eg. https://snipeit.example.com/api/v1
#your api token from Snipe-IT
apikey = apikey
apikey = ENV:SNIPE_API_KEY
# Whether to make changes in Snipe or just print them
dryrun = ENV:DRY_RUN
#The manufacturer id for Apple devices you created in snipe-it
manufacturer_id = 1
#Devices have to be put into categories. We chose to use a "Computer" category for MacOS devices, "Mobile/Tablet" category for iOS, and "Media Players" for tvOS.
Expand All @@ -39,6 +41,8 @@ defaultStatus = 4
rate_limit = 120
#enable image downloading/checking for Apple models
apple_image_check = True
#enable friendly name downloading for Apple models
apple_friendly_name_check = True

[api-mapping]
#leftside is the snipe-it field name, rightside is the mosyle field name
Expand Down
Loading