Scripts: add ssh jump to psyncup

This commit is contained in:
Akemi Izuko 2024-02-23 18:49:55 -07:00
parent b1dc75d095
commit 2c2d824934
Signed by: akemi
GPG key ID: 8DE0764E1809E9FC

View file

@ -4,18 +4,13 @@ import datetime as dt
import json
import os
import subprocess
from pathlib import Path
CONFIG_NAME = ".psyncup.json"
epilog = (
"""\
This program reads configuration from a file called """
+ CONFIG_NAME
+ """, which must be
in the same directory as the script runs in. For example:
CONFIG_EXAMPLE = """\
{
"ssh_remote": "emiliko@10.0.0.1",
"ssh_jump": "emiliko@178.54.2.3",
"remote_dir": "/home/emiliko/.configs_pointer",
"update_date": "2023-12-01_01:01:01_UTC",
"exclude": [
@ -23,6 +18,15 @@ in the same directory as the script runs in. For example:
"bin/rewritten_in_rust/target"
]
}"""
epilog = (
"""\
This program reads configuration from a file called """
+ CONFIG_NAME
+ f""", which must be
in the same directory as the script runs in. For example:
{CONFIG_EXAMPLE}"""
)
parser = argparse.ArgumentParser(
@ -73,6 +77,11 @@ group.add_argument(
)
group.add_argument(
"--init",
action="store_true",
help="Initialize a .psyncup.json in the cwd",
)
group.add_argument(
"--pull",
type=str,
metavar="<remote-directory>",
action="store",
@ -148,7 +157,21 @@ def build_commands(args, config):
# ==== Perform command =====
def run_init(remote_dir, is_debug=False):
def run_init():
pwd = Path(".")
config = pwd / ".psyncup.json"
if config.exists():
print(".psyncup.json already exists. Here the content:")
with open(config, "r") as f:
for line in f:
print(line, end="")
else:
with open(config, "w") as f:
f.write(CONFIG_EXAMPLE + "\n")
def run_pull(remote_dir, is_debug=False):
rsync_cmd = [
"rsync",
f"{remote_dir}/{CONFIG_NAME}",
@ -156,7 +179,7 @@ def run_init(remote_dir, is_debug=False):
]
if is_debug:
print("DEBUG rsync init cmd: ", rsync_cmd)
print("DEBUG rsync pull cmd: ", rsync_cmd)
rsync_code = subprocess.call(rsync_cmd)
@ -229,7 +252,11 @@ def run_down(rsync_cmd, remote_dir_str):
if __name__ == "__main__":
if args.init:
run_init(args.init, args.debug)
run_init()
exit(0)
if args.pull:
run_pull(args.pull, args.debug)
args.down = True
config = get_config()