summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKamil Kisiel <kamil@kamilkisiel.net>2009-11-02 19:15:26 +0000
committerKamil Kisiel <kamil@kamilkisiel.net>2009-11-02 19:15:26 +0000
commit6e851ee0526ab732cc05968b6ba7cc4ccee908f9 (patch)
tree208374aa6d9b156eeabfeef81c84eae66afa5e27
parentc12d704ff03e0d183149a37724d341b90d42a4c3 (diff)
downloadbcfg2-6e851ee0526ab732cc05968b6ba7cc4ccee908f9.tar.gz
bcfg2-6e851ee0526ab732cc05968b6ba7cc4ccee908f9.tar.bz2
bcfg2-6e851ee0526ab732cc05968b6ba7cc4ccee908f9.zip
Greatly sped up launchd client tool. Also made services reload correctly.
git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@5522 ce84e21b-d406-0410-9b95-82705330c041
-rw-r--r--src/lib/Client/Tools/launchd.py58
1 files changed, 33 insertions, 25 deletions
diff --git a/src/lib/Client/Tools/launchd.py b/src/lib/Client/Tools/launchd.py
index baca30bdd..ba2c3086f 100644
--- a/src/lib/Client/Tools/launchd.py
+++ b/src/lib/Client/Tools/launchd.py
@@ -3,6 +3,27 @@ __revision__ = '$Revision$'
import os
import Bcfg2.Client.Tools
+import popen2
+
+'''Locate plist file that provides given reverse-fqdn name
+/Library/LaunchAgents Per-user agents provided by the administrator.
+/Library/LaunchDaemons System wide daemons provided by the administrator.
+/System/Library/LaunchAgents Mac OS X Per-user agents.
+/System/Library/LaunchDaemons Mac OS X System wide daemons.'''
+plistLocations = ["/Library/LaunchDaemons", "/System/Library/LaunchDaemons"]
+plistMapping = {}
+for directory in plistLocations:
+ for daemon in os.listdir(directory):
+ try:
+ if daemon.endswith(".plist"):
+ d = daemon[:-6]
+ else:
+ d = daemon
+ (stdout, _) = popen2.popen2('defaults read %s/%s Label' % (directory, d))
+ label = stdout.read().strip()
+ plistMapping[label] = "%s/%s" % (directory, daemon)
+ except KeyError: #perhaps this could be more robust
+ pass
class launchd(Bcfg2.Client.Tools.Tool):
'''Support for Mac OS X Launchd Services'''
@@ -16,29 +37,7 @@ class launchd(Bcfg2.Client.Tools.Tool):
and Name is acually a reverse-fqdn (or the label)
'''
def FindPlist(self, entry):
- '''Locate plist file that provides given reverse-fqdn name
- /Library/LaunchAgents Per-user agents provided by the administrator.
- /Library/LaunchDaemons System wide daemons provided by the administrator.
- /System/Library/LaunchAgents Mac OS X Per-user agents.
- /System/Library/LaunchDaemons Mac OS X System wide daemons.'''
- plistLocations = ["/Library/LaunchDaemons", "/System/Library/LaunchDaemons"]
- plistMapping = {}
- for directory in plistLocations:
- for daemon in os.listdir(directory):
- try:
- if daemon.endswith(".plist"):
- d = daemon[:(len(daemon)-6)]
- else:
- d = daemon
- plistMapping[self.cmd.run( \
- "defaults read %s/%s Label" % (directory, d))[1][0]] = \
- "%s/%s"%(directory, daemon)
- except KeyError: #perhaps this could be more robust
- pass
- try:
- return plistMapping[entry.get('name')]
- except KeyError:
- return None
+ return plistMapping.get(entry.get('name'), None)
def os_version(self):
version = ""
@@ -77,11 +76,16 @@ class launchd(Bcfg2.Client.Tools.Tool):
def InstallService(self, entry):
'''Enable or Disable launchd Item'''
+ name = entry.get('name')
if entry.get('status') == 'on':
+ self.logger.error("Installing service %s" % name)
cmdrc = self.cmd.run("/bin/launchctl load -w %s" % self.FindPlist(entry))[0]
+ cmdrc = self.cmd.run("/bin/launchctl start %s" % name)
else:
+ self.logger.error("Uninstalling service %s" % name)
+ cmdrc = self.cmd.run("/bin/launchctl stop %s" % name)
cmdrc = self.cmd.run("/bin/launchctl unload -w %s" % self.FindPlist(entry))[0]
- return cmdrc == 0
+ return cmdrc[0] == 0
def Remove(self, svcs):
'''Remove Extra launchd entries'''
@@ -106,12 +110,16 @@ class launchd(Bcfg2.Client.Tools.Tool):
if not self.canInstall(entry):
self.logger.error("Insufficient information to restart service %s" % (entry.get('name')))
else:
+ name = entry.get('name')
if entry.get('status') == 'on' and self.FindPlist(entry):
- self.logger.info("Reloading launchd service %s" % (entry.get("name")))
+ self.logger.info("Reloading launchd service %s" % name)
#stop?
+ self.cmd.run("/bin/launchctl stop %s" % name)
self.cmd.run("/bin/launchctl unload -w %s" % (self.FindPlist(entry)))#what if it disappeared? how do we stop services that are currently running but the plist disappeared?!
self.cmd.run("/bin/launchctl load -w %s" % (self.FindPlist(entry)))
+ self.cmd.run("/bin/launchctl start %s" % name)
else:
#only if necessary....
+ self.cmd.run("/bin/launchctl stop %s" % name)
self.cmd.run("/bin/launchctl unload -w %s" % (self.FindPlist(entry)))