Created
November 2, 2015 20:58
-
-
Save arubdesu/24db242bac5708ea6daf to your computer and use it in GitHub Desktop.
example for the munki wiki
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>OnDemand</key> | |
<true/> | |
<key>_metadata</key> | |
<dict> | |
<key>created_by</key> | |
<string>abanks</string> | |
<key>creation_date</key> | |
<date>2015-11-02T20:19:08Z</date> | |
<key>munki_version</key> | |
<string>2.3.1.2558</string> | |
<key>os_version</key> | |
<string>10.11.2</string> | |
</dict> | |
<key>autoremove</key> | |
<false/> | |
<key>catalogs</key> | |
<array> | |
<string>testing</string> | |
</array> | |
<key>category</key> | |
<string>SelfService</string> | |
<key>display_name</key> | |
<string>Change Timezone</string> | |
<key>installer_type</key> | |
<string>nopkg</string> | |
<key>minimum_os_version</key> | |
<string>10.4.0</string> | |
<key>name</key> | |
<string>change_tz</string> | |
<key>preinstall_script</key> | |
<string>#!/usr/bin/python | |
'''Uses a web geoip service and cocoaDialog to allow end users to change | |
the time zone on their Mac (assuming they can get on the internet)''' | |
import json | |
import subprocess | |
import sys | |
import urllib2 | |
from gmacpyutil import cocoadialog | |
def grab_geoip(): | |
'''returns probable timezone info via geoip lookup''' | |
geoip_checksite = 'http://freegeoip.net/json/' | |
response = urllib2.urlopen(geoip_checksite).read() | |
if response: | |
dict_response = json.loads(response) | |
tzone = dict_response.get('time_zone', None) | |
return tzone | |
else: | |
print 'foo' | |
def change_tzone(region): | |
'''heavy lifting to interact w/ sys setup''' | |
cmd = ['/usr/sbin/systemsetup', '-settimezone', region] | |
result = subprocess.check_call(cmd) | |
return result | |
def get_tzone(): | |
'''Grab current tz from sys setup''' | |
cmd = ['/usr/sbin/systemsetup', '-gettimezone'] | |
result = subprocess.check_output(cmd) | |
return result.strip()[11:] | |
def cleanup(result): | |
'''catch errors with feedback, or show success''' | |
if result == 'foo': | |
tooold_msg = cocoadialog.MsgBox(title="Sorry!") | |
tooold_msg.SetInformativeText("There was an error setting your timezone. Please schedule a time for follow-up with the servicedesk.") | |
button_pressed = tooold_msg.Show() | |
button = button_pressed.split('\n')[0] | |
sys.exit(1) | |
elif result == 'already there': | |
complete_msg = cocoadialog.MsgBox(title="Pardon") | |
complete_msg.SetInformativeText("Your computer already seems to be set to the correct timezone. Please schedule a time for follow-up with the servicedesk if you still need assistance.") | |
button_pressed = complete_msg.Show() | |
button = button_pressed.split('\n')[0] | |
if button: | |
sys.exit(0) | |
else: | |
complete_msg = cocoadialog.MsgBox(title="Complete") | |
complete_msg.SetText("Glad to be of assistance!") | |
button_pressed = complete_msg.Show() | |
button = button_pressed.split('\n')[0] | |
if button: | |
sys.exit(0) | |
def main(): | |
'''gimme some main''' | |
region = grab_geoip() | |
current_tz = get_tzone() | |
if region == current_tz: | |
cleanup('already there') | |
cd_title = "TimeZone Change Utility" | |
welcome_prompt = cocoadialog.MsgBox(title=cd_title) | |
welcome_prompt.SetText("We've detected this timezone: " + region) | |
welcome_prompt.SetInformativeText("Shall we go ahead and change to that timezone?") | |
welcome_prompt.SetButton1('Okay') | |
welcome_prompt.SetButton2('Cancel') | |
welcome_showed = welcome_prompt.Show() | |
button, __ = welcome_showed.split('\n')[:2] | |
if button == 'Okay': | |
exit_code = change_tzone(region) | |
if exit_code != 0: | |
cleanup('foo') | |
else: | |
cleanup('success') | |
if __name__ == '__main__': | |
main() | |
</string> | |
<key>version</key> | |
<string>1.0</string> | |
</dict> | |
</plist> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment