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