-
Notifications
You must be signed in to change notification settings - Fork 81
/
deploy-morgan.py
338 lines (268 loc) · 12.4 KB
/
deploy-morgan.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env python3
"""
Will deploy nightly Bitconch chain, codename morgan
"""
import logging
import stat
import shutil
import os, re, argparse, sys,crypt
import getpass
import click
from subprocess import Popen, check_call, PIPE, check_output, CalledProcessError
from shutil import copy2, copytree, rmtree
from colorama import init
init()
from colorama import Fore, Back, Style
def rmtree_onerror(self, func, file_path, exc_info):
"""
Error handler for ``shutil.rmtree``.
If the error is due to an access error (read only file)
it attempts to add write permission and then retries.
If the error is for another reason it re-raises the error.
Usage : ``shutil.rmtree(path, onerror=onerror)``
"""
logging.warning(str(exc_info))
logging.warning("rmtree error,check the file exists or try to chmod the file,then retry rmtree action.")
os.chmod(file_path, stat.S_IWRITE) #chmod to writeable
if os.path.isdir(file_path):
#file exists
func(file_path)
else:
#handle whatever
raise
def execute_shell(command, silent=False, cwd=None, shell=True, env=None):
"""
Execute a system command
"""
if env is not None:
env = dict(**os.environ, **env)
if silent:
p = Popen(
command, shell=shell, stdout=PIPE, stderr=PIPE, cwd=cwd, env=env)
stdout, _ = p.communicate()
return stdout
else:
check_call(command, shell=shell, cwd=cwd, env=env)
def prnt_warn(in_text):
"""
Print a warning message
"""
print(Fore.YELLOW + "[!]"+in_text)
print(Style.RESET_ALL)
def prnt_run(in_text):
"""
Print a processing message
"""
print(Fore.WHITE + "[~]"+in_text)
print(Style.RESET_ALL)
def prnt_error(in_text):
"""
Print an error message
"""
print(Fore.RED + "[~]"+in_text)
print(Style.RESET_ALL)
def update_submodules():
"""
Pull the latest submodule code from upstream
"""
prnt_warn('This repo uses submodules to manage the codes')
prnt_run("Use git to update the submodules")
# Ensure the submodule is initialized
execute_shell("git submodule update --init --recursive", silent=False)
# Fetch upstream changes
execute_shell("git submodule foreach --recursive git fetch ", silent=False)
# Reset to upstream
execute_shell("git submodule foreach git reset --hard origin/HEAD", silent=False)
# Update include/
if os.path.exists("include"):
prnt_run("Clean the include folder")
rmtree("include",onerror=rmtree_onerror)
prnt_run("Copy the latest header file from vendor/rustelo-rust/include")
copytree("vendor/rustelo-rust/include", "include")
def add_submodules():
"""
Add submodule into vendor
"""
if not os.path.exists(f"vendor/morgan"):
prnt_run("Use git to add the submodules")
execute_shell("git submodule add https://github.com/luhuimao/morgan.git", silent=False, cwd="vendor")
def build(rust_version,cargoFeatures,release=False):
target_list = execute_shell("rustup target list", silent=True).decode()
m = re.search(r"(.*?)\s*\(default\)", target_list)
#currentWorking directory
pwd = os.getcwd()
default_target =m[1]
# building priority:
# 1. x86_64-pc-windows-gnu for 64-bit MinGW (Windows 7+)
# 2. x86_64-unknown-linux-musl for linux musl
# 3. x86_64-unknown-linux-musl for linux ubuntu,debian
# 4. x86_64-apple-darwin for macOS-10
target_list = [
"x86_64-pc-windows-gnu",
"x86_64-unknown-linux-musl",
"x86_64-unknown-linux-gnu",
"x86_64-apple-darwin"
]
prefix = {
"x86_64-pc-windows-gnu": "x86_64-w64-mingw32-",
"x86_64-unknown-linux-musl": "x86_64-linux-musl-",
"x86_64-unknown-linux-gnu": "x86_64-linux-gnu-",
"x86_64-apple-darwin": ""
}
artifact = {
"x86_64-pc-windows-gnu": "rustelo.dll",
"x86_64-unknown-linux-musl": "librustelo.so",
"x86_64-unknown-linux-gnu": "librustelo.so",
"x86_64-apple-darwin": "librustelo.dylib"
}
if release:
for target in target_list:
prnt_run(f"Build rust source for {target}")
if target != default_target:
execute_shell(["rustup", "target", "add", target],
shell=False,
silent=True,
#cwd="vendor/rustelo-rust")
cwd="buffett2")
profile = "--release" if release else ''
execute_shell(f"cargo build --all --target {target} {profile}",
#cwd="vendor/rustelo-rust",
cwd="buffett2",
env={
"CC": f"{prefix[target]}gcc",
"CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER": f"{prefix[target]}gcc",
"CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER": f"{prefix[target]}gcc",
})
if target.endswith("-apple-darwin"):
execute_shell(f"strip -Sx {artifact[target]}",
cwd=f"vendor/rustelo-rust/soros/target/{target}/release", silent=True)
else:
execute_shell(f"{prefix[target]}strip --strip-unneeded -d -x {artifact[target]}",
#cwd=f"vendor/rustelo-rust/target/{target}/release")
cwd=f"vendor/rustelo-rust/soros/target/{target}/release")
#copy2(f"vendor/rustelo-rust/target/{target}/release/{artifact[target]}", f"libs/{target}/")
copy2(f"vendor/rustelo-rust/soros/target/{target}/release/soros-fullnode", f"libs/{target}/buffett-fullnode")
#copy2(f"vendor/rustelo-rust/soros/target/{target}/release/soros-fullnode-config", f"libs/{target}/buffett-fullnode-config")
copy2(f"vendor/rustelo-rust/soros/target/{target}/release/soros-drone", f"libs/{target}/buffett-drone")
copy2(f"vendor/rustelo-rust/soros/target/{target}/release/soros-bench-tps", f"libs/{target}/buffett-bench-tps")
copy2(f"vendor/rustelo-rust/soros/target/{target}/release/soros-ledgerbot", f"libs/{target}/buffett-ledgerbot")
copy2(f"vendor/rustelo-rust/soros/target/{target}/release/soros-genesis", f"libs/{target}/buffett-genesis")
copy2(f"vendor/rustelo-rust/soros/target/{target}/release/soros-keybot", f"libs/{target}/buffett-keybot")
else:
target = default_target
# For development; build only the _default_ target
prnt_run(f"Build the rust+c code in soros for {target}")
execute_shell(f"cargo build --all --release --features=erasure", cwd="./vendor/morgan")
# Copy _default_ lib over
if not os.path.exists(f"libs/{target}/"):
prnt_run(f"Check the lib folder, if not , create one ")
os.makedirs(f"libs/{target}/")
if not os.path.exists(f"libs/{target}/bin/deps"):
prnt_run(f"Check the the depslib folder, if not , create one ")
os.makedirs(f"libs/{target}/bin/deps/")
prnt_run(f"Copy the generated artifact file and dependencies")
BIN_CRATES=[
"bench-exchange",
"bench-streamer",
"benchbot",
"tokenbot",
"genesis",
"gossip",
"install",
"keybot",
"ledgerbot",
"validator",
"wallet"
]
for crate in BIN_CRATES:
# execute_shell(f"set -x && cargo install --force --path '{crate}' --root '{pwd}/libs/{target}/' --features='erasure'", cwd="vendor/rustelo-rust/soros")
execute_shell(f"set -x && cargo install --force --path '{crate}' --root '{pwd}/libs/{target}/' --features='erasure'", cwd="./vendor/morgan")
# copy dependencies into deps folder
# execute_shell(f"set -x && cp *.so {pwd}/libs/{target}/bin/deps",cwd="vendor/rustelo-rust/soros/target/release")
execute_shell(f"set -x && cp libmorgan*.so {pwd}/libs/{target}/bin/deps",cwd="./vendor/morgan/target/release/deps")
deploy_bin(target)
def commit():
sha = execute_shell("git rev-parse --short HEAD", cwd="vendor/rustelo-rust", silent=True).decode().strip()
execute_shell("git add ./vendor/rustelo-rust ./libs ./include")
try:
execute_shell(f"git commit -m \"Build libs/ and sync include/ from rustelo#{sha}\"")
execute_shell("git push")
except CalledProcessError:
# Commit likely failed because there was nothing to commit
pass
def createUser(name,username, password):
prnt_run(f"Create a new user")
encPass =crypt.crypt(password,"22")
return os.system("useradd -p"+encPass+"-s"+"/bin/bash"+"-d"+"/home/"+username+"-m"+"-c \""+name +"\""+username)
def deploy_bin(target):
# installation location /usr/bin/bitconch
# remove previous installed version
if os.path.exists("/usr/bin/bitconch"):
prnt_run("Remove previous installed version")
rmtree("/usr/bin/bitconch",onerror=rmtree_onerror)
prnt_run("Copy the compiled binaries to /usr/bin/bitconch")
# cp the binary into the folder
copytree(f"libs/{target}/", "/usr/bin/bitconch")
# seth PATH variable
prnt_run(f"Set PATH to include soros executables ")
execute_shell("echo 'export PATH=/usr/bin/bitconch/bin:$PATH' >>~/.profile")
execute_shell("echo 'export PATH=/usr/bin/bitconch/bin/deps:$PATH' >>~/.profile")
# execute_shell("source ~/.profile")
# remove the previous installed service file
if os.path.exists("/etc/systemd/system/soros-leader.service"):
prnt_run("Remove previous installed service file:soros-leader.service")
os.remove("/etc/systemd/system/soros-leader.service")
if os.path.exists("/etc/systemd/system/soros-leader.socket"):
prnt_run("Remove previous installed socket file:soros-leader.socket")
os.remove("/etc/systemd/system/soros-leader.socket")
if os.path.exists("/etc/systemd/system/soros-tokenbot.service"):
prnt_run("Remove previous installed service file:soros-tokenbot.service")
os.remove("/etc/systemd/system/soros-tokenbot.service")
if os.path.exists("/etc/systemd/system/soros-tokenbot.socket"):
prnt_run("Remove previous installed socket file:soros-tokenbot.socket")
os.remove("/etc/systemd/system/soros-tokenbot.socket")
if os.path.exists("/etc/systemd/system/soros-validator.service"):
prnt_run("Remove previous installed service file:soros-validator.service")
os.remove("/etc/systemd/system/soros-validator.service")
if os.path.exists("/etc/systemd/system/soros-validator.socket"):
prnt_run("Remove previous installed socket file:soros-validator.socket")
os.remove("/etc/systemd/system/soros-validator.socket")
# cp the service files into service folder
execute_shell("cp morgan.service.template/* /etc/systemd/system")
if os.path.exists("/bitconch/morgan"):
prnt_run("Remove previous installed version")
rmtree("/bitconch/morgan",onerror=rmtree_onerror)
prnt_run("Copy the morgan scripts to /bitconch/morgan")
# create the working directory data directory
copytree(f"vendor/morgan/multinode-demo", "/bitconch/morgan/demo")
copytree(f"vendor/morgan/scripts", "/bitconch/morgan/scripts")
parser = argparse.ArgumentParser()
parser.add_argument(
"-R", "--release", help="build in release mode", action="store_true")
parser.add_argument(
"-C", "--commit", help="commit include/ and libs/", action="store_true")
argv = parser.parse_args(sys.argv[1:])
#add_submodules()
#update_submodules()
build("1.35","erasure",release=argv.release)
prnt_run("Update PATH")
# execute_shell(f"source ~/.profile")
prnt_run("Please run /usr/bin/bitconch/morgan/demo/setup.sh")
# Setup the boot leader with stake of 500K dif
if click.confirm('Do you want to run setup to create genesis file and id files?', default=True):
execute_shell("/bitconch/morgan/demo/setup.sh",cwd="/bitconch/morgan")
#
if click.confirm('Do you want to reload the systemctl daemon?', default=True):
execute_shell("systemctl daemon-reload")
if click.confirm('Are you running on the leader node?', default=True):
# backup the existing rsync configuration file
if os.path.exists("/etc/rsyncd.conf"):
prnt_run("Backup the existing rsyncd.conf.")
copy2(f"/etc/rsyncd.conf", f"/etc/rsyncd.conf.bk")
os.remove("/etc/rsyncd.conf")
prnt_run("Setup new rsyncd.conf.")
copy2(f"rsyncd-soros.conf", f"/etc/rsyncd.conf")
execute_shell("systemctl enable rsync")
execute_shell("systemctl start rsync")
if argv.commit and argv.release:
commit()