-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrhcos-toolbox
executable file
·135 lines (113 loc) · 3.24 KB
/
rhcos-toolbox
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
#!/bin/bash
set -eo pipefail
trap cleanup EXIT
setup() {
REGISTRY=registry.redhat.io
IMAGE=rhel7/support-tools
TOOLBOX_NAME=toolbox-"${USER}"
# Allow user overrides
toolboxrc="${HOME}"/.toolboxrc
if [ -f "${toolboxrc}" ]; then
echo ".toolboxrc file detected, overriding defaults..."
source "${toolboxrc}"
fi
TOOLBOX_IMAGE="${REGISTRY}"/"${IMAGE}"
}
run() {
if ! image_exists; then
image_pull
fi
local runlabel=$(image_runlabel)
if ! container_exists; then
echo "Spawning a container '$TOOLBOX_NAME' with image '$TOOLBOX_IMAGE'"
if [[ -z "$runlabel" ]]; then
container_create
else
echo "Detected RUN label in the container image. Using that as the default..."
container_runlabel
return
fi
else
echo "Container '$TOOLBOX_NAME' already exists. Trying to start..."
echo "(To remove the container and start with a fresh toolbox, run: sudo podman rm '$TOOLBOX_NAME')"
fi
local state=$(container_state)
if [[ "$state" == configured ]] || [[ "$state" == exited ]] || [[ "$state" == stopped ]]; then
container_start
elif [[ "$state" != running ]]; then
echo "Container '$TOOLBOX_NAME' in unknown state: '$state'"
return 1
fi
echo "Container started successfully. To exit, type 'exit'."
container_exec "$@"
}
cleanup() {
sudo podman stop "$TOOLBOX_NAME" &>/dev/null
}
container_exists() {
sudo podman inspect "$TOOLBOX_NAME" &>/dev/null
}
container_state() {
sudo podman inspect "$TOOLBOX_NAME" --format '{{.State.Status}}'
}
image_exists() {
sudo podman inspect "$TOOLBOX_IMAGE" &>/dev/null
}
image_runlabel() {
sudo podman container runlabel --display RUN "$TOOLBOX_IMAGE"
}
image_pull() {
if ! sudo podman pull "$TOOLBOX_IMAGE"; then
read -r -p "Would you like to authenticate to registry: '${REGISTRY}' and try again? [y/N] "
if [[ $REPLY =~ ^([Yy][Ee][Ss]|[Yy])+$ ]]; then
sudo podman login "${REGISTRY}"
sudo podman pull "$TOOLBOX_IMAGE"
else
echo "Exiting..."
exit 1
fi
fi
}
container_create() {
if ! sudo podman create \
--hostname toolbox \
--name "$TOOLBOX_NAME" \
--network host \
--privileged \
--security-opt label=disable \
--tty \
--volume /:/media/root:rslave \
"$TOOLBOX_IMAGE" 2>&1; then
echo "$0: failed to create container '$TOOLBOX_NAME'"
exit 1
fi
}
container_start() {
if ! sudo podman start "$TOOLBOX_NAME" 2>&1; then
echo "$0: failed to start container '$TOOLBOX_NAME'"
exit 1
fi
}
container_runlabel() {
if ! sudo podman container runlabel --name "$TOOLBOX_NAME" RUN "$TOOLBOX_IMAGE" 2>&1; then
echo "$0: failed to runlabel on image '$TOOLBOX_IMAGE'"
exit 1
fi
}
container_exec() {
sudo podman exec \
--env LANG="$LANG" \
--env TERM="$TERM" \
--tty \
"$TOOLBOX_NAME" \
"$@"
}
main() {
setup
run "$@"
cleanup
}
if [ ! -n "$*" ]; then
set /bin/sh "$@"
fi
main "$@"