-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-ssh-config.py
111 lines (95 loc) · 2.72 KB
/
add-ssh-config.py
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
import subprocess
import os
import sys
import argparse
import re
def get_args(arg_input):
"""Takes args input and returns them as a argparse parser
Parameters
-------------
arg_input : list, shape (n_nargs,)
contains list of arguments passed to function
Returns
-------------
args : namespace
contains namespace with keys and values for each parser argument
"""
parser = argparse.ArgumentParser(description='tpu creation script')
parser.add_argument(
'--prefix',
type=str,
default='gcloud-',
help='prefix to use for ssh configs',
)
parser.add_argument(
'--project',
type=str,
default='trc-generative',
help='gcloud project name',
)
parser.add_argument(
'--name',
type=str,
default='tpu',
help='Name to use for tpu vm',
)
parser.add_argument(
'--zone',
type=str,
default='europe-west4-a',
help='zone',
)
parser.add_argument(
'--identityfile',
type=str,
default='~/.ssh/google_compute_engine',
help='ssh identity file path',
)
parser.add_argument(
'--user',
type=str,
default='user',
help='remote username',
)
parser.add_argument(
'--outfile',
type=str,
default='sshconfig',
help='file to append config to',
)
args = parser.parse_args(arg_input)
return args
def get_tpu_ips(args):
#list_result = subprocess.check_output(["gcloud", "alpha", "compute", "tpus", "tpu-vm", "list"])
##print(list_result)
#name = re.search(r".*STATUS\n(.*) ", str(list_result)).expand(r"\1")
#print(name)
describe_result = subprocess.check_output(["gcloud", "alpha", "compute", "tpus", "tpu-vm", "describe", args.name, "--zone", args.zone])
#print(f"Describe returned: {describe_result}")
ip = re.search(r"externalIp: ([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)", str(describe_result)).expand(r"\1")
if ip:
print(f"Found IP: {ip}")
else:
print(f"Could not find ip")
return
text = f"""Host {args.prefix}{args.name}
Hostname {ip}
IdentityFile {args.identityfile}
User {args.user}
IdentitiesOnly yes
"""
answer = input(f"Append this hostname to {args.outfile}?:\n{text}\n(y/n)?")
if answer.lower() not in ["y","yes"]:
print("Cancelling operation")
return
else:
print("Writing")
with open(args.outfile, "a") as f:
f.write(f"\n{text}\n")
def main(args=None):
if args == None:
arg_input = sys.argv[1:]
args = get_args(arg_input)
get_tpu_ips(args)
if __name__ == '__main__':
main()