Skip to content

Commit

Permalink
Make MSC responsible for triggering a restart if needed and GUI user …
Browse files Browse the repository at this point in the history
…is present to avoid process synchronization issues
  • Loading branch information
gregneagle committed Feb 25, 2015
1 parent c43a1e6 commit b5bc93b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
Expand Up @@ -156,6 +156,8 @@ def updateStatus_(self, notification):
#NSApp.activateIgnoringOtherApps_(YES) #? do we really want to do this?
#self.statusWindowController.window().orderFrontRegardless()
elif command == 'showRestartAlert':
if self.session_started:
self.sessionEnded_(0)
self.doRestartAlert()
elif command == 'quit':
self.sessionEnded_(0)
Expand Down Expand Up @@ -223,7 +225,7 @@ def restartAlertDidEnd_returnCode_contextInfo_(
self, alert, returncode, contextinfo):
'''Called when restartAlert ends'''
self._status_restartAlertDismissed = 1
# TO-DO: initiate actual restart
munki.restartNow()

def setMessage_(self, messageText):
'''Display main status message'''
Expand Down
19 changes: 19 additions & 0 deletions code/apps/Managed Software Center/Managed Software Center/munki.py
Expand Up @@ -46,6 +46,25 @@ def call(cmd):
(output, err) = proc.communicate()
return proc.returncode


def osascript(osastring):
"""Wrapper to run AppleScript commands"""
cmd = ['/usr/bin/osascript', '-e', osastring]
proc = subprocess.Popen(cmd, shell=False, bufsize=1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = proc.communicate()
if proc.returncode != 0:
print >> sys.stderr, 'Error: ', err
if out:
return str(out).decode('UTF-8').rstrip('\n')


def restartNow():
'''Trigger a restart'''
osascript('tell application "System Events" to restart')


BUNDLE_ID = u'ManagedInstalls'

def reload_prefs():
Expand Down
Expand Up @@ -290,6 +290,10 @@ def updateStatus_(self, notification):
if command == 'activate':
self.window.orderFrontRegardless()
elif command == 'showRestartAlert':
# clean up timer
if self.timer:
self.timer.invalidate()
self.timer = None
self.doRestartAlert()
elif command == 'quit':
self.cleanUpStatusSession()
Expand Down
2 changes: 1 addition & 1 deletion code/apps/MunkiStatus/MunkiStatus/munki.py
Expand Up @@ -23,6 +23,7 @@
import os
import stat
import subprocess
from SystemConfiguration import SCDynamicStoreCopyConsoleUser

INSTALLATLOGOUTFILE = "/private/tmp/com.googlecode.munki.installatlogout"

Expand All @@ -36,7 +37,6 @@ def call(cmd):


def getconsoleuser():
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
cfuser = SCDynamicStoreCopyConsoleUser( None, None, None )
return cfuser[0]

Expand Down
14 changes: 10 additions & 4 deletions code/client/managedsoftwareupdate
Expand Up @@ -272,13 +272,19 @@ def doRestart():
dummy_retcode = subprocess.call(['/sbin/shutdown', '-r', 'now'])
else:
if munkicommon.munkistatusoutput:
# someone is logged in and we're using munkistatus
# someone is logged in and we're using Managed Software Center.
# We need to notifiy the active user that a restart is required.
# We actually should almost never get here; generally Munki knows
# a restart is needed before even starting the updates and forces
# a logout before applying the updates
munkicommon.display_info(
'Notifying currently logged-in user to restart.')
munkistatus.activate()
munkistatus.restartAlert()
munkicommon.osascript(
'tell application "System Events" to restart')
# Managed Software Center will trigger a restart
# when the alert is dismissed. If a user gets clever and subverts
# this restart (perhaps by force-quitting the app),
# that's their problem...
else:
print 'Please restart immediately.'

Expand Down Expand Up @@ -357,7 +363,7 @@ def sendStartNotification():


def sendEndNotification():
'''Sends a start notification via NSDistributedNotificationCenter'''
'''Sends an ended notification via NSDistributedNotificationCenter'''
userInfo = {'pid': os.getpid()}
sendDistrubutedNotification(
'com.googlecode.munki.managedsoftwareupdate.ended',
Expand Down

0 comments on commit b5bc93b

Please sign in to comment.