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