Skip to content

Commit

Permalink
Put variables in the .env file instead of feeding them into the docke…
Browse files Browse the repository at this point in the history
…r-compose process. Added comments marking autogenerated files.
  • Loading branch information
lukasz-zaroda committed Feb 12, 2024
1 parent c479492 commit d267cfd
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 42 deletions.
6 changes: 3 additions & 3 deletions core/dk/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ def display_help(arguments=None):

# All reminder arguments, no matter if flags or not.
reminder_args = sys.argv[2:]
variables = config_manager.get_vars()
if service is None:
command = [command_file] + reminder_args
process_executor.execute(command)
process_executor.execute(command, variables=variables)
else:
command_variables = config_manager.get_vars()
process_executor.execute_inside_container(
service,
command_file,
reminder_args,
command_variables
variables
)
4 changes: 4 additions & 0 deletions core/dk/compose_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ def list_services(self) -> list:
def to_string(self) -> str:
"""Returns the content of the compose file as a string.
"""

compose_string = yaml.safe_dump(self.__content, default_flow_style=False)
if self.is_substituted_variables():
compose_string = self.__variables_resolver(compose_string)

compose_string = ('# This file is autogenerated because the environment contains a '
'recipe. To modify the services, modify the recipe.\n') + compose_string
return compose_string


Expand Down
30 changes: 25 additions & 5 deletions core/dk/process_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def get_command_base(self) -> list:
def env_build(self, substitute_vars: bool = False):
"""Build the environment's definition.
"""
# Build the compose file.
recipe_path = self.__get_recipe_path()
if os.path.exists(recipe_path):
with open(recipe_path, "r", encoding='utf8') as f:
Expand All @@ -53,6 +54,17 @@ def env_build(self, substitute_vars: bool = False):
compose.set_substituted_variables(substitute_vars)
self.hook_manager.addon_alter_services(recipe, compose)
self.compose_manager.save(compose)
# Save the .env file.
variables = self.config.get_vars()
dotenv_lines = [
'# This file is autogenerated. Don\'t modify it directly. Instead, if you want to add '
'your custom variables, do it through the configuration files. See documentation.'
]
for var in variables:
dotenv_lines.append(f"{var}={variables[var]}")
dotenv_content = "\n".join(dotenv_lines)
with open(self.config.get_env_path() + os.sep + '.env', "w", encoding='utf8') as text_file:
text_file.write(dotenv_content)

def env_start(self) -> None:
"""Start environment.
Expand All @@ -75,9 +87,17 @@ def env_destroy(self) -> None:
command.extend(['down', '-v'])
self.execute(command)

def execute(self, command: list, pass_stdin: bool = False, container: bool = False) -> None:
def execute(
self,
command: list,
variables: dict = None,
pass_stdin: bool = False,
container: bool = False,
) -> None:
"""Executes given command.
"""
if variables is None:
variables = {}
if pass_stdin:
if self.stdin_passed:
raise ValueError("stdin has already been used up")
Expand All @@ -88,7 +108,7 @@ def execute(self, command: list, pass_stdin: bool = False, container: bool = Fal
# the container, or on the host.
# @todo Figure out why that's the case and write an explanation here.
stdin = sys.stdin if pass_stdin else DEVNULL if container else None
run(command, check=False, stdin=stdin, env=self.config.get_vars())
run(command, check=False, stdin=stdin, env=variables)

def execute_pipe(
self,
Expand All @@ -110,7 +130,7 @@ def execute_pipe(
stdin = previous_process.stdout if previous_process else default_stdin

with Popen(
first_command, stdout=stdout, stderr=sys.stderr, stdin=stdin, env=self.config.get_vars()
first_command, stdout=stdout, stderr=sys.stderr, stdin=stdin
) as process:
if commands:
self.execute_pipe(commands, process)
Expand Down Expand Up @@ -139,7 +159,7 @@ def execute_inside_container(
# Prepare the dir path for the script to be copied.
mkdir_command = self.get_command_base()
mkdir_command.extend(['exec', '-T', service, 'mkdir', '-p', dest_path])
self.execute(mkdir_command, False, True)
self.execute(mkdir_command, pass_stdin=False, container=True)

# Copy script into container to avoid having to pipe commands, as that would disable
# coloring.
Expand All @@ -160,7 +180,7 @@ def execute_inside_container(
command.extend(['-T'])
command.extend([service, dest])
command.extend(reminder_args)
self.execute(command, True, True)
self.execute(command, pass_stdin=True, container=True)

def __get_recipe_path(self) -> str:
return f"{self.config.get_env_path()}/docker-compose.recipe.yml"
Expand Down
1 change: 1 addition & 0 deletions core/resources/empty-template/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*local.dk.yml
.env
Loading

0 comments on commit d267cfd

Please sign in to comment.