-
Notifications
You must be signed in to change notification settings - Fork 0
/
do-sync
executable file
·65 lines (50 loc) · 1.71 KB
/
do-sync
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
#!/usr/bin/env python
import os
import sshconfig as S
from os.path import expanduser
import argparse
def make_parser():
"""Create the argument parser."""
parser = argparse.ArgumentParser()
parser.add_argument("-c", action="store", metavar="PATH", help="ssh config file to read")
parser.add_argument("-o", action="store", metavar="PATH", help="ssh config file to write")
parser.add_argument("-n", action="store_true", dest="dryrun", help="dry-run mode")
# parser.add_argument("-v", action="store_true", dest="verbose", help="verbose output")
return parser
def opt_ssh_config_path(opts):
if opts.c:
return opts.c
else:
home = expanduser("~")
return os.path.join(home, ".ssh", "config")
def cmd_sync(opts):
ssh_config_path = opt_ssh_config_path(opts)
hentries = S.read_ssh_config(ssh_config_path)
droplets = []
for host, ip in S.tugboat_droplets():
entry = [ "Host {}".format(host), " # droplet", " Hostname {}".format(ip), "" ]
droplets.append( S.Droplet(host = host, ip_addr = ip, entry = S.HostEntry(entry)) )
(hnew, remove, add) = S.update_droplets(hentries, droplets)
for h in remove:
print "removing", h.host()
for d in add:
print "adding", d.host, "->", d.ip_addr
has_changes = remove or add
if not has_changes:
print "(no changes)"
# hnew is modified by add_entries
S.add_entries(hnew, [ d.entry for d in add ])
if not opts.dryrun:
out_path = opts.o or ssh_config_path
if out_path == "-":
print S.to_config(hnew)
else:
if has_changes:
S.write_ssh_config(out_path, hnew)
print "wrote", out_path
def main():
parser = make_parser()
opts = parser.parse_args()
cmd_sync(opts)
if __name__ == '__main__':
main()