-
Notifications
You must be signed in to change notification settings - Fork 9
/
sb_install.sh
executable file
·240 lines (198 loc) · 6.59 KB
/
sb_install.sh
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
#!/bin/bash
#shellcheck disable=SC2220
#########################################################################
# Title: Saltbox Install Script #
# Author(s): desimaniac, salty #
# URL: https://github.com/saltyorg/sb #
# -- #
#########################################################################
# GNU General Public License v3.0 #
#########################################################################
################################
# Variables
################################
VERBOSE=false
VERBOSE_OPT=""
SB_REPO="https://github.com/saltyorg/sb.git"
SB_PATH="/srv/git/sb"
RELEASE_FILE="/srv/git/sb/release.txt"
TARGET_BINARY_PATH="/srv/git/sb/sb"
SB_INSTALL_SCRIPT="$SB_PATH/sb_install.sh"
SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
BRANCH="master"
################################
# Functions
################################
run_cmd() {
local error_output
local cmd_exit_code
if $VERBOSE; then
printf '%s\n' "+ $*" >&2
"$@"
else
error_output=$("$@" 2>&1)
fi
cmd_exit_code=$?
if [ $cmd_exit_code -ne 0 ]; then
echo "Command failed with exit code $cmd_exit_code: $*" >&2
if [ -n "$error_output" ]; then
echo "Error output: $error_output" >&2
fi
exit $cmd_exit_code
fi
}
download_binary() {
local github_tag
local version
local download_url
local temp_binary_path
local file_type
if ! command -v file > /dev/null 2>&1; then
run_cmd sudo apt-get update
run_cmd sudo apt-get install -y file
fi
if [ ! -f "${RELEASE_FILE}" ]; then
echo "Error: ${RELEASE_FILE} does not exist." >&2
exit 1
fi
github_tag=$(head -n 1 "${RELEASE_FILE}" | tr -d '[:space:]')
if [[ ! "$github_tag" =~ ^refs/tags/ ]]; then
echo "Error: Invalid tag format in ${RELEASE_FILE}." >&2
exit 1
fi
version=${github_tag#refs/tags/}
if [ -z "$version" ]; then
echo "Error: No version found in tag $github_tag." >&2
exit 1
fi
download_url="https://github.com/saltyorg/sb/releases/download/$version/sb"
temp_binary_path="${TARGET_BINARY_PATH}.tmp"
run_cmd curl -L -o "${temp_binary_path}" "${download_url}"
file_type=$(file -b --mime-type "${temp_binary_path}")
if [[ "$file_type" != application/* ]]; then
echo "Error: Downloaded file is not a binary. Detected type: $file_type" >&2
rm -f "${temp_binary_path}"
exit 1
fi
run_cmd mv -f "${temp_binary_path}" "${TARGET_BINARY_PATH}"
run_cmd chmod +x "${TARGET_BINARY_PATH}"
}
################################
# Argument Parser
################################
while getopts 'vb:' f; do
case $f in
v) VERBOSE=true
VERBOSE_OPT="-v"
;;
b) BRANCH=$OPTARG
;;
esac
done
################################
# Main
################################
# Check if Cloudbox is installed
# develop
if [ -d "/srv/git/cloudbox" ]; then
echo "==== Cloudbox Install Detected ===="
echo "Cloudbox installed. Exiting..."
echo "==== Cloudbox Install Detected ===="
exit 1
fi
# master
for directory in /home/*/*/ ; do
base=$(basename "$directory")
if [ "$base" == "cloudbox" ]; then
echo "==== Cloudbox Install Detected ===="
echo "Cloudbox installed. Exiting..."
echo "==== Cloudbox Install Detected ===="
exit 1
fi
done
# Check for supported Ubuntu Releases
release=$(lsb_release -cs 2>/dev/null | grep -v "No LSB modules are available.")
# Add more releases like (focal|jammy)$
if [[ $release =~ (focal|jammy)$ ]]; then
echo "${release^} is currently supported."
elif [[ $release =~ (noble)$ ]]; then
echo "${release^} is currently in testing."
else
echo "==== UNSUPPORTED OS ===="
echo "Install cancelled: ${release^} is not supported."
echo "Supported OS: 20.04 (Focal), 22.04 (Jammy) and 24.04 (Nobel)"
echo "==== UNSUPPORTED OS ===="
exit 1
fi
# Check if using valid arch
arch=$(uname -m)
if [[ $arch =~ (x86_64)$ ]]; then
echo "$arch is currently supported."
else
echo "==== UNSUPPORTED CPU Architecture ===="
echo "Install cancelled: $arch is not supported."
echo "Supported CPU Architecture(s): x86_64"
echo "==== UNSUPPORTED CPU Architecture ===="
exit 1
fi
# Check for LXC using systemd-detect-virt
if systemd-detect-virt -c | grep -qi 'lxc'; then
echo "==== UNSUPPORTED VIRTUALIZATION ===="
echo "Install cancelled: Running in an LXC container is not supported."
echo "==== UNSUPPORTED VIRTUALIZATION ===="
exit 1
fi
# Check if specific desktop packages are installed
if dpkg -l ubuntu-desktop &>/dev/null; then
echo "==== UNSUPPORTED DESKTOP INSTALL ===="
echo "Install cancelled: Only Ubuntu Server is supported."
echo "==== UNSUPPORTED DESKTOP INSTALL ===="
exit 1
fi
# Define required CPU features for x86-64-v2
#required_features=("sse4_2" "popcnt")
# Check for x86-64-v2 support
#for feature in "${required_features[@]}"; do
# if ! grep -q " $feature " /proc/cpuinfo; then
# echo "==== UNSUPPORTED CPU Microarchitecture ===="
# echo "Install cancelled: CPU does not support minimum microarchitecture level: x86-64-v2"
# echo "==== UNSUPPORTED CPU Microarchitecture ===="
# exit 1
# fi
#done
echo "Installing Saltbox Dependencies."
$VERBOSE && echo "Script Path: $SCRIPT_PATH"
# Update apt cache
run_cmd apt-get update
# Install git
run_cmd apt-get install -y git curl
# Remove existing repo folder
if [ -d "$SB_PATH" ]; then
run_cmd rm -rf $SB_PATH;
fi
# Clone SB repo
run_cmd mkdir -p /srv/git
run_cmd mkdir -p /srv/ansible
run_cmd git clone --branch master "${SB_REPO}" "$SB_PATH"
download_binary
# Set chmod +x on script files
run_cmd chmod +x $SB_PATH/*.sh
$VERBOSE && echo "Script Path: $SCRIPT_PATH"
$VERBOSE && echo "SB Install Path: "$SB_INSTALL_SCRIPT
# Check if /usr/local/bin exists, create it if not
if [ ! -d "/usr/local/bin" ]; then
run_cmd mkdir -m 0755 -p /usr/local/bin
fi
## Create script symlinks in /usr/local/bin
shopt -s nullglob
for i in "$SB_PATH"/*.sh; do
if [ ! -f "/usr/local/bin/$(basename "${i%.*}")" ]; then
run_cmd ln -s "${i}" "/usr/local/bin/$(basename "${i%.*}")"
fi
done
shopt -u nullglob
# Install Saltbox Dependencies
run_cmd bash -H $SB_PATH/sb_dep.sh $VERBOSE_OPT
# Clone Saltbox Repo
run_cmd bash -H $SB_PATH/sb_repo.sh -b "${BRANCH}" $VERBOSE_OPT
echo "Saltbox Dependencies were successfully installed."