start.py changes about checks on docker compose
This commit is contained in:
64
start.py
64
start.py
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import argparse, sys, platform, os, multiprocessing
|
||||
import argparse, sys, platform, os, multiprocessing, subprocess
|
||||
|
||||
pref = "\033["
|
||||
reset = f"{pref}0m"
|
||||
@@ -20,18 +20,47 @@ def puts(text, *args, color=colors.white, is_bold=False, **kwargs):
|
||||
|
||||
def sep(): puts("-----------------------------------", is_bold=True)
|
||||
|
||||
def composecmd(cmd):
|
||||
return os.system(f"(which docker-compose &> /dev/null && (docker-compose -p firegex {cmd} || exit 0)) || (which docker &> /dev/null && (docker compose -p firegex {cmd} || exit 0)) || echo 'Docker not found!, please install docker and docker compose'")
|
||||
def check_if_exists(program):
|
||||
return subprocess.call(['sh', '-c',program], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) == 0
|
||||
|
||||
def composecmd(cmd, composefile=None):
|
||||
if composefile:
|
||||
cmd = f"-f {composefile} {cmd}"
|
||||
if not check_if_exists("docker ps"):
|
||||
return puts("Cannot use docker, the user hasn't the permission or docker isn't running", color=colors.red)
|
||||
elif check_if_exists("docker compose"):
|
||||
return os.system(f"docker compose {cmd}")
|
||||
elif check_if_exists("docker-compose"):
|
||||
return os.system(f"docker-compose {cmd}")
|
||||
else:
|
||||
puts("Docker compose not found! please install docker compose!", color=colors.red)
|
||||
|
||||
def dockercmd(cmd):
|
||||
return os.system(f"(which docker &> /dev/null && (docker {cmd} || exit 0)) || echo 'Docker not found!, please install docker and docker-compose'")
|
||||
if check_if_exists("docker"):
|
||||
return os.system(f"docker {cmd}")
|
||||
elif not check_if_exists("docker ps"):
|
||||
puts("Cannot use docker, the user hasn't the permission or docker isn't running", color=colors.red)
|
||||
else:
|
||||
puts("Docker not found! please install docker!", color=colors.red)
|
||||
|
||||
def run_checks():
|
||||
if not check_if_exists("docker"):
|
||||
puts("Docker not found! please install docker and docker compose!", color=colors.red)
|
||||
exit()
|
||||
elif not check_if_exists("docker-compose") and not check_if_exists("docker compose"):
|
||||
print(check_if_exists("docker-compose"), check_if_exists("docker compose"))
|
||||
puts("Docker compose not found! please install docker compose!", color=colors.red)
|
||||
exit()
|
||||
if not check_if_exists("docker ps"):
|
||||
puts("Cannot use docker, the user hasn't the permission or docker isn't running", color=colors.red)
|
||||
exit()
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--port', "-p", type=int, required=False, help='Port where open the web service of the firewall', default=4444)
|
||||
parser.add_argument('--threads', "-t", type=int, required=False, help='Number of threads started for each service/utility', default=-1)
|
||||
parser.add_argument('--no-autostart', "-n", required=False, action="store_true", help='Save docker-compose file and not start the container', default=False)
|
||||
parser.add_argument('--keep','-k', required=False, action="store_true", help='Keep the docker-compose file generated', default=False)
|
||||
parser.add_argument('--build', "-b", required=False, action="store_true", help='Build the container locally', default=False)
|
||||
parser.add_argument('--keep','-k', required=False, action="store_true", help='Keep the firegex-compose.yml file generated', default=False)
|
||||
parser.add_argument('--stop', '-s', required=False, action="store_true", help='Stop firegex execution', default=False)
|
||||
parser.add_argument('--restart', '-r', required=False, action="store_true", help='Restart firegex', default=False)
|
||||
parser.add_argument('--psw-no-interactive',type=str, required=False, help='Password for no-interactive mode', default=None)
|
||||
@@ -41,11 +70,11 @@ parser.add_argument('--startup-psw','-P', required=False, action="store_true", h
|
||||
args = parser.parse_args()
|
||||
os.chdir(os.path.dirname(os.path.realpath(__file__)))
|
||||
|
||||
run_checks()
|
||||
|
||||
start_operation = not (args.stop or args.restart)
|
||||
|
||||
if args.build and not os.path.isfile("./Dockerfile"):
|
||||
puts("This is not a clone of firegex, to build firegex the clone of the repository is needed!", color=colors.red)
|
||||
exit()
|
||||
to_build = os.path.isfile("./Dockerfile") and os.path.isfile("./tests/results/test-firegex.xlsx") #random file in firegex repo
|
||||
|
||||
if args.threads < 1:
|
||||
args.threads = multiprocessing.cpu_count()
|
||||
@@ -64,7 +93,7 @@ if start_operation:
|
||||
puts("Insert the password for firegex: ", end="" , color=colors.yellow, is_bold=True)
|
||||
psw_set = input()
|
||||
|
||||
with open("docker-compose.yml","wt") as compose:
|
||||
with open("firegex-compose.yml","wt") as compose:
|
||||
|
||||
if "linux" in sys.platform and not 'microsoft-standard' in platform.uname().release: #Check if not is a wsl also
|
||||
compose.write(f"""
|
||||
@@ -73,7 +102,7 @@ version: '3.9'
|
||||
services:
|
||||
firewall:
|
||||
restart: unless-stopped
|
||||
{"build: ." if args.build else "image: ghcr.io/pwnzer0tt1/firegex"}
|
||||
{"build: ." if to_build else "image: ghcr.io/pwnzer0tt1/firegex"}
|
||||
network_mode: "host"
|
||||
environment:
|
||||
- PORT={args.port}
|
||||
@@ -95,7 +124,7 @@ version: '3.9'
|
||||
services:
|
||||
firewall:
|
||||
restart: unless-stopped
|
||||
{"build: ." if args.build else "image: ghcr.io/pwnzer0tt1/firegex"}
|
||||
{"build: ." if to_build else "image: ghcr.io/pwnzer0tt1/firegex"}
|
||||
ports:
|
||||
- {args.port}:{args.port}
|
||||
environment:
|
||||
@@ -109,22 +138,23 @@ services:
|
||||
""")
|
||||
sep()
|
||||
if not args.no_autostart:
|
||||
composefile = None if to_build else "firegex-compose.yml"
|
||||
try:
|
||||
if args.restart:
|
||||
puts("Running 'docker-compose restart'\n", color=colors.green)
|
||||
composecmd("restart")
|
||||
composecmd("restart", composefile)
|
||||
elif args.stop:
|
||||
puts("Running 'docker-compose down'\n", color=colors.green)
|
||||
composecmd("down")
|
||||
composecmd("down", composefile)
|
||||
else:
|
||||
if not args.build:
|
||||
if not to_build:
|
||||
puts("Downloading docker image from github packages 'docker pull ghcr.io/pwnzer0tt1/firegex'", color=colors.green)
|
||||
dockercmd("pull ghcr.io/pwnzer0tt1/firegex")
|
||||
puts("Running 'docker-compose up -d --build'\n", color=colors.green)
|
||||
composecmd("up -d --build")
|
||||
composecmd("up -d --build", composefile)
|
||||
finally:
|
||||
if not args.keep:
|
||||
os.remove("docker-compose.yml")
|
||||
os.remove("firegex-compose.yml")
|
||||
else:
|
||||
puts("Done! You can start/stop firegex with docker-compose up -d --build", color=colors.yellow)
|
||||
sep()
|
||||
|
||||
Reference in New Issue
Block a user