def execute_command(context, conf): logger = log.get_logger(category="ServiceShell") logger.info("[service shell with %s] begin" % (json.dumps(conf, encoding="UTF-8", ensure_ascii=False), )) hosts_config = context.get("hosts") host_id = conf.get("host") command = conf.get("command") parameters = conf.get("parameters", None) if parameters is not None: for parameter in parameters: command = command.replace("@%s@" % parameter, parameters.get(parameter)) host_config = hosts_config.get(host_id) rsh = rshell(host_config) rsh.connect() stdin, stdout, stderr = rsh.execute(command) for line in stdout.readlines(): line = line.replace("\n", "") print("\033[1;32m %s \033[0m" % line) for line in stderr.readlines(): os.sys.stderr.write(line) rsh.disconnect() logger.info("[service shell with %s] end" % (json.dumps(conf, encoding="UTF-8", ensure_ascii=False), ))
def clear_after_archive(context, conf): hosts_config = context.get("hosts") archive_configs = conf.get("Archives") for archive_config in archive_configs: host_id = archive_config.get("host") host_config = hosts_config.get(host_id) rsh = rshell(host_config) rsh.connect() items = archive_config.get("items") for item in items: base_dir = item.get("basedir", None) if base_dir is not None: stdin, stdout, stderr = rsh.execute("cd %s" % (base_dir, )) error = stderr.read() if error is not "": sys.stderr.write("Error: %s\n" % (error, )) source_files_str = item.get("sources") target_file_str = item.get("target") stdin, stdout, stderr = rsh.execute("rm -rf %s" % (source_files_str, )) error = stderr.read() if error is not "": sys.stderr.write("Error: %s\n" % (error, )) rsh.disconnect()
def zip_archive(conf): hosts_config = conf.get("hosts") archive_config = conf.get("Archives") archive_groups = archive_config.get("groups") for group in archive_groups: host_id = group.get("host") host_config = hosts_config.get(host_id) source_files_str = group.get("sources") target_file_str = group.get("target") rsh = rshell(host_config) rsh.connect() rsh.execute("zip -ru %s %s" % (target_file_str, source_files_str)) rsh.disconnect()
def rsync(host_config, sync_items, base_type=None, base_source=None, base_target=None): rsh = rshell(host_config) rsh.connect() for item in sync_items: if base_target is not None and isinstance(item, basestring): if base_type == "put": rsh.put(item, base_target) else: rsh.get(item, base_target) else: sync_type = item.get("type", base_type) source_str = item.get("source", base_source) target_dir_str = item.get("target", base_target) if sync_type == "put": rsh.put(source_str, target_dir_str) else: rsh.get(source_str, target_dir_str) rsh.disconnect()