Skip to content

Commit 3958689

Browse files
authored
Bootstrap and remove scripts
1 parent 3437302 commit 3958689

File tree

2 files changed

+221
-0
lines changed

2 files changed

+221
-0
lines changed

bootstrap.sh

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#!/bin/sh
2+
3+
set -Ceu
4+
5+
IMAGE="quay.io/skupper/bootstrap"
6+
INPUT_PATH="${1:-${PWD}}"
7+
OUTPUT_PATH="${XDG_DATA_HOME:-${HOME}/.local/share}/skupper"
8+
SERVICE_DIR="${XDG_CONFIG_HOME:-${HOME}/.config}/systemd/user"
9+
if [ -z "${UID:-}" ]; then
10+
UID="$(id -u)"
11+
export UID
12+
fi
13+
LOG_FILE="$(mktemp /tmp/skupper-bootstrap.XXXXX.log)"
14+
15+
exit_error() {
16+
echo "$*"
17+
exit 1
18+
}
19+
20+
is_sock_endpoint() {
21+
case "${CONTAINER_ENDPOINT}" in
22+
"/"*)
23+
return 0
24+
;;
25+
"unix://"*)
26+
return 0
27+
;;
28+
esac
29+
return 1
30+
}
31+
32+
is_container_platform() {
33+
[ "${SKUPPER_PLATFORM}" != "systemd" ] && [ "${SKUPPER_PLATFORM}" != "bundle" ] && return 0
34+
return 1
35+
}
36+
37+
is_bundle_platform() {
38+
[ "${SKUPPER_PLATFORM}" = "bundle" ] && return 0
39+
return 1
40+
}
41+
42+
container_env() {
43+
export SKUPPER_PLATFORM="${SKUPPER_PLATFORM:-podman}"
44+
export CONTAINER_ENGINE="${CONTAINER_ENGINE:-podman}"
45+
# Determining if the respective binaries are available
46+
case "${SKUPPER_PLATFORM}" in
47+
systemd)
48+
command -v skrouterd > /dev/null || exit_error "SystemD platform cannot be used: skrouterd not found"
49+
command -v "${CONTAINER_ENGINE}" > /dev/null || exit_error "SystemD platform cannot be used: ${CONTAINER_ENGINE} (container engine) not found"
50+
;;
51+
docker)
52+
command -v docker > /dev/null || exit_error "Docker platform cannot be used: docker not found"
53+
export CONTAINER_ENGINE=docker
54+
;;
55+
*)
56+
command -v podman > /dev/null || exit_error "Podman platform cannot be used: podman not found"
57+
export CONTAINER_ENGINE=podman
58+
;;
59+
esac
60+
export CONTAINER_ENDPOINT_DEFAULT="unix://${XDG_RUNTIME_DIR:-/run/user/${UID}}/podman/podman.sock"
61+
GID=$(id -g "${UID}")
62+
export RUNAS="${UID}:${GID}"
63+
export USERNS="keep-id"
64+
if [ "${CONTAINER_ENGINE}" = "docker" ]; then
65+
export CONTAINER_ENDPOINT_DEFAULT="unix:///run/docker.sock"
66+
export USERNS="host"
67+
DOCKER_GID=$(getent group docker | cut -d: -f3)
68+
export RUNAS="${UID}:${DOCKER_GID}"
69+
fi
70+
if [ "${UID}" -eq 0 ]; then
71+
if [ "${CONTAINER_ENGINE}" = "podman" ]; then
72+
export CONTAINER_ENDPOINT_DEFAULT="unix:///run/podman/podman.sock"
73+
fi
74+
export USERNS=""
75+
export OUTPUT_PATH="/usr/local/share/skupper"
76+
export SERVICE_DIR="/etc/systemd/system"
77+
fi
78+
mkdir -p "${OUTPUT_PATH}"
79+
export CONTAINER_ENDPOINT="${CONTAINER_ENDPOINT:-${CONTAINER_ENDPOINT_DEFAULT}}"
80+
}
81+
82+
create_service() {
83+
# do not create service when preparing a site bundle
84+
if is_bundle_platform; then
85+
return
86+
fi
87+
88+
# if systemd is not available, skip it
89+
if [ "${UID}" -eq 0 ]; then
90+
systemctl list-units > /dev/null 2>&1 || return
91+
else
92+
systemctl --user list-units > /dev/null 2>&1 || return
93+
fi
94+
95+
# generated service file
96+
site_name="$(grep -E 'Site ".*" has been created' "${LOG_FILE}" | awk -F'"' '{print $2}')"
97+
if [ -z "${site_name}" ]; then
98+
echo "Unable to create SystemD service (site name could not be identified)"
99+
return
100+
fi
101+
service_name="skupper-site-${site_name}.service"
102+
service_file="${OUTPUT_PATH}/sites/${site_name}/runtime/scripts/${service_name}"
103+
if [ ! -f "${service_file}" ]; then
104+
echo "SystemD service has not been defined"
105+
return
106+
fi
107+
108+
# Moving it to the appropriate location
109+
if [ "${UID}" -eq 0 ]; then
110+
mv "${service_file}" /etc/systemd/system/
111+
systemctl enable --now "${service_name}"
112+
systemctl daemon-reload
113+
else
114+
if [ ! -d "${SERVICE_DIR}" ]; then
115+
echo "Unable to define path to SystemD service"
116+
return
117+
fi
118+
mv "${service_file}" "${SERVICE_DIR}"
119+
systemctl --user enable --now "${service_name}"
120+
systemctl --user daemon-reload
121+
fi
122+
}
123+
124+
main() {
125+
if [ -z "${INPUT_PATH}" ] || [ ! -d "${INPUT_PATH}" ]; then
126+
exit_error "Use: bootstrap.sh <local path to CRs>"
127+
fi
128+
129+
# Parse Skupper Platform and Container Engine settings
130+
container_env
131+
132+
# Must be mounted into the container
133+
MOUNTS=""
134+
ENV_VARS=""
135+
136+
# Mounts
137+
if is_sock_endpoint && is_container_platform; then
138+
file_container_endpoint=$(echo "${CONTAINER_ENDPOINT}" | sed -e "s#unix://##g")
139+
MOUNTS="${MOUNTS} -v '${file_container_endpoint}:/${CONTAINER_ENGINE}.sock:z'"
140+
fi
141+
MOUNTS="${MOUNTS} -v '${INPUT_PATH}:/input:z'"
142+
MOUNTS="${MOUNTS} -v '${OUTPUT_PATH}:/output:z'"
143+
144+
# Env vars
145+
if is_container_platform; then
146+
if is_sock_endpoint; then
147+
ENV_VARS="${ENV_VARS} -e 'CONTAINER_ENDPOINT=/${CONTAINER_ENGINE}.sock'"
148+
else
149+
ENV_VARS="${ENV_VARS} -e 'CONTAINER_ENDPOINT=${CONTAINER_ENDPOINT}'"
150+
fi
151+
fi
152+
ENV_VARS="${ENV_VARS} -e 'OUTPUT_PATH=${OUTPUT_PATH}'"
153+
ENV_VARS="${ENV_VARS} -e 'SKUPPER_PLATFORM=${SKUPPER_PLATFORM}'"
154+
155+
# Running the bootstrap
156+
${CONTAINER_ENGINE} pull ${IMAGE}
157+
if eval "${CONTAINER_ENGINE}" run --rm --name skupper-bootstrap \
158+
--network host --security-opt label=disable -u \""${RUNAS}"\" --userns=\""${USERNS}"\" \
159+
"${MOUNTS}" \
160+
"${ENV_VARS}" \
161+
"${IMAGE}" 2>&1 | tee "${LOG_FILE}"; then
162+
create_service
163+
fi
164+
}
165+
166+
main "$@"

remove.sh

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/sh
2+
3+
set -Ceu
4+
5+
if [ $# -eq 0 ] || [ -z "${1}" ]; then
6+
echo "Use: ${0##*/} <site-name>"
7+
exit 1
8+
fi
9+
10+
if [ -z "${UID:-}" ]; then
11+
UID="$(id -u)"
12+
export UID
13+
fi
14+
site=$1
15+
sites_path="${HOME}/.local/share/skupper/sites"
16+
service_path="${HOME}/.config/systemd/user"
17+
systemctl="systemctl --user"
18+
if [ "${UID}" -eq 0 ]; then
19+
sites_path="/usr/local/share/skupper/sites"
20+
service_path="/etc/systemd/system"
21+
systemctl="systemctl"
22+
fi
23+
24+
remove_definition() {
25+
platform_file="${sites_path}/${site}/runtime/state/platform.yaml"
26+
SKUPPER_PLATFORM=$(grep '^platform: ' "${platform_file}" | sed -e 's/.*: //g')
27+
if [ "${SKUPPER_PLATFORM}" != "systemd" ]; then
28+
${SKUPPER_PLATFORM} rm -f "${site}-skupper-router"
29+
fi
30+
rm -rf "${sites_path:?}/${site:?}/"
31+
}
32+
33+
remove_service() {
34+
service="skupper-site-${site}.service"
35+
${systemctl} stop "${service}"
36+
${systemctl} disable "${service}"
37+
rm -f "${service_path:?}/${service:?}"
38+
${systemctl} daemon-reload
39+
${systemctl} reset-failed
40+
}
41+
42+
main () {
43+
if ! echo "${site:?}" | grep -E '^[a-z0-9]([-a-z0-9]*[a-z0-9])?$'; then
44+
echo "Invalid site name"
45+
exit 0
46+
fi
47+
if [ ! -d "${sites_path:?}/${site:?}" ]; then
48+
echo "Site does not exist"
49+
exit 0
50+
fi
51+
remove_definition
52+
remove_service
53+
}
54+
55+
main "$@"

0 commit comments

Comments
 (0)