scp-helper
This commit is contained in:
parent
27b7953c8c
commit
8e09cb3ed9
76
scp_helper → helperlib.py
Executable file → Normal file
76
scp_helper → helperlib.py
Executable file → Normal file
@ -1,29 +1,47 @@
|
||||
#!/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 get_config():
|
||||
config_path = os.environ.get('SSH_HELPER_HOST_LIST')
|
||||
config = {}
|
||||
with open(config_path, 'r') as f:
|
||||
for line in f:
|
||||
if line[0] not in ('#', ' ', '\n'):
|
||||
connection_name, host, user, port, *_ = line.split()
|
||||
config[connection_name] = {
|
||||
'host': host,
|
||||
'user': user,
|
||||
'port': port
|
||||
}
|
||||
return config
|
||||
|
||||
def parse_arg(s):
|
||||
# Парсим путь. Проверяем тип пути
|
||||
# Парсим путь. Разделяем на 3 части:
|
||||
# хост - если не указан, то вызвращает local
|
||||
# work_path - рабочая директория
|
||||
# obj - обхект в рабочей директории
|
||||
|
||||
if s.find(':') > -1:
|
||||
connection_name, path = s.split(':')
|
||||
else:
|
||||
connection_name = 'local'
|
||||
path = s
|
||||
|
||||
if path[0] != '/':
|
||||
if path[0] not in ('/', '~', '.'):
|
||||
if connection_name == 'local':
|
||||
path = './' + path
|
||||
else:
|
||||
path = '~/' + path
|
||||
|
||||
path_parts = path.split('/')
|
||||
work_path = '/'.join(path_parts[0:-1])
|
||||
obj = path_parts[-1]
|
||||
last_slash = path.rfind('/')
|
||||
|
||||
work_path = path[0:last_slash]
|
||||
if len(work_path) == 0:
|
||||
work_path = '/'
|
||||
obj = path[last_slash+1:]
|
||||
|
||||
return {
|
||||
'connection_name': connection_name,
|
||||
@ -32,10 +50,14 @@ def parse_arg(s):
|
||||
}
|
||||
|
||||
def make_pack_cmd(work_path, obj):
|
||||
assert obj is not None
|
||||
assert len(obj) > 0
|
||||
cmd = f'tar -C {work_path} -c {obj}'
|
||||
return cmd
|
||||
|
||||
def make_unpack_cmd(work_path, obj):
|
||||
if work_path == '/':
|
||||
work_path = ''
|
||||
cmd = f'tar -C {work_path}/{obj} -x'
|
||||
return cmd
|
||||
|
||||
@ -49,7 +71,7 @@ def make_ssh_cmd(host, port, user, cmd):
|
||||
|
||||
def get_connection_config(con_name):
|
||||
# Получаем конфигурацию подключения по SSH
|
||||
config_path = os.environ.get('SSH_CONNECTOR_HOST_LIST')
|
||||
config_path = os.environ.get('SSH_HELPER_HOST_LIST')
|
||||
res = None
|
||||
with open(config_path, 'r') as f:
|
||||
for line in f:
|
||||
@ -98,33 +120,3 @@ def make_full_cmd(config, tar_func):
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
|
||||
|
||||
|
||||
39
scp-helper
Executable file
39
scp-helper
Executable file
@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env python3
|
||||
# Копирование файлов из/на сервер
|
||||
# scp_helper [host1:]/path [host2:]/path
|
||||
|
||||
import argparse
|
||||
import os
|
||||
|
||||
from helperlib import *
|
||||
|
||||
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("Будет выполнена команда:")
|
||||
print(full_cmd)
|
||||
print("продолжить (y/n)")
|
||||
if input() in ('y','Y'):
|
||||
os.system(full_cmd)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user