Fix bug 1811 - Disabling RO OpenNebula plugin
[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 LXD_VERSION=4.0
19 JUJU_VERSION=2.8
20 JUJU_AGENT_VERSION=2.8.11
21 K8S_CLOUD_NAME="k8s-cloud"
22 KUBECTL="microk8s.kubectl"
23 MICROK8S_VERSION=1.19
24 OSMCLIENT_VERSION=9.0
25 IMAGES_OVERLAY_FILE=~/.osm/images-overlay.yaml
26 PATH=/snap/bin:${PATH}
27
28 MODEL_NAME=osm
29
30 OSM_BUNDLE=cs:osm-54
31 OSM_HA_BUNDLE=cs:osm-ha-40
32 TAG=9
33
34 function check_arguments(){
35 while [ $# -gt 0 ] ; do
36 case $1 in
37 --bundle) BUNDLE="$2" ;;
38 --k8s) KUBECFG="$2" ;;
39 --vca) CONTROLLER="$2" ;;
40 --lxd) LXD_CLOUD="$2" ;;
41 --lxd-cred) LXD_CREDENTIALS="$2" ;;
42 --microstack) MICROSTACK=y ;;
43 --ha) BUNDLE=$OSM_HA_BUNDLE ;;
44 --tag) TAG="$2" ;;
45 --registry) REGISTRY_INFO="$2" ;;
46 esac
47 shift
48 done
49
50 # echo $BUNDLE $KUBECONFIG $LXDENDPOINT
51 }
52
53 function install_snaps(){
54 if [ ! -v KUBECFG ]; then
55 sudo snap install microk8s --classic --channel=${MICROK8S_VERSION}/stable
56 sudo cat /var/snap/microk8s/current/args/kube-apiserver | grep advertise-address || (
57 echo "--advertise-address $DEFAULT_IP" | sudo tee -a /var/snap/microk8s/current/args/kube-apiserver
58 microk8s.stop
59 microk8s.start
60 )
61 sudo usermod -a -G microk8s `whoami`
62 mkdir -p ~/.kube
63 sudo chown -f -R `whoami` ~/.kube
64 KUBEGRP="microk8s"
65 sg ${KUBEGRP} -c "microk8s status --wait-ready"
66 KUBECONFIG=~/.osm/microk8s-config.yaml
67 sg ${KUBEGRP} -c "microk8s config" | tee ${KUBECONFIG}
68 else
69 KUBECTL="kubectl"
70 sudo snap install kubectl --classic
71 export KUBECONFIG=${KUBECFG}
72 KUBEGRP=$(id -g -n)
73 fi
74 sudo snap install juju --classic --channel=$JUJU_VERSION/stable
75 }
76
77 function bootstrap_k8s_lxd(){
78 [ -v CONTROLLER ] && ADD_K8S_OPTS="--controller ${CONTROLLER}" && CONTROLLER_NAME=$CONTROLLER
79 [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="osm-vca"
80
81 if [ -v BOOTSTRAP_NEEDED ]; then
82 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
83 if [ $CONTROLLER_PRESENT -ge 1 ]; then
84 cat << EOF
85 Threre is already a VCA present with the installer reserved name of "${CONTROLLER_NAME}".
86 You may either explicitly use this VCA with the "--vca ${CONTROLLER_NAME}" option, or remove it
87 using this command:
88
89 juju destroy-controller --release-storage --destroy-all-models -y ${CONTROLLER_NAME}
90
91 Please retry the installation once this conflict has been resolved.
92 EOF
93 exit 1
94 fi
95 else
96 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
97 if [ $CONTROLLER_PRESENT -le 0 ]; then
98 cat << EOF
99 Threre is no VCA present with the name "${CONTROLLER_NAME}". Please specify a VCA
100 that exists, or remove the --vca ${CONTROLLER_NAME} option.
101
102 Please retry the installation with one of the solutions applied.
103 EOF
104 exit 1
105 fi
106 fi
107
108 if [ -v KUBECFG ]; then
109 cat $KUBECFG | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
110 [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME \
111 --config controller-service-type=loadbalancer \
112 --agent-version=$JUJU_AGENT_VERSION
113 else
114 sg ${KUBEGRP} -c "echo ${DEFAULT_IP}-${DEFAULT_IP} | microk8s.enable metallb"
115 sg ${KUBEGRP} -c "microk8s.enable ingress"
116 sg ${KUBEGRP} -c "microk8s.enable storage dns"
117 TIME_TO_WAIT=30
118 start_time="$(date -u +%s)"
119 while true
120 do
121 now="$(date -u +%s)"
122 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
123 echo "Microk8s storage failed to enable"
124 sg ${KUBEGRP} -c "microk8s.status"
125 exit 1
126 fi
127 storage_status=`sg ${KUBEGRP} -c "microk8s.status -a storage"`
128 if [[ $storage_status == "enabled" ]]; then
129 break
130 fi
131 sleep 1
132 done
133
134 [ ! -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
135 [ -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c \
136 "juju bootstrap microk8s $CONTROLLER_NAME --config controller-service-type=loadbalancer --agent-version=$JUJU_AGENT_VERSION" \
137 && K8S_CLOUD_NAME=microk8s
138 fi
139
140 if [ -v LXD_CLOUD ]; then
141 if [ ! -v LXD_CREDENTIALS ]; then
142 echo "The installer needs the LXD server certificate if the LXD is external"
143 exit 1
144 fi
145 else
146 LXDENDPOINT=$DEFAULT_IP
147 LXD_CLOUD=~/.osm/lxd-cloud.yaml
148 LXD_CREDENTIALS=~/.osm/lxd-credentials.yaml
149 # Apply sysctl production values for optimal performance
150 sudo cp /usr/share/osm-devops/installers/60-lxd-production.conf /etc/sysctl.d/60-lxd-production.conf
151 sudo sysctl --system
152 # Install LXD snap
153 sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client
154 sudo snap install lxd --channel $LXD_VERSION/stable
155 # Configure LXD
156 sudo usermod -a -G lxd `whoami`
157 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"
158 sg lxd -c "lxd waitready"
159 DEFAULT_MTU=$(ip addr show $DEFAULT_IF | perl -ne 'if (/mtu\s(\d+)/) {print $1;}')
160 sg lxd -c "lxc profile device set default eth0 mtu $DEFAULT_MTU"
161 sg lxd -c "lxc network set lxdbr0 bridge.mtu $DEFAULT_MTU"
162
163 cat << EOF > $LXD_CLOUD
164 clouds:
165 lxd-cloud:
166 type: lxd
167 auth-types: [certificate]
168 endpoint: "https://$LXDENDPOINT:8443"
169 config:
170 ssl-hostname-verification: false
171 EOF
172 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"
173 local server_cert=`cat /var/snap/lxd/common/lxd/server.crt | sed 's/^/ /'`
174 local client_cert=`cat ~/.osm/client.crt | sed 's/^/ /'`
175 local client_key=`cat ~/.osm/client.key | sed 's/^/ /'`
176
177 cat << EOF > $LXD_CREDENTIALS
178 credentials:
179 lxd-cloud:
180 lxd-cloud:
181 auth-type: certificate
182 server-cert: |
183 $server_cert
184 client-cert: |
185 $client_cert
186 client-key: |
187 $client_key
188 EOF
189 lxc config trust add local: ~/.osm/client.crt
190 fi
191
192 juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force
193 juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS
194 sg lxd -c "lxd waitready"
195 juju controller-config features=[k8s-operators]
196 }
197
198 function wait_for_port(){
199 SERVICE=$1
200 INDEX=$2
201 TIME_TO_WAIT=30
202 start_time="$(date -u +%s)"
203 while true
204 do
205 now="$(date -u +%s)"
206 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
207 echo "Failed to expose external ${SERVICE} interface port"
208 exit 1
209 fi
210
211 if [ $(sg ${KUBEGRP} -c "${KUBECTL} get ingresses.networking -n osm -o json | jq -r '.items[$INDEX].metadata.name'") == ${SERVICE} ] ; then
212 break
213 fi
214 sleep 1
215 done
216 }
217
218 function deploy_charmed_osm(){
219 if [ -v REGISTRY_INFO ] ; then
220 registry_parts=(${REGISTRY_INFO//@/ })
221 if [ ${#registry_parts[@]} -eq 1 ] ; then
222 # No credentials supplied
223 REGISTRY_USERNAME=""
224 REGISTRY_PASSWORD=""
225 REGISTRY_URL=${registry_parts[0]}
226 else
227 credentials=${registry_parts[0]}
228 credential_parts=(${credentials//:/ })
229 REGISTRY_USERNAME=${credential_parts[0]}
230 REGISTRY_PASSWORD=${credential_parts[1]}
231 REGISTRY_URL=${registry_parts[1]}
232 fi
233 # Ensure the URL ends with a /
234 case $REGISTRY_URL in
235 */) ;;
236 *) REGISTRY_URL=${REGISTRY_URL}/
237 esac
238 fi
239
240 create_overlay
241 echo "Creating OSM model"
242 if [ -v KUBECFG ]; then
243 juju add-model $MODEL_NAME $K8S_CLOUD_NAME
244 else
245 sg ${KUBEGRP} -c "juju add-model $MODEL_NAME $K8S_CLOUD_NAME"
246 fi
247 echo "Deploying OSM with charms"
248 images_overlay=""
249 if [ -v REGISTRY_URL ]; then
250 [ ! -v TAG ] && TAG='latest'
251 fi
252 [ -v TAG ] && generate_images_overlay && images_overlay="--overlay $IMAGES_OVERLAY_FILE"
253
254 if [ -v BUNDLE ]; then
255 juju deploy -m $MODEL_NAME $BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay
256 else
257 juju deploy -m $MODEL_NAME $OSM_BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay
258 fi
259
260 echo "Waiting for deployment to finish..."
261 check_osm_deployed
262 echo "OSM with charms deployed"
263 if [ ! -v KUBECFG ]; then
264 API_SERVER=${DEFAULT_IP}
265 else
266 API_SERVER=$(kubectl config view --minify | grep server | cut -f 2- -d ":" | tr -d " ")
267 proto="$(echo $API_SERVER | grep :// | sed -e's,^\(.*://\).*,\1,g')"
268 url="$(echo ${API_SERVER/$proto/})"
269 user="$(echo $url | grep @ | cut -d@ -f1)"
270 hostport="$(echo ${url/$user@/} | cut -d/ -f1)"
271 API_SERVER="$(echo $hostport | sed -e 's,:.*,,g')"
272 fi
273
274 # Expose OSM services
275 # Expose NBI
276 juju config -m $MODEL_NAME nbi site_url=https://nbi.${API_SERVER}.nip.io
277 juju config -m $MODEL_NAME ng-ui site_url=https://ui.${API_SERVER}.nip.io
278
279 # Expose Grafana
280 juju config -m $MODEL_NAME grafana-k8s juju-external-hostname=grafana.${API_SERVER}.nip.io
281 juju expose -m $MODEL_NAME grafana-k8s
282 wait_for_port grafana-k8s 0
283
284 # Expose Prometheus
285 juju config -m $MODEL_NAME prometheus-k8s juju-external-hostname=prometheus.${API_SERVER}.nip.io
286 juju expose -m $MODEL_NAME prometheus-k8s
287 wait_for_port prometheus-k8s 1
288
289 # Apply annotations
290 sg ${KUBEGRP} -c "${KUBECTL} annotate ingresses.networking nginx.ingress.kubernetes.io/proxy-body-size=0 -n osm -l juju-app=ng-ui"
291 }
292
293 function check_osm_deployed() {
294 TIME_TO_WAIT=600
295 start_time="$(date -u +%s)"
296 total_service_count=14
297 previous_count=0
298 while true
299 do
300 service_count=$(juju status -m $MODEL_NAME | grep kubernetes | grep active | wc -l)
301 echo "$service_count / $total_service_count services active"
302 if [ $service_count -eq $total_service_count ]; then
303 break
304 fi
305 if [ $service_count -ne $previous_count ]; then
306 previous_count=$service_count
307 start_time="$(date -u +%s)"
308 fi
309 now="$(date -u +%s)"
310 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
311 echo "Timed out waiting for OSM services to become ready"
312 exit 1
313 fi
314 sleep 10
315 done
316 }
317
318 function create_overlay() {
319 sudo snap install jq
320 sudo snap install yq
321 local HOME=/home/$USER
322 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
323 local vca_password=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
324 local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
325 local vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 2)
326 local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
327 local vca_cloud="lxd-cloud"
328 # Get the VCA Certificate
329 local vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.ca-cert - | base64 | tr -d \\n)
330
331 # Calculate the default route of this machine
332 local DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
333
334 # Generate a new overlay.yaml, overriding any existing one
335 cat << EOF > /tmp/vca-overlay.yaml
336 applications:
337 lcm-k8s:
338 options:
339 vca_user: $vca_user
340 vca_password: $vca_password
341 vca_host: $vca_host
342 vca_port: $vca_port
343 vca_pubkey: $vca_pubkey
344 vca_cacert: $vca_cacert
345 vca_cloud: $vca_cloud
346 vca_k8s_cloud: $K8S_CLOUD_NAME
347 mon-k8s:
348 options:
349 vca_user: $vca_user
350 vca_password: $vca_password
351 vca_host: $vca_host
352 vca_cacert: $vca_cacert
353 EOF
354 mv /tmp/vca-overlay.yaml ~/.osm/
355 OSM_VCA_HOST=$vca_host
356 }
357
358 function generate_images_overlay(){
359 cat << EOF > /tmp/nbi_registry.yaml
360 registrypath: ${REGISTRY_URL}opensourcemano/nbi:$TAG
361 EOF
362 cat << EOF > /tmp/ng_ui_registry.yaml
363 registrypath: ${REGISTRY_URL}opensourcemano/ng-ui:$TAG
364 EOF
365 if [ ! -z "$REGISTRY_USERNAME" ] ; then
366 REGISTRY_CREDENTIALS=$(cat <<EOF
367
368 image_username: $REGISTRY_USERNAME
369 image_password: $REGISTRY_PASSWORD
370 EOF
371 );
372 echo username: $REGISTRY_USERNAME >> /tmp/nbi_registry.yaml
373 echo password: $REGISTRY_PASSWORD >> /tmp/nbi_registry.yaml
374 echo username: $REGISTRY_USERNAME >> /tmp/ng_ui_registry.yaml
375 echo password: $REGISTRY_PASSWORD >> /tmp/ng_ui_registry.yaml
376 fi
377
378 cat << EOF > /tmp/images-overlay.yaml
379 applications:
380 lcm-k8s:
381 options:
382 image: ${REGISTRY_URL}opensourcemano/lcm:$TAG ${REGISTRY_CREDENTIALS}
383 mon-k8s:
384 options:
385 image: ${REGISTRY_URL}opensourcemano/mon:$TAG ${REGISTRY_CREDENTIALS}
386 ro-k8s:
387 options:
388 image: ${REGISTRY_URL}opensourcemano/ro:$TAG ${REGISTRY_CREDENTIALS}
389 nbi:
390 resources:
391 image: /tmp/nbi_registry.yaml
392 pol-k8s:
393 options:
394 image: ${REGISTRY_URL}opensourcemano/pol:$TAG ${REGISTRY_CREDENTIALS}
395 pla:
396 options:
397 image: ${REGISTRY_URL}opensourcemano/pla:8 ${REGISTRY_CREDENTIALS}
398 ng-ui:
399 resources:
400 image: /tmp/ng_ui_registry.yaml
401 keystone:
402 options:
403 image: ${REGISTRY_URL}opensourcemano/keystone:$TAG ${REGISTRY_CREDENTIALS}
404 EOF
405 mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE
406 }
407
408 function refresh_osmclient_snap() {
409 osmclient_snap_install_refresh refresh
410 }
411
412 function install_osm_client_snap() {
413 osmclient_snap_install_refresh install
414 }
415
416 function osmclient_snap_install_refresh() {
417 channel_preference="stable candidate beta edge"
418 for channel in $channel_preference; do
419 echo "Trying to install osmclient from channel $OSMCLIENT_VERSION/$channel"
420 sudo snap $1 osmclient --channel $OSMCLIENT_VERSION/$channel 2> /dev/null && echo osmclient snap installed && break
421 done
422 }
423 function install_osmclient() {
424 snap info osmclient | grep -E ^installed: && refresh_osmclient_snap || install_osm_client_snap
425 }
426
427 function add_local_k8scluster() {
428 osm --all-projects vim-create \
429 --name _system-osm-vim \
430 --account_type dummy \
431 --auth_url http://dummy \
432 --user osm --password osm --tenant osm \
433 --description "dummy" \
434 --config '{management_network_name: mgmt}'
435 tmpfile=$(mktemp --tmpdir=${HOME})
436 cp ${KUBECONFIG} ${tmpfile}
437 osm --all-projects k8scluster-add \
438 --creds ${tmpfile} \
439 --vim _system-osm-vim \
440 --k8s-nets '{"net1": null}' \
441 --version '1.19' \
442 --description "OSM Internal Cluster" \
443 _system-osm-k8s
444 rm -f ${tmpfile}
445 }
446
447 function install_microstack() {
448 # Install and init microstack
449 sudo snap install microstack --channel beta --devmode
450 sudo snap set microstack config.network.ports.dashboard=8080
451 sudo microstack.init --auto --control
452
453 # Basic configuration
454 microstack.openstack network create --enable --no-share osm-ext
455 microstack.openstack subnet create osm-ext-subnet --network osm-ext --dns-nameserver 8.8.8.8 --subnet-range 172.16.0.0/24
456 microstack.openstack router create external-router
457 microstack.openstack router add subnet external-router osm-ext-subnet
458 microstack.openstack router set --external-gateway external external-router
459 for i in $(microstack.openstack security group list | awk '/default/{ print $2 }'); do
460 microstack.openstack security group rule create $i --protocol icmp --remote-ip 0.0.0.0/0
461 microstack.openstack security group rule create $i --protocol tcp --remote-ip 0.0.0.0/0
462 done
463 KEYPAIR_PATH=~/.ssh/microstack
464 if ! test -f $KEYPAIR_PATH; then
465 echo "Generating ssh keypair for microstack"
466 ssh-keygen -t rsa -N "" -f $KEYPAIR_PATH
467 fi
468 microstack.openstack keypair create --public-key $KEYPAIR_PATH.pub microstack
469
470 # Add xenial, bionic, and focal images to microstack
471 echo "curl https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img | microstack.openstack image create --public --container-format=bare --disk-format=qcow2 ubuntu16.04"
472 echo "curl https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img | microstack.openstack image create --public --container-format=bare --disk-format=qcow2 ubuntu18.04"
473 echo "curl https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img | microstack.openstack image create --public --container-format=bare --disk-format=qcow2 ubuntu20.04"
474
475 # Load ENV variables
476 . /var/snap/microstack/common/etc/microstack.rc
477 osm vim-create --name microstack \
478 --user "$OS_USERNAME" \
479 --password "$OS_PASSWORD" \
480 --auth_url "$OS_AUTH_URL/v3" \
481 --tenant "$OS_USERNAME" \
482 --account_type openstack \
483 --config='{
484 use_floating_ip: True,
485 management_network_name: osm-ext,
486 keypair: microstack,
487 project_name: admin,
488 user_domain_name: default,
489 region_name: microstack,
490 insecure: True,
491 availability_zone: nova,
492 version: 3
493 }'
494 }
495
496 DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
497 DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
498
499 check_arguments $@
500 mkdir -p ~/.osm
501 install_snaps
502 bootstrap_k8s_lxd
503 deploy_charmed_osm
504 install_osmclient
505 export OSM_HOSTNAME=$(juju config nbi site_url | sed "s/http.*\?:\/\///"):443
506 add_local_k8scluster
507
508 if [ -v MICROSTACK ]; then
509 install_microstack
510 fi
511
512
513 echo "Your installation is now complete, follow these steps for configuring the osmclient:"
514 echo
515 echo "1. Create the OSM_HOSTNAME environment variable with the NBI IP"
516 echo
517 echo "export OSM_HOSTNAME=$OSM_HOSTNAME"
518 echo
519 echo "2. Add the previous command to your .bashrc for other Shell sessions"
520 echo
521 echo "echo \"export OSM_HOSTNAME=$OSM_HOSTNAME\" >> ~/.bashrc"
522 echo
523 echo "DONE"