def config_write_path(name, mode='w'): """ Get file-like object for the config file with the given name, taking into account the XDG_CONFIG_HOME environment variable. Create intermidient directories and the file itself according to the XDG Specification in case the file does not exist. May raise EnvironmentError if the file or directories cannot be created. @param name: The name of the config file @type name: str @param mode: The mode to use when opening the file, 'w' by default. @type mode: str @returns: a file-like object """ path = os.path.join(config_home_path(), APPLICATION, name) dirname = os.path.dirname(path) if not os.path.exists(dirname): # XDG spec demands mode 0700 makedirs(dirname, 0700) return file(path, mode)
def unbundle(self, bundle): """ Unbundle the given bundle. @type bundle: L{flumotion.common.bundle.Bundle} @rtype: string @returns: the full path to the directory where it was unpacked """ directory = self.unbundlePath(bundle) filelike = StringIO.StringIO(bundle.getZip()) zipFile = zipfile.ZipFile(filelike, "r") zipFile.testzip() filepaths = zipFile.namelist() for filepath in filepaths: path = os.path.join(directory, filepath) parent = os.path.split(path)[0] try: makedirs(parent) except OSError, err: # Reraise error unless if it's an already existing if err.errno != errno.EEXIST or not os.path.isdir(parent): raise data = zipFile.read(filepath) # atomically write to path, see #373 fd, tempname = tempfile.mkstemp(dir=parent) handle = os.fdopen(fd, 'wb') handle.write(data) handle.close() rename(tempname, path)
def createManager(self, name, port=7531): """ Create a sample manager. @returns: whether or not the config was created. """ self.info("Creating manager %s" % name) managerDir = os.path.join(self.managersDir, name) if os.path.exists(managerDir): raise errors.FatalError, \ "Manager directory %s already exists" % managerDir makedirs(managerDir) planetFile = os.path.join(managerDir, 'planet.xml') # create a default.pem file if it doesn't exist yet pemFile = os.path.join(configure.configdir, 'default.pem') if not os.path.exists(pemFile): # files in datadir are usually not executable, so call through sh retval = os.system( "sh %s %s" % (os.path.join(configure.datadir, 'make-dummy-cert'), pemFile)) # If we couldn't generate the file, it means that we probably # don't have openssl installed. If so, don't include the complete # to the pemfile which means that the the default pem file which # is shipped with flumotion will be used instead. if retval != 0: pemFile = 'default.pem' # generate the file handle = open(planetFile, 'w') handle.write("""<planet> <manager> <debug>4</debug> <host>localhost</host> <port>%(port)d</port> <transport>ssl</transport> <!-- certificate path can be relative to $sysconfdir/flumotion, or absolute --> <certificate>%(pemFile)s</certificate> <component name="manager-bouncer" type="htpasswdcrypt-bouncer"> <property name="data"><![CDATA[ user:PSfNpHTkpTx1M ]]></property> </component> </manager> </planet> """ % locals()) handle.close() return True
def createManager(self, name, port=7531): """ Create a sample manager. @returns: whether or not the config was created. """ self.info("Creating manager %s" % name) managerDir = os.path.join(self.managersDir, name) if os.path.exists(managerDir): raise errors.FatalError, "Manager directory %s already exists" % managerDir makedirs(managerDir) planetFile = os.path.join(managerDir, "planet.xml") # create a default.pem file if it doesn't exist yet pemFile = os.path.join(configure.configdir, "default.pem") if not os.path.exists(pemFile): # files in datadir are usually not executable, so call through sh retval = os.system("sh %s %s" % (os.path.join(configure.datadir, "make-dummy-cert"), pemFile)) # If we couldn't generate the file, it means that we probably # don't have openssl installed. If so, don't include the complete # to the pemfile which means that the the default pem file which # is shipped with flumotion will be used instead. if retval != 0: pemFile = "default.pem" # generate the file handle = open(planetFile, "w") handle.write( """<planet> <manager> <debug>4</debug> <host>localhost</host> <port>%(port)d</port> <transport>ssl</transport> <!-- certificate path can be relative to $sysconfdir/flumotion, or absolute --> <certificate>%(pemFile)s</certificate> <component name="manager-bouncer" type="htpasswdcrypt-bouncer"> <property name="data"><![CDATA[ user:PSfNpHTkpTx1M ]]></property> </component> </manager> </planet> """ % locals() ) handle.close() return True
def ensureDir(directory, description): """ Ensure the given directory exists, creating it if not. @raise errors.FatalError: if the directory could not be created. """ if not os.path.exists(directory): try: makedirs(directory) except OSError, e: from flumotion.common import errors raise errors.FatalError("could not create %s directory %s: %s" % (description, directory, str(e)))
def createWorker(self, name, managerPort=7531, randomFeederports=False): """ Create a sample worker. @returns: whether or not the config was created. """ makedirs(self.workersDir) self.info("Creating worker %s" % name) workerFile = os.path.join(self.workersDir, "%s.xml" % name) if os.path.exists(workerFile): raise errors.FatalError, "Worker file %s already exists." % workerFile feederports = " <!-- <feederports>8600-8639</feederports> -->" if randomFeederports: feederports = ' <feederports random="True" />' # generate the file handle = open(workerFile, "w") handle.write( """<worker> <debug>4</debug> <manager> <host>localhost</host> <port>%(managerPort)s</port> </manager> <authentication type="plaintext"> <username>user</username> <password>test</password> </authentication> %(feederports)s </worker> """ % locals() ) handle.close() return True
def createWorker(self, name, managerPort=7531, randomFeederports=False): """ Create a sample worker. @returns: whether or not the config was created. """ makedirs(self.workersDir) self.info("Creating worker %s" % name) workerFile = os.path.join(self.workersDir, "%s.xml" % name) if os.path.exists(workerFile): raise errors.FatalError, \ "Worker file %s already exists." % workerFile feederports = " <!-- <feederports>8600-8639</feederports> -->" if randomFeederports: feederports = ' <feederports random="True" />' # generate the file handle = open(workerFile, 'w') handle.write("""<worker> <debug>4</debug> <manager> <host>localhost</host> <port>%(managerPort)s</port> </manager> <authentication type="plaintext"> <username>user</username> <password>test</password> </authentication> %(feederports)s </worker> """ % locals()) handle.close() return True
def _saveFlowFile(self, filename): """Opens a file that the flow should be written to. Note that the returned file object might be an existing file, opened in append mode; if the loadConfiguration operation succeeds, the file should first be truncated before writing. """ self.vishnu.adminAction(self.remoteIdentity, '_saveFlowFile', (), {}) def ensure_sane(name, extra=''): if not re.match('^[a-zA-Z0-9_' + extra + '-]+$', name): raise errors.ConfigError, \ 'Invalid planet or saveAs name: %s' % name ensure_sane(self.vishnu.configDir, '/') ensure_sane(filename) directory = os.path.join(self.vishnu.configDir, "flows") self.debug('told to save flow as %s/%s.xml', directory, filename) try: makedirs(directory, 0770) except OSError, e: if e.errno != errno.EEXIST: raise e