11# encoding: utf-8
22
33# SimpleMDMRepo.py
4- # Version 1.2.4
4+ # Version 1.3.0
55
66from __future__ import absolute_import , print_function
77
2121
2222from munkilib .munkirepo import Repo , RepoError
2323from munkilib .wrappers import readPlistFromString , PlistReadError
24-
24+
2525DEFAULT_BASE_URL = 'https://a.simplemdm.com/munki/plugin'
2626CONFIG_PATH = '/usr/local/simplemdm/munki-plugin/config.plist'
2727
2828class SimpleMDMRepo (Repo ):
2929
3030 def __init__ (self , baseurl ):
31- self .base_url = DEFAULT_BASE_URL
32- if 'SIMPLEMDM_BASE_URL' in os .environ :
33- self .base_url = os .environ ['SIMPLEMDM_BASE_URL' ]
31+ self .base_url = self ._fetch_base_url ()
3432
3533 self .getter = URLGetter ()
3634 self .auth_header = self ._fetch_auth_header ()
37-
35+
36+ def _fetch_base_url (self ):
37+ # fetch from environment argument
38+
39+ if 'SIMPLEMDM_BASE_URL' in os .environ :
40+ print ('Using base URL provided by environment variable.' )
41+ return os .environ ['SIMPLEMDM_BASE_URL' ]
42+
43+ # fetch from config file
44+
45+ config = self ._read_config_file ()
46+
47+ if config :
48+ key = config .get ('base_url' , None )
49+ if key and len (key ) > 0 :
50+ print ('Using base URL provided in config file.' )
51+ return key
52+
53+ return DEFAULT_BASE_URL
54+
3855 def _fetch_api_key (self ):
3956 # fetch from environment argument
4057
@@ -44,6 +61,19 @@ def _fetch_api_key(self):
4461
4562 # fetch from config file
4663
64+ config = self ._read_config_file ()
65+ if config :
66+ key = config .get ('key' , None )
67+ if key and len (key ) > 0 :
68+ print ('Using API key provided in config file.' )
69+ return key
70+
71+ # fetch interactively
72+
73+ print ('Please provide a SimpleMDM API key' )
74+ return getpass .getpass ()
75+
76+ def _read_config_file (self ):
4777 try :
4878 with open (CONFIG_PATH ,'rb' ) as f :
4979 config_str = f .read ()
@@ -58,15 +88,8 @@ def _fetch_api_key(self):
5888 except PlistReadError as e :
5989 print ('WARNING: Could not parse config file: {error}' .format (error = e ))
6090 else :
61- key = config .get ('key' , None )
62- if key and len (key ) > 0 :
63- print ('Using API key provided in key file.' )
64- return key
91+ return config
6592
66- # fetch interactively
67-
68- print ('Please provide a SimpleMDM API key' )
69- return getpass .getpass ()
7093
7194 def _fetch_auth_header (self ):
7295 key = self ._fetch_api_key ()
@@ -100,7 +123,7 @@ def _curl(self, simplemdm_path_or_url, commands=None, form_data=None, headers=No
100123 curl_cmd .extend (['-F' , '{key}={value}' .format (key = key ,value = value )])
101124
102125 # commands
103-
126+
104127 curl_cmd .extend (commands )
105128 curl_cmd .append ('-v' )
106129
@@ -157,7 +180,7 @@ def put_from_local_file(self, resource_identifier, local_file_path):
157180 }
158181 resp = self ._curl ('pkgs/create_url' , form_data = form_data )
159182 upload_url = resp .decode ("UTF-8" )
160-
183+
161184 # upload binary
162185
163186 headers = { 'Content-type' : 'application/octet-stream' }
@@ -177,7 +200,7 @@ def put_from_local_file(self, resource_identifier, local_file_path):
177200 self ._curl (resource_identifier , headers = headers , commands = commands )
178201
179202 def delete (self , resource_identifier ):
180- raise ProcessorError ("This action is not supported by SimpleMDM" )
203+ raise ProcessorError ("This action is not supported by SimpleMDM" )
181204
182205 def makecatalogs (self , options , output_fn = None ):
183206 return []
0 commit comments