[CalendarServer-changes] [5711] CalendarServer/trunk

source_changes at macosforge.org source_changes at macosforge.org
Wed Jun 9 09:02:53 PDT 2010


Revision: 5711
          http://trac.macosforge.org/projects/calendarserver/changeset/5711
Author:   sagen at apple.com
Date:     2010-06-09 09:02:51 -0700 (Wed, 09 Jun 2010)
Log Message:
-----------
So that resources/locations can be created before the calendar server is ever started, calendarserver_manage_principals and calendarserver_gateway create the DataRoot directory if it doesn't exist.  Also, augments.xml is only created when an augment record is actually added (not during __init__ as before).

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/tap/caldav.py
    CalendarServer/trunk/calendarserver/tools/gateway.py
    CalendarServer/trunk/calendarserver/tools/principals.py
    CalendarServer/trunk/calendarserver/tools/util.py
    CalendarServer/trunk/twistedcaldav/directory/augment.py

Modified: CalendarServer/trunk/calendarserver/tap/caldav.py
===================================================================
--- CalendarServer/trunk/calendarserver/tap/caldav.py	2010-06-09 14:09:15 UTC (rev 5710)
+++ CalendarServer/trunk/calendarserver/tap/caldav.py	2010-06-09 16:02:51 UTC (rev 5711)
@@ -93,6 +93,7 @@
 from calendarserver.webadmin.resource import WebAdminResource
 from calendarserver.webcal.resource import WebCalendarResource
 from calendarserver.tap.util import getRootResource, computeProcessCount
+from calendarserver.tools.util import checkDirectory
 
 log = Logger()
 
@@ -249,6 +250,9 @@
 
         config.updateDefaults(self.overrides)
         
+    def checkDirectory(self, dirpath, description, access=None, create=None):
+        checkDirectory(dirpath, description, access=access, create=create)
+
     def checkConfiguration(self):
         uid, gid = None, None
 
@@ -336,53 +340,8 @@
             self.log_info("WARNING: changing umask from: 0%03o to 0%03o"
                           % (oldmask, config.umask))
 
-    def checkDirectory(self, dirpath, description, access=None, create=None):
-        if not os.path.exists(dirpath):
-            try:
-                mode, username, groupname = create
-            except TypeError:
-                raise ConfigurationError("%s does not exist: %s"
-                                         % (description, dirpath))
-            try:
-                os.mkdir(dirpath)
-            except (OSError, IOError), e:
-                self.log_error("Could not create %s: %s" % (dirpath, e))
-                raise ConfigurationError(
-                    "%s does not exist and cannot be created: %s"
-                    % (description, dirpath)
-                )
 
-            if username:
-                uid = getpwnam(username).pw_uid
-            else:
-                uid = -1
 
-            if groupname:
-                gid = getgrnam(groupname).gr_gid
-            else:
-                gid = -1
-
-            try:
-                os.chmod(dirpath, mode)
-                os.chown(dirpath, uid, gid)
-            except (OSError, IOError), e:
-                self.log_error("Unable to change mode/owner of %s: %s"
-                               % (dirpath, e))
-
-            self.log_info("Created directory: %s" % (dirpath,))
-
-        if not os.path.isdir(dirpath):
-            raise ConfigurationError("%s is not a directory: %s"
-                                     % (description, dirpath))
-
-        if access and not os.access(dirpath, access):
-            raise ConfigurationError(
-                "Insufficient permissions for server on %s directory: %s"
-                % (description, dirpath)
-            )
-
-
-
 class GroupOwnedUNIXServer(UNIXServer, object):
     """
     A L{GroupOwnedUNIXServer} is a L{UNIXServer} which changes the group

Modified: CalendarServer/trunk/calendarserver/tools/gateway.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/gateway.py	2010-06-09 14:09:15 UTC (rev 5710)
+++ CalendarServer/trunk/calendarserver/tools/gateway.py	2010-06-09 16:02:51 UTC (rev 5711)
@@ -32,7 +32,7 @@
 from twistedcaldav.directory.directory import DirectoryError
 from twext.web2.dav import davxml
 
-from calendarserver.tools.util import loadConfig, getDirectory, setupMemcached, setupNotifications
+from calendarserver.tools.util import loadConfig, getDirectory, setupMemcached, setupNotifications, checkDirectory
 from calendarserver.tools.principals import (
     principalForPrincipalID, proxySubprincipal, addProxy, removeProxy,
     ProxyError, ProxyWarning, updateRecord
@@ -95,6 +95,15 @@
     try:
         loadConfig(configFileName)
 
+        # Create the DataRoot directory before shedding privileges
+        if config.DataRoot.startswith(config.ServerRoot + os.sep):
+            checkDirectory(
+                config.DataRoot,
+                "Data root",
+                access=os.W_OK,
+                create=(0750, config.UserName, config.GroupName),
+            )
+
         # Shed privileges
         if config.UserName and config.GroupName and os.getuid() == 0:
             uid = getpwnam(config.UserName).pw_uid

Modified: CalendarServer/trunk/calendarserver/tools/principals.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/principals.py	2010-06-09 14:09:15 UTC (rev 5710)
+++ CalendarServer/trunk/calendarserver/tools/principals.py	2010-06-09 16:02:51 UTC (rev 5711)
@@ -37,7 +37,7 @@
 from twistedcaldav.directory.directory import UnknownRecordTypeError, DirectoryError
 from twistedcaldav.directory import augment
 
-from calendarserver.tools.util import loadConfig, getDirectory, setupMemcached, setupNotifications, booleanArgument
+from calendarserver.tools.util import loadConfig, getDirectory, setupMemcached, setupNotifications, booleanArgument, checkDirectory
 
 __all__ = [
     "principalForPrincipalID", "proxySubprincipal", "addProxy", "removeProxy",
@@ -217,6 +217,7 @@
         # some logging activity at whatever log level the plist says
         clearLogLevels()
 
+
         config.DefaultLogLevel = "debug" if verbose else "error"
 
         #
@@ -225,6 +226,14 @@
         observer = StandardIOObserver()
         observer.start()
 
+        # Create the DataRoot directory before shedding privileges
+        if config.DataRoot.startswith(config.ServerRoot + os.sep):
+            checkDirectory(
+                config.DataRoot,
+                "Data root",
+                access=os.W_OK,
+                create=(0750, config.UserName, config.GroupName),
+            )
 
         # Shed privileges
         if config.UserName and config.GroupName and os.getuid() == 0:

Modified: CalendarServer/trunk/calendarserver/tools/util.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/util.py	2010-06-09 14:09:15 UTC (rev 5710)
+++ CalendarServer/trunk/calendarserver/tools/util.py	2010-06-09 16:02:51 UTC (rev 5711)
@@ -22,12 +22,16 @@
     "booleanArgument",
 ]
 
-import os
+import os, sys
 from time import sleep
 import socket
+from pwd import getpwnam
+from grp import getgrnam
 
 from twisted.python.reflect import namedClass
+from twext.python.log import Logger
 
+
 from calendarserver.provision.root import RootResource
 from twistedcaldav import memcachepool
 from twistedcaldav.config import config, ConfigurationError
@@ -38,6 +42,7 @@
 from twistedcaldav.static import CalendarHomeProvisioningFile
 from twistedcaldav.stdconfig import DEFAULT_CONFIG_FILE
 
+log = Logger()
 
 def loadConfig(configFileName):
     if configFileName is None:
@@ -96,11 +101,7 @@
 
     # Load augment/proxy db classes now
     augmentClass = namedClass(config.AugmentService.type)
-    try:
-        augment.AugmentService = augmentClass(**config.AugmentService.params)
-    except IOError, e:
-        # FIXME: Augments DB tries to write to disk, which seems annoying                                                                                                              
-        raise DirectoryError(e)
+    augment.AugmentService = augmentClass(**config.AugmentService.params)
 
     proxydbClass = namedClass(config.ProxyDBService.type)
     calendaruserproxy.ProxyDBService = proxydbClass(**config.ProxyDBService.params)
@@ -205,3 +206,48 @@
             config.Notifications.InternalNotificationPort,
         )
 
+def checkDirectory(dirpath, description, access=None, create=None):
+    if not os.path.exists(dirpath):
+        try:
+            mode, username, groupname = create
+        except TypeError:
+            raise ConfigurationError("%s does not exist: %s"
+                                     % (description, dirpath))
+        try:
+            os.mkdir(dirpath)
+        except (OSError, IOError), e:
+            log.error("Could not create %s: %s" % (dirpath, e))
+            raise ConfigurationError(
+                "%s does not exist and cannot be created: %s"
+                % (description, dirpath)
+            )
+
+        if username:
+            uid = getpwnam(username).pw_uid
+        else:
+            uid = -1
+
+        if groupname:
+            gid = getgrnam(groupname).gr_gid
+        else:
+            gid = -1
+
+        try:
+            os.chmod(dirpath, mode)
+            os.chown(dirpath, uid, gid)
+        except (OSError, IOError), e:
+            log.error("Unable to change mode/owner of %s: %s"
+                           % (dirpath, e))
+
+        log.info("Created directory: %s" % (dirpath,))
+
+    if not os.path.isdir(dirpath):
+        raise ConfigurationError("%s is not a directory: %s"
+                                 % (description, dirpath))
+
+    if access and not os.access(dirpath, access):
+        raise ConfigurationError(
+            "Insufficient permissions for server on %s directory: %s"
+            % (description, dirpath)
+        )
+

Modified: CalendarServer/trunk/twistedcaldav/directory/augment.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/augment.py	2010-06-09 14:09:15 UTC (rev 5710)
+++ CalendarServer/trunk/twistedcaldav/directory/augment.py	2010-06-09 16:02:51 UTC (rev 5711)
@@ -170,61 +170,23 @@
     """
     XMLFile based augment database implementation.
     """
-    
+
     def __init__(self, xmlFiles, cacheTimeout=30):
-        
+
         self.xmlFiles = [fullServerPath(config.DataRoot, path) for path in xmlFiles]
         self.cacheTimeout = cacheTimeout * 60 # Value is mins we want secs
         self.lastCached = 0
         self.db = {}
-        
-        # Preflight existence of files
-        missing = list()
-        for xmlFile in self.xmlFiles:
-            if not os.path.exists(xmlFile):
-                missing.append(xmlFile)
-                
-        # For each missing one create an empty xml file
-        if missing:
-            # If all files are missing, then create one augment file that defaults
-            # to all records being enabled
-            doDefault = (len(missing) == len(self.xmlFiles))
-            for missedFile in missing:
-                
-                _ignore_etree, root = newElementTreeWithRoot(xmlaugmentsparser.ELEMENT_AUGMENTS)
-                if doDefault:
-                    record = addSubElement(root, xmlaugmentsparser.ELEMENT_RECORD)
-                    addSubElement(record, xmlaugmentsparser.ELEMENT_UID, "Default")
-                    addSubElement(record, xmlaugmentsparser.ELEMENT_ENABLE, "true")
-                    addSubElement(record, xmlaugmentsparser.ELEMENT_ENABLECALENDAR, "true")
-                    addSubElement(record, xmlaugmentsparser.ELEMENT_ENABLEADDRESSBOOK, "true")
-                    doDefault = False
-                writeXML(missedFile, root)
 
-                # Set permissions
-                uid = -1
-                if config.UserName:
-                    try:
-                        uid = pwd.getpwnam(config.UserName).pw_uid
-                    except KeyError:
-                        log.error("User not found: %s" % (config.UserName,))
-                gid = -1
-                if config.GroupName:
-                    try:
-                        gid = grp.getgrnam(config.GroupName).gr_gid
-                    except KeyError:
-                        log.error("Group not found: %s" % (config.GroupName,))
-                if uid != -1 and gid != -1:
-                    os.chown(missedFile, uid, gid)
-            
         try:
             self.db = self._parseXML()
         except RuntimeError:
             log.error("Failed to parse XML augments file - fatal error on startup")
             raise
-            
+
         self.lastCached = time.time()
 
+
     @inlineCallbacks
     def getAllUIDs(self):
         """
@@ -284,9 +246,44 @@
         return succeed(None)
 
     def _doAddToFile(self, xmlfile, records):
-    
+
+        if not os.path.exists(xmlfile):
+
+            # File doesn't yet exist.  Create it with items in self.db, and
+            # set file permissions.
+
+            _ignore_etree, augments_node = newElementTreeWithRoot(xmlaugmentsparser.ELEMENT_AUGMENTS)
+            for record in self.db.itervalues():
+                record_node = addSubElement(augments_node, xmlaugmentsparser.ELEMENT_RECORD)
+                addSubElement(record_node, xmlaugmentsparser.ELEMENT_UID, record.uid)
+                addSubElement(record_node, xmlaugmentsparser.ELEMENT_ENABLE, "true" if record.enabled else "false")
+                addSubElement(record_node, xmlaugmentsparser.ELEMENT_HOSTEDAT, record.hostedAt)
+                addSubElement(record_node, xmlaugmentsparser.ELEMENT_ENABLECALENDAR, "true" if record.enabledForCalendaring else "false")
+                addSubElement(record_node, xmlaugmentsparser.ELEMENT_ENABLEADDRESSBOOK, "true" if record.enabledForAddressBooks else "false")
+                addSubElement(record_node, xmlaugmentsparser.ELEMENT_AUTOSCHEDULE, "true" if record.autoSchedule else "false")
+
+
+            writeXML(xmlfile, augments_node)
+
+            # Set permissions
+            uid = -1
+            if config.UserName:
+                try:
+                    uid = pwd.getpwnam(config.UserName).pw_uid
+                except KeyError:
+                    log.error("User not found: %s" % (config.UserName,))
+            gid = -1
+            if config.GroupName:
+                try:
+                    gid = grp.getgrnam(config.GroupName).gr_gid
+                except KeyError:
+                    log.error("Group not found: %s" % (config.GroupName,))
+            if uid != -1 and gid != -1:
+                os.chown(xmlfile, uid, gid)
+
+
         _ignore_etree, augments_node = readXML(xmlfile)
-    
+
         # Create new record
         for record in records:
             record_node = addSubElement(augments_node, xmlaugmentsparser.ELEMENT_RECORD)
@@ -302,6 +299,9 @@
         
     def _doModifyInFile(self, xmlfile, records):
     
+        if not os.path.exists(xmlfile):
+            return
+
         _ignore_etree, augments_node = readXML(xmlfile)
     
         # Map uid->record for fast lookup
@@ -393,15 +393,31 @@
 
         self.removeAugmentRecords(self.db.keys())
         return succeed(None)
-        
+
     def _parseXML(self):
-        
+        """
+        Parse self.xmlFiles into AugmentRecords.
+
+        If none of the xmlFiles exist, create a default record.
+        """
+
         # Do each file
         results = {}
+
+        allMissing = True
         for xmlFile in self.xmlFiles:
-            
-            # Creating a parser does the parse
-            XMLAugmentsParser(xmlFile, results)
+            if os.path.exists(xmlFile):
+                # Creating a parser does the parse
+                XMLAugmentsParser(xmlFile, results)
+                allMissing = False
+
+        if allMissing:
+            results["Default"] = AugmentRecord(
+                "Default",
+                enabled=True,
+                enabledForCalendaring=True,
+                enabledForAddressBooks=True,
+            )
         
         return results
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20100609/00c98376/attachment-0001.html>


More information about the calendarserver-changes mailing list