def verify_mandatory_parameter(configuration): """Ensure that all mandatory parameters are in the configuration object.""" verify_params_swift_auth(configuration) if (len([ 1 for backup in configuration["backups"] if "swift_container" in backup ]) != len(configuration["backups"]) ) and "swift_container" not in configuration: raise exceptions.ConfigurationExceptions( "swift_container has not been specified for every backups and no global setting has been set" ) # Backup Parameters if "backups" not in configuration: raise exceptions.ConfigurationExceptions( "No backups field encountered") if len(configuration["backups"]) == 0: raise exceptions.ConfigurationExceptions( "Backups has no backup configured") for backup in configuration["backups"]: if "name" not in backup: raise exceptions.ConfigurationExceptions( "A backup has the name field missing")
def load_configuration(conf): """Load the swiftbackmeup configuration file.""" file_path = check_configuration_file_existence(conf.get('file_path')) try: file_path_content = open(file_path, 'r').read() except IOError as exc: raise exceptions.ConfigurationExceptions(exc) try: conf = yaml.load(file_path_content) except yaml.YAMLError as exc: raise exceptions.ConfigurationExceptions(exc) return conf
def load_configuration(conf): """Load the swiftbackmeup configuration file.""" file_path = check_configuration_file_existence(conf.get("file_path")) try: with open(file_path, "r") as fd: conf = yaml.safe_load(fd) except IOError as exc: raise exceptions.ConfigurationExceptions(exc) except yaml.YAMLError as exc: raise exceptions.ConfigurationExceptions(exc) load_swift_auth_conf_from_env(conf) verify_mandatory_parameter(conf) return conf
def build_dump_command(self): if self.database == "all": command = "pg_dumpall" else: command = "pg_dump" # pg_dumpall *-only options management if self.globals_only and self.roles_only: raise exceptions.ConfigurationExceptions( "%s: options globals_only and roles_only cannot be used together" % self.database ) # noqa elif self.globals_only and self.tablespaces_only: raise exceptions.ConfigurationExceptions( "%s: options globals_only and tablespaces_only cannot be used together" % self.database ) # noqa elif self.tablespaces_only and self.roles_only: raise exceptions.ConfigurationExceptions( "%s: options tablespaces_only and roles_only cannot be used together" % self.database ) # noqa for param in list(_PARAMS.keys()): if getattr(self, param, None): command += " %s" % _PARAMS[param] if self.dump_options and not self.database == "all": command += " %s" % self.dump_options if self.user: command += " -U %s" % self.user if self.host: command += " -h %s" % self.host if self.database and not self.database == "all": command += " %s" % self.database if self.password: self.env["PGPASSWORD"] = self.password return command
def build_dump_command(self): if self.database == 'all': command = 'pg_dumpall' else: command = 'pg_dump' # pg_dumpall *-only options management if self.globals_only and self.roles_only: raise exceptions.ConfigurationExceptions( '%s: options globals_only and roles_only cannot be used together' % self.database) elif self.globals_only and self.tablespaces_only: raise exceptions.ConfigurationExceptions( '%s: options globals_only and tablespaces_only cannot be used together' % self.database) elif self.tablespaces_only and self.roles_only: raise exceptions.ConfigurationExceptions( '%s: options tablespaces_only and roles_only cannot be used together' % self.database) for param in _PARAMS.keys(): if getattr(self, param, None): command += ' %s' % _PARAMS[param] if self.dump_options and not self.database == 'all': command += ' %s' % self.dump_options if self.user: command += ' -U %s' % self.user if self.host: command += ' -h %s' % self.host if self.database and not self.database == 'all': command += ' %s' % self.database if self.password: self.env['PGPASSWORD'] = self.password return command
def check_configuration_file_existence(configuration_file_path=None): """Check if the configuration file is present.""" if configuration_file_path: if not os.path.exists(configuration_file_path): raise exceptions.ConfigurationExceptions("File %s does not exist" % configuration_file_path) file_path = configuration_file_path elif os.getenv("SWIFTBACKMEUP_CONFIGURATION"): if not os.path.exists(os.getenv("SWIFTBACKMEUP_CONFIGURATION")): raise exceptions.ConfigurationExceptions( "File %s does not exist" % os.getenv("SWIFTBACKMEUP_CONFIGURATION")) file_path = os.getenv("SWIFTBACKMEUP_CONFIGURATION") else: if not os.path.exists("/etc/swiftbackmeup.conf"): raise exceptions.ConfigurationExceptions( "File /etc/swiftbackmeup.conf does not exist " "(you could specify an alternate location using --conf)") file_path = "/etc/swiftbackmeup.conf" return file_path
def verify_mandatory_parameter(configuration): """Ensure that all mandatory parameters are in the configuration object.""" # Swift Parameters os_username = configuration.get('os_username', os.getenv('OS_USERNAME')) os_password = configuration.get('os_password', os.getenv('OS_PASSWORD')) os_tenant_name = configuration.get('os_tenant_name', os.getenv('OS_TENANT_NAME')) os_auth_url = configuration.get('os_auth_url', os.getenv('OS_AUTH_URL')) if not (os_username and os_password and os_tenant_name and os_auth_url): raise exceptions.ConfigurationExceptions( 'One of the following parameter is not configured: os_username, os_password, os_tenant_name, os_auth_url' ) if (len([ 1 for backup in configuration['backups'] if 'swift_container' in backup ]) != len(configuration['backups']) ) and 'swift_container' not in configuration: raise exceptions.ConfigurationExceptions( 'swift_container has not been specified for every backups and no global setting has been set' ) # Backup Parameters if 'backups' not in configuration: raise exceptions.ConfigurationExceptions( 'No backups field encountered') if len(configuration['backups']) == 0: raise exceptions.ConfigurationExceptions( 'Backups has no backup configured') for backup in configuration['backups']: if 'name' not in backup: raise exceptions.ConfigurationExceptions( 'A backup has the name field missing')
def verify_params_swift_auth(conf): """Validate mandatory parameters specific to Swift store auth""" if not conf["os_identity_api_version"]: if "/v2.0" in conf["os_auth_url"]: conf["os_identity_api_version"] = 2 elif "/v3" in conf["os_auth_url"]: conf["os_identity_api_version"] = 3 else: raise exceptions.ConfigurationException( "Could not determine the Identity API version to use from " "OS_IDENTITY_VERSION or OS_AUTH_URL.") if conf["os_identity_api_version"] == 2: v2_mandatory = ( "os_username", "os_password", "os_tenant_name", "os_auth_url", "os_region_name", ) if not all((conf[var] for var in v2_mandatory)): raise exceptions.ConfigurationExceptions( "One of the following parameter is not configured: %s" % ", ".join(v2_mandatory)) elif conf["os_identity_api_version"] == 3: v3_mandatory = ( "os_username", "os_password", "os_project_name", "os_auth_url", "os_region_name", ) if not all((conf[var] for var in v3_mandatory)): raise exceptions.ConfigurationExceptions( "One of the following parameter is not configured: %s" % ", ".join(v3_mandatory))