-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvsync.py
More file actions
executable file
·159 lines (137 loc) · 6.64 KB
/
envsync.py
File metadata and controls
executable file
·159 lines (137 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
"""
envsync: Environment Variable Synchronizer
This tool synchronizes an environment variables file from a local machine to a remote server using the system's SSH and SCP commands.
It performs the following functions:
- Copies a specified environment file (e.g., '~/.config/stow/magic/magic_vars') to a target location on a remote system.
- Detects the most appropriate shell profile file on the remote system (e.g., ~/.profile, ~/.xprofile, or ~/.bashrc).
- Automatically appends a "source <remote_file>" command to the detected profile if it is not already present.
- Supports a verbosity flag (-v/--verbose) for detailed logging of executed commands.
- Forwards any additional arguments after the local file and target to the SSH/SCP commands so that custom SSH options can be used.
- Optionally skips SSH host key checking if -k/--skip-host-key-check is specified.
Usage Example:
envsync [-v|--verbose] [-k|--skip-host-key-check] local_file user@server:remote_file [ssh_options...]
Arguments:
-v, --verbose Enable verbose logging.
-k, --skip-host-key-check
Skip SSH host key checking (adds "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null")
local_file Local environment file to copy.
user@server:remote_file
Target SSH destination and remote file path.
ssh_options Additional options passed directly to the ssh and scp commands.
This script can be used as both a standalone command-line tool and an importable library.
Dependencies: Uses system SSH and SCP commands.
Author: [Your Name]
License: [Your License]
"""
import sys
import subprocess
import re
import argparse
# Global verbosity flag
VERBOSE = False
def log_info(message):
"""Log information to stderr if verbose is enabled."""
if VERBOSE:
print(f"INFO: {message}", file=sys.stderr)
def parse_ssh_target(target):
"""
Parse a target in the form of user@server:remote_file.
Returns a tuple (user, server, remote_file).
"""
match = re.match(r'([^@]+)@([^:]+):(.+)', target)
if not match:
raise ValueError(f"Invalid SSH target format: {target} # should be usr@srv:~/path")
return match.groups()
def run_remote_command(user, server, remote_cmd, extra_ssh_args):
"""
Execute a remote command via SSH and return (stdout, stderr, returncode).
"""
ssh_target = f"{user}@{server}"
ssh_cmd = ["ssh"] + extra_ssh_args + [ssh_target, remote_cmd]
log_info("Executing: " + " ".join(ssh_cmd))
result = subprocess.run(ssh_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
return result.stdout, result.stderr, result.returncode
def find_best_profile(user, server, extra_ssh_args):
"""
Detect the best profile file on the remote machine.
Tries in order: ~/.profile, ~/.xprofile, ~/.bashrc.
"""
profile_files = ["~/.profile", "~/.xprofile", "~/.bashrc"]
for profile in profile_files:
cmd = f"test -f {profile} && echo exists || echo missing"
out, _, _ = run_remote_command(user, server, cmd, extra_ssh_args)
if "exists" in out:
log_info(f"Found profile file: {profile}")
return profile
log_info("No profile file found; defaulting to ~/.profile")
return "~/.profile"
def add_source_to_profile(user, server, remote_env_file, profile_file, extra_ssh_args):
"""
Ensure that the 'source <remote_env_file>' line is present in the remote profile file.
If not, append it.
"""
source_line = f"source {remote_env_file}"
# Check if the line already exists
check_cmd = f"grep -Fxq '{source_line}' {profile_file} && echo found || echo missing"
out, _, _ = run_remote_command(user, server, check_cmd, extra_ssh_args)
if "found" in out:
log_info(f"'{source_line}' is already present in {profile_file}")
return
# For .bashrc, append with a preceding comment; for others, simply append.
if profile_file.endswith(".bashrc"):
append_cmd = f"echo -e '\\n# Load custom env variables\\n{source_line}' >> {profile_file}"
else:
append_cmd = f"echo '{source_line}' >> {profile_file}"
log_info(f"Appending source command to {profile_file}: {append_cmd}")
run_remote_command(user, server, append_cmd, extra_ssh_args)
log_info(f"Added '{source_line}' to {profile_file}")
def copy_file(local_file, target, extra_ssh_args):
"""
Copy the local file to the remote target using scp.
"""
scp_cmd = ["scp"] + extra_ssh_args + [local_file, target]
log_info("Copying file: " + " ".join(scp_cmd))
subprocess.check_call(scp_cmd)
def main():
parser = argparse.ArgumentParser(
description="Copy an environment variable file to a remote server and update the remote profile to source it. "
"This tool uses system SSH and SCP commands."
)
parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbose logging.")
parser.add_argument(
"-k", "--skip-host-key-check",
action="store_true",
help="Skip SSH host key checking (adds -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null)"
)
parser.add_argument("local_file", help="Local file to copy (e.g., ~/.config/stow/magic/magic_vars)")
parser.add_argument("target", help="Remote target in the format user@server:remote_file")
parser.add_argument("ssh_options", nargs=argparse.REMAINDER, help="Additional options to pass to ssh/scp commands")
args = parser.parse_args()
# Set global verbosity flag
global VERBOSE
VERBOSE = args.verbose
# If skip-host-key-check is set, prepend the corresponding SSH options
if args.skip_host_key_check:
args.ssh_options.insert(0, "-o")
args.ssh_options.insert(1, "StrictHostKeyChecking=no")
args.ssh_options.insert(2, "-o")
args.ssh_options.insert(3, "UserKnownHostsFile=/dev/null")
try:
user, server, remote_file = parse_ssh_target(args.target)
except ValueError as e:
print(f"ERROR: {e}", file=sys.stderr)
sys.exit(1)
try:
# Copy the environment file to the remote system
copy_file(args.local_file, args.target, args.ssh_options)
except subprocess.CalledProcessError:
print("ERROR: SCP failed.", file=sys.stderr)
sys.exit(1)
# Determine the best profile file on the remote system
profile_file = find_best_profile(user, server, args.ssh_options)
log_info(f"Using profile file: {profile_file}")
# Append the source command to the profile file if it is not already present
add_source_to_profile(user, server, remote_file, profile_file, args.ssh_options)
if __name__ == "__main__":
main()