def _CheckExtStorageFile(base_dir, filename, required): """Check prereqs for an ExtStorage file. Check if file exists, if it is a regular file and in case it is one of extstorage scripts if it is executable. @type base_dir: string @param base_dir: Base directory containing ExtStorage installations. @type filename: string @param filename: The basename of the ExtStorage file. @type required: bool @param required: Whether the file is required or not. @rtype: String @return: The file path if the file is found and is valid, None if the file is not found and not required. @raises BlockDeviceError: In case prereqs are not met (found and not valid/executable, not found and required) """ file_path = utils.PathJoin(base_dir, filename) try: st = os.stat(file_path) except EnvironmentError, err: if not required: logging.info("Optional file '%s' under path '%s' is missing", filename, base_dir) return None base.ThrowError("File '%s' under path '%s' is missing (%s)" % (filename, base_dir, utils.ErrnoOrStr(err)))
def _check_socket(self): sock_stat = None try: sock_stat = os.stat(self.monitor_filename) except EnvironmentError, err: if err.errno == errno.ENOENT: raise errors.HypervisorError("No monitor socket found") else: raise errors.HypervisorError( "Error checking monitor socket: %s", utils.ErrnoOrStr(err))
def _check_socket(self): sock_stat = None try: sock_stat = os.stat(self.monitor_filename) except EnvironmentError as err: if err.errno == errno.ENOENT: raise errors.HypervisorError("No monitor socket found") else: raise errors.HypervisorError( "Error checking monitor socket: %s", utils.ErrnoOrStr(err)) if not stat.S_ISSOCK(sock_stat.st_mode): raise errors.HypervisorError("Monitor socket is not a socket")
def BalloonInstanceMemory(self, instance, mem): """Balloon an instance memory to a certain value. @type instance: L{objects.Instance} @param instance: instance to be accepted @type mem: int @param mem: actual memory size to use for instance runtime """ if not self._IsAlive(instance.name): raise errors.HypervisorError( "Failed to balloon memory for %s: %s" % (instance.name, "not running")) try: self._MarkUp(instance, mem) except EnvironmentError, err: raise errors.HypervisorError( "Failed to balloon memory for %s: %s" % (instance.name, utils.ErrnoOrStr(err)))
def _CheckExtStorageFile(base_dir, filename): """Check prereqs for an ExtStorage file. Check if file exists, if it is a regular file and in case it is one of extstorage scripts if it is executable. @type base_dir: string @param base_dir: Base directory containing ExtStorage installations. @type filename: string @param filename: The basename of the ExtStorage file. @raises BlockDeviceError: In case prereqs are not met. """ file_path = utils.PathJoin(base_dir, filename) try: st = os.stat(file_path) except EnvironmentError, err: base.ThrowError("File '%s' under path '%s' is missing (%s)" % (filename, base_dir, utils.ErrnoOrStr(err)))
# script or None, depending on the corresponding checks. See the # function's docstrings for more on these checks. es_files[filename] = _CheckExtStorageFile(es_dir, filename, required) except errors.BlockDeviceError, err: return False, str(err) parameters = [] if constants.ES_PARAMETERS_FILE in es_files: parameters_file = es_files[constants.ES_PARAMETERS_FILE] try: parameters = utils.ReadFile(parameters_file).splitlines() except EnvironmentError, err: return False, ( "Error while reading the EXT parameters file at %s: %s" % (parameters_file, utils.ErrnoOrStr(err))) parameters = [v.split(None, 1) for v in parameters] es_obj = \ objects.ExtStorage(name=name, path=es_dir, create_script=es_files[constants.ES_SCRIPT_CREATE], remove_script=es_files[constants.ES_SCRIPT_REMOVE], grow_script=es_files[constants.ES_SCRIPT_GROW], attach_script=es_files[constants.ES_SCRIPT_ATTACH], detach_script=es_files[constants.ES_SCRIPT_DETACH], setinfo_script=es_files[constants.ES_SCRIPT_SETINFO], verify_script=es_files[constants.ES_SCRIPT_VERIFY], snapshot_script=es_files[constants.ES_SCRIPT_SNAPSHOT], open_script=es_files[constants.ES_SCRIPT_OPEN], close_script=es_files[constants.ES_SCRIPT_CLOSE], supported_parameters=parameters)
def ExtStorageFromDisk(name, base_dir=None): """Create an ExtStorage instance from disk. This function will return an ExtStorage instance if the given name is a valid ExtStorage name. @type base_dir: string @keyword base_dir: Base directory containing ExtStorage installations. Defaults to a search in all the ES_SEARCH_PATH dirs. @rtype: tuple @return: True and the ExtStorage instance if we find a valid one, or False and the diagnose message on error """ if base_dir is None: es_base_dir = pathutils.ES_SEARCH_PATH else: es_base_dir = [base_dir] es_dir = utils.FindFile(name, es_base_dir, os.path.isdir) if es_dir is None: return False, ("Directory for External Storage Provider %s not" " found in search path" % name) # ES Files dictionary: this will be populated later with the absolute path # names for each script; currently we denote for each script if it is # required (True) or optional (False) es_files = dict.fromkeys(constants.ES_SCRIPTS, True) # Let the snapshot, open, and close scripts be optional # for backwards compatibility es_files[constants.ES_SCRIPT_SNAPSHOT] = False es_files[constants.ES_SCRIPT_OPEN] = False es_files[constants.ES_SCRIPT_CLOSE] = False es_files[constants.ES_PARAMETERS_FILE] = True for (filename, required) in es_files.items(): try: # Here we actually fill the dict with the ablsolute path name for each # script or None, depending on the corresponding checks. See the # function's docstrings for more on these checks. es_files[filename] = _CheckExtStorageFile(es_dir, filename, required) except errors.BlockDeviceError as err: return False, str(err) parameters = [] if constants.ES_PARAMETERS_FILE in es_files: parameters_file = es_files[constants.ES_PARAMETERS_FILE] try: parameters = utils.ReadFile(parameters_file).splitlines() except EnvironmentError as err: return False, ("Error while reading the EXT parameters file at %s: %s" % (parameters_file, utils.ErrnoOrStr(err))) parameters = [v.split(None, 1) for v in parameters] es_obj = \ objects.ExtStorage(name=name, path=es_dir, create_script=es_files[constants.ES_SCRIPT_CREATE], remove_script=es_files[constants.ES_SCRIPT_REMOVE], grow_script=es_files[constants.ES_SCRIPT_GROW], attach_script=es_files[constants.ES_SCRIPT_ATTACH], detach_script=es_files[constants.ES_SCRIPT_DETACH], setinfo_script=es_files[constants.ES_SCRIPT_SETINFO], verify_script=es_files[constants.ES_SCRIPT_VERIFY], snapshot_script=es_files[constants.ES_SCRIPT_SNAPSHOT], open_script=es_files[constants.ES_SCRIPT_OPEN], close_script=es_files[constants.ES_SCRIPT_CLOSE], supported_parameters=parameters) return True, es_obj