Update osm bundles to cs:osm-66 and cs:osm-ha-51
[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.9
19 K8S_CLOUD_NAME="k8s-cloud"
20 KUBECTL="microk8s.kubectl"
21 MICROK8S_VERSION=1.19
22 OSMCLIENT_VERSION=9.0
23 IMAGES_OVERLAY_FILE=~/.osm/images-overlay.yaml
24 PATH=/snap/bin:${PATH}
25
26 MODEL_NAME=osm
27
28 OSM_BUNDLE=cs:osm-66
29 OSM_HA_BUNDLE=cs:osm-ha-51
30 TAG=testing-daily
31
32 function check_arguments(){
33 while [ $# -gt 0 ] ; do
34 case $1 in
35 --bundle) BUNDLE="$2" ;;
36 --overlay) OVERLAY="$2" ;;
37 --k8s) KUBECFG="$2" ;;
38 --vca) CONTROLLER="$2" ;;
39 --lxd) LXD_CLOUD="$2" ;;
40 --lxd-cred) LXD_CREDENTIALS="$2" ;;
41 --microstack) MICROSTACK=y ;;
42 --ha) BUNDLE=$OSM_HA_BUNDLE ;;
43 --tag) TAG="$2" ;;
44 --registry) REGISTRY_INFO="$2" ;;
45 --only-vca) ONLY_VCA=y ;;
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=2.8/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
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 echo "Creating OSM model"
241 if [ -v KUBECFG ]; then
242 juju add-model $MODEL_NAME $K8S_CLOUD_NAME
243 else
244 sg ${KUBEGRP} -c "juju add-model $MODEL_NAME $K8S_CLOUD_NAME"
245 fi
246 echo "Deploying OSM with charms"
247 images_overlay=""
248 if [ -v REGISTRY_URL ]; then
249 [ ! -v TAG ] && TAG='latest'
250 fi
251 [ -v TAG ] && generate_images_overlay && images_overlay="--overlay $IMAGES_OVERLAY_FILE"
252
253 if [ -v OVERLAY ]; then
254 extra_overlay="--overlay $OVERLAY"
255 fi
256
257 if [ -v BUNDLE ]; then
258 juju deploy -m $MODEL_NAME $BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay $extra_overlay
259 else
260 juju deploy -m $MODEL_NAME $OSM_BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay $extra_overlay
261 fi
262
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 # Expose OSM services
274 juju config -m $MODEL_NAME nbi site_url=https://nbi.${API_SERVER}.nip.io
275 juju config -m $MODEL_NAME ng-ui site_url=https://ui.${API_SERVER}.nip.io
276 juju config -m $MODEL_NAME grafana site_url=https://grafana.${API_SERVER}.nip.io
277 juju config -m $MODEL_NAME prometheus site_url=https://prometheus.${API_SERVER}.nip.io
278
279 echo "Waiting for deployment to finish..."
280 check_osm_deployed
281 echo "OSM with charms deployed"
282 }
283
284 function check_osm_deployed() {
285 TIME_TO_WAIT=600
286 start_time="$(date -u +%s)"
287 total_service_count=14
288 previous_count=0
289 while true
290 do
291 service_count=$(juju status -m $MODEL_NAME | grep kubernetes | grep active | wc -l)
292 echo "$service_count / $total_service_count services active"
293 if [ $service_count -eq $total_service_count ]; then
294 break
295 fi
296 if [ $service_count -ne $previous_count ]; then
297 previous_count=$service_count
298 start_time="$(date -u +%s)"
299 fi
300 now="$(date -u +%s)"
301 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
302 echo "Timed out waiting for OSM services to become ready"
303 exit 1
304 fi
305 sleep 10
306 done
307 }
308
309 function create_overlay() {
310 sudo snap install jq
311 sudo snap install yq
312 local HOME=/home/$USER
313 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
314 local vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
315 local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
316 local vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 2)
317 local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
318 local vca_cloud="lxd-cloud"
319 # Get the VCA Certificate
320 local vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.ca-cert - | base64 | tr -d \\n)
321
322 # Calculate the default route of this machine
323 local DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
324
325 # Generate a new overlay.yaml, overriding any existing one
326 cat << EOF > /tmp/vca-overlay.yaml
327 applications:
328 lcm:
329 options:
330 vca_user: $vca_user
331 vca_secret: $vca_secret
332 vca_host: $vca_host
333 vca_port: $vca_port
334 vca_pubkey: $vca_pubkey
335 vca_cacert: $vca_cacert
336 vca_cloud: $vca_cloud
337 vca_k8s_cloud: $K8S_CLOUD_NAME
338 mon:
339 options:
340 vca_user: $vca_user
341 vca_secret: $vca_secret
342 vca_host: $vca_host
343 vca_cacert: $vca_cacert
344 EOF
345 mv /tmp/vca-overlay.yaml ~/.osm/
346 OSM_VCA_HOST=$vca_host
347 }
348
349 function generate_images_overlay(){
350 echo "applications:" > /tmp/images-overlay.yaml
351
352 charms_with_resources="nbi lcm mon pol ng-ui ro pla keystone"
353 for charm in $charms_with_resources; do
354 cat << EOF > /tmp/${charm}_registry.yaml
355 registrypath: ${REGISTRY_URL}opensourcemano/${charm}:$TAG
356 EOF
357 if [ ! -z "$REGISTRY_USERNAME" ] ; then
358 echo username: $REGISTRY_USERNAME >> /tmp/${charm}_registry.yaml
359 echo password: $REGISTRY_PASSWORD >> /tmp/${charm}_registry.yaml
360 fi
361
362 cat << EOF >> /tmp/images-overlay.yaml
363 ${charm}:
364 resources:
365 image: /tmp/${charm}_registry.yaml
366
367 EOF
368 done
369
370 mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE
371 }
372
373 function refresh_osmclient_snap() {
374 osmclient_snap_install_refresh refresh
375 }
376
377 function install_osm_client_snap() {
378 osmclient_snap_install_refresh install
379 }
380
381 function osmclient_snap_install_refresh() {
382 channel_preference="stable candidate beta edge"
383 for channel in $channel_preference; do
384 echo "Trying to install osmclient from channel $OSMCLIENT_VERSION/$channel"
385 sudo snap $1 osmclient --channel $OSMCLIENT_VERSION/$channel 2> /dev/null && echo osmclient snap installed && break
386 done
387 }
388 function install_osmclient() {
389 snap info osmclient | grep -E ^installed: && refresh_osmclient_snap || install_osm_client_snap
390 }
391
392 function add_local_k8scluster() {
393 osm --all-projects vim-create \
394 --name _system-osm-vim \
395 --account_type dummy \
396 --auth_url http://dummy \
397 --user osm --password osm --tenant osm \
398 --description "dummy" \
399 --config '{management_network_name: mgmt}'
400 tmpfile=$(mktemp --tmpdir=${HOME})
401 cp ${KUBECONFIG} ${tmpfile}
402 osm --all-projects k8scluster-add \
403 --creds ${tmpfile} \
404 --vim _system-osm-vim \
405 --k8s-nets '{"net1": null}' \
406 --version '1.19' \
407 --description "OSM Internal Cluster" \
408 _system-osm-k8s
409 rm -f ${tmpfile}
410 }
411
412 function install_microstack() {
413 sudo snap install microstack --classic --beta
414 sudo microstack.init --auto
415 wget https://cloud-images.ubuntu.com/releases/16.04/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img -P ~/.osm/
416 microstack.openstack image create \
417 --public \
418 --disk-format qcow2 \
419 --container-format bare \
420 --file ~/.osm/ubuntu-16.04-server-cloudimg-amd64-disk1.img \
421 ubuntu1604
422 ssh-keygen -t rsa -N "" -f ~/.ssh/microstack
423 microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack
424 export OSM_HOSTNAME=`juju status -m $MODEL_NAME --format json | jq -rc '.applications."nbi".address'`
425 osm vim-create --name microstack-site \
426 --user admin \
427 --password keystone \
428 --auth_url http://10.20.20.1:5000/v3 \
429 --tenant admin \
430 --account_type openstack \
431 --config='{security_groups: default,
432 keypair: microstack,
433 project_name: admin,
434 user_domain_name: default,
435 region_name: microstack,
436 insecure: True,
437 availability_zone: nova,
438 version: 3}'
439 }
440
441 DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
442 DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
443
444 check_arguments $@
445 mkdir -p ~/.osm
446 install_snaps
447 bootstrap_k8s_lxd
448 create_overlay
449 if [ -v ONLY_VCA ]; then
450 HOME=/home/$USER
451 vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
452 vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
453 vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
454 vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 2)
455 vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
456 vca_cloud="lxd-cloud"
457 vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.ca-cert - | base64 | tr -d \\n)
458 hostname=`cat /etc/hostname`
459
460 echo "Use the following command to register the installed VCA to your OSM:"
461 echo -e " osm vca-add --endpoints $vca_host:$vca_port \ \n --user $vca_user \ \n --secret $vca_secret \ \n --cacert $vca_cacert \ \n --lxd-cloud lxd-cloud \ \n --lxd-credentials lxd-cloud \ \n --k8s-cloud microk8s \ \n --k8s-credentials microk8s\ \n $hostname-vca"
462 else
463 deploy_charmed_osm
464 install_osmclient
465 export OSM_HOSTNAME=$(juju config -m $MODEL_NAME nbi site_url | sed "s/http.*\?:\/\///"):443
466 sleep 10
467 add_local_k8scluster
468 if [ -v MICROSTACK ]; then
469 install_microstack
470 fi
471
472 echo "Your installation is now complete, follow these steps for configuring the osmclient:"
473 echo
474 echo "1. Create the OSM_HOSTNAME environment variable with the NBI IP"
475 echo
476 echo "export OSM_HOSTNAME=$OSM_HOSTNAME"
477 echo
478 echo "2. Add the previous command to your .bashrc for other Shell sessions"
479 echo
480 echo "echo \"export OSM_HOSTNAME=$OSM_HOSTNAME\" >> ~/.bashrc"
481 echo
482 echo "DONE"
483 fi
484