forked from lorcalhost/BTB-manager-telegram
-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker_setup.py
195 lines (153 loc) · 5.34 KB
/
docker_setup.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
import argparse
import os
import pathlib
import shlex
import shutil
import subprocess
import colorama
SUBPIPE = subprocess.PIPE
COLORS = {
"R": colorama.Fore.RED,
"G": colorama.Fore.GREEN,
"Y": colorama.Fore.YELLOW,
"RESET": colorama.Fore.RESET,
}
PATH = pathlib.Path(__file__).parent.absolute()
os.chdir(PATH)
def delete_image() -> None:
try:
command = shlex.split("docker rmi -f btbmt")
process = subprocess.Popen(command, stderr=SUBPIPE, stdin=SUBPIPE)
stderr = process.communicate()
if stderr[1] == b"Error: No such image: btbmt\n":
print(f"{COLORS['R']}[-] Error: No such image: btbmt{COLORS['RESET']}")
except KeyboardInterrupt:
process.kill()
def make_image() -> None:
command = shlex.split("docker image inspect btbmt")
check_process = subprocess.Popen(
command, stdout=SUBPIPE, stderr=SUBPIPE, stdin=SUBPIPE
)
out, err = check_process.communicate()
if out == b"[]\n":
command = shlex.split("docker build --no-cache -t btbmt .")
try:
make_process = subprocess.Popen(command, stdin=SUBPIPE)
make_process.communicate()
except KeyboardInterrupt:
make_process.kill()
else:
make_new = input(
f"{COLORS['Y']}[*] A docker image already exists "
f"Do you wish to update it(y/n)?: "
)
if make_new in ["y", "Y"]:
print(f"[*] Updating the image...{COLORS['RESET']}")
update_image()
def update_image() -> None:
delete_image()
make_image()
def docker_setup() -> None:
print(f"{COLORS['Y']}[*] Setting things up for docker...{COLORS['RESET']}")
command = shlex.split("docker image inspect btbmt")
try:
process = subprocess.Popen(
command, stdout=SUBPIPE, stderr=SUBPIPE, stdin=SUBPIPE
)
process.communicate()
except KeyboardInterrupt:
process.kill()
except Exception:
make_image()
finally:
update_image()
def color_copy_file(src: str, dest: str):
try:
shutil.copyfile(src, dest)
print(f"{COLORS['G']}[+] Copying {src} to {dest}{COLORS['RESET']}")
except Exception:
print(
f"{COLORS['R']}[-] Unable to find file {src}\n"
f"\tPlease manually create it at {dest}{COLORS['RESET']}"
)
def default() -> None:
if not os.path.exists("binance-trade-bot"):
subprocess.call(
"git clone https://github.com/edeng23/binance-trade-bot >/dev/null",
shell=True,
)
exists_previous_btb_install = input(
f"{COLORS['Y']}[*] Is a Binance Trade Bot installation already present on your filesystem (y/n)?: {COLORS['RESET']}"
)
if exists_previous_btb_install in ["y", "Y"]:
btb_installation_dir = input(
f"{COLORS['Y']}[*] Enter path to your previous Binance Trade bot installation (e.g. ../binance-trade-bot/): {COLORS['RESET']}"
)
while not os.path.exists(btb_installation_dir):
btb_installation_dir = input(
f"{COLORS['R']}[-] Couldn't find the specified path on the filesystem, try again: {COLORS['RESET']}"
)
else:
print(
f"{COLORS['G']}[+] Path {btb_installation_dir} found on the filesystem.{COLORS['RESET']}"
)
color_copy_file(
os.path.join(btb_installation_dir, "user.cfg"),
"./binance-trade-bot/user.cfg",
)
color_copy_file(
os.path.join(btb_installation_dir, "supported_coin_list"),
"./binance-trade-bot/supported_coin_list",
)
color_copy_file(
os.path.join(btb_installation_dir, "config/apprise.yml"),
"./binance-trade-bot/config/apprise.yml",
)
elif exists_previous_btb_install in ["n", "N"]:
print(
f"{COLORS['Y']}[*] Please manually create/edit the following files:\n"
f"\t- ./binance-trade-bot/user.cfg\n"
f"\t- ./binance-trade-bot/supported_coin_list\n"
f"\t- ./binance-trade-bot/config/apprise.yml"
)
docker = input(
f"{COLORS['Y']}[*] Would you like to run the setup script for running the bot in a docker container (y/n)?: "
)
if docker in ["y", "Y"]:
docker_setup()
print(f"{COLORS['G']}[*] All set!{COLORS['RESET']}")
else:
print(
f"{COLORS['Y']}[*] Skipping setup for dockerizing the bot{COLORS['RESET']}"
)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"-m",
"--make-image",
help="Create a docker image for the bot.",
action="store_true",
)
parser.add_argument(
"-u",
"--update-image",
help="Update the docker image for the bot.",
action="store_true",
)
parser.add_argument(
"-D",
"--delete-image",
help="Delete the docker image for the bot.",
action="store_true",
)
args = parser.parse_args()
if not any([args.update_image, args.delete_image, args.make_image]):
default()
elif args.update_image and not (args.delete_image or args.make_image):
update_image()
elif args.delete_image and not args.update_image:
delete_image()
elif args.make_image:
make_image()
if __name__ == "__main__":
main()