-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall.py
238 lines (224 loc) · 8.72 KB
/
install.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import os, sys, re, os.path
import time
import threading
# TODO: change to dynamically calculate current node id
curr_node_id = 0
def load_environment(fname="info.txt"):
env = {
"home": os.path.expanduser('~')
}
env["home"] = env["home"] + "/"
if os.path.exists(fname):
lines = [line.strip() for line in open(fname)]
env["user"] = lines[0]
env["repo"] = lines[1]
else:
f = open(fname)
env["user"] = input("enter user name: ")
f.write(env["user"] + "\n")
env["repo"] = os.getcwd()
if input("confirm path of the repo: {}, y/n? ".format(
env["repo"])) != "y":
env["repo"] = input("enter new path: ")
if env["repo"][-1] != "/":
env["repo"] = env["repo"] + "/"
f.write(env["repo"] + "\n")
return env
def load_ipaddr(ifconfig, start=0, end=100, storage_start=0, storage_end=100):
nodes = []
storage_nodes = []
itr = 0
f = open(ifconfig, "r")
for addr in f:
if ":" not in addr and "=" not in addr:
continue
if addr[0] == '#':
continue
elif addr[0] == '=' and addr[1] == 'l':
break
if itr > end:
break
if itr >= start:
nodes.append(addr.split(":")[0])
itr += 1
f.close()
is_storage = False
itr = 0
f = open(ifconfig, "r")
for addr in f:
if ":" not in addr and "=" not in addr:
continue
if addr[0] == "#":
continue
elif addr[0] == '=' and addr[1] == 's':
is_storage = True
print("start storage")
continue
if is_storage:
if itr > storage_end:
break
if itr >= storage_start:
storage_nodes.append(addr.split(":")[0])
itr += 1
f.close()
return nodes, storage_nodes
class myThread(threading.Thread):
def __init__(self, env, ipaddr, cmd, node_id, all_addrs, is_storage=False):
threading.Thread.__init__(self)
self.usr = env["user"]
self.ipaddr = ipaddr
self.homedir = env["repo"]
self.cmd = cmd
self.node_id = node_id
self.all_addrs = all_addrs
self.root = env["home"]
self.is_storage = is_storage
def exec(self, cmd):
ret = os.system(cmd)
if ret != 0:
print("Error executing: {}".format(cmd))
exit(0)
else:
print("Success executing: {}".format(cmd))
def remote_exec(self, cmd):
self.exec("ssh -l {} {} '{}' ".format(self.usr, self.ipaddr, cmd))
def run(self):
if self.cmd == "install_local":
if self.node_id != curr_node_id:
return
self.exec("cd tools; chmod +x setup_basic.sh; "
"chmod +x setup_grpc.sh; "
"chmod +x setup_redis.sh; "
"chmod +x setup_conf.sh; "
"chmod +x setup_env.sh; "
"chmod +x setup_proto.sh; "
"chmod +x compile.sh; "
"chmod +x run.sh; ")
self.exec("cd tools; ./setup_basic.sh; ./setup_redis.sh; "
"./setup_grpc.sh;")
self.exec("sudo bash {}tools/setup_conf.sh {}src ; ".format(
self.homedir, self.homedir))
elif self.cmd == "install_remote":
# remote command
if self.node_id == curr_node_id:
return
self.exec("scp -r {} {}@{}:{};".format(self.homedir, self.usr,
self.ipaddr, self.homedir))
self.remote_exec("cd Sundial-Private; cd tools; "
"./setup_basic.sh; "
"./setup_grpc.sh; "
"./setup_redis.sh; ")
if self.cmd == "grant_scripts":
if self.node_id != curr_node_id:
return
self.exec("cd tools; chmod +x setup_basic.sh; "
"chmod +x setup_grpc.sh; "
"chmod +x setup_redis.sh; "
"chmod +x setup_conf.sh; "
"chmod +x setup_env.sh; "
"chmod +x setup_proto.sh; "
"chmod +x compile.sh; "
"chmod +x run.sh; ")
elif self.cmd == "config_local":
if self.node_id != curr_node_id:
return
self.exec("sudo bash {}tools/setup_conf.sh {}src ; ".format(
self.homedir, self.homedir))
self.exec("bash {}tools/setup_proto.sh {}src/proto {}tools ; "
"".format(self.homedir, self.homedir, self.homedir))
elif self.cmd == "config_remote":
if self.node_id == curr_node_id:
return
self.remote_exec("sudo bash {}tools/setup_conf.sh {}src ; ".format(
self.homedir, self.homedir))
self.remote_exec(
"bash {}tools/setup_proto.sh {}src/proto {}tools ; "
"".format(self.homedir, self.homedir, self.homedir))
elif self.cmd == "setkey_remote":
if self.node_id == curr_node_id:
return
self.remote_exec(
"cd {}; sudo python3 install.py setkey_local {}".format(
self.homedir, self.node_id))
elif self.cmd == "setkey_local":
if self.node_id != curr_node_id:
return
for itr, addr in enumerate(self.all_addrs):
if itr == self.node_id:
continue
# add ssh key to each node's authorized keys
self.exec("sudo cat {}.ssh/id_ed25519.pub " \
"| sudo ssh {} \"cat >> {}.ssh/authorized_keys\"".format(
self.root, addr, self.root))
elif self.cmd == "sync":
if (not self.is_storage) and self.node_id == curr_node_id:
return
self.exec(
"rsync -av --exclude 'outputs' --exclude 'src/proto/' "
"--delete {} {}@{}:{}".format(self.homedir, self.usr, self.ipaddr, self.homedir))
self.exec("scp {}src/proto/sundial.proto {}@{}:{}src/proto/sundial.proto ".format(self.homedir,
self.usr,
self.ipaddr,
self.homedir))
elif self.cmd == "kill":
if self.node_id == curr_node_id:
return
self.remote_exec("cd Sundial-Private; sudo pkill -f rundb")
elif self.cmd == "clean_outputs":
if self.node_id == curr_node_id:
return
self.remote_exec("cd Sundial-Private/outputs; rm stats.json")
elif self.cmd == "clean_logs":
if self.node_id == curr_node_id:
return
self.remote_exec("cd Sundial-Private; rm -f log_*")
if __name__ == "__main__":
# sample usage
# python3 install.py sync 1 0-2
# copy code from current node 1 to node 0, 1, 2 (inclusive)
# parse commands
cmd = sys.argv[1]
env = load_environment()
# set current node
if len(sys.argv) > 2:
curr_node_id = int(sys.argv[2])
else:
if input("current node id = 0, y/n? ") != "y":
curr_node_id = int(input("enter a different id: "))
# setup range of update
start = 0
end = 100
storage_start = 100
storage_end = -1
if len(sys.argv) >= 4:
limit = sys.argv[3]
start = int(limit.split("-")[0].strip())
end = int(limit.split("-")[1].strip())
if len(sys.argv) >= 5:
limit = sys.argv[4]
storage_start = int(limit.split("-")[0].strip())
storage_end = int(limit.split("-")[1].strip())
else:
print("operation apply from node 0 to all nodes in ifconfig.txt")
# go through each node to complete the task
threads = []
print("apply to compute node {} to {} and storage node {} to {}".format(
start, end, storage_start, storage_end))
addrs, storage = load_ipaddr("src/ifconfig.txt", start, end, storage_start,
storage_end)
print("compute addrs: {}".format(addrs))
print("storage addrs: {}".format(storage))
for itr, addr in enumerate(addrs):
thread1 = myThread(env, addr, cmd, itr, addrs)
thread1.start()
threads.append(thread1)
time.sleep(0.5)
for t in threads:
t.join()
for itr, addr in enumerate(storage):
thread1 = myThread(env, addr, cmd, itr, addrs, is_storage=True)
thread1.start()
threads.append(thread1)
time.sleep(0.5)
for t in threads:
t.join()