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 shared_main(configFileName, method, *args, **kwds): try: loadConfig(configFileName) # 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) try: rootResource = getRootResource(config) directory = rootResource.getDirectory() except DirectoryError, e: print "Error: %s" % (e,) return setupMemcached(config) setupNotifications(config)
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 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
sys.exit(0) elif opt in ("-x", "--xmlfile"): xmlFileName = arg if args: usage("Too many arguments: %s" % (" ".join(args),)) observer = StandardIOObserver() observer.start() # # 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,))
elif opt in ("-s", "--ssl"): useSSL = True elif opt in ("-v", "--verbose"): verbose = True else: raise NotImplementedError(opt) if len(args) != 1: usage("Username not specified") username = args[0] if host is None: # No host specified, so try loading config to look up settings try: loadConfig(configFileName) except ConfigurationError, e: print("Error in configuration: %s" % (e, )) sys.exit(1) useSSL = config.EnableSSL or config.BehindTLSProxy host = config.ServerHostName port = config.SSLPort if useSSL else config.HTTPPort if port is None: usage("Must specify a port number") if admin: password = getpass("Password for administrator %s: " % (admin, )) else: password = getpass("Password for %s: " % (username, ))
def main(): configFileName = DEFAULT_CONFIG_FILE primary = False secondary = False action = None tzpath = None xmlfile = None changed = None url = None tzvers = None updatepkg = False # Get options options, _ignore_args = getopt.getopt( sys.argv[1:], "f:hx:z:", [ "refresh", "refreshpkg", "create", "update", "list", "changed=", "url=", "cache", "tzvers=", ] ) for option, value in options: if option == "-h": usage() elif option == "-f": configFileName = value elif option == "-x": xmlfile = value elif option == "-z": tzpath = value elif option == "--refresh": action = "refresh" primary = True elif option == "--refreshpkg": action = "refresh" primary = True updatepkg = True elif option == "--create": action = "create" primary = True elif option == "--update": action = "update" primary = True elif option == "--list": action = "list" primary = True elif option == "--changed": action = "changed" primary = True changed = value elif option == "--url": url = value secondary = True elif option == "--cache": action = "cache" secondary = True elif option == "--tzvers": tzvers = value else: usage("Unrecognized option: %s" % (option,)) if configFileName is None: usage("A configuration file must be specified") try: loadConfig(configFileName) except ConfigurationError, e: sys.stdout.write("%s\n" % (e,)) sys.exit(1)
def test_loadConfig(self): testRoot = os.path.join(os.path.dirname(__file__), "util") configPath = os.path.join(testRoot, "caldavd.plist") config = loadConfig(configPath) self.assertEquals(config.EnableCalDAV, True) self.assertEquals(config.EnableCardDAV, True)
def main(): configFileName = DEFAULT_CONFIG_FILE primary = False secondary = False action = None tzpath = None xmlfile = None changed = None url = None tzvers = None updatepkg = False # Get options options, _ignore_args = getopt.getopt(sys.argv[1:], "f:hx:z:", [ "refresh", "refreshpkg", "create", "update", "list", "changed=", "url=", "cache", "tzvers=", ]) for option, value in options: if option == "-h": usage() elif option == "-f": configFileName = value elif option == "-x": xmlfile = value elif option == "-z": tzpath = value elif option == "--refresh": action = "refresh" primary = True elif option == "--refreshpkg": action = "refresh" primary = True updatepkg = True elif option == "--create": action = "create" primary = True elif option == "--update": action = "update" primary = True elif option == "--list": action = "list" primary = True elif option == "--changed": action = "changed" primary = True changed = value elif option == "--url": url = value secondary = True elif option == "--cache": action = "cache" secondary = True elif option == "--tzvers": tzvers = value else: usage("Unrecognized option: %s" % (option, )) if configFileName is None: usage("A configuration file must be specified") try: loadConfig(configFileName) except ConfigurationError, e: sys.stdout.write("%s\n" % (e, )) sys.exit(1)
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