Repository navigation
Expand file tree
/
Copy pathocboot.sh
More file actions
executable file
·166 lines (141 loc) · 4.98 KB
/
Copy pathocboot.sh
File metadata and controls
executable file
·166 lines (141 loc) · 4.98 KB
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
#!/bin/bash
shopt -s expand_aliases
set -e
DEFAULT_REPO=registry.cn-beijing.aliyuncs.com/yunionio
IMAGE_REPOSITORY=${IMAGE_REPOSITORY:-$DEFAULT_REPO}
VERSION=${VERSION:-v4-k3s.6}
OCBOOT_IMAGE="$IMAGE_REPOSITORY/ocboot:$VERSION"
CUR_DIR="$(pwd)"
CONTAINER_NAME="buildah-ocboot"
alias buildah="sudo buildah"
ensure_buildah() {
if ! [ -x "$(command -v buildah)" ]; then
echo "Installing buildah ..."
./scripts/install-buildah.sh
fi
}
buildah_from_image() {
if buildah ps | grep $CONTAINER_NAME; then
buildah rm $CONTAINER_NAME
fi
local img="$1"
echo "Using buildah pull $img"
buildah pull --policy missing "$img"
buildah from --pull=false --name $CONTAINER_NAME "$img"
}
ensure_buildah
buildah_from_image "$OCBOOT_IMAGE"
mkdir -p "$HOME/.ssh"
ROOT_DIR='/ocboot'
CMD=""
is_ocboot_subcmd() {
local subcmds="install upgrade add-node add-lbagent backup restore setup-container-env switch-edition clickhouse setup-ai-env start stop set-calico-version fabric-setup"
for subcmd in $subcmds; do
if [[ "$1" == "$subcmd" ]]; then
return 0
fi
done
return 1
}
if is_ocboot_subcmd "$1"; then
CMD="$ROOT_DIR/ocboot.py"
fi
buildah_version=$(buildah --version | awk '{print $3}')
buildah_version_major=$(echo "$buildah_version" | awk -F. '{print $1}')
buildah_version_minor=$(echo "$buildah_version" | awk -F. '{print $2}')
buildah_extra_args=()
# buildah accepts --env since 1.23
echo "buildah version: $buildah_version"
# Pass selected host env vars with env(1). buildah run does not inherit the
# caller environment, and --env is unavailable on buildah <= 1.23.
ansible_verbosity="${ANSIBLE_VERBOSITY:-0}"
echo "ansible verbosity: $ansible_verbosity"
container_env=("ANSIBLE_VERBOSITY=${ansible_verbosity}")
if [[ -n "${IGNORE_ALL_CHECKS:-}" ]]; then
echo "IGNORE_ALL_CHECKS: ${IGNORE_ALL_CHECKS}"
container_env+=("IGNORE_ALL_CHECKS=${IGNORE_ALL_CHECKS}")
fi
if [[ $buildah_version_major -eq 1 ]] && [[ "$buildah_version_minor" -gt 23 ]]; then
buildah_extra_args+=(-e HOME="$HOME")
fi
cmd_extra_args=""
origin_args=()
normalize_installer_path() {
local path="$1"
if [[ "$path" == /* ]]; then
if [[ "$path" == "$(pwd)"/* ]] || [[ "$path" == "$(pwd)" ]]; then
if [[ "$path" == "$(pwd)" ]]; then
echo "$ROOT_DIR"
else
echo "$ROOT_DIR/${path#$(pwd)/}"
fi
else
echo "$path"
fi
else
path="${path#./}"
echo "$ROOT_DIR/$path"
fi
}
prev_arg=""
for arg in "$@"; do
case "$prev_arg" in
--nvidia-driver-installer-path|--cuda-installer-path)
origin_args+=("$(normalize_installer_path "$arg")")
;;
*)
origin_args+=("$arg")
;;
esac
prev_arg="$arg"
done
if [[ "${origin_args[0]:-}" == "run.py" ]]; then
if [[ "$IMAGE_REPOSITORY" != "$DEFAULT_REPO" ]]; then
cmd_extra_args="$cmd_extra_args -i $IMAGE_REPOSITORY"
fi
origin_args[0]="$ROOT_DIR/run.py"
fi
mkdir -p "$HOME/.kube"
# Parse --nvidia-driver-installer-path and --cuda-installer-path from original args and
# add bind-mounts so installer files outside $(pwd) are accessible in the container.
extra_installer_volumes=()
prev_arg=""
declare -A mounted_installer_dirs
for arg in "$@"; do
case "$prev_arg" in
--nvidia-driver-installer-path|--cuda-installer-path)
# Only handle absolute paths that are not already under $(pwd)
if [[ "$arg" == /* ]] && [[ "${arg#$(pwd)/}" == "$arg" ]] && [[ "$arg" != "$(pwd)" ]]; then
_dir="$(dirname "$arg")"
if [[ -z "${mounted_installer_dirs[$_dir]+x}" ]]; then
extra_installer_volumes+=(-v "$_dir:$_dir:ro")
mounted_installer_dirs[$_dir]=1
fi
fi
;;
esac
prev_arg="$arg"
done
# Bind the config file after extra_installer_volumes is initialized.
if [[ "${origin_args[0]:-}" == "$ROOT_DIR/run.py" ]]; then
if [ -n "${origin_args[2]}" ] && [ -f "${origin_args[2]}" ]; then
config_file_fullpath=$(readlink -f "${origin_args[2]}")
config_file_name=$(basename "$config_file_fullpath")
echo "Using config file: $config_file_fullpath -> /$config_file_name"
extra_installer_volumes+=(-v "$config_file_fullpath:/$config_file_name:ro")
origin_args[2]="/$config_file_name"
echo "extra_installer_volumes: ${extra_installer_volumes[@]}"
fi
fi
buildah run --isolation chroot --user $(id -u):$(id -g) \
-t "${buildah_extra_args[@]}" \
--net=host \
-v "$(mktemp -d):$HOME/.ansible" \
-v "$HOME/.ssh:$HOME/.ssh" \
-v "$HOME/.kube:$HOME/.kube" \
-v "/etc/passwd:/etc/passwd:ro" \
-v "/etc/group:/etc/group:ro" \
-v "$(pwd):$ROOT_DIR" \
-v "$(pwd)/airgap_assets/k3s-install.sh:/airgap_assets/k3s-install.sh:ro" \
"${extra_installer_volumes[@]}" \
"$CONTAINER_NAME" env "${container_env[@]}" $CMD "${origin_args[@]}" $cmd_extra_args