Skip to content

Commit 60fd88c

Browse files
authored
Merge pull request #71 from salopensource/requests
Requests
2 parents 7b0c984 + 4e6fef0 commit 60fd88c

File tree

14 files changed

+557
-404
lines changed

14 files changed

+557
-404
lines changed

build_python_framework.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/zsh
22
# Build script for Python 3 framework for Sal scripts
33
TOOLSDIR=$(dirname "$0")
4-
PYTHON_VERSION=3.8.2
4+
PYTHON_VERSION=3.8.3
55

66
# build the framework
77
/tmp/relocatable-python-git/make_relocatable_python_framework.py \

payload/usr/local/munki/postflight.d/sal-postflight

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,21 @@ def check_server_connection():
5757

5858
def check_server_online():
5959
# is the offline report pref true?
60-
if not sal.pref('SendOfflineReport'):
60+
if not sal.sal_pref('SendOfflineReport'):
6161
return
6262
# read report
6363
report = munki_checkin.get_managed_install_report()
6464
# check for errors and warnings
6565
if not check_for_errors(report):
66-
sal.set_pref('LastRunWasOffline', False)
66+
sal.set_sal_pref('LastRunWasOffline', False)
6767
return
6868
# if they're there check is server is really offline
6969
if check_server_connection():
70-
sal.set_pref('LastRunWasOffline', True)
70+
sal.set_sal_pref('LastRunWasOffline', True)
7171
return
7272

7373
# If we get here, it's online
74-
sal.set_pref('LastRunWasOffline', False)
74+
sal.set_sal_pref('LastRunWasOffline', False)
7575

7676
def write_touch_file():
7777
if os.path.exists(TOUCH_FILE_PATH):

payload/usr/local/munki/preflight.d/sal-preflight

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ Retrieves plugin scripts to run on client.
66

77

88
import argparse
9-
import json
109
import os
1110
import pathlib
1211
import shutil
1312
import sys
1413
import urllib
1514

15+
import requests.exceptions
1616
import sal
1717
sys.path.append('/usr/local/munki')
1818
from munkilib import munkicommon
@@ -23,11 +23,12 @@ EXTERNAL_SCRIPTS_DIR = '/usr/local/sal/external_scripts'
2323

2424
def main():
2525
set_verbosity()
26+
sal.setup_sal_client()
2627

27-
if sal.pref('SyncScripts') == True:
28+
if sal.sal_pref('SyncScripts') == True:
2829
if not os.path.exists(EXTERNAL_SCRIPTS_DIR):
2930
os.makedirs(EXTERNAL_SCRIPTS_DIR)
30-
server_scripts = get_checksum()
31+
server_scripts = get_checksums()
3132
if server_scripts:
3233
create_dirs(server_scripts)
3334
download_scripts(server_scripts)
@@ -39,34 +40,39 @@ def main():
3940
def get_prefs():
4041
# Check for mandatory prefs and bail if any are missing.
4142
required_prefs = {}
42-
required_prefs["key"] = sal.pref('key')
43-
required_prefs["ServerURL"] = sal.pref('ServerURL').rstrip('/')
43+
required_prefs["key"] = sal.sal_pref('key')
44+
required_prefs["ServerURL"] = sal.sal_pref('ServerURL').rstrip('/')
4445

4546
for key, val in required_prefs.items():
4647
if not val:
4748
sys.exit(f'Required Sal preference "{key}" is not set.')
4849
return required_prefs
4950

5051

51-
def get_checksum():
52+
def get_checksums():
5253
"""Downloads the checksum of existing scripts.
5354
5455
Returns:
5556
A dict with the script name, plugin name and hash of the script
5657
or None if no external scripts are used.
5758
"""
58-
preflight_url = f"{sal.pref('ServerURL')}/preflight-v2/"
59-
stdout, stderr = sal.send_report(preflight_url, form_data={'os_family': 'Darwin'})
60-
61-
if stderr:
62-
munkicommon.display_debug2(stderr)
63-
stdout_list = stdout.split("\n")
64-
if "<h1>Page not found</h1>" not in stdout_list:
65-
munkicommon.display_debug2(stdout)
59+
sal_client = sal.get_sal_client()
60+
error_msg = None
61+
try:
62+
response = sal_client.post('preflight-v2/', data={'os_family': 'Darwin'})
63+
except requests.exceptions.RequestException as error:
64+
munkicommon.display_debug2(str(error_msg))
65+
return
66+
if response.status_code != requests.status_codes.codes.okay:
67+
munkicommon.display_debug2(f'Request failed with HTTP {response.status_code}')
68+
return
69+
if response and "<h1>Page not found</h1>" in response.text:
70+
munkicommon.display_debug2(response.text)
71+
return
6672

6773
try:
68-
return json.loads(stdout)
69-
except:
74+
return response.json()
75+
except ValueError:
7076
munkicommon.display_debug2("Didn't receive valid JSON.")
7177
return None
7278

@@ -91,22 +97,26 @@ def download_scripts(server_scripts):
9197

9298
def download_and_write_script(server_script):
9399
"""Gets script from the server and makes it execuatble."""
94-
script_url = (
95-
f"{sal.pref('ServerURL')}/preflight-v2/get-script/"
96-
f"{server_script['plugin']}/{server_script['filename']}/")
97-
stdout, stderr = sal.curl(script_url)
98-
if stderr:
100+
try:
101+
response = sal.get_sal_client().get(
102+
f"preflight-v2/get-script/{server_script['plugin']}/{server_script['filename']}/")
103+
except requests.exceptions.RequestException as error:
104+
munkicommon.display_debug2('Error received downloading script:')
105+
munkicommon.display_debug2(str(error))
106+
return
107+
108+
if response.status_code != requests.status_codes.codes.okay:
99109
munkicommon.display_debug2('Error received downloading script:')
100-
munkicommon.display_debug2(stderr)
110+
munkicommon.display_debug2(response.text)
101111

102112
script = open(
103113
os.path.join(EXTERNAL_SCRIPTS_DIR, server_script['plugin'], server_script['filename']),
104114
'w')
105115
try:
106-
data = json.loads(stdout)
107-
except:
116+
data = response.json()
117+
except ValueError:
108118
munkicommon.display_debug2('Did not receive valid JSON when requesting script content.')
109-
return False
119+
return
110120

111121
script.write(data[0]['content'])
112122
script.close()

0 commit comments

Comments
 (0)