forked from IBM/action-runner-image-pz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·270 lines (224 loc) · 8.58 KB
/
Copy pathrun.sh
File metadata and controls
executable file
·270 lines (224 loc) · 8.58 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/bin/bash
set -euo pipefail
# --- Reusable Helper Functions ---
# Function to display help message
show_help() {
cat << EOF
Usage: $(basename "$0") [OPTIONS]
Description:
This is an interactive wrapper script designed to facilitate the setup of
various environments including VM (Host), LXD Container, LXD VM, Docker, and Podman.
It guides the user through a menu-driven process to select:
1. Target Environment (VM, LXD Container, LXD VM, Docker, Podman)
2. Operating System & Version
3. Setup Type (Minimal vs Complete)
4. Infrastructure specific arguments (Worker size, Architecture)
Options:
-h, --help Show this help message and exit.
-------------------------------------------------------------------------
IMPORTANT NOTE ON SUBSCRIPTS:
This script acts as a runner. It delegates the actual configuration logic
to specific scripts located in the 'scripts/' directory (e.g., scripts/lxd_container.sh).
If you need detailed information about specific parameters, flags, or
underlying logic for a specific environment, please run the help command
on that subscript directly.
Examples:
scripts/lxd_container.sh --help
-------------------------------------------------------------------------
EOF
}
# A generic function to display a menu and get user's choice.
# Arguments:
# $1: The prompt to display to the user.
# $2+: The list of menu options.
select_menu() {
local prompt="$1"
shift
local options=("$@")
# PS3 is the prompt string for the select command.
PS3="$prompt"
select opt in "${options[@]}"; do
if [[ -n "$opt" ]]; then
echo "$opt"
return
else
echo "Invalid choice. Please try again." >&2
fi
done
}
# Final execution function. Centralizes the sudo call.
run_setup() {
local env="$1" os="$2" version="$3" setup_type="$4"
local worker_arg="${5:-}" # Default to empty string if not provided
local arch_arg="${6:-}" # Default to empty string if not provided
echo "Proceeding with ${env} setup for ${os} ${version}..."
echo "Setup type: ${setup_type}"
[[ -n "$worker_arg" ]] && echo "Worker size: ${worker_arg#-}"
[[ -n "$arch_arg" ]] && echo "Architecture flag: ${arch_arg#-}"
# Use an array for safer argument passing
local script_args=("${os}" "${version}" "${worker_arg}" "${arch_arg}" "${setup_type}")
if [[ "$env" == "vm" ]]; then
script_args=("${script_args[@]}" "--skip-snap-lxd")
fi
# The script to be run inside the new shell.
# It sources the target script and passes along all of its own arguments ("$@").
local inner_script=". 'scripts/${env}.sh' \"\$@\""
# Execute using sudo bash -c, preserving GITHUB_TOKEN if set
# The first argument after the script string ('bash') becomes $0 inside the new shell.
# The subsequent arguments ("${script_args[@]}") become $1, $2, $3, etc.
sudo ${GITHUB_TOKEN:+GITHUB_TOKEN="${GITHUB_TOKEN}"} bash -c "${inner_script}" bash "${script_args[@]}"
}
# --- Parameter Gathering Functions ---
# Get OS and Version from the user for a given environment.
get_os_details() {
local env="$1"
local os_options=()
case "$env" in
lxd_container|lxd_vm) os_options=("Ubuntu" "Back");;
*) os_options=("Ubuntu" "CentOS/AlmaLinux" "RHEL" "Back");;
esac
local os_choice
os_choice=$(select_menu "Select the OS for $env setup: " "${os_options[@]}")
case "$os_choice" in
"Ubuntu")
local version
version=$(select_menu "Select Ubuntu version: " "24.04" "22.04" "Back")
[[ "$version" == "Back" ]] && return 1
echo "ubuntu $version"
;;
"CentOS/AlmaLinux")
local version
version=$(select_menu "Select CentOS/AlmaLinux version: " "9" "Back")
[[ "$version" == "Back" ]] && return 1
echo "centos $version"
;;
"RHEL")
local version
version=$(select_menu "Select RHEL version: " "9" "10" "Back")
[[ "$version" == "Back" ]] && return 1
echo "rhel $version"
;;
"Back")
return 1
;;
esac
}
# Determine the setup type (Minimal/Complete) based on environment and OS.
get_setup_type() {
local env="$1" os="$2"
if [[ "$os" == "centos" || "$os" == "rhel" || "$env" == "docker" || "$env" == "podman" ]]; then
echo "minimal" # These combinations only support minimal setup
return
fi
# For VM or LXD on Ubuntu, ask the user
local setup_choice
setup_choice=$(select_menu "Choose setup type: " "Minimal" "Complete" "Back")
case "$setup_choice" in
"Minimal") echo "minimal";;
"Complete") echo "complete";;
"Back") return 1;;
esac
}
# Get LXD-specific worker and architecture arguments.
get_lxd_args() {
local worker_arg=""
local worker_choice
worker_choice=$(select_menu "Choose worker category: " "default" "2xlarge" "4xlarge")
case "$worker_choice" in
"2xlarge") worker_arg="-2xlarge";;
"4xlarge") worker_arg="-4xlarge";;
esac
local arch_arg=""
# More robust check for POWER10 CPU
if grep -q -E 'cpu\s+:\s+POWER10' /proc/cpuinfo 2>/dev/null; then
arch_arg="-p10"
fi
echo "$worker_arg $arch_arg"
}
# --- Main Logic ---
main() {
while true; do
# `|| true` prevents script exit if user presses Ctrl+D
main_choice=$(select_menu "Select the setup type: " "VM (host machine)" "LXD Container" "LXD VM" "Docker" "Podman" "Exit") || true
local env=""
local os_details=""
local os=""
local version=""
local setup_type=""
local lxd_args=""
local worker_arg=""
local arch_arg=""
case "$main_choice" in
"VM (host machine)")
env="vm"
# Source /etc/os-release for a reliable and efficient way to get OS info
if [[ -f /etc/os-release ]]; then
# shellcheck source=/dev/null
. /etc/os-release
os=$(echo "$ID" | tr '[:upper:]' '[:lower:]')
version="$VERSION_ID"
echo "Detected Host OS: $os $version"
os_details="$os $version"
else
echo "Could not detect OS from /etc/os-release. Please choose manually." >&2
# Fallback to manual selection
os_details=$(get_os_details "$env") || continue
fi
;;
"LXD Container"|"LXD VM"|"Docker"|"Podman")
env=$(echo "$main_choice" | tr '[:upper:]' '[:lower:]' | tr ' ' '_')
os_details=$(get_os_details "$env") || continue
;;
"Exit")
echo "Exiting."
break
;;
"") # Handles Ctrl+D from select menu
echo "Exiting."
break
;;
esac
# Parse os_details into os and version
read -r os version <<< "$os_details"
# Check architecture support
local arch
arch=$(uname -m)
case "$arch" in
ppc64le|s390x|x86_64)
# Architecture is supported, proceed
;;
*)
echo "Architecture '$arch' is not supported." >&2
local back_choice
back_choice=$(select_menu "Choose an option: " "Return to main menu" "Exit")
[[ "$back_choice" == "Exit" ]] && exit 0
continue
;;
esac
# Get the setup type (Minimal/Complete)
setup_type=$(get_setup_type "$env" "$os") || continue
# Get extra args only if the environment is LXD
if [[ "$env" == "lxd_container" || "$env" == "lxd_vm" ]]; then
lxd_args=$(get_lxd_args)
read -r worker_arg arch_arg <<< "$lxd_args"
fi
# Run the final setup
run_setup "$env" "$os" "$version" "$setup_type" "$worker_arg" "$arch_arg"
echo "Setup script finished."
# Optional: Ask to continue or exit after a successful run
local continue_choice
continue_choice=$(select_menu "What next?" "Return to main menu" "Exit")
if [[ "$continue_choice" == "Exit" ]]; then
echo "Exiting."
break
fi
done
}
# --- Argument Parsing ---
# Check if help is requested before starting main execution
if [[ "${1:-}" == "-h" ]] || [[ "${1:-}" == "--help" ]]; then
show_help
exit 0
fi
# Run the main function
main