-
Notifications
You must be signed in to change notification settings - Fork 438
/
Copy pathdistrobox-enter
executable file
·207 lines (185 loc) · 5.44 KB
/
distrobox-enter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/sh
# POSIX
# Expected env variables:
# HOME
# USER
# SHELL
trap '[ "$?" -ne 0 ] && printf "\nAn error occurred\n"' EXIT
# Defaults
container_command="${SHELL:-"bash"} -l"
container_name="fedora-toolbox-35"
headless=0
verbose=0
version="distrobox_version_placeholder"
# Print usage to stdout.
# Arguments:
# None
# Outputs:
# print usage with examples.
show_help() {
cat <<EOF
distrobox version: ${version}
distrobox-enter takes care of entering the container with the name specified.
Default command executed is your SHELL, buf you can specify different shells or
entire commands to execute.
If using it inside a script, an application or a service, you can specify the
--headless mode to disable tty and interactivity.
Usage:
distrobox-enter --name fedora-toolbox-35 -- bash -l
Options:
--name/-n: name for the distrobox default: fedora-toolbox-35
--/-e: end arguments execute the rest as command to execute at login default: bash -l
--headless/-H: do not instantiate a tty
--help/-h: show this message
--verbose/-v: show more verbosity
--version/-V: show version
EOF
}
# Parse arguments
while :; do
case $1 in
-h | --help)
# Call a "show_help" function to display a synopsis, then exit.
show_help
exit 0
;;
-v | --verbose)
shift
verbose=1
;;
-H | --headless)
shift
headless=1
;;
-V | --version)
printf "distrobox: %s\n" "${version}"
exit 0
;;
-n | --name)
if [ -n "$2" ]; then
container_name="$2"
shift
shift
fi
;;
-e | --exec | --)
shift
container_command=$*
break
;;
*) # Default case: If no more options then break out of the loop.
break ;;
esac
done
set -o errexit
set -o nounset
# set verbosity
if [ "${verbose}" -ne 0 ]; then
set -o xtrace
fi
# We depend on a container manager let's be sure we have it
# First we use podman, else docker
container_manager="podman"
if ! command -v podman >/dev/null; then
container_manager="docker"
if ! command -v docker >/dev/null; then
printf >&2 "Missing dependency: we need a container manager\n."
printf >&2 "Please install one of podman or docker.\n"
exit 127
fi
fi
# Generate Podman or Docker command to execute.
# Arguments:
# None
# Outputs:
# prints the podman or docker command to enter the distrobox container
generate_command() {
result_command="${container_manager}"
# add verbose if -v is specified
if [ "${verbose}" -ne 0 ]; then
result_command="${result_command} --log-level debug"
fi
result_command="${result_command} exec"
result_command="${result_command}
--user=${USER}"
if [ "${headless}" -eq 0 ]; then
result_command="${result_command}
--interactive
--tty"
fi
# Entering container using our user and workdir.
# Start container from working directory. Else default to home. Else do /.
# pass distrobox-enter path, it will be used in the distrobox-export tool.
result_command="${result_command}
--workdir=${PWD:-${HOME:-"/"}}
--env=DISTROBOX_ENTER_PATH=$(command -v distrobox-enter)"
# Loop through all the environment vars
# and export them to the container.
set +o xtrace
# disable logging fot this snippet, or it will be too talkative.
for i in $(printenv | grep '=' | grep -v ' ' | grep -v '"'); do
result_command="${result_command} --env=\"${i}\""
done
# re-enable logging if it was enabled previously.
if [ "${verbose}" -ne 0 ]; then
set -o xtrace
fi
# Run selected container with specified command.
result_command="${result_command} ${container_name} ${container_command}"
# Return generated command.
printf "%s" "${result_command}"
}
# Inspect the container we're working with.
container_status="$("${container_manager}" inspect --type container "${container_name}" --format '{{.State.Status}}')"
container_exists="$?"
# Does the container exists? check if inspect reported errors
if [ "${container_exists}" -gt 0 ]; then
# If not, prompt to create it first
printf >&2 "Cannot find container %s, does it exist?\n" "${container_name}"
printf >&2 "\nTry running first:\n"
printf >&2 "\tdistrobox-create --name <name-of-container> --image <remote>/<docker>:<tag>\n"
exit 1
fi
# If the container is not already running, we need to start if first
if [ "${container_status}" != "running" ]; then
# If container is not running, start it first
# Here, we save the timestamp before launching the start command, so we can
# be sure we're working with this very same session of logs later.
log_timestamp="$(date +%FT%T.%N%:z)"
"${container_manager}" start "${container_name}" >/dev/null
printf >&2 "Starting container %s\n" "${container_name}"
printf >&2 "run this command to follow along:\n"
printf >&2 "\t%s logs -f %s\n" "${container_manager}" "${container_name}"
# Wait for container to start successfully.
# We will probe the container logs every 1s to check if we have either:
# Error or container_setup_done
#
# In the end, print eventual Warnings that occurred.
while :; do
container_manager_log="$("${container_manager}" logs -t \
--since "${log_timestamp}" \
"${container_name}" 2>/dev/null)"
case "${container_manager_log}" in
*"Error"*)
printf >&2 "%s\n" "${container_manager_log}"
exit 1
;;
*"container_setup_done"*)
break
;;
*)
printf >&2 "."
sleep 1
;;
esac
done
printf >&2 "\ndone!\n"
# Print eventual warnings in the log.
"${container_manager}" logs -t \
--since "${log_timestamp}" \
"${container_name}" 2>/dev/null | grep "Warning" >&2 || :
fi
# Generate the exec command and run it
cmd="$(generate_command)"
# shellcheck disable=SC2086
eval ${cmd}