Skip to content

Commit

Permalink
Updated the run_on_docker() method
Browse files Browse the repository at this point in the history
  • Loading branch information
NovusEdge committed Apr 10, 2021
2 parents 541472e + 1816e6e commit ecd6c5e
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 198 deletions.
12 changes: 5 additions & 7 deletions btb_manager_telegram/__main__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import subprocess
import argparse
import os
import sys
import time
import shlex
import subprocess

import colorama

Expand All @@ -26,7 +25,7 @@
)


def pre_run_main() -> bool:
def pre_run_main() -> None:
parser = argparse.ArgumentParser(
description="Thanks for using Binance Trade Bot Manager Telegram. "
'By default the program will use "../binance-trade-bot/" as binance-trade-bot installation path.'
Expand Down Expand Up @@ -55,7 +54,8 @@ def pre_run_main() -> bool:
args = parser.parse_args()

if args.docker:
return True
run_on_docker()
exit(1)

settings.ROOT_PATH = args.path
settings.TOKEN = args.token
Expand Down Expand Up @@ -117,7 +117,6 @@ def run_on_docker() -> None:
process = subprocess.Popen(command, stdout=SUBPIPE)

out = process.communicate()
print(out)

if out == b'[]\n':
print(f"{colorama.Fore.RED}[-] E: Docker image not found{colorama.Fore.RESET}")
Expand All @@ -135,13 +134,12 @@ def run_on_docker() -> None:
except KeyboardInterrupt:
process.kill()

except:


if __name__ == "__main__":
on_docker = pre_run_main()
if on_docker:
run_on_docker()
sys.exit(-1)

pre_run_main()
main()
159 changes: 70 additions & 89 deletions docker_setup.py
Original file line number Diff line number Diff line change
@@ -1,113 +1,94 @@
import os
import pathlib
import shutil
import subprocess

import colorama
import argparse
import pathlib
import shlex
import sys
import os

from setup_scripts.docker import main as d_setup

PATH = pathlib.Path(__file__).parent.absolute()
os.chdir(PATH)

SUBPIPE = subprocess.PIPE


COLORS = {
"R": colorama.Fore.RED,
"G": colorama.Fore.GREEN,
"Y": colorama.Fore.YELLOW,
"RESET": colorama.Fore.RESET,
}

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 auto_run() -> None:
print(f"{COLORS['Y']}[*] Setting things up for docker...{COLORS['RESET']}")
command = shlex.split('docker image inspect btbmt')

def color_copy_file(src: str, dest: str):
try:
process = subprocess.Popen(command, stdout=SUBPIPE,
stderr=SUBPIPE, stdin=SUBPIPE)
process.communicate()

except KeyboardInterrupt:
process.kill()

except:
make_image()


finally:
update_image()

def main() -> None:
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"
shutil.copyfile(src, dest)
print(f"{COLORS['G']}[+] Copying {src} to {dest}{COLORS['RESET']}")
except Exception:
print(
f"{COLORS['R']}[-] Couldn't find the file: {src}\n"
f"\tPlease manually create it at {dest}{COLORS['RESET']}"
)


def main():
if not os.path.exists("binance-trade-bot"):
subprocess.call(
"git clone https://github.com/edeng23/binance-trade-bot >/dev/null",
shell=True,
)

isBTB = input(
f"{COLORS['Y']}[*] Is a Binance Trade Bot installation already present on your filesystem (y/n)?: {COLORS['RESET']}"
)
parser.add_argument(
'-D', '--delete-image',
help="Delete the docker image for the bot.", action="store_true"
if isBTB 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']}"
)
os.path.exists(btb_installation_dir)

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 isBTB 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)?: "
)

args = parser.parse_args()
if docker in ["y", "Y"]:
d_setup()

if not any([args.update_image, args.delete_image, args.make_image]):
auto_run()
else:
print(
f"{COLORS['Y']}[*] Skipping setup for dockerizing the bot{COLORS['RESET']}"
)
return

if 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()
print(f"{COLORS['G']}[*] All set!{COLORS['RESET']}")


if __name__ == "__main__":
Expand Down
102 changes: 0 additions & 102 deletions setup.py

This file was deleted.

Loading

0 comments on commit ecd6c5e

Please sign in to comment.