From 14b6579a949897238ea7471bc42cca84e77b371e Mon Sep 17 00:00:00 2001 From: Artem Bliznetsov Date: Thu, 22 Feb 2024 17:46:45 +0300 Subject: [PATCH] Init commit --- hosts | 50 +++++++++++++++ scp_helper | 130 ++++++++++++++++++++++++++++++++++++++ ssh_connector_comletition | 17 +++++ ssh_helper | 5 ++ 4 files changed, 202 insertions(+) create mode 100644 hosts create mode 100755 scp_helper create mode 100644 ssh_connector_comletition create mode 100755 ssh_helper diff --git a/hosts b/hosts new file mode 100644 index 0000000..0a85b4a --- /dev/null +++ b/hosts @@ -0,0 +1,50 @@ +# Конфигурация для подклбчения к серверам +# <ИМЯ_ПОДКЛЮЧЕНИЯ> <ИМЯ_СЕРВЕРА> <ПОЛЬЗОВАТЕЛЬ> <ПОРТ> +calendar calendar.stageoffice.ru ubuntu 22 +catalog catalog.stageoffice.ru ubuntu 22 +frontend frontend.stageoffice.ru ubuntu 22 +mail-mailion mail-mailion.stageoffice.ru ubuntu 22 +hybrid hybrid.stageoffice.ru debian 22 +ui-devoffice ui.devoffice.ru artem.bliznetsov 22 +bliznetsov-mailion 10.7.98.140 astra 22 +bliznetsov-jaeger 10.7.97.245 astra 22 +# Стенды zulu +zulu-app-1 ucs-apps-1.zulu.stageoffice.ru centos 22 +zulu-app-2 ucs-apps-2.zulu.stageoffice.ru centos 22 +zulu-db-1 ucs-db-1.zulu.stageoffice.ru centos 22 +zulu-db-2 ucs-db-2.zulu.stageoffice.ru centos 22 +zulu-db-3 ucs-db-3.zulu.stageoffice.ru centos 22 +zulu-doc-converter-1 ucs-doc-converter-1.zulu.stageoffice.ru centos 22 +zulu-doc-converter-2 ucs-doc-converter-2.zulu.stageoffice.ru centos 22 +zulu-frontend-1 ucs-frontend-1.zulu.stageoffice.ru centos 22 +zulu-frontend-2 ucs-frontend-2.zulu.stageoffice.ru centos 22 +zulu-infra-1 ucs-infra-1.zulu.stageoffice.ru centos 22 +zulu-mail-1 ucs-mail-1.zulu.stageoffice.ru centos 22 +zulu-mail-2 ucs-mail-2.zulu.stageoffice.ru centos 22 +zulu-obst-1 ucs-obst-1.zulu.stageoffice.ru centos 22 +zulu-obst-2 ucs-obst-2.zulu.stageoffice.ru centos 22 +zulu-obst-3 ucs-obst-3.zulu.stageoffice.ru centos 22 +zulu-search-1 ucs-search-1.zulu.stageoffice.ru centos 22 +zulu-search-2 ucs-search-2.zulu.stageoffice.ru centos 22 +zulu-search-3 ucs-search-3.zulu.stageoffice.ru centos 22 + +# Стенды yankee +yankee-apps-1 ucs-apps-1.yankee.stageoffice.ru centos 22 +yankee-catalog-1 ucs-catalog-1.yankee.stageoffice.ru centos 22 +yankee-converter-1 ucs-converter-1.yankee.stageoffice.ru centos 22 +yankee-db-1 ucs-db-1.yankee.stageoffice.ru centos 22 +yankee-db-2 ucs-db-2.yankee.stageoffice.ru centos 22 +yankee-db-3 ucs-db-3.yankee.stageoffice.ru centos 22 +yankee-frontend-1 ucs-frontend-1.yankee.stageoffice.ru centos 22 +yankee-infra-1 ucs-infra-1.yankee.stageoffice.ru centos 22 +yankee-mail-1 ucs-mail-1.yankee.stageoffice.ru centos 22 +yankee-mail-2 ucs-mail-2.yankee.stageoffice.ru centos 22 +yankee-obst-1 ucs-obst-1.yankee.stageoffice.ru centos 22 +yankee-obst-2 ucs-obst-2.yankee.stageoffice.ru centos 22 +yankee-obst-3 ucs-obst-3.yankee.stageoffice.ru centos 22 +yankee-search-1 ucs-search-1.yankee.stageoffice.ru centos 22 + +# Временные стенды для геораспреда +georaspred-temp-1 10.20.134.101 ubuntu 31222 +georaspred-temp-2 10.20.134.101 ubuntu 31223 +georaspred-temp-3 10.20.134.101 ubuntu 31224 diff --git a/scp_helper b/scp_helper new file mode 100755 index 0000000..2d0818c --- /dev/null +++ b/scp_helper @@ -0,0 +1,130 @@ +#!/usr/bin/env python3 +# Копирование файлов из/на сервер +# scp_helper [host1:]/path [host2:]/path +# + +import argparse +import os + +CMD_TAR_DECOMP='tar -xz' +CMD_TAR_COMP='tar -cz' +SUDO_REMOTE=True + +def parse_arg(s): + # Парсим путь. Проверяем тип пути + if s.find(':') > -1: + connection_name, path = s.split(':') + else: + connection_name = 'local' + path = s + + if path[0] != '/': + path = './' + path + + path_parts = path.split('/') + work_path = '/'.join(path_parts[0:-1]) + obj = path_parts[-1] + + return { + 'connection_name': connection_name, + 'work_path': work_path, + 'obj': obj + } + +def make_pack_cmd(work_path, obj): + cmd = f'tar -C {work_path} -c {obj}' + return cmd + +def make_unpack_cmd(work_path, obj): + cmd = f'tar -C {work_path}/{obj} -x' + return cmd + +def add_sudo(s): + res = 'sudo ' + s + return res + +def make_ssh_cmd(host, port, user, cmd): + res = f"ssh {user}@{host} -p {port} '{cmd}'" + return res + +def get_connection_config(con_name): + # Получаем конфигурацию подключения по SSH + config_path = os.environ.get('SSH_CONNECTOR_HOST_LIST') + res = None + with open(config_path, 'r') as f: + for line in f: + if line[0] not in ('#', ' ', '\n'): + connection_name, host, user, port, *_ = line.split() + if connection_name == con_name: + res = { + 'connection_name': connection_name, + 'host': host, + 'user': user, + 'port': port + } + break + if res is None: + raise Exception('Connection name not found') + return res + +def get_path_config(path): + # Объединяем конфигурацию пути и конфигурацию подключения, если подключение удаленное + config = parse_arg(path) + + if config['connection_name'] != 'local': + res_config = { + **config, + **get_connection_config( + config['connection_name'] + ) + } + else: + res_config = config + + return res_config + +def make_full_cmd(config, tar_func): + cmd = tar_func(config['work_path'], config['obj']) + + if SUDO_REMOTE and config['connection_name'] != 'local': + cmd = add_sudo(cmd) + + if config['connection_name'] != 'local': + cmd = make_ssh_cmd( + config['host'], + config['port'], + config['user'], + cmd + ) + + return cmd + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description='Copy files' + ) + parser.add_argument('in_path', type=str) + parser.add_argument('out_path', type=str) + + + args = parser.parse_args() + + in_config = get_path_config(args.in_path) + out_config = get_path_config(args.out_path) + + in_cmd = make_full_cmd( + in_config, + make_pack_cmd + ) + + out_cmd = make_full_cmd( + out_config, + make_unpack_cmd + ) + + full_cmd = f'{in_cmd} | {out_cmd}' + print(full_cmd) + + + + diff --git a/ssh_connector_comletition b/ssh_connector_comletition new file mode 100644 index 0000000..a44923c --- /dev/null +++ b/ssh_connector_comletition @@ -0,0 +1,17 @@ +function _ssh_connector_completion() +{ + latest="${COMP_WORDS[$COMP_CWORD]}" + words=$(grep -v -E "\#" $SSH_CONNECTOR_HOST_LIST | awk '{print $1}') + COMPREPLY=($(compgen -W "$words" -- $latest)) + return 0 +} + +function _scp_path_completition() +{ + latest="${COMP_WORDS[$COMP_CWORD]}" + COMPREPLY=$(grep -v -E "\#" $SSH_CONNECTOR_HOST_LIST | awk '{print $1}') + return 0 +} + +complete -F _ssh_connector_completion ssh_helper +complete -F _ssh_connector_completion scp_helper diff --git a/ssh_helper b/ssh_helper new file mode 100755 index 0000000..26757ac --- /dev/null +++ b/ssh_helper @@ -0,0 +1,5 @@ +#!/bin/bash + +CONFIG=($(grep -E "^$1" $SSH_CONNECTOR_HOST_LIST)) + +ssh ${CONFIG[2]}@${CONFIG[1]} -p ${CONFIG[3]}