-
Notifications
You must be signed in to change notification settings - Fork 3
/
mno-iso.sh
executable file
·276 lines (233 loc) · 9.78 KB
/
mno-iso.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
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
273
274
275
276
#!/bin/bash
#
# Helper script to generate bootable ISO with OpenShift agent based installer
# usage: ./mno-iso.sh -h
#
if ! type "yq" > /dev/null; then
echo "Cannot find yq in the path, please install yq on the node first. ref: https://github.com/mikefarah/yq#install"
fi
if ! type "jinja2" > /dev/null; then
echo "Cannot find jinja2 in the path, will install it with pip3 install jinja2-cli and pip3 install jinja2-cli[yaml]"
pip3 install --user jinja2-cli
pip3 install --user jinja2-cli[yaml]
fi
info(){
printf $(tput setaf 2)"%-54s %-10s"$(tput sgr0)"\n" "$@"
}
warn(){
printf $(tput setaf 3)"%-54s %-10s"$(tput sgr0)"\n" "$@"
}
usage(){
info "Usage: $0 [config file] [ocp version]"
info "config file and ocp version are optional, examples:"
info "- $0 mno130.yaml" " equals: $0 mno130.yaml stable-4.12"
info "- $0 mno130.yaml 4.12.10"
echo
info "Prepare a configuration file by following the example in config.yaml.sample"
echo "-----------------------------------"
echo "# content of config.yaml.sample"
cat config.yaml.sample
echo
echo "-----------------------------------"
echo
info "Example to run it: $0 config-mno130.yaml"
echo
}
if [ $# -lt 1 ]
then
usage
exit
fi
if [[ ( $@ == "--help") || $@ == "-h" ]]
then
usage
exit
fi
basedir="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
templates=$basedir/templates
operators=$basedir/operators
config_file_input=$1; shift
ocp_release=$1; shift
if [ -z "$config_file_input" ]
then
config_file=config.yaml
fi
if [ -z "$ocp_release" ]
then
ocp_release='stable-4.12'
fi
ocp_release_version=$(curl -s https://mirror.openshift.com/pub/openshift-v4/x86_64/clients/ocp/${ocp_release}/release.txt | grep 'Version:' | awk -F ' ' '{print $2}')
#if release not available on mirror.openshift.com/pub/openshift-v4/x86_64/clients/ocp/, probably ec (early candidate) version.
if [ -z $ocp_release_version ]; then
ocp_release_version=$ocp_release
fi
export ocp_y_release=$(echo $ocp_release_version |cut -d. -f1-2)
export OCP_Y_VERSION=$ocp_y_release
export OCP_Z_VERSION=$ocp_release_version
cluster_name=$(yq '.cluster.name' $config_file_input)
cluster_workspace=$basedir/instances/$cluster_name
if [[ -d "${cluster_workspace}" ]]; then
echo "${cluster_workspace} already exists, please delete the folder ${cluster_workspace} and re-run the script."
exit -1
fi
mkdir -p $cluster_workspace
mkdir -p $cluster_workspace/openshift
config_file="$cluster_workspace/config-resolved.yaml"
if [ $(cat $config_file_input |grep -E 'OCP_Y_RELEASE|OCP_Z_RELEASE' |wc -l) -gt 0 ]; then
sed "s/OCP_Y_RELEASE/$ocp_y_release/g;s/OCP_Z_RELEASE/$ocp_release_version/g" $config_file_input > $config_file
else
cp $config_file_input $config_file
fi
echo "Will use $config_file as the configuration in other sno-* scripts."
if [ ! -f $basedir/openshift-install-linux.$ocp_release_version.tar.gz ]; then
echo "You are going to download OpenShift installer $ocp_release: ${ocp_release_version}"
echo
status_code=$(curl -s -o /dev/null -w "%{http_code}" https://mirror.openshift.com/pub/openshift-v4/x86_64/clients/ocp/$ocp_release_version/)
if [ $status_code = "200" ]; then
curl -L https://mirror.openshift.com/pub/openshift-v4/x86_64/clients/ocp/${ocp_release_version}/openshift-install-linux.tar.gz -o $basedir/openshift-install-linux.$ocp_release_version.tar.gz
if [[ $? -eq 0 ]]; then
tar zxf $basedir/openshift-install-linux.$ocp_release_version.tar.gz -C $basedir openshift-install
else
rm -f $basedir/openshift-install-linux.$ocp_release_version.tar.gz
exit -1
fi
else
#fetch from image
if [[ $ocp_release == *"nightly"* ]] || [[ $ocp_release == *"ci"* ]]; then
oc adm release extract --command=openshift-install registry.ci.openshift.org/ocp/release:$ocp_release_version --registry-config=$(yq '.pull_secret' $config_file) --to="$basedir"
else
oc adm release extract --command=openshift-install quay.io/openshift-release-dev/ocp-release:$ocp_release_version-x86_64 --registry-config=$(yq '.pull_secret' $config_file) --to="$basedir"
fi
fi
else
tar zxf $basedir/openshift-install-linux.$ocp_release_version.tar.gz -C $basedir openshift-install
fi
enable_crun(){
if [ "4.12" = $ocp_y_release ]; then
warn "Container runtime crun(4.13+):" "disabled"
else
#4.13+ by default enabled.
if [ "false" = "$(yq '.day1.crun' $config_file)" ]; then
warn "Container runtime crun(4.13+):" "disabled"
else
info "Container runtime crun(4.13+):" "enabled"
cp $templates/day1/crun/*.yaml $cluster_workspace/openshift/
fi
fi
}
install_operators(){
if [[ $(yq '.day1.operators' $config_file) != "null" ]]; then
readarray -t keys < <(yq ".day1.operators|keys" $config_file|yq '.[]')
for ((k=0; k<${#keys[@]}; k++)); do
key="${keys[$k]}"
desc=$(yq ".operators.$key.desc" $operators/operators.yaml)
if [[ "true" == $(yq ".day1.operators.$key.enabled" $config_file) ]]; then
info "$desc" "enabled"
cp $operators/$key/*.yaml $cluster_workspace/openshift/ 2>/dev/null
#render j2 files
j2files=$(ls $operators/$key/*.j2 2>/dev/null)
for f in $j2files; do
tname=$(basename $f)
fname=${tname//.j2/}
yq ".day1.operators.$key" $config_file| jinja2 $f > $cluster_workspace/openshift/$fname
done
else
warn "$desc" "disabled"
fi
done
fi
}
apply_extra_manifests(){
if [ -d $basedir/extra-manifests ]; then
echo "Copy customized CRs from extra-manifests folder if present"
find $basedir/extra-manifests/day1/ -type f \( -name "*.yaml" -o -name "*.yaml.j2" \) -printf ' - %P\n'
cp $basedir/extra-manifests/day1/*.yaml $cluster_workspace/openshift/ 2>/dev/null
#render j2 files
j2files=$(ls $basedir/extra-manifests/day1/*.j2 2>/dev/null)
for f in $j2files; do
tname=$(basename $f)
fname=${tname//.j2/}
jinja2 $f > $cluster_workspace/openshift/$fname
done
fi
}
operator_catalog_sources(){
if [ "4.12" = $ocp_y_release ] || [ "4.13" = $ocp_y_release ] || [ "4.14" = $ocp_y_release ] || [ "4.15" = $ocp_y_release ]; then
if [[ $(yq '.container_registry' $config_file) != "null" ]]; then
jinja2 $templates/day1/operatorhub.yaml.j2 $config_file > $cluster_workspace/openshift/operatorhub.yaml
fi
else
#4.16+, disable marketplace operator
cp $templates/day1/marketplace/09-openshift-marketplace-ns.yaml $cluster_workspace/openshift/
#create unmanaged catalog sources
if [[ "$(yq '.container_registry.catalog_sources.defaults' $config_file)" != "null" ]]; then
#enable the ones in container_registry.catalog_sources.defaults
local size=$(yq '.container_registry.catalog_sources.defaults|length' $config_file)
for ((k=0; k<$size; k++)); do
local name=$(yq ".container_registry.catalog_sources.defaults[$k]" $config_file)
jinja2 $templates/day1/catalogsource/$name.yaml.j2 > $cluster_workspace/openshift/$name.yaml
done
else
#by default redhat-operators and certified-operators shall be enabled
jinja2 $templates/day1/catalogsource/redhat-operators.yaml.j2 > $cluster_workspace/openshift/redhat-operators.yaml
jinja2 $templates/day1/catalogsource/certified-operators.yaml.j2 > $cluster_workspace/openshift/certified-operators.yaml
fi
fi
#all versions
if [ "$(yq '.container_registry.catalog_sources.customs' $config_file)" != "null" ]; then
local size=$(yq '.container_registry.catalog_sources.customs|length' $config_file)
for ((k=0; k<$size; k++)); do
yq ".container_registry.catalog_sources.customs[$k]" $config_file |jinja2 $templates/day1/catalogsource/catalogsource.yaml.j2 > $cluster_workspace/openshift/catalogsource-$k.yaml
done
fi
#all versions
if [ "$(yq '.container_registry.icsp' $config_file)" != "null" ]; then
local size=$(yq '.container_registry.icsp|length' $config_file)
for ((k=0; k<$size; k++)); do
local name=$(yq ".container_registry.icsp[$k]" $config_file)
if [ -f "$name" ]; then
info "$name" "copy to $cluster_workspace/openshift/"
cp $name $cluster_workspace/openshift/
else
warn "$name" "not a file or not exist"
fi
done
fi
}
operator_catalog_sources
enable_crun
install_operators
apply_extra_manifests
pull_secret=$(yq '.pull_secret' $config_file)
export pull_secret=$(cat $pull_secret)
ssh_key=$(yq '.ssh_key' $config_file)
if [[ -z "$ssh_key" ]] || [[ ! -f "$ssh_key" ]]; then
warn "ssh-key" "ssh_key not set or file missing"
exit -1
fi
export ssh_key=$(cat $ssh_key)
bundle_file=$(yq '.additional_trust_bundle' $config_file)
if [[ "null" != "$bundle_file" ]]; then
export additional_trust_bundle=$(cat $bundle_file)
fi
jinja2 $templates/agent-config.yaml.j2 $config_file > $cluster_workspace/agent-config.yaml
jinja2 $templates/install-config.yaml.j2 $config_file > $cluster_workspace/install-config.yaml
mirror_source=$(yq '.container_registry.image_source' $config_file)
if [[ "null" != "$mirror_source" ]]; then
cat $mirror_source >> $cluster_workspace/install-config.yaml
fi
cp $cluster_workspace/agent-config.yaml $cluster_workspace/agent-config.backup.yaml
cp $cluster_workspace/install-config.yaml $cluster_workspace/install-config.backup.yaml
echo
echo "Generating boot image..."
echo
$basedir/openshift-install --dir $cluster_workspace agent --log-level info create image
echo ""
echo "------------------------------------------------"
echo "kubeconfig: $cluster_workspace/auth/kubeconfig."
echo "kubeadmin password: $cluster_workspace/auth/kubeadmin-password."
echo "------------------------------------------------"
echo
echo "Next step: Go to your BMC console and boot the node from ISO: $cluster_workspace/agent.x86_64.iso."
echo "You can also run ./mno-install.sh to boot the node from the image automatically if you have a HTTP server serves the image."
echo "Enjoy!"