forked from rackerlabs/genestack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.sh
More file actions
executable file
·272 lines (228 loc) · 8.9 KB
/
bootstrap.sh
File metadata and controls
executable file
·272 lines (228 loc) · 8.9 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
271
272
#!/usr/bin/env bash
# Copyright 2024, Rackspace Technology, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Function to display help message
show_help() {
cat << EOF
Usage: $0 [OPTIONS]
Bootstrap script for Genestack deployment environment.
OPTIONS:
--no-wait-cloud-init Skip waiting for cloud-init to complete
-h, --help Show this help message and exit
DESCRIPTION:
This script sets up the Genestack environment by:
- Waiting for cloud-init to complete (unless --no-wait-cloud-init is used)
- Installing required packages
- Setting up Python virtual environment
- Configuring Ansible and collections
- Creating necessary directory structures and symlinks
EXAMPLES:
$0 # Run with default behavior (wait for cloud-init)
$0 --no-wait-cloud-init # Skip cloud-init wait
$0 --help # Show this help message
EOF
}
# Parse command line arguments
WAIT_CLOUD_INIT=true
while [[ $# -gt 0 ]]; do
case $1 in
--no-wait-cloud-init)
WAIT_CLOUD_INIT=false
shift
;;
-h|--help)
show_help
exit 0
;;
*)
echo "Error: Unknown option '$1'"
echo "Use '$0 --help' for usage information."
exit 1
;;
esac
done
export LC_ALL=C.UTF-8
mkdir -p ~/.venvs
BASEDIR="$(dirname "$0")"
cd "${BASEDIR}" || error "Could not change to ${BASEDIR}"
source scripts/lib/functions.sh
success "Environment variables:"
env | grep -E '^(SUDO|RPC_|ANSIBLE_|GENESTACK_|K8S|CONTAINER_|OPENSTACK_|OSH_)' | sort -u
# Explictily do not exit script on non-zero returns
set +e
# Wait until cloud-init is finished before proceeding (unless skipped)
if [[ "$WAIT_CLOUD_INIT" == "true" ]]; then
echo "Waiting for cloud-init to finish..."
wait_for_cloud_init
if [[ $? -eq 0 ]]; then
echo "Cloud-init completed successfully!"
elif [[ $? -eq 1 ]]; then
echo "Cloud-init crashed or experienced a serious issue."
elif [[ $? -eq 2 ]]; then
echo "Cloud-init completed with errors."
else
echo "Cloud-init command not found."
fi
else
echo "Skipping cloud-init wait (--no-wait-cloud-init flag used)."
fi
# NOTE: (brew) This function will determine wether DNF or APT should be used
# to install packages and will install them.
# Package: scripts/lib/funcitons.sh ['apt_packages', 'dnf_packages']
wait_and_install_packages
if [ $? -gt 1 ]; then
error "Check for ansible errors at ~/genestack-base-package-install.log"
else
success "Local base OS packages installed."
fi
# Set script to exit on any non-zero error code
set -e
ensureYq
ensureHelm
# Install project dependencies
success "Configuring genestack directory and overrides directory structure:"
test -L "$GENESTACK_CONFIG" 2>&1 || mkdir -p "${GENESTACK_CONFIG}"
# Set config
test -f "$GENESTACK_CONFIG/provider" || echo "${K8S_PROVIDER}" > "${GENESTACK_CONFIG}/provider"
mkdir -p "$GENESTACK_CONFIG/inventory/group_vars" "${GENESTACK_CONFIG}/inventory/credentials"
# Copy default k8s config
PRODUCT_DIR="ansible/inventory/genestack"
if [ "$(find "${GENESTACK_CONFIG}/inventory" -name \*.yaml -o -name \*.yml 2>/dev/null | wc -l)" -eq 0 ]; then
cp -r "${PRODUCT_DIR}"/* "${GENESTACK_CONFIG}/inventory"
fi
# Copy gateway-api example configs
test -d "$GENESTACK_CONFIG/gateway-api" || cp -a "${BASEDIR}/etc/gateway-api" "$GENESTACK_CONFIG"/
# Create venv and prepare Ansible
python3 -m venv "${HOME}/.venvs/genestack"
"${HOME}/.venvs/genestack/bin/pip" install pip --upgrade
source "${HOME}/.venvs/genestack/bin/activate" && success "Switched to venv ~/.venvs/genestack."
pip install -r "${BASEDIR}/requirements.txt" && success "Installed ansible package."
ansible-playbook "${BASEDIR}/scripts/get-ansible-collection-requirements.yml" \
-e collections_file="${ANSIBLE_COLLECTION_FILE}" \
-e user_collections_file="${USER_COLLECTION_FILE}"
source "${BASEDIR}/scripts/genestack.rc"
success "Environment sourced per ${BASEDIR}/scripts/genestack.rc."
message "OpenStack Release: ${OPENSTACK_RELEASE}"
message "Target OS Distro: ${CONTAINER_DISTRO_NAME}:${CONTAINER_DISTRO_VERSION}"
message "Deploy Mulinode: ${OSH_DEPLOY_MULTINODE}"
# Ensure /etc/genestack exists
mkdir -p /etc/genestack
# Ensure each service from /opt/genestack/base-kustomize
# exists in /etc/genestack/kustomize and symlink
# all the sub-directories
base_source_dir="/opt/genestack/base-kustomize"
base_target_dir="/etc/genestack/kustomize"
for service in "$base_source_dir"/*; do
service_name=$(basename "$service")
if [ -d "$service" ]; then
# Check if the service has subdirectories
if [ "$(find "$service" -mindepth 1 -type d | wc -l)" -eq 0 ]; then
# If no subdirectories, symlink the service directly under the target dir
if [ ! -e "$base_target_dir/$service_name" ] && [ ! -L "$base_target_dir/$service_name" ]; then
ln -s "$service" "$base_target_dir/$service_name"
success "Created symlink for $service_name directly under $base_target_dir."
else
message "Symlink for $service_name already exists directly under $base_target_dir."
fi
else
if [ -d "$base_target_dir/$service_name" ]; then
message "$base_target_dir/$service_name already exists."
else
message "Creating $base_target_dir/$service_name."
mkdir -p "$base_target_dir/$service_name"
fi
for item in "$service"/*; do
item_name=$(basename "$item")
if [ ! -e "$base_target_dir/$service_name/$item_name" ] && [ ! -L "$base_target_dir/$service_name/$item_name" ]; then
ln -s "$item" "$base_target_dir/$service_name/$item_name"
success "Created symlink for $service_name/$item_name."
else
message "Symlink for $service_name/$item_name already exists."
fi
done
fi
else
message "$service_name is not a directory, skipping..."
fi
done
# Symlink /opt/genestack/base-kustomize/kustomize.sh to
# /etc/genestack/kustomize/kustomize.sh
ln -sf $base_source_dir/kustomize.sh $base_target_dir/kustomize.sh
# Ensure kustomization.yaml exists in each
# service base/overlay directory
# Directory paths
overlay_target_dir="/etc/genestack/kustomize"
kustomization_content="apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../base
"
for service in "$overlay_target_dir"/*; do
if [ -d "$service" ] && [ -d "$service/base" ]; then
overlay_path="${service}/overlay"
if [ ! -d "$overlay_path" ]; then
mkdir -p "$overlay_path"
success "Creating overlay path $overlay_path."
fi
if [ ! -f "$overlay_path/kustomization.yaml" ]; then
echo "$kustomization_content" > "$overlay_path/kustomization.yaml"
success "Created overlay and kustomization.yaml for $(basename "$service")."
else
message "kustomization.yaml already exists for $(basename "$service"), skipping..."
fi
else
message "No base directory for $(basename "$service"), skipping..."
fi
done
#!/bin/bash
if [ ! -d "/etc/genestack/helm-configs" ]; then
mkdir -p /etc/genestack/helm-configs
success "Created /etc/genestack/helm-configs."
else
message "/etc/genestack/helm-configs already exists, skipping creation."
fi
for src_dir in /opt/genestack/base-helm-configs/*; do
if [ -d "$src_dir" ]; then
dir_name=$(basename "$src_dir")
dest_dir="/etc/genestack/helm-configs/$dir_name"
if [ ! -d "$dest_dir" ]; then
mkdir -p "$dest_dir"
success "Created $dest_dir."
else
message "$dest_dir already exists, skipping creation."
fi
fi
done
if [ ! -d "/etc/genestack/helm-configs/global_overrides" ]; then
mkdir -p /etc/genestack/helm-configs/global_overrides
echo "Created /etc/genestack/helm-configs/global_overrides."
else
echo "/etc/genestack/helm-configs/global_overrides already exists, skipping creation."
fi
# Copy manifests if it does not already exist
if [ ! -d "/etc/genestack/manifests" ]; then
cp -r /opt/genestack/manifests /etc/genestack/
success "Copied manifests to /etc/genestack/."
else
message "manifests already exists in /etc/genestack, skipping copy."
fi
# Copy yaml editor to /usr/local/bin
cp /opt/genestack/yaml-editor/ye /usr/local/bin/ye
if [ ! -f "/etc/genestack/helm-chart-versions.yaml" ]; then
cp /opt/genestack/helm-chart-versions.yaml /etc/genestack/
success "Copied helm-chart-versions.yaml to /etc/genestack."
else
message "helm-chart-versions.yaml already exists in /etc/genestack, skipping copy."
fi
echo