def before_all(context): """ HOOK: To be executed before all. - Load project properties - Init Aiakos API Client """ __logger__.info("Setting UP execution") # Load project properties. # The loaded properties will be available in 'configuration_manager.config' variable. __logger__.info("Loading project properties") configuration_manager.set_up_project() config = configuration_manager.config # Init Aiakos Client context.aiakos_client = \ AiakosApiClient(protocol=config[PROPERTIES_CONFIG_AIAKOS][PROPERTIES_CONFIG_SERVICE_PROTOCOL], host=config[PROPERTIES_CONFIG_AIAKOS][PROPERTIES_CONFIG_SERVICE_HOST], port=config[PROPERTIES_CONFIG_AIAKOS][PROPERTIES_CONFIG_SERVICE_PORT], base_resource=config[PROPERTIES_CONFIG_AIAKOS][PROPERTIES_CONFIG_SERVICE_RESOURCE]) # Init remote-command client context.remote_executor = \ FabricUtils(host_name=config[PROPERTIES_CONFIG_AIAKOS][PROPERTIES_CONFIG_SERVICE_HOST], host_username=config[PROPERTIES_CONFIG_AIAKOS][PROPERTIES_CONFIG_SERVICE_HOST_USER], host_ssh_key=config[PROPERTIES_CONFIG_AIAKOS][PROPERTIES_CONFIG_SERVICE_HOST_PKEY])
def __init__( self, master_hostname, master_username, master_password, configuration_file_path, master_keyfile=None, glancesyc_bin_path=None, ): """ Init GlanceSync client. :param master_hostname (string): Hostname of Master. :param master_username (string): Username. :param master_password (string): Password. :param configuration_file_path (string): Path where configuration file is located :param master_keyfile (string): SSH private key file :param glancesyc_bin_path (string): Path where GlanceSyn binary are located :return: """ self.fabric_utils = FabricUtils(master_hostname, master_username, master_password, master_keyfile) self.conf_file_path = configuration_file_path self.conf_file_backup_path = None self.bin_path = glancesyc_bin_path self.host = master_hostname
def __init__(self, master_hostname, master_username, master_password, configuration_file_path, master_keyfile=None, glancesyc_bin_path=None): """ Init GlanceSync client. :param master_hostname (string): Hostname of Master. :param master_username (string): Username. :param master_password (string): Password. :param configuration_file_path (string): Path where configuration file is located :param master_keyfile (string): SSH private key file :param glancesyc_bin_path (string): Path where GlanceSyn binary are located :return: """ self.fabric_utils = FabricUtils(master_hostname, master_username, master_password, master_keyfile) self.conf_file_path = configuration_file_path self.conf_file_backup_path = None self.bin_path = glancesyc_bin_path self.host = master_hostname
class GlanceSyncRemoteCmdClient: """ Remote GlanceSync client for testing purposes """ def __init__(self, master_hostname, master_username, master_password, configuration_file_path, master_keyfile=None, glancesyc_bin_path=None): """ Init GlanceSync client. :param master_hostname (string): Hostname of Master. :param master_username (string): Username. :param master_password (string): Password. :param configuration_file_path (string): Path where configuration file is located :param master_keyfile (string): SSH private key file :param glancesyc_bin_path (string): Path where GlanceSyn binary are located :return: """ self.fabric_utils = FabricUtils(master_hostname, master_username, master_password, master_keyfile) self.conf_file_path = configuration_file_path self.conf_file_backup_path = None self.bin_path = glancesyc_bin_path self.host = master_hostname def change_configuration_file(self, section, key, value): """ Change properties in the configuration file. :param section (String): Section. :param key (String): Property name. :param value (String): Property value. :return (String): Command output """ command = "crudini --set {config_file} {section} {key} {value}".format( config_file=self.conf_file_path, section=section, key=key, value=value) return self.execute_command(command) def backup_glancesync_config_file(self, backup_dir): """ Create a backup of configuration file. :param backup_dir (String): Copy the GlanceSync configuration file to tmp backup_dir :return: None """ self.conf_file_backup_path = "{backup_dir}/glancesync.conf.backup".format( backup_dir=backup_dir) command = "cp -f {config_file} {backup_file}".format( config_file=self.conf_file_path, backup_file=self.conf_file_backup_path) return self.execute_command(command) def restore_backup(self): """ Restore backup of the configuration file. :return: None """ if self.conf_file_backup_path: command = "cp -f {backup_file} {config_file}".format( backup_file=self.conf_file_backup_path, config_file=self.conf_file_path) return self.execute_command(command) def get_output_log_list(self): """ This method return the content of executing a 'ls' command filtering by output parallel logs dir name :return (String): Command output """ command = "ls -d {output_files_pater}*/*".format( bin_path=self.bin_path, output_files_pater=OUTPUT_PARALLEL_LOGS) return self.execute_command(command) def get_output_log_content(self, file_absolute_path): """ This method return the content of the given file. :param file_absolute_path: Absolute path of the file (given by get_output_log_list function) :return (String): Command output (content of the file) """ command = "cat {file_absolute_path}".format( file_absolute_path=file_absolute_path) return self.execute_command(command) def clean_all_parallel_output_logs(self): """ Remove all output files coming from a parallel execution :return (String): Command output """ command = "rm -rf {output_files_pater}".format( bin_path=self.bin_path, output_files_pater=OUTPUT_PARALLEL_LOGS) return self.execute_command(command) def sync(self, list_nodes=None, options=None): """ Execute SYNC command. If options are given, they will be passed to the GlanceSync CLI. :param list_nodes (String): String with the list of nodes. e.i: "Burgos" "master:Burgos" "Burgos target2:Madrid" "master:Burgos target2:Madrid" :param options (String): GlanceSync CLI options. :return (String): Command output """ command = "{}/{}".format( self.bin_path, COMMAND_SYNC) if self.bin_path is not None else "sync" command = "{command} {options}".format( command=command, options=options) if options else command command = "{command} {list_nodes}".format( command=command, list_nodes=list_nodes) if list_nodes else command return self.execute_command(command) def execute_command(self, command): if self.host == "localhost" or self.host == "fiwareglancesync": return self.execute_command_locally(command) else: return self.fabric_utils.execute_command(command) def execute_command_locally(self, command): p = Popen(command, shell=True, stdout=PIPE) metadatajson, err = p.communicate() if err: return None if not metadatajson: return 'ok' return metadatajson
class GlanceSyncRemoteCmdClient: """ Remote GlanceSync client for testing purposes """ def __init__(self, master_hostname, master_username, master_password, configuration_file_path, master_keyfile=None, glancesyc_bin_path=None): """ Init GlanceSync client. :param master_hostname (string): Hostname of Master. :param master_username (string): Username. :param master_password (string): Password. :param configuration_file_path (string): Path where configuration file is located :param master_keyfile (string): SSH private key file :param glancesyc_bin_path (string): Path where GlanceSyn binary are located :return: """ self.fabric_utils = FabricUtils(master_hostname, master_username, master_password, master_keyfile) self.conf_file_path = configuration_file_path self.conf_file_backup_path = None self.bin_path = glancesyc_bin_path def change_configuration_file(self, section, key, value): """ Change properties in the configuration file. :param section (String): Section. :param key (String): Property name. :param value (String): Property value. :return (String): Command output """ command = "crudini --set {config_file} {section} {key} {value}".format(config_file=self.conf_file_path, section=section, key=key, value=value) return self.fabric_utils.execute_command(command) def backup_glancesync_config_file(self, backup_dir): """ Create a backup of configuration file. :param backup_dir (String): Copy the GlanceSync configuration file to tmp backup_dir :return: None """ self.conf_file_backup_path = "{backup_dir}/glancesync.conf.backup".format(backup_dir=backup_dir) command = "cp -f {config_file} {backup_file}".format(config_file=self.conf_file_path, backup_file=self.conf_file_backup_path) self.fabric_utils.execute_command(command) def restore_backup(self): """ Restore backup of the configuration file. :return: None """ if self.conf_file_backup_path: command = "cp -f {backup_file} {config_file}".format(backup_file=self.conf_file_backup_path, config_file=self.conf_file_path) self.fabric_utils.execute_command(command) def get_output_log_list(self): """ This method return the content of executing a 'ls' command filtering by output parallel logs dir name :return (String): Command output """ command = "ls -d {output_files_pater}*/*".format(bin_path=self.bin_path, output_files_pater=OUTPUT_PARALLEL_LOGS) return self.fabric_utils.execute_command(command) def get_output_log_content(self, file_absolute_path): """ This method return the content of the given file. :param file_absolute_path: Absolute path of the file (given by get_output_log_list function) :return (String): Command output (content of the file) """ command = "cat {file_absolute_path}".format(file_absolute_path=file_absolute_path) return self.fabric_utils.execute_command(command) def clean_all_parallel_output_logs(self): """ Remove all output files coming from a parallel execution :return (String): Command output """ command = "rm -rf {output_files_pater}".format(bin_path=self.bin_path, output_files_pater=OUTPUT_PARALLEL_LOGS) return self.fabric_utils.execute_command(command) def sync(self, list_nodes=None, options=None): """ Execute SYNC command. If options are given, they will be passed to the GlanceSync CLI. :param list_nodes (String): String with the list of nodes. e.i: "Burgos" "master:Burgos" "Burgos target2:Madrid" "master:Burgos target2:Madrid" :param options (String): GlanceSync CLI options. :return (String): Command output """ command = "{}/{}".format(self.bin_path, COMMAND_SYNC) if self.bin_path is not None else "sync" command = "{command} {options}".format(command=command, options=options) if options else command command = "{command} {list_nodes}".format(command=command, list_nodes=list_nodes) if list_nodes else command return self.fabric_utils.execute_command(command)