Skip to content

Commit

Permalink
adding the option to use the docker compose plugin (#46)
Browse files Browse the repository at this point in the history
* adding the option to use the docker compose plugin

* updating the python core client version

* installing version 2023.7.23 of pipenv

* fixing syntax

* using the method from DockerCompose to run the down command

* avoiding to fail on error when checking the docker compose plugin

* using shell=true to run docker-compose commands

* enabling the option to run core without nginx (#45)

* enabling the option to run core without nginx

* updating the conditions for adding the nginx volume

* using tag to reference the python core dep

* updating the syntax to install core-client

* installing pipenv==2023.7.23

* adding the option to use the docker compose plugin

* using the method from DockerCompose to run the down command

* avoiding to fail on error when checking the docker compose plugin

* using shell=true to run docker-compose commands
  • Loading branch information
balda-rdx authored Aug 22, 2023
1 parent 98a8c1b commit 8b9d2c1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
3 changes: 2 additions & 1 deletion node-runner-cli/monitoring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from config.Renderer import Renderer
from config.EnvVars import COMPOSE_HTTP_TIMEOUT
from setup.DockerCompose import DockerCompose
from utils.utils import Helpers, run_shell_command


Expand Down Expand Up @@ -162,4 +163,4 @@ def start_monitoring(composefile, auto_approve=False):

@staticmethod
def stop_monitoring(composefile, remove_volumes):
Helpers.docker_compose_down(composefile, remove_volumes)
DockerCompose().run_docker_compose_down(composefile, remove_volumes)
51 changes: 34 additions & 17 deletions node-runner-cli/setup/DockerCompose.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import os
import sys

from config.EnvVars import COMPOSE_HTTP_TIMEOUT
from config.Renderer import Renderer
from config.SystemDConfig import SystemDConfig
from log_util.logger import get_logger
from setup.DockerCommandArguments import DockerInstallArguments
from utils.utils import Helpers, run_shell_command

logger = get_logger(__name__)

class DockerCompose:
@staticmethod
Expand All @@ -32,13 +35,13 @@ def install_standalone_gateway_in_docker(systemd_config: SystemDConfig, auto_app
def stop_gateway_containers():
docker_compose_file: str = f"{Helpers.get_home_dir()}/gateway.docker-compose.yml"
if os.path.exists(docker_compose_file):
DockerCompose.run_docker_compose_down(docker_compose_file)
DockerCompose().run_docker_compose_down(docker_compose_file)

@staticmethod
def restart_gateway_containers():
docker_compose_file: str = f"{Helpers.get_home_dir()}/gateway.docker-compose.yml"
if os.path.exists(docker_compose_file):
DockerCompose.run_docker_compose_down(docker_compose_file)
DockerCompose().run_docker_compose_down(docker_compose_file)
DockerCompose.run_docker_compose_up(docker_compose_file)

@staticmethod
Expand All @@ -49,21 +52,35 @@ def confirm_run_docker_compose(argument_object: DockerInstallArguments, compose_
else:
should_start = input("\nOkay to start the containers [Y/n]?:")
if Helpers.check_Yes(should_start):
DockerCompose.run_docker_compose_up(compose_file)
DockerCompose().run_docker_compose_up(compose_file)

@staticmethod
def run_docker_compose_down(composefile, removevolumes=False):
Helpers.docker_compose_down(composefile, removevolumes)
def run_docker_compose_down(self, composefile, remove_volumes=False):
if self._is_docker_compose_plugin_installed():
command = f"docker compose -f {composefile} down"
else:
docker_compose_binary = os.getenv("DOCKER_COMPOSE_LOCATION", 'docker-compose')
command = f"{docker_compose_binary} -f {composefile} down"
if remove_volumes:
command += ' -v'
result = run_shell_command(command, shell=True, fail_on_error=False)
if result.returncode != 0:
logger.info(f"Command: {command} failed.")
sys.exit(1)

@staticmethod
def run_docker_compose_up(composefile):
docker_compose_binary = os.getenv("DOCKER_COMPOSE_LOCATION", 'docker-compose')
result = run_shell_command([docker_compose_binary, '-f', composefile, 'up', '-d'],
env={
COMPOSE_HTTP_TIMEOUT: os.getenv(COMPOSE_HTTP_TIMEOUT, "200")
}, fail_on_error=False)
def run_docker_compose_up(self, composefile):
if self._is_docker_compose_plugin_installed():
logger.info("Using the docker compose plugin to start the environment")
command = f"docker compose -f {composefile} up -d"
else:
docker_compose_binary = os.getenv("DOCKER_COMPOSE_LOCATION", 'docker-compose')
command = f"{docker_compose_binary} -f {composefile} up -d"

result = run_shell_command(command, shell=True)
if result.returncode != 0:
run_shell_command([docker_compose_binary, '-f', composefile, 'up', '-d'],
env={
COMPOSE_HTTP_TIMEOUT: os.getenv(COMPOSE_HTTP_TIMEOUT, "200")
}, fail_on_error=True)
logger.info(f"Command: {command} failed.")
sys.exit(1)

@staticmethod
def _is_docker_compose_plugin_installed():
result = run_shell_command('docker compose version', shell=True, fail_on_error=False)
return result.returncode == 0
1 change: 0 additions & 1 deletion node-runner-cli/setup/DockerSetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,6 @@ def confirm_docker_compose_file_changes(docker_config: DockerConfig, autoapprove

@staticmethod
def chown_files(docker_config: DockerConfig):
import getpass
username = getpass.getuser()
run_shell_command(['sudo', 'chown', f'{username}:{username}',
f'{docker_config.core_node.keydetails.keyfile_path}/{docker_config.core_node.keydetails.keyfile_name}'])
Expand Down
15 changes: 0 additions & 15 deletions node-runner-cli/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,6 @@ def get_nginx_user(usertype, default_username):
"password": os.environ.get("%s" % nginx_password)
})

@staticmethod
def docker_compose_down(composefile, remove_volumes):

docker_compose_binary = os.getenv("DOCKER_COMPOSE_LOCATION", 'docker-compose')
command = [docker_compose_binary, '-f', composefile, 'down']
if remove_volumes:
command.append('-v')
result = run_shell_command(command, env={
COMPOSE_HTTP_TIMEOUT: os.getenv(COMPOSE_HTTP_TIMEOUT, "200")
}, fail_on_error=False)
if result.returncode != 0:
run_shell_command(command, env={
COMPOSE_HTTP_TIMEOUT: os.getenv(COMPOSE_HTTP_TIMEOUT, "200")
})

@staticmethod
def get_public_ip():
return requests.get('https://api.ipify.org').text
Expand Down

0 comments on commit 8b9d2c1

Please sign in to comment.