def run(self): # This method can be called as the result of an agent request. We # check to see if memcached is there for each call because the server # could have stopped/started since the last time. for pool in config.Memcached.Pools.itervalues(): pool.ClientEnabled = True autoDisableMemcached(config) from twistedcaldav.directory import calendaruserproxy if calendaruserproxy.ProxyDBService is not None: # Reset the proxy db memcacher because memcached may have come or # gone since the last time through here. # TODO: figure out a better way to do this calendaruserproxy.ProxyDBService._memcacher._memcacheProtocol = None try: for command in self.commands: commandName = command['command'] methodName = "command_%s" % (commandName,) if hasattr(self, methodName): (yield getattr(self, methodName)(command)) else: self.respondWithError("Unknown command '%s'" % (commandName,)) except Exception, e: self.respondWithError("Command failed: '%s'" % (str(e),)) raise
def main(): usage = "%prog [options] ACTION" epilog = """ ACTION is one of add|modify|remove|print add: add a user record modify: modify a user record remove: remove a user record """ description = "Tool to manipulate CalendarServer augments XML file" version = "%prog v1.0" parser = OptionParser(usage=usage, description=description, version=version) parser.epilog = epilog parser.format_epilog = lambda _:epilog parser.add_option("-f", "--file", dest="configfilename", help="caldavd.plist defining Augment Service", metavar="FILE") parser.add_option("-u", "--uid", dest="uid", help="OD GUID to manipulate", metavar="UID") parser.add_option("-i", "--uidfile", dest="uidfile", help="File containing a list of GUIDs to manipulate", metavar="UIDFILE") parser.add_option("-n", "--node", dest="node", help="Partition node to assign to UID", metavar="NODE") parser.add_option("-c", "--enable-calendar", action="store_true", dest="enable_calendar", default=True, help="Enable calendaring for this UID: %default") parser.add_option("-a", "--enable-addressbooks", action="store_true", dest="enable_addressbook", default=True, help="Enable calendaring for this UID: %default") parser.add_option("-s", "--auto-schedule", action="store_true", dest="auto_schedule", default=False, help="Enable auto-schedule for this UID: %default") (options, args) = parser.parse_args() if len(args) != 1: parser.error("incorrect number of arguments") observer = StandardIOObserver() observer.start() # # Get configuration # try: loadConfig(options.configfilename) setLogLevelForNamespace(None, "warn") # Shed privileges if config.UserName and config.GroupName and os.getuid() == 0: uid = getpwnam(config.UserName).pw_uid gid = getgrnam(config.GroupName).gr_gid switchUID(uid, uid, gid) os.umask(config.umask) config.directory = getDirectory() autoDisableMemcached(config) except ConfigurationError, e: usage("Unable to start: %s" % (e,))
def utilityMain(configFileName, serviceClass, reactor=None, serviceMaker=CalDAVServiceMaker): """ Shared main-point for utilities. This function will: - Load the configuration file named by C{configFileName}, - launch a L{CalDAVServiceMaker}'s with the C{ProcessType} of C{"Utility"} - run the reactor, with start/stop events hooked up to the service's C{startService}/C{stopService} methods. It is C{serviceClass}'s responsibility to stop the reactor when it's complete. @param configFileName: the name of the configuration file to load. @type configuration: C{str} @param serviceClass: a 1-argument callable which takes an object that provides L{ICalendarStore} and/or L{IAddressbookStore} and returns an L{IService}. @param reactor: if specified, the L{IReactorTime} / L{IReactorThreads} / L{IReactorTCP} (etc) provider to use. If C{None}, the default reactor will be imported and used. """ if reactor is None: from twisted.internet import reactor try: config = loadConfig(configFileName) # If we don't have permission to access the DataRoot directory, we # can't proceed. If this fails it should raise OSError which we # catch below. os.listdir(config.DataRoot) config.ProcessType = "Utility" config.UtilityServiceClass = serviceClass autoDisableMemcached(config) maker = serviceMaker() options = CalDAVOptions service = maker.makeService(options) reactor.addSystemEventTrigger("during", "startup", service.startService) reactor.addSystemEventTrigger("before", "shutdown", service.stopService) except (ConfigurationError, OSError), e: sys.stderr.write("Error: %s\n" % (e,)) return
def run(self): # This method can be called as the result of an agent request. We # check to see if memcached is there for each call because the server # could have stopped/started since the last time. for pool in config.Memcached.Pools.itervalues(): pool.ClientEnabled = True autoDisableMemcached(config) try: for command in self.commands: commandName = command['command'] methodName = "command_%s" % (commandName,) if hasattr(self, methodName): (yield getattr(self, methodName)(command)) else: self.respondWithError("Unknown command '%s'" % (commandName,)) except Exception, e: self.respondWithError("Command failed: '%s'" % (str(e),)) raise
def run(self): # This method can be called as the result of an agent request. We # check to see if memcached is there for each call because the server # could have stopped/started since the last time. for pool in config.Memcached.Pools.itervalues(): pool.ClientEnabled = True autoDisableMemcached(config) try: for command in self.commands: commandName = command['command'] methodName = "command_%s" % (commandName,) if hasattr(self, methodName): (yield getattr(self, methodName)(command)) else: self.respondWithError("Unknown command '%s'" % (commandName,)) except Exception, e: self.respondWithError("Command failed: '%s'" % (str(e),)) raise
def utilityMain( configFileName, serviceClass, reactor=None, serviceMaker=None, patchConfig=None, onShutdown=None, verbose=False, loadTimezones=False, ): """ Shared main-point for utilities. This function will: - Load the configuration file named by C{configFileName}, - launch a L{CalDAVServiceMaker}'s with the C{ProcessType} of C{"Utility"} - run the reactor, with start/stop events hooked up to the service's C{startService}/C{stopService} methods. It is C{serviceClass}'s responsibility to stop the reactor when it's complete. @param configFileName: the name of the configuration file to load. @type configuration: C{str} @param serviceClass: a 1-argument callable which takes an object that provides L{ICalendarStore} and/or L{IAddressbookStore} and returns an L{IService}. @param patchConfig: a 1-argument callable which takes a config object and makes and changes necessary for the tool. @param onShutdown: a 0-argument callable which will run on shutdown. @param reactor: if specified, the L{IReactorTime} / L{IReactorThreads} / L{IReactorTCP} (etc) provider to use. If C{None}, the default reactor will be imported and used. """ from calendarserver.tap.caldav import CalDAVServiceMaker, CalDAVOptions if serviceMaker is None: serviceMaker = CalDAVServiceMaker # We want to validate that the actual service is always an instance of WorkerService, so wrap the # service maker callback inside a function that does that check def _makeValidService(store): service = serviceClass(store) assert isinstance(service, WorkerService) return service # Install std i/o observer if verbose: observer = StandardIOObserver() observer.start() if reactor is None: from twisted.internet import reactor try: config = loadConfig(configFileName) if patchConfig is not None: patchConfig(config) checkDirectories(config) utilityLogFile = LogFile.fromFullPath( config.UtilityLogFile, rotateLength=config.ErrorLogRotateMB * 1024 * 1024, maxRotatedFiles=config.ErrorLogMaxRotatedFiles, ) utilityLogObserver = FileLogObserver(utilityLogFile) utilityLogObserver.start() config.ProcessType = "Utility" config.UtilityServiceClass = _makeValidService autoDisableMemcached(config) maker = serviceMaker() # Only perform post-import duties if someone has explicitly said to maker.doPostImport = getattr(maker, "doPostImport", False) options = CalDAVOptions service = maker.makeService(options) reactor.addSystemEventTrigger("during", "startup", service.startService) reactor.addSystemEventTrigger("before", "shutdown", service.stopService) if onShutdown is not None: reactor.addSystemEventTrigger("before", "shutdown", onShutdown) if loadTimezones: TimezoneCache.create() except (ConfigurationError, OSError), e: sys.stderr.write("Error: %s\n" % (e,)) return
# Get configuration # try: loadConfig(configFileName) setLogLevelForNamespace(None, "warn") # Shed privileges if config.UserName and config.GroupName and os.getuid() == 0: uid = getpwnam(config.UserName).pw_uid gid = getgrnam(config.GroupName).gr_gid switchUID(uid, uid, gid) os.umask(config.umask) config.directory = getDirectory() autoDisableMemcached(config) except ConfigurationError, e: usage("Unable to start: %s" % (e,)) try: dbxml = AugmentXMLDB((xmlFileName,)) if not remove else None except IOError, e: usage("Could not read XML augment file: %s" % (e,)) # # Start the reactor # reactor.callLater(0, run, dbxml) reactor.run() @inlineCallbacks
def utilityMain(configFileName, serviceClass, reactor=None, serviceMaker=None, patchConfig=None, onShutdown=None, verbose=False): """ Shared main-point for utilities. This function will: - Load the configuration file named by C{configFileName}, - launch a L{CalDAVServiceMaker}'s with the C{ProcessType} of C{"Utility"} - run the reactor, with start/stop events hooked up to the service's C{startService}/C{stopService} methods. It is C{serviceClass}'s responsibility to stop the reactor when it's complete. @param configFileName: the name of the configuration file to load. @type configuration: C{str} @param serviceClass: a 1-argument callable which takes an object that provides L{ICalendarStore} and/or L{IAddressbookStore} and returns an L{IService}. @param patchConfig: a 1-argument callable which takes a config object and makes and changes necessary for the tool. @param onShutdown: a 0-argument callable which will run on shutdown. @param reactor: if specified, the L{IReactorTime} / L{IReactorThreads} / L{IReactorTCP} (etc) provider to use. If C{None}, the default reactor will be imported and used. """ from calendarserver.tap.caldav import CalDAVServiceMaker, CalDAVOptions if serviceMaker is None: serviceMaker = CalDAVServiceMaker # We want to validate that the actual service is always an instance of WorkerService, so wrap the # service maker callback inside a function that does that check def _makeValidService(store): service = serviceClass(store) assert isinstance(service, WorkerService) return service # Install std i/o observer if verbose: observer = StandardIOObserver() observer.start() if reactor is None: from twisted.internet import reactor try: config = loadConfig(configFileName) if patchConfig is not None: patchConfig(config) checkDirectories(config) config.ProcessType = "Utility" config.UtilityServiceClass = _makeValidService autoDisableMemcached(config) maker = serviceMaker() # Only perform post-import duties if someone has explicitly said to maker.doPostImport = getattr(maker, "doPostImport", False) options = CalDAVOptions service = maker.makeService(options) reactor.addSystemEventTrigger("during", "startup", service.startService) reactor.addSystemEventTrigger("before", "shutdown", service.stopService) if onShutdown is not None: reactor.addSystemEventTrigger("before", "shutdown", onShutdown) except (ConfigurationError, OSError), e: sys.stderr.write("Error: %s\n" % (e, )) return
def utilityMain(configFileName, serviceClass, reactor=None, serviceMaker=CalDAVServiceMaker, patchConfig=None, onShutdown=None, verbose=False): """ Shared main-point for utilities. This function will: - Load the configuration file named by C{configFileName}, - launch a L{CalDAVServiceMaker}'s with the C{ProcessType} of C{"Utility"} - run the reactor, with start/stop events hooked up to the service's C{startService}/C{stopService} methods. It is C{serviceClass}'s responsibility to stop the reactor when it's complete. @param configFileName: the name of the configuration file to load. @type configuration: C{str} @param serviceClass: a 1-argument callable which takes an object that provides L{ICalendarStore} and/or L{IAddressbookStore} and returns an L{IService}. @param patchConfig: a 1-argument callable which takes a config object and makes and changes necessary for the tool. @param onShutdown: a 0-argument callable which will run on shutdown. @param reactor: if specified, the L{IReactorTime} / L{IReactorThreads} / L{IReactorTCP} (etc) provider to use. If C{None}, the default reactor will be imported and used. """ # Install std i/o observer if verbose: observer = StandardIOObserver() observer.start() if reactor is None: from twisted.internet import reactor try: config = loadConfig(configFileName) if patchConfig is not None: patchConfig(config) checkDirectories(config) config.ProcessType = "Utility" config.UtilityServiceClass = serviceClass autoDisableMemcached(config) maker = serviceMaker() # Only perform post-import duties if someone has explicitly said to maker.doPostImport = getattr(maker, "doPostImport", False) options = CalDAVOptions service = maker.makeService(options) reactor.addSystemEventTrigger("during", "startup", service.startService) reactor.addSystemEventTrigger("before", "shutdown", service.stopService) if onShutdown is not None: reactor.addSystemEventTrigger("before", "shutdown", onShutdown) except (ConfigurationError, OSError), e: sys.stderr.write("Error: %s\n" % (e,)) return