Changeset 5522
- Timestamp:
- 11/02/09 13:15:26 (3 weeks ago)
- Files:
-
- 1 modified
-
trunk/bcfg2/src/lib/Client/Tools/launchd.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/bcfg2/src/lib/Client/Tools/launchd.py
r5408 r5522 4 4 import os 5 5 import Bcfg2.Client.Tools 6 import popen2 7 8 '''Locate plist file that provides given reverse-fqdn name 9 /Library/LaunchAgents Per-user agents provided by the administrator. 10 /Library/LaunchDaemons System wide daemons provided by the administrator. 11 /System/Library/LaunchAgents Mac OS X Per-user agents. 12 /System/Library/LaunchDaemons Mac OS X System wide daemons.''' 13 plistLocations = ["/Library/LaunchDaemons", "/System/Library/LaunchDaemons"] 14 plistMapping = {} 15 for directory in plistLocations: 16 for daemon in os.listdir(directory): 17 try: 18 if daemon.endswith(".plist"): 19 d = daemon[:-6] 20 else: 21 d = daemon 22 (stdout, _) = popen2.popen2('defaults read %s/%s Label' % (directory, d)) 23 label = stdout.read().strip() 24 plistMapping[label] = "%s/%s" % (directory, daemon) 25 except KeyError: #perhaps this could be more robust 26 pass 6 27 7 28 class launchd(Bcfg2.Client.Tools.Tool): … … 17 38 ''' 18 39 def FindPlist(self, entry): 19 '''Locate plist file that provides given reverse-fqdn name 20 /Library/LaunchAgents Per-user agents provided by the administrator. 21 /Library/LaunchDaemons System wide daemons provided by the administrator. 22 /System/Library/LaunchAgents Mac OS X Per-user agents. 23 /System/Library/LaunchDaemons Mac OS X System wide daemons.''' 24 plistLocations = ["/Library/LaunchDaemons", "/System/Library/LaunchDaemons"] 25 plistMapping = {} 26 for directory in plistLocations: 27 for daemon in os.listdir(directory): 28 try: 29 if daemon.endswith(".plist"): 30 d = daemon[:(len(daemon)-6)] 31 else: 32 d = daemon 33 plistMapping[self.cmd.run( \ 34 "defaults read %s/%s Label" % (directory, d))[1][0]] = \ 35 "%s/%s"%(directory, daemon) 36 except KeyError: #perhaps this could be more robust 37 pass 38 try: 39 return plistMapping[entry.get('name')] 40 except KeyError: 41 return None 40 return plistMapping.get(entry.get('name'), None) 42 41 43 42 def os_version(self): … … 78 77 def InstallService(self, entry): 79 78 '''Enable or Disable launchd Item''' 79 name = entry.get('name') 80 80 if entry.get('status') == 'on': 81 self.logger.error("Installing service %s" % name) 81 82 cmdrc = self.cmd.run("/bin/launchctl load -w %s" % self.FindPlist(entry))[0] 83 cmdrc = self.cmd.run("/bin/launchctl start %s" % name) 82 84 else: 85 self.logger.error("Uninstalling service %s" % name) 86 cmdrc = self.cmd.run("/bin/launchctl stop %s" % name) 83 87 cmdrc = self.cmd.run("/bin/launchctl unload -w %s" % self.FindPlist(entry))[0] 84 return cmdrc == 088 return cmdrc[0] == 0 85 89 86 90 def Remove(self, svcs): … … 107 111 self.logger.error("Insufficient information to restart service %s" % (entry.get('name'))) 108 112 else: 113 name = entry.get('name') 109 114 if entry.get('status') == 'on' and self.FindPlist(entry): 110 self.logger.info("Reloading launchd service %s" % (entry.get("name")))115 self.logger.info("Reloading launchd service %s" % name) 111 116 #stop? 117 self.cmd.run("/bin/launchctl stop %s" % name) 112 118 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?! 113 119 self.cmd.run("/bin/launchctl load -w %s" % (self.FindPlist(entry))) 120 self.cmd.run("/bin/launchctl start %s" % name) 114 121 else: 115 122 #only if necessary.... 123 self.cmd.run("/bin/launchctl stop %s" % name) 116 124 self.cmd.run("/bin/launchctl unload -w %s" % (self.FindPlist(entry))) 117 125