Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wkdev-enter: quiet mode and related options for scripts #68

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/container-only/.wkdev-sync-runtime-state
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License: MIT

source "${WKDEV_SDK}/utilities/application.sh" || { echo "Please set \${WKDEV_SDK} to point to the root of the wkdev-sdk checkout."; exit 1; }
init_application "${0}" "" container-only
init_application "${0}" "" container-only with-quiet-support

argsparse_allow_no_argument true
argsparse_use_option trace "Enable 'xtrace' mode for this script"
Expand Down
34 changes: 30 additions & 4 deletions scripts/host-only/wkdev-enter
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License: MIT

[ -f "${WKDEV_SDK}/.wkdev-sdk-root" ] && source "${WKDEV_SDK}/utilities/application.sh" || { echo "Please set \${WKDEV_SDK} to point to the root of the wkdev-sdk checkout."; exit 1; }
init_application "${0}" "Launch a command or spawn an interactive shell in a container built by 'wkdev-create'" host-only
init_application "${0}" "Launch a command or spawn an interactive shell in a container built by 'wkdev-create'" host-only with-quiet-support

# Source utility script fragments
source "${WKDEV_SDK}/utilities/host-setup-tasks.sh"
Expand All @@ -14,6 +14,9 @@ argsparse_use_option trace "Enable 'xtrace' mode for this script"
argsparse_use_option =verbose "Increase verbosity of this script"

argsparse_use_option =root "Login as root user in container (mapped to $(id --user --name) on host)"
argsparse_use_option no-tty "Disable tty allocation (you need this if you need want stdout and stderr to be different streams)" short:T
argsparse_use_option no-interactive "Disable stdin (useful in scripts, see Caveats)" short:I

argsparse_use_option =exec "Treat all remaining non-option arguments (or everything after the '--' character sequence) as command to execute in the container instead of spawning a shell"
argsparse_use_option =name: "Name of container" default:wkdev

Expand All @@ -39,6 +42,18 @@ argsparse_usage_description="$(cat <<EOF
$ ${application_name} --name <container-name>
$ ${application_name} --root --name <container-name>
$ ${application_name} --exec --name <container-name> -- uptime

<< Caveats >>

By default, this command uses podman exec --interactive under the hood.

podman exec --interactive uses sockets to pipe stdin to the contained process, and as a
consequence it will read as much input from stdin as it is available, even if the
contained process doesn't request it. This can lead to hangs or corrupted stdin if the
same stdin is connected to a script that calls wkdev-enter as anything but the last
command. Therefore, when writing scripts, use non-interactive mode where stdin is not
necessary and avoid sharing stdin with several interactive executions.

EOF
)"

Expand Down Expand Up @@ -144,8 +159,12 @@ run() {
fi
fi

# Request interactive session with pseudo-tty allocation.
local podman_exec_arguments=("--interactive" "--tty")
local podman_exec_arguments=()

# Request pseudo-tty allocation unless explicitly disabled
if ! argsparse_is_option_set "no-tty"; then
podman_exec_arguments+=("--tty")
fi

# Ensure WKDEV_SDK is set. It is done here and not creation to support older containers.
podman_exec_arguments+=("--env" "WKDEV_SDK=/wkdev-sdk")
Expand All @@ -161,14 +180,21 @@ run() {

podman_exec_arguments+=("${container_name}")

run_podman "${podman_arguments[@]}" exec "${podman_exec_arguments[@]}" /wkdev-sdk/scripts/container-only/.wkdev-sync-runtime-state
# Mount pipewire, wayland, etc...
run_podman "${podman_arguments[@]}" exec "${podman_exec_arguments[@]}" /wkdev-sdk/scripts/container-only/.wkdev-sync-runtime-state "${quiet_args[@]}" </dev/null

if argsparse_is_option_set "exec"; then
podman_exec_arguments+=("${program_params[@]}")
else
podman_exec_arguments+=("/usr/bin/env" "USER=$(id --user --name)" "${SHELL}" "--login")
fi

# --interactive is needed so that the process has access to *any* stdin.
# It is important that we use --interactive *only here*, as doing so in the
# previous podman exec call would eat stdin away. (See the Caveats section in Usage.)
if ! argsparse_is_option_set no-interactive; then
podman_exec_arguments=(--interactive "${podman_exec_arguments[@]}")
fi
run_podman "${podman_arguments[@]}" exec "${podman_exec_arguments[@]}"
}

Expand Down
39 changes: 38 additions & 1 deletion utilities/application.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,43 @@ application_description=""
# The 'init_application' method serves as common entry point for all scripts in the wkdev-sdk.
# Is is assumed that all scripts call 'init_application' in their preamble, before executing any logic.

log_to_stdout_enabled=1 # Disabled by --quiet.
quiet_args=() # Contains --quiet if --quiet is passed in argv, used
# to propagate it to other scripts.
app_argv=("${@}") # init_application requires argv to check for --quiet.

enable_quiet_support() {
# Register the option for the sake of --help.
argsparse_use_option =quiet "Silence all WebKit container SDK messages (use this when you need \
to launch a program in the container from the host with clean output)"

# We do the argument parsing ourselves instead of with argsparse
# as we need to know whether we need to be quiet before printing the
# application description message, before the application has set all
# its own argsparse settings.
for flag in "${app_argv[@]}"; do
case "${flag}" in
--quiet|-q)
log_to_stdout_enabled=0
quiet_args=(--quiet)
;;
--)
break
;;
esac
done
}

# Add log message to stdout.
_log_() {

local log_message="${1-}"
echo "${log_message}"
if [ $log_to_stdout_enabled -eq 1 ]; then
echo "${log_message}"
fi
# Log to journald if available. This can help users troubleshoot issues when
# using `wkdev-enter --quiet` inside scripts.
logger -p local0.info -t "${application_name}" -- "${log_message}" || true
}

# Aborts the application/script.
Expand Down Expand Up @@ -102,6 +134,11 @@ init_application() {
_abort_ "Unknown constraints '${application_constraints}' passed as third parameter to 'init_application'"
fi

local extra_options="${4-}" # Currently either "with-quiet-support" or nothing
if [ "${extra_options}" == "with-quiet-support" ]; then
enable_quiet_support
fi

[ -z "${application_description}" ] || _log_ "${application_name}: ${application_description}"
}

Expand Down