@@ -6,13 +6,13 @@ Retrieves plugin scripts to run on client.
6
6
7
7
8
8
import argparse
9
- import json
10
9
import os
11
10
import pathlib
12
11
import shutil
13
12
import sys
14
13
import urllib
15
14
15
+ import requests .exceptions
16
16
import sal
17
17
sys .path .append ('/usr/local/munki' )
18
18
from munkilib import munkicommon
@@ -23,11 +23,12 @@ EXTERNAL_SCRIPTS_DIR = '/usr/local/sal/external_scripts'
23
23
24
24
def main ():
25
25
set_verbosity ()
26
+ sal .setup_sal_client ()
26
27
27
- if sal .pref ('SyncScripts' ) == True :
28
+ if sal .sal_pref ('SyncScripts' ) == True :
28
29
if not os .path .exists (EXTERNAL_SCRIPTS_DIR ):
29
30
os .makedirs (EXTERNAL_SCRIPTS_DIR )
30
- server_scripts = get_checksum ()
31
+ server_scripts = get_checksums ()
31
32
if server_scripts :
32
33
create_dirs (server_scripts )
33
34
download_scripts (server_scripts )
@@ -39,34 +40,39 @@ def main():
39
40
def get_prefs ():
40
41
# Check for mandatory prefs and bail if any are missing.
41
42
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 ('/' )
44
45
45
46
for key , val in required_prefs .items ():
46
47
if not val :
47
48
sys .exit (f'Required Sal preference "{ key } " is not set.' )
48
49
return required_prefs
49
50
50
51
51
- def get_checksum ():
52
+ def get_checksums ():
52
53
"""Downloads the checksum of existing scripts.
53
54
54
55
Returns:
55
56
A dict with the script name, plugin name and hash of the script
56
57
or None if no external scripts are used.
57
58
"""
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
66
72
67
73
try :
68
- return json . loads ( stdout )
69
- except :
74
+ return response . json ( )
75
+ except ValueError :
70
76
munkicommon .display_debug2 ("Didn't receive valid JSON." )
71
77
return None
72
78
@@ -91,22 +97,26 @@ def download_scripts(server_scripts):
91
97
92
98
def download_and_write_script (server_script ):
93
99
"""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 :
99
109
munkicommon .display_debug2 ('Error received downloading script:' )
100
- munkicommon .display_debug2 (stderr )
110
+ munkicommon .display_debug2 (response . text )
101
111
102
112
script = open (
103
113
os .path .join (EXTERNAL_SCRIPTS_DIR , server_script ['plugin' ], server_script ['filename' ]),
104
114
'w' )
105
115
try :
106
- data = json . loads ( stdout )
107
- except :
116
+ data = response . json ( )
117
+ except ValueError :
108
118
munkicommon .display_debug2 ('Did not receive valid JSON when requesting script content.' )
109
- return False
119
+ return
110
120
111
121
script .write (data [0 ]['content' ])
112
122
script .close ()
0 commit comments