Skip to content

Commit

Permalink
Fixed an issue with "host commands" not actually running on the host,…
Browse files Browse the repository at this point in the history
… but in the `draky` container instead.

Some other small fixes.
  • Loading branch information
lukasz-zaroda committed Dec 20, 2024
1 parent c643ba4 commit 5ea9929
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
8 changes: 8 additions & 0 deletions bin/templates/draky.template
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ execute_core() {
start_core
fi

# If the command references a local one, then run it on the host directly.
local LOCAL_COMMAND
LOCAL_COMMAND="$(docker exec --user="$DRAKY_HOST_UID:$DRAKY_HOST_GID" "${CONTAINER_NAME}" dk-core core __internal is-local-command "$1" < /dev/null)"
if [ -n "${LOCAL_COMMAND}" ]; then
"${LOCAL_COMMAND}" "$@" < /dev/stdin
exit "$?"
fi

docker exec "${ARGS[@]}" "${CONTAINER_NAME}" dk-core "$@" < /dev/stdin
exit "$?"
}
Expand Down
34 changes: 23 additions & 11 deletions core/dk/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


config_manager = ConfigManager()
custom_commands_provider = CustomCommandsProvider(config_manager)

# First we need to handle the "get-project-path" command that may be run before requirements for
# the rest of the core are fulfilled.
Expand All @@ -26,7 +27,21 @@
and sys.argv[2] == '__internal'
and sys.argv[3] == 'get-project-path'
):
CoreCommandsProvider.get_project_path(config_manager)
CoreCommandsProvider.print_project_path(config_manager)
sys.exit(0)

# We check if given command is local, because if that's the case, then core has nothing more to do.
if (
len(sys.argv) >= 5
and sys.argv[1] == 'core'
and sys.argv[2] == '__internal'
and sys.argv[3] == 'is-local-command'
):
command_name = sys.argv[4]
if custom_commands_provider.supports(command_name):
command = custom_commands_provider.get_command(command_name)
if not command.service:
print(command.cmd, end='')
sys.exit(0)

# If we are initializing, we need to complete initialization before running anything else, as
Expand Down Expand Up @@ -61,8 +76,6 @@ def display_help(arguments=None):
)
args_parser.add_command_group(core_commands_provider)

custom_commands_provider = CustomCommandsProvider(config_manager)

# Add custom commands to the parser. This is needed for them to be included in the help command.
args_parser.add_commands(custom_commands_provider.get_commands())

Expand Down Expand Up @@ -103,12 +116,11 @@ def display_help(arguments=None):
reminder_args = sys.argv[2:]
variables = config_manager.get_vars()
if custom_command.service is None:
command = [custom_command.cmd] + reminder_args
exit_code = process_executor.execute(command, variables=variables)
else:
exit_code = process_executor.execute_inside_container(
custom_command,
reminder_args,
variables
)
raise RuntimeError("This command was supposed to run on host.")

exit_code = process_executor.execute_inside_container(
custom_command,
reminder_args,
variables
)
sys.exit(exit_code)
2 changes: 1 addition & 1 deletion core/dk/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ class CallableCommand(EmptyCommand):
class ServiceCommand(EmptyCommand):
"""Dataclass representing a service command.
"""
service: str
service: str|None
cmd: str
user: str = '0'
2 changes: 1 addition & 1 deletion core/dk/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def sort_configs_by_dependencies(configs: list[Configs]):
print(f"{Fore.RED}[ERROR] The following dependencies are unmet:", file=sys.stderr)
for dep in unmet_dependencies:
print(f"'{dep[0]}' in '{dep[1]}'", file=sys.stderr)
print(Style.RESET_ALL)
print(Style.RESET_ALL, file=sys.stderr)
sys.exit(1)

# Create a dictionary with dependencies only.
Expand Down
2 changes: 1 addition & 1 deletion core/dk/core_commands_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __update_draky(self, _reminder_args: list[str]):
print("To be implemented.")

@staticmethod
def get_project_path(config_manager: ConfigManager):
def print_project_path(config_manager: ConfigManager) -> None:
"""Handles the internal command for getting project's path.
"""
project_path =\
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ TEST_PROJECT_PATH="/draky-test-environment"
ENV_PATH="${TEST_PROJECT_PATH}/.draky/env/dev"
RECIPE_PATH="${ENV_PATH}/docker-compose.recipe.yml"
COMPOSE_PATH="${ENV_PATH}/docker-compose.yml"
HOST_STORAGE_PATH="/storage"

setup() {
mkdir -p "${TEST_PROJECT_PATH}"
mkdir -p "${HOST_STORAGE_PATH}"
}

teardown() {
${DRAKY} env down
rm -r "${TEST_PROJECT_PATH}"
rm -r "${HOST_STORAGE_PATH}"
}

_initialize_test_environment() {
Expand Down Expand Up @@ -496,6 +499,7 @@ EOF
_initialize_test_environment

TEST_SERVICE=test_service
HOST_FILE=$HOST_STORAGE_PATH/file

# Create the compose file.
cat > "$COMPOSE_PATH" << EOF
Expand All @@ -511,6 +515,7 @@ EOF
cat > "${TEST_COMMAND_PATH}" << EOF
#!/usr/bin/env sh
echo "${TEST_COMMAND_MESSAGE}"
touch $HOST_FILE
EOF
chmod a+x "${TEST_COMMAND_PATH}"

Expand All @@ -530,6 +535,8 @@ EOF
${DRAKY} -h | grep -q ${TEST_COMMAND_NAME}
run ${DRAKY} ${TEST_COMMAND_NAME}
[[ "$output" == *"${TEST_COMMAND_MESSAGE}"* ]]
# Make sure that the file created has been created on the host.
[[ -f $HOST_FILE ]]

# Test command running inside the service
${DRAKY} -h | grep -q ${TEST_SERVICE_COMMAND_NAME}
Expand Down

0 comments on commit 5ea9929

Please sign in to comment.