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