Esempio n. 1
0
def checkDirectory(dirpath, description, access=None, create=None, wait=False):
    """
    Make sure dirpath is an existing directory, and optionally ensure it has the
    expected permissions.  Alternatively the function can create the directory or
    can wait for someone else to create it.

    @param dirpath: The directory path we're checking
    @type dirpath: string
    @param description: A description of what the directory path represents, used in
        log messages
    @type description: string
    @param access: The type of access we're expecting, either os.W_OK or os.R_OK
    @param create: A tuple of (file permissions mode, username, groupname) to use
        when creating the directory.  If create=None then no attempt will be made
        to create the directory.
    @type create: tuple
    @param wait: Wether the function should wait in a loop for the directory to be
        created by someone else (or mounted, etc.)
    @type wait: boolean
    """

    # Note: we have to use print here because the logging mechanism has not
    # been set up yet.

    if not os.path.exists(dirpath) or (
            diagnose.detectPhantomVolume(dirpath)
            == diagnose.EXIT_CODE_PHANTOM_DATA_VOLUME):

        if wait:

            # If we're being told to wait, post an alert that we can't continue
            # until the volume is mounted
            if not os.path.exists(dirpath) or (
                    diagnose.detectPhantomVolume(dirpath)
                    == diagnose.EXIT_CODE_PHANTOM_DATA_VOLUME):
                from calendarserver.tap.util import AlertPoster
                AlertPoster.postAlert("MissingDataVolumeAlert", 0,
                                      ["volumePath", dirpath])

            while not os.path.exists(dirpath) or (
                    diagnose.detectPhantomVolume(dirpath)
                    == diagnose.EXIT_CODE_PHANTOM_DATA_VOLUME):
                if not os.path.exists(dirpath):
                    print("Path does not exist: %s" % (dirpath, ))
                else:
                    print("Path is not a real volume: %s" % (dirpath, ))
                sleep(5)
        else:
            try:
                mode, username, groupname = create
            except TypeError:
                raise ConfigurationError("%s does not exist: %s" %
                                         (description, dirpath))
            try:
                os.mkdir(dirpath)
            except (OSError, IOError), e:
                print("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:
                print("Unable to change mode/owner of %s: %s" % (dirpath, e))

            print("Created directory: %s" % (dirpath, ))
Esempio n. 2
0
def checkDirectory(dirpath, description, access=None, create=None, wait=False):
    """
    Make sure dirpath is an existing directory, and optionally ensure it has the
    expected permissions.  Alternatively the function can create the directory or
    can wait for someone else to create it.

    @param dirpath: The directory path we're checking
    @type dirpath: string
    @param description: A description of what the directory path represents, used in
        log messages
    @type description: string
    @param access: The type of access we're expecting, either os.W_OK or os.R_OK
    @param create: A tuple of (file permissions mode, username, groupname) to use
        when creating the directory.  If create=None then no attempt will be made
        to create the directory.
    @type create: tuple
    @param wait: Wether the function should wait in a loop for the directory to be
        created by someone else (or mounted, etc.)
    @type wait: boolean
    """

    # Note: we have to use print here because the logging mechanism has not
    # been set up yet.

    if not os.path.exists(dirpath) or (diagnose.detectPhantomVolume(dirpath) == diagnose.EXIT_CODE_PHANTOM_DATA_VOLUME):

        if wait:

            # If we're being told to wait, post an alert that we can't continue
            # until the volume is mounted
            if not os.path.exists(dirpath) or (
                diagnose.detectPhantomVolume(dirpath) == diagnose.EXIT_CODE_PHANTOM_DATA_VOLUME
            ):
                from calendarserver.tap.util import postAlert

                postAlert("MissingDataVolumeAlert", ["volumePath", dirpath])

            while not os.path.exists(dirpath) or (
                diagnose.detectPhantomVolume(dirpath) == diagnose.EXIT_CODE_PHANTOM_DATA_VOLUME
            ):
                if not os.path.exists(dirpath):
                    print("Path does not exist: %s" % (dirpath,))
                else:
                    print("Path is not a real volume: %s" % (dirpath,))
                sleep(5)
        else:
            try:
                mode, username, groupname = create
            except TypeError:
                raise ConfigurationError("%s does not exist: %s" % (description, dirpath))
            try:
                os.mkdir(dirpath)
            except (OSError, IOError), e:
                print("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:
                print("Unable to change mode/owner of %s: %s" % (dirpath, e))

            print("Created directory: %s" % (dirpath,))