logouthelper: remove import of munkicommon; replace with the refactored modules

This commit is contained in:
Greg Neagle
2017-03-02 09:04:45 -08:00
parent 3ca9d000a0
commit e04345b620

View File

@@ -24,47 +24,61 @@ a certain deadline.
"""
# standard libs
import os
import time
from munkilib import munkicommon
# our libs
from munkilib import info
from munkilib import munkilog
from munkilib import osutils
from munkilib import prefs
from munkilib import processes
from munkilib import FoundationPlist
# Apple frameworks via PyObjC
# PyLint cannot properly find names inside Cocoa libraries, so issues bogus
# No name 'Foo' in module 'Bar' warnings. Disable them.
# pylint: disable=E0611
from Foundation import NSDate
from Foundation import NSDictionary
from Foundation import NSDistributedNotificationCenter
from Foundation import NSNotificationDeliverImmediately
from Foundation import NSNotificationPostToAllSessions
# pylint: enable=E0611
NOTIFICATION_MINS = [240, 180, 120, 90, 60, 45, 30, 15, 10, 5]
MANDATORY_NOTIFICATIONS = [60, 30, 10, 5]
PROCESS_ID = 'com.googlecode.munki.logouthelper'
def log(msg):
'''Logs messages from this tool with an identifier'''
PROCESS_ID = 'com.googlecode.munki.logouthelper'
munkicommon.log('%s: %s' % (PROCESS_ID, msg))
munkilog.log('%s: %s' % (PROCESS_ID, msg))
def earliestForceInstallDate():
def earliest_force_install_date():
'''Check installable packages for force_install_after_dates
Returns None or earliest force_install_after_date converted to local time
'''
earliest_date = None
ManagedInstallDir = munkicommon.pref('ManagedInstallDir')
managed_install_dir = prefs.pref('ManagedInstallDir')
installinfo_types = {
'InstallInfo.plist' : 'managed_installs',
'AppleUpdates.plist': 'AppleUpdates'
}
installinfopath = os.path.join(ManagedInstallDir, 'InstallInfo.plist')
installinfopath = os.path.join(managed_install_dir, 'InstallInfo.plist')
try:
installinfo = FoundationPlist.readPlist(installinfopath)
except FoundationPlist.NSPropertyListSerializationException:
return None
for plist_name in installinfo_types.keys():
for plist_name in installinfo_types:
key_to_check = installinfo_types[plist_name]
plist_path = os.path.join(ManagedInstallDir, plist_name)
plist_path = os.path.join(managed_install_dir, plist_name)
try:
installinfo = FoundationPlist.readPlist(plist_path)
except FoundationPlist.NSPropertyListSerializationException:
@@ -74,7 +88,7 @@ def earliestForceInstallDate():
force_install_date = install.get('force_install_after_date')
if force_install_date:
force_install_date = munkicommon.subtract_tzoffset_from_date(
force_install_date = info.subtract_tzoffset_from_date(
force_install_date)
if not earliest_date or force_install_date < earliest_date:
earliest_date = force_install_date
@@ -82,23 +96,23 @@ def earliestForceInstallDate():
return earliest_date
def alertUserOfForcedLogout(info=None):
def alert_user_of_forced_logout(info_dict=None):
'''Uses Managed Software Center.app to notify the user of an
upcoming forced logout.
Args:
info: dict of data to send with the notification.
'''
consoleuser = munkicommon.getconsoleuser()
if not munkicommon.find_processes(
exe="/Applications/Managed Software Center.app",
user=consoleuser):
consoleuser = osutils.getconsoleuser()
if not processes.find_processes(
exe="/Applications/Managed Software Center.app",
user=consoleuser):
# Managed Software Center.app isn't running.
# Use our LaunchAgent to start
# Managed Software Center.app in the user context.
launchfile = '/var/run/com.googlecode.munki.ManagedSoftwareCenter'
f = open(launchfile, 'w')
f.close()
fileref = open(launchfile, 'w')
fileref.close()
# now wait a bit for it to launch before proceeding
# because if we don't, sending the logoutwarn notification
# may fall on deaf ears.
@@ -107,8 +121,8 @@ def alertUserOfForcedLogout(info=None):
os.unlink(launchfile)
# if set, convert Python dictionary to NSDictionary.
if info is not None:
info = NSDictionary.dictionaryWithDictionary_(info)
if info_dict is not None:
info_dict = NSDictionary.dictionaryWithDictionary_(info_dict)
# cause MSC.app to display the Forced Logout warning
dnc = NSDistributedNotificationCenter.defaultCenter()
dnc.postNotificationName_object_userInfo_options_(
@@ -117,15 +131,15 @@ def alertUserOfForcedLogout(info=None):
NSNotificationDeliverImmediately + NSNotificationPostToAllSessions)
# make sure flag is in place to cause munki to install at logout
f = open('/private/tmp/com.googlecode.munki.installatlogout', 'w')
f.close()
fileref = open('/private/tmp/com.googlecode.munki.installatlogout', 'w')
fileref.close()
def main():
'''Check for logged-in users and upcoming forced installs;
notify the user if needed; sleep a minute and do it again.'''
if munkicommon.pref('LogToSyslog'):
munkicommon.configure_syslog()
if prefs.pref('LogToSyslog'):
munkilog.configure_syslog()
log('launched')
sent_notifications = []
@@ -135,7 +149,7 @@ def main():
minimum_notifications_logout_time = NSDate.date().addTimeInterval_(
60 * max(MANDATORY_NOTIFICATIONS) + 30)
while True:
if not munkicommon.currentGUIusers():
if not osutils.currentGUIusers():
# no-one is logged in, so bail
log('no-one logged in')
time.sleep(10) # makes launchd happy
@@ -144,7 +158,7 @@ def main():
# we check each time because items might have been added or removed
# from the list; or their install date may have been changed.
next_logout_time = earliestForceInstallDate()
next_logout_time = earliest_force_install_date()
if not next_logout_time:
# no forced logout needed, so bail
log('no forced installs found')
@@ -179,23 +193,23 @@ def main():
break
minutes_until_logout = int(logout_time.timeIntervalSinceNow() / 60)
info = {'logout_time': logout_time}
info_dict = {'logout_time': logout_time}
if minutes_until_logout in NOTIFICATION_MINS:
sent_notifications.append(minutes_until_logout)
log('Warning user of %s minutes until forced logout'
% minutes_until_logout)
alertUserOfForcedLogout(info)
alert_user_of_forced_logout(info_dict)
elif minutes_until_logout < 1:
log('Forced logout in 60 seconds')
alertUserOfForcedLogout(info)
alert_user_of_forced_logout(info_dict)
time.sleep(60)
if minutes_until_logout < 1:
break
if munkicommon.currentGUIusers() and earliestForceInstallDate():
if osutils.currentGUIusers() and earliest_force_install_date():
log('Beginning forced logout')
munkicommon.force_logout_now()
processes.force_logout_now()
log('exited')
exit(0)