forked from chilcote/outset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutset
executable file
·255 lines (231 loc) · 9.18 KB
/
outset
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env python
'''
This script scripts and installs pkgs at boot and/or login.
'''
##############################################################################
# Copyright 2014 Joseph Chilcote
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
##############################################################################
import os
import sys
import subprocess
import shutil
import logging
import datetime
import platform
import socket
import time
import plistlib
import datetime
from stat import *
outset_dir = '/usr/local/outset'
login_every_dir = os.path.join(outset_dir, 'login-every')
login_once_dir = os.path.join(outset_dir, 'login-once')
firstboot_scripts_dir = os.path.join(outset_dir, 'firstboot-scripts')
firstboot_packages_dir = os.path.join(outset_dir, 'firstboot-packages')
everyboot_scripts_dir = os.path.join(outset_dir, 'everyboot-scripts')
launch_daemon = '/Library/LaunchDaemons/com.github.outset.boot.plist'
if os.geteuid() == 0:
log_file = '/var/log/outset.log'
else:
if not os.path.exists(os.path.expanduser('~/Library/Logs')):
os.makedirs(os.path.expanduser('~/Library/Logs'))
log_file = os.path.expanduser('~/Library/Logs/outset.log')
run_once_plist = os.path.expanduser('~/Library/Preferences/com.github.outset.once.plist')
logging.basicConfig(format='%(asctime)s - %(levelname)s: %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p',
level=logging.DEBUG,
filename=log_file)
def network_up():
'''Returns True if network is up'''
cmd = ['/sbin/ifconfig', '-a', 'inet']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(out, err) = proc.communicate()
for line in str(out).splitlines():
if 'inet' in line:
if not line.split()[1] in ['127.0.0.1', '0.0.0.0']:
return True
return False
def wait_for_network():
'''Waits for a valid IP before continuing'''
while True:
if network_up():
break
else:
logging.info('Waiting for network')
time.sleep(10)
def disable_loginwindow():
'''Disables the loginwindow process'''
logging.info('Disabling loginwindow process')
cmd = ['/bin/launchctl', 'unload',
'/System/Library/LaunchDaemons/com.apple.loginwindow.plist']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(out, err) = proc.communicate()
def enable_loginwindow():
'''Enables the loginwindow process'''
logging.info('Enabling loginwindow process')
cmd = ['/bin/launchctl', 'load',
'/System/Library/LaunchDaemons/com.apple.loginwindow.plist']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(out, err) = proc.communicate()
def get_hardwaremodel():
'''Returns the hardware model of the Mac'''
cmd = ['/usr/sbin/sysctl', '-n', 'hw.model']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(out, err) = proc.communicate()
return out.strip()
def get_serialnumber():
'''Returns the serial number of the Mac'''
cmd = "/usr/sbin/ioreg -c \"IOPlatformExpertDevice\" | awk -F '\"' \
'/IOPlatformSerialNumber/ {print $4}'"
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(out, err) = proc.communicate()
return out.strip()
def get_buildversion():
'''Returns the os build version of the Mac'''
cmd = ['/usr/sbin/sysctl', '-n', 'kern.osversion']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(out, err) = proc.communicate()
return out.strip()
def get_osversion():
'''Returns the os x version of this Mac'''
return platform.mac_ver()[0]
def sys_report():
'''Logs system information to log file'''
logging.debug('Model: %s' % get_hardwaremodel())
logging.debug('Serial: %s' % get_serialnumber())
logging.debug('OS: %s' % get_osversion())
logging.debug('Build: %s' % get_buildversion())
def cleanup(pathname):
'''Deletes given script'''
try:
os.remove(pathname)
except:
shutil.rmtree(pathname)
def mount_dmg(dmg):
'''Attaches dmg'''
dmg_path = os.path.join(dmg)
cmd = ['hdiutil', 'attach', '-nobrowse','-noverify',
'-noautoopen', dmg_path]
logging.info('Attaching %s' % dmg_path)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(out, err) = proc.communicate()
return out.split('\n')[-2].split('\t')[-1]
def detach_dmg(dmg_mount):
'''Detaches dmg'''
logging.info('Detaching %s' % dmg_mount)
cmd = ['hdiutil', 'detach', '-force', dmg_mount]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(out, err) = proc.communicate()
if err:
logging.error('Unable to detach %s: %s' % (dmg_mount, err))
def install_package(pkg):
'''Installs pkg onto boot drive'''
if pkg.lower().endswith('dmg'):
dmg_mount = mount_dmg(pkg)
for f in os.listdir(dmg_mount):
if f.lower().endswith(('pkg', 'mpkg')):
pkg_to_install = os.path.join(dmg_mount, f)
elif pkg.lower().endswith(('pkg', 'mpkg')):
dmg_mount = False
pkg_to_install = pkg
logging.info('Installing %s' % pkg_to_install)
cmd = ['/usr/sbin/installer', '-pkg', pkg_to_install, '-target', '/',]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(out, err) = proc.communicate()
if err:
logging.info('Failure installing %s: %s' % (pkg_to_install, err))
if dmg_mount:
time.sleep(5)
detach_dmg(dmg_mount)
def check_perms(pathname):
mode = os.stat(pathname).st_mode
owner = os.stat(pathname).st_uid
if owner == 0 and (mode & S_IXOTH) and not (mode & S_IWOTH):
return True
return False
def run_script(pathname):
try:
proc = subprocess.Popen(pathname, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
logging.info('Processing %s' % pathname)
(out, err) = proc.communicate()
if err:
logging.error('Failure processing %s: %s' % (pathname, err))
except OSError:
logging.error('Failure processing %s: %s' % (pathname, OSError))
def process_items(path, delete_items=False, once=False):
'''Processes scripts/packages to run once at first boot'''
if not os.path.exists(path):
logging.error('%s does not exists. Exiting' % path)
sys.exit(1)
for f in os.listdir(path):
pathname = os.path.join(path, f)
if check_perms(pathname):
if pathname.lower().endswith(('py', 'rb', 'sh')):
if once:
try:
d = plistlib.readPlist(run_once_plist)
except IOError:
d = {}
if pathname not in d:
run_script(pathname)
d[pathname] = datetime.datetime.now()
plistlib.writePlist(d, run_once_plist)
else:
run_script(pathname)
elif pathname.lower().endswith(('pkg', 'mpkg', 'dmg')):
install_package(pathname)
else:
logging.error('No valid script or package found')
else:
logging.error('Bad permissions: %s' % pathname)
if delete_items:
cleanup(pathname)
def main():
'''Main method'''
if not len(sys.argv) == 2:
logging.error('No or too many argument(s) given')
sys.exit(0)
if sys.argv[1] == '--boot':
if os.listdir(firstboot_scripts_dir):
disable_loginwindow()
sys_report()
wait_for_network()
process_items(firstboot_scripts_dir, delete_items=True)
enable_loginwindow()
if os.listdir(everyboot_scripts_dir):
process_items(everyboot_scripts_dir)
if os.listdir(firstboot_packages_dir):
process_items(firstboot_packages_dir, delete_items=True)
logging.info('boot processing complete')
elif sys.argv[1] == '--login':
if os.listdir(login_every_dir):
process_items(login_every_dir)
if os.listdir(login_once_dir):
process_items(login_once_dir, once=True)
else:
logging.error('Argument did not match "--boot" or "--login"')
sys.exit(0)
if __name__ == '__main__':
main()