0
0
forked from moritz/migrabrator

Merge Rsync server copy and domain renaming

This commit is contained in:
Moritz 2023-12-18 12:16:57 +01:00
parent d3c2d48ef1
commit 9b5c3857fb

View File

@ -6,6 +6,8 @@ import json
import os import os
import tempfile import tempfile
import tarfile import tarfile
from time import sleep
from icecream import ic
from shutil import copyfile, rmtree from shutil import copyfile, rmtree
from pathlib import Path from pathlib import Path
@ -25,16 +27,19 @@ def main(loglevel, source_app, dst_domain, target_server, move_volumes):
def move_app(source_app, target_server=False, target_domain=False, move_vols=False): def move_app(source_app, target_server=False, target_domain=False, move_vols=False):
#backup(source_app) backup(source_app)
print(abra('app', 'undeploy', '-n', source_app)) print(abra('app', 'undeploy', '-n', source_app))
sleep(10)
copy_volumes(source_app, target_server, target_domain, move_vols) copy_volumes(source_app, target_server, target_domain, move_vols)
copy_configs(source_app, target_server, target_domain) copy_configs(source_app, target_server, target_domain)
copy_secrets(source_app, target_domain) copy_secrets(source_app, target_domain)
def backup(app): def backup(app):
logging.info(f"Start Backup of {app}...") logging.info(f"Start Backup of {app}...")
output = abra('app', 'backup', 'create', app) #output = abra('app', 'backup', 'create', app)
print(output) backupbot = get_backupbot(app)
print(f'Run backupbot on {backupbot}')
subprocess.run(['abra', 'app', 'run', backupbot, 'app', '--', 'backup','-m','create'])
def copy_files_between_servers(source_server, source_dir, destination_server, destination_dir): def copy_files_between_servers(source_server, source_dir, destination_server, destination_dir):
# Generate temporary SSH key pair # Generate temporary SSH key pair
@ -43,19 +48,20 @@ def copy_files_between_servers(source_server, source_dir, destination_server, de
private_key_path = os.path.join(ssh_key_pair_dir, 'id_ed25519') private_key_path = os.path.join(ssh_key_pair_dir, 'id_ed25519')
public_key_path = os.path.join(ssh_key_pair_dir, 'id_ed25519.pub') public_key_path = os.path.join(ssh_key_pair_dir, 'id_ed25519.pub')
subprocess.run(['ssh-keygen', '-t', 'ed25519', '-N', '', subprocess.run(['ssh-keygen', '-t', 'ed25519', '-N', '',
'-f', private_key_path, '-C', ssh_key_id], check=True) '-f', private_key_path, '-C', ssh_key_id], check=True, capture_output=True)
try: try:
# Copy the private key to the source server # Copy the private key to the source server
source_key_dir = run_ssh(source_server, 'mktemp -d', True) source_key_dir = run_ssh(source_server, 'mktemp -d', True)
subprocess.run( subprocess.run(
['scp', private_key_path, f'{source_server}:{source_key_dir}'], check=True) ['scp', private_key_path, f'{source_server}:{source_key_dir}'], check=True, capture_output=True)
source_key_file = f'{source_key_dir}/id_ed25519' source_key_file = f'{source_key_dir}/id_ed25519'
run_ssh(source_server, f'chmod 600 {source_key_file}') run_ssh(source_server, f'chmod 600 {source_key_file}')
# Add the public key to the authorized hosts of the destination server # Add the public key to the authorized hosts of the destination server
subprocess.run(['ssh-copy-id', '-i', public_key_path, subprocess.run(['ssh-copy-id', '-i', public_key_path,
destination_server], check=True) destination_server], check=True, capture_output=True)
# Run rsync over SSH on the source server to copy files to the destination server # Run rsync over SSH on the source server to copy files to the destination server
source_rsync_cmd = f'rsync -az -e "ssh -i {source_key_file} -o StrictHostKeyChecking=accept-new" {source_dir} {destination_server}:{destination_dir}' source_rsync_cmd = f'rsync -az -e "ssh -i {source_key_file} -o StrictHostKeyChecking=accept-new" {source_dir} {destination_server}:{destination_dir}'
print(source_rsync_cmd)
run_ssh(source_server, source_rsync_cmd) run_ssh(source_server, source_rsync_cmd)
# Remove the SSH key pair from the source server # Remove the SSH key pair from the source server
run_ssh(source_server, f'rm -r {source_key_dir}') run_ssh(source_server, f'rm -r {source_key_dir}')
@ -67,7 +73,7 @@ def copy_files_between_servers(source_server, source_dir, destination_server, de
rmtree(ssh_key_pair_dir) rmtree(ssh_key_pair_dir)
def copy_volumes(source_app, target_server=False, target_domain=False, move=False): def copy_volumes(source_app, target_server, target_domain, move=False):
if not any([target_domain, target_server]): if not any([target_domain, target_server]):
logging.error( logging.error(
'At leat one of target_domain or target_app need to be speicified') 'At leat one of target_domain or target_app need to be speicified')
@ -76,29 +82,35 @@ def copy_volumes(source_app, target_server=False, target_domain=False, move=Fals
source_service = source_app.replace(".", "_") source_service = source_app.replace(".", "_")
volume_dir = f'/var/lib/docker/volumes/{source_service}_*' volume_dir = f'/var/lib/docker/volumes/{source_service}_*'
target_dir = f'/var/lib/docker/volumes' target_dir = f'/var/lib/docker/volumes'
if target_server: if target_server and not target_domain:
copy_files_between_servers( copy_files_between_servers(
source_server, volume_dir, target_server, target_dir) source_server, volume_dir, target_server, target_dir)
server = None
if target_server and target_domain:
server = target_server
cmd = 'mv'
elif target_domain:
server = source_server
cmd = 'rsync -a --delete'
if move: if move:
cmd = 'mv' cmd = 'mv'
else:
cmd = 'rsync -a --delete'
if target_server:
server = target_server
else:
server = source_server
if target_domain: if target_domain:
paths = run_ssh(server, f'echo {volume_dir}', True).split() source_paths = run_ssh(source_server, f'echo {volume_dir}', True)
if paths[0] == volume_dir: if not source_paths:
logging.error("No path for {volume_dir} found")
exit(1)
source_paths = source_paths.split()
if source_paths[0] == volume_dir:
logging.error(f"Path {volume_dir} does not exists") logging.error(f"Path {volume_dir} does not exists")
return return
target_service = target_domain.replace(".", "_") target_service = target_domain.replace(".", "_")
for old_path in paths: for old_path in source_paths:
new_dir = Path(old_path).name.replace(source_service, target_service) new_dir = Path(old_path).name.replace(source_service, target_service)
new_path = f'{target_dir}/{new_dir}' new_path = f'{target_dir}/{new_dir}'
print(f'{cmd} {old_path}/ {new_path}') print(f'{cmd} {old_path}/ {new_path}')
run_ssh(server, f'{cmd} {old_path}/ {new_path}') if target_server:
copy_files_between_servers(source_server, f'{old_path}/', target_server, new_path)
else:
run_ssh(server, f'{cmd} {old_path}/ {new_path}')
def copy_configs(source_app, target_server=False, target_domain=False): def copy_configs(source_app, target_server=False, target_domain=False):
@ -111,8 +123,10 @@ def copy_configs(source_app, target_server=False, target_domain=False):
target_env = target_path.joinpath(f'{target_domain}.env') target_env = target_path.joinpath(f'{target_domain}.env')
else: else:
target_env = target_path.joinpath(f'{source_app}.env') target_env = target_path.joinpath(f'{source_app}.env')
print(f"copy env {source_env} to {target_env}")
copy_env(source_env, target_env) copy_env(source_env, target_env)
if target_domain: if target_domain:
print(f"\t replace {source_app} with {target_domain}")
replace_domain(source_app, target_domain, target_env) replace_domain(source_app, target_domain, target_env)