Pin MicroK8s version
[osm/devops.git] / installers / charmed_install.sh
1 #! /bin/bash
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15
16 # set -eux
17
18 JUJU_AGENT_VERSION=2.8.6
19 K8S_CLOUD_NAME="k8s-cloud"
20 KUBECTL="microk8s.kubectl"
21 MICROK8S_VERSION=1.19
22 IMAGES_OVERLAY_FILE=~/.osm/images-overlay.yaml
23 PATH=/snap/bin:${PATH}
24
25 function check_arguments(){
26 while [ $# -gt 0 ] ; do
27 case $1 in
28 --bundle) BUNDLE="$2" ;;
29 --k8s) KUBECFG="$2" ;;
30 --vca) CONTROLLER="$2" ;;
31 --lxd) LXD_CLOUD="$2" ;;
32 --lxd-cred) LXD_CREDENTIALS="$2" ;;
33 --microstack) MICROSTACK=y ;;
34 --ha) BUNDLE="cs:osm-ha" ;;
35 --tag) TAG="$2" ;;
36 --registry) REGISTRY_INFO="$2" ;;
37 esac
38 shift
39 done
40
41 # echo $BUNDLE $KUBECONFIG $LXDENDPOINT
42 }
43
44 function install_snaps(){
45 if [ ! -v KUBECFG ]; then
46 sudo snap install microk8s --classic --channel=${MICROK8S_VERSION}/stable
47 sudo usermod -a -G microk8s `whoami`
48 mkdir -p ~/.kube
49 sudo chown -f -R `whoami` ~/.kube
50 KUBEGRP="microk8s"
51 sg ${KUBEGRP} -c "microk8s status --wait-ready"
52 else
53 KUBECTL="kubectl"
54 sudo snap install kubectl --classic
55 export KUBECONFIG=${KUBECFG}
56 KUBEGRP=$(id -g -n)
57 fi
58 sudo snap install juju --classic --channel=2.8/stable
59 }
60
61 function bootstrap_k8s_lxd(){
62 [ -v CONTROLLER ] && ADD_K8S_OPTS="--controller ${CONTROLLER}" && CONTROLLER_NAME=$CONTROLLER
63 [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="osm-vca"
64
65 if [ -v BOOTSTRAP_NEEDED ]; then
66 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
67 if [ $CONTROLLER_PRESENT -ge 1 ]; then
68 cat << EOF
69 Threre is already a VCA present with the installer reserved name of "${CONTROLLER_NAME}".
70 You may either explicitly use this VCA with the "--vca ${CONTROLLER_NAME}" option, or remove it
71 using this command:
72
73 juju destroy-controller --release-storage --destroy-all-models -y ${CONTROLLER_NAME}
74
75 Please retry the installation once this conflict has been resolved.
76 EOF
77 exit 1
78 fi
79 else
80 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
81 if [ $CONTROLLER_PRESENT -le 0 ]; then
82 cat << EOF
83 Threre is no VCA present with the name "${CONTROLLER_NAME}". Please specify a VCA
84 that exists, or remove the --vca ${CONTROLLER_NAME} option.
85
86 Please retry the installation with one of the solutions applied.
87 EOF
88 exit 1
89 fi
90 fi
91
92 if [ -v KUBECFG ]; then
93 cat $KUBECFG | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
94 [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME \
95 --config controller-service-type=loadbalancer \
96 --agent-version=$JUJU_AGENT_VERSION
97 else
98 sg ${KUBEGRP} -c "echo ${DEFAULT_IP}-${DEFAULT_IP} | microk8s.enable metallb"
99 sg ${KUBEGRP} -c "microk8s.enable ingress"
100 sg ${KUBEGRP} -c "microk8s.enable storage dns"
101 TIME_TO_WAIT=30
102 start_time="$(date -u +%s)"
103 while true
104 do
105 now="$(date -u +%s)"
106 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
107 echo "Microk8s storage failed to enable"
108 sg ${KUBEGRP} -c "microk8s.status"
109 exit 1
110 fi
111 storage_status=`sg ${KUBEGRP} -c "microk8s.status -a storage"`
112 if [[ $storage_status == "enabled" ]]; then
113 break
114 fi
115 sleep 1
116 done
117
118 [ ! -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
119 [ -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c \
120 "juju bootstrap microk8s $CONTROLLER_NAME --config controller-service-type=loadbalancer --agent-version=$JUJU_AGENT_VERSION" \
121 && K8S_CLOUD_NAME=microk8s
122 fi
123
124 if [ -v LXD_CLOUD ]; then
125 if [ ! -v LXD_CREDENTIALS ]; then
126 echo "The installer needs the LXD server certificate if the LXD is external"
127 exit 1
128 fi
129 else
130 LXDENDPOINT=$DEFAULT_IP
131 LXD_CLOUD=~/.osm/lxd-cloud.yaml
132 LXD_CREDENTIALS=~/.osm/lxd-credentials.yaml
133 # Apply sysctl production values for optimal performance
134 sudo cp /usr/share/osm-devops/installers/60-lxd-production.conf /etc/sysctl.d/60-lxd-production.conf
135 sudo sysctl --system
136 # Install LXD snap
137 sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client
138 sudo snap install lxd
139 # Configure LXD
140 sudo usermod -a -G lxd `whoami`
141 cat /usr/share/osm-devops/installers/lxd-preseed.conf | sed 's/^config: {}/config:\n core.https_address: '$LXDENDPOINT':8443/' | sg lxd -c "lxd init --preseed"
142 sg lxd -c "lxd waitready"
143 DEFAULT_MTU=$(ip addr show $DEFAULT_IF | perl -ne 'if (/mtu\s(\d+)/) {print $1;}')
144 sg lxd -c "lxc profile device set default eth0 mtu $DEFAULT_MTU"
145 sg lxd -c "lxc network set lxdbr0 bridge.mtu $DEFAULT_MTU"
146
147 cat << EOF > $LXD_CLOUD
148 clouds:
149 lxd-cloud:
150 type: lxd
151 auth-types: [certificate]
152 endpoint: "https://$LXDENDPOINT:8443"
153 config:
154 ssl-hostname-verification: false
155 EOF
156 openssl req -nodes -new -x509 -keyout ~/.osm/client.key -out ~/.osm/client.crt -days 365 -subj "/C=FR/ST=Nice/L=Nice/O=ETSI/OU=OSM/CN=osm.etsi.org"
157 local server_cert=`cat /var/snap/lxd/common/lxd/server.crt | sed 's/^/ /'`
158 local client_cert=`cat ~/.osm/client.crt | sed 's/^/ /'`
159 local client_key=`cat ~/.osm/client.key | sed 's/^/ /'`
160
161 cat << EOF > $LXD_CREDENTIALS
162 credentials:
163 lxd-cloud:
164 lxd-cloud:
165 auth-type: certificate
166 server-cert: |
167 $server_cert
168 client-cert: |
169 $client_cert
170 client-key: |
171 $client_key
172 EOF
173 lxc config trust add local: ~/.osm/client.crt
174 fi
175
176 juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force
177 juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS
178 sg lxd -c "lxd waitready"
179 juju controller-config features=[k8s-operators]
180 }
181
182 function wait_for_port(){
183 SERVICE=$1
184 INDEX=$2
185 TIME_TO_WAIT=30
186 start_time="$(date -u +%s)"
187 while true
188 do
189 now="$(date -u +%s)"
190 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
191 echo "Failed to expose external ${SERVICE} interface port"
192 exit 1
193 fi
194
195 if [ $(sg ${KUBEGRP} -c "${KUBECTL} get ingresses.networking -n osm -o json | jq -r '.items[$INDEX].metadata.name'") == ${SERVICE} ] ; then
196 break
197 fi
198 sleep 1
199 done
200 }
201
202 function deploy_charmed_osm(){
203 if [ -v REGISTRY_INFO ] ; then
204 registry_parts=(${REGISTRY_INFO//@/ })
205 if [ ${#registry_parts[@]} -eq 1 ] ; then
206 # No credentials supplied
207 REGISTRY_USERNAME=""
208 REGISTRY_PASSWORD=""
209 REGISTRY_URL=${registry_parts[0]}
210 else
211 credentials=${registry_parts[0]}
212 credential_parts=(${credentials//:/ })
213 REGISTRY_USERNAME=${credential_parts[0]}
214 REGISTRY_PASSWORD=${credential_parts[1]}
215 REGISTRY_URL=${registry_parts[1]}
216 fi
217 # Ensure the URL ends with a /
218 case $REGISTRY_URL in
219 */) ;;
220 *) REGISTRY_URL=${REGISTRY_URL}/
221 esac
222 fi
223
224 create_overlay
225 echo "Creating OSM model"
226 if [ -v KUBECFG ]; then
227 juju add-model osm $K8S_CLOUD_NAME
228 else
229 sg ${KUBEGRP} -c "juju add-model osm $K8S_CLOUD_NAME"
230 fi
231 echo "Deploying OSM with charms"
232 images_overlay=""
233 if [ -v REGISTRY_URL ]; then
234 [ ! -v TAG ] && TAG='latest'
235 fi
236 [ -v TAG ] && generate_images_overlay && images_overlay="--overlay $IMAGES_OVERLAY_FILE"
237
238 if [ -v BUNDLE ]; then
239 juju deploy $BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay
240 else
241 juju deploy cs:osm --channel edge --overlay ~/.osm/vca-overlay.yaml $images_overlay
242 fi
243
244 echo "Waiting for deployment to finish..."
245 check_osm_deployed
246 echo "OSM with charms deployed"
247 if [ ! -v KUBECFG ]; then
248 API_SERVER=${DEFAULT_IP}
249 else
250 API_SERVER=$(kubectl config view --minify | grep server | cut -f 2- -d ":" | tr -d " ")
251 proto="$(echo $API_SERVER | grep :// | sed -e's,^\(.*://\).*,\1,g')"
252 url="$(echo ${API_SERVER/$proto/})"
253 user="$(echo $url | grep @ | cut -d@ -f1)"
254 hostport="$(echo ${url/$user@/} | cut -d/ -f1)"
255 API_SERVER="$(echo $hostport | sed -e 's,:.*,,g')"
256 fi
257
258 # Expose OSM services
259 # Expose Grafana
260 juju config grafana-k8s juju-external-hostname=grafana.${API_SERVER}.xip.io
261 juju expose grafana-k8s
262 wait_for_port grafana-k8s 0
263
264 # Expose NBI
265 juju config nbi-k8s juju-external-hostname=nbi.${API_SERVER}.xip.io
266 juju expose nbi-k8s
267 wait_for_port nbi-k8s 1
268
269 # Expose NG UI
270 juju config ng-ui juju-external-hostname=ui.${API_SERVER}.xip.io
271 juju expose ng-ui
272 wait_for_port ng-ui 2
273
274 # Expose Prometheus
275 juju config prometheus-k8s juju-external-hostname=prometheus.${API_SERVER}.xip.io
276 juju expose prometheus-k8s
277 wait_for_port prometheus-k8s 3
278
279 # Apply annotations
280 sg ${KUBEGRP} -c "${KUBECTL} annotate ingresses.networking nginx.ingress.kubernetes.io/backend-protocol=HTTPS -n osm -l juju-app=nbi-k8s"
281 sg ${KUBEGRP} -c "${KUBECTL} annotate ingresses.networking nginx.ingress.kubernetes.io/proxy-body-size=0 -n osm -l juju-app=nbi-k8s"
282 sg ${KUBEGRP} -c "${KUBECTL} annotate ingresses.networking nginx.ingress.kubernetes.io/proxy-body-size=0 -n osm -l juju-app=ng-ui"
283 }
284
285 function check_osm_deployed() {
286 TIME_TO_WAIT=600
287 start_time="$(date -u +%s)"
288 total_service_count=14
289 previous_count=0
290 while true
291 do
292 service_count=$(juju status | grep kubernetes | grep active | wc -l)
293 echo "$service_count / $total_service_count services active"
294 if [ $service_count -eq $total_service_count ]; then
295 break
296 fi
297 if [ $service_count -ne $previous_count ]; then
298 previous_count=$service_count
299 start_time="$(date -u +%s)"
300 fi
301 now="$(date -u +%s)"
302 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
303 echo "Timed out waiting for OSM services to become ready"
304 exit 1
305 fi
306 sleep 10
307 done
308 }
309
310 function create_overlay() {
311 sudo snap install jq
312 sudo apt install python3-pip -y
313 python3 -m pip install yq
314 PATH=$PATH:$HOME/.local/bin # make yq command available
315 local HOME=/home/$USER
316 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME].user')
317 local vca_password=$(cat $HOME/.local/share/juju/accounts.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME].password')
318 local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME]["api-endpoints"][0]' --raw-output | cut -d ":" -f 1)
319 local vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME]["api-endpoints"][0]' --raw-output | cut -d ":" -f 2)
320 local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
321 local vca_cloud="lxd-cloud"
322 # Get the VCA Certificate
323 local vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME]["ca-cert"]' --raw-output | base64 | tr -d \\n)
324
325 # Calculate the default route of this machine
326 local DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
327
328 # Generate a new overlay.yaml, overriding any existing one
329 cat << EOF > /tmp/vca-overlay.yaml
330 applications:
331 lcm-k8s:
332 options:
333 vca_user: $vca_user
334 vca_password: $vca_password
335 vca_host: $vca_host
336 vca_port: $vca_port
337 vca_pubkey: $vca_pubkey
338 vca_cacert: $vca_cacert
339 vca_cloud: $vca_cloud
340 vca_k8s_cloud: $K8S_CLOUD_NAME
341 mon-k8s:
342 options:
343 vca_user: $vca_user
344 vca_password: $vca_password
345 vca_host: $vca_host
346 vca_cacert: $vca_cacert
347 EOF
348 mv /tmp/vca-overlay.yaml ~/.osm/
349 OSM_VCA_HOST=$vca_host
350 }
351
352 function generate_images_overlay(){
353 if [ ! -z "$REGISTRY_USERNAME" ] ; then
354 REGISTRY_CREDENTIALS=$(cat <<EOF
355
356 image_username: $REGISTRY_USERNAME
357 image_password: $REGISTRY_PASSWORD
358 EOF
359 );
360 fi
361
362 cat << EOF > /tmp/images-overlay.yaml
363 applications:
364 lcm-k8s:
365 options:
366 image: ${REGISTRY_URL}opensourcemano/lcm:$TAG ${REGISTRY_CREDENTIALS}
367 mon-k8s:
368 options:
369 image: ${REGISTRY_URL}opensourcemano/mon:$TAG ${REGISTRY_CREDENTIALS}
370 ro-k8s:
371 options:
372 image: ${REGISTRY_URL}opensourcemano/ro:$TAG ${REGISTRY_CREDENTIALS}
373 nbi-k8s:
374 options:
375 image: ${REGISTRY_URL}opensourcemano/nbi:$TAG ${REGISTRY_CREDENTIALS}
376 pol-k8s:
377 options:
378 image: ${REGISTRY_URL}opensourcemano/pol:$TAG ${REGISTRY_CREDENTIALS}
379 pla:
380 options:
381 image: ${REGISTRY_URL}opensourcemano/pla:$TAG ${REGISTRY_CREDENTIALS}
382 ng-ui:
383 options:
384 image: ${REGISTRY_URL}opensourcemano/ng-ui:$TAG ${REGISTRY_CREDENTIALS}
385 keystone:
386 options:
387 image: ${REGISTRY_URL}opensourcemano/keystone:$TAG ${REGISTRY_CREDENTIALS}
388 EOF
389 mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE
390 }
391
392 function install_osmclient() {
393 sudo snap install osmclient
394 sudo snap alias osmclient.osm osm
395 }
396
397
398 function install_microstack() {
399 sudo snap install microstack --classic --beta
400 sudo microstack.init --auto
401 wget https://cloud-images.ubuntu.com/releases/16.04/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img -P ~/.osm/
402 microstack.openstack image create \
403 --public \
404 --disk-format qcow2 \
405 --container-format bare \
406 --file ~/.osm/ubuntu-16.04-server-cloudimg-amd64-disk1.img \
407 ubuntu1604
408 ssh-keygen -t rsa -N "" -f ~/.ssh/microstack
409 microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack
410 export OSM_HOSTNAME=`juju status --format json | jq -rc '.applications."nbi-k8s".address'`
411 osm vim-create --name microstack-site \
412 --user admin \
413 --password keystone \
414 --auth_url http://10.20.20.1:5000/v3 \
415 --tenant admin \
416 --account_type openstack \
417 --config='{security_groups: default,
418 keypair: microstack,
419 project_name: admin,
420 user_domain_name: default,
421 region_name: microstack,
422 insecure: True,
423 availability_zone: nova,
424 version: 3}'
425 }
426
427 DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
428 DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
429
430 check_arguments $@
431 mkdir -p ~/.osm
432 install_snaps
433 bootstrap_k8s_lxd
434 deploy_charmed_osm
435 install_osmclient
436 if [ -v MICROSTACK ]; then
437 install_microstack
438 fi
439
440 OSM_HOSTNAME=$(juju config nbi-k8s juju-external-hostname):443
441
442 echo "Your installation is now complete, follow these steps for configuring the osmclient:"
443 echo
444 echo "1. Create the OSM_HOSTNAME environment variable with the NBI IP"
445 echo
446 echo "export OSM_HOSTNAME=$OSM_HOSTNAME"
447 echo
448 echo "2. Add the previous command to your .bashrc for other Shell sessions"
449 echo
450 echo "echo \"export OSM_HOSTNAME=$OSM_HOSTNAME\" >> ~/.bashrc"
451 echo
452 echo "DONE"