Update RO charm for the newRO and update bundle
[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.6
19 K8S_CLOUD_NAME="k8s-cloud"
20 KUBECTL="microk8s.kubectl"
21 IMAGES_OVERLAY_FILE=~/.osm/images-overlay.yaml
22 PATH=/snap/bin:${PATH}
23
24 function check_arguments(){
25 while [ $# -gt 0 ] ; do
26 case $1 in
27 --bundle) BUNDLE="$2" ;;
28 --k8s) KUBECFG="$2" ;;
29 --vca) CONTROLLER="$2" ;;
30 --lxd) LXD_CLOUD="$2" ;;
31 --lxd-cred) LXD_CREDENTIALS="$2" ;;
32 --microstack) MICROSTACK=y ;;
33 --ha) BUNDLE="cs:osm-ha" ;;
34 --tag) TAG="$2" ;;
35 --registry) REGISTRY_INFO="$2" ;;
36 esac
37 shift
38 done
39
40 # echo $BUNDLE $KUBECONFIG $LXDENDPOINT
41 }
42
43 function install_snaps(){
44 if [ ! -v KUBECFG ]; then
45 sudo snap install microk8s --classic
46 sudo usermod -a -G microk8s `whoami`
47 mkdir -p ~/.kube
48 sudo chown -f -R `whoami` ~/.kube
49 KUBEGRP="microk8s"
50 sg ${KUBEGRP} -c "microk8s status --wait-ready"
51 else
52 KUBECTL="kubectl"
53 sudo snap install kubectl --classic
54 export KUBECONFIG=${KUBECFG}
55 KUBEGRP=$(id -g -n)
56 fi
57 sudo snap install juju --classic --channel=2.8/stable
58 }
59
60 function bootstrap_k8s_lxd(){
61 [ -v CONTROLLER ] && ADD_K8S_OPTS="--controller ${CONTROLLER}" && CONTROLLER_NAME=$CONTROLLER
62 [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="osm-vca"
63
64 if [ -v BOOTSTRAP_NEEDED ]; then
65 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
66 if [ $CONTROLLER_PRESENT -ge 1 ]; then
67 cat << EOF
68 Threre is already a VCA present with the installer reserved name of "${CONTROLLER_NAME}".
69 You may either explicitly use this VCA with the "--vca ${CONTROLLER_NAME}" option, or remove it
70 using this command:
71
72 juju destroy-controller --release-storage --destroy-all-models -y ${CONTROLLER_NAME}
73
74 Please retry the installation once this conflict has been resolved.
75 EOF
76 exit 1
77 fi
78 else
79 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
80 if [ $CONTROLLER_PRESENT -le 0 ]; then
81 cat << EOF
82 Threre is no VCA present with the name "${CONTROLLER_NAME}". Please specify a VCA
83 that exists, or remove the --vca ${CONTROLLER_NAME} option.
84
85 Please retry the installation with one of the solutions applied.
86 EOF
87 exit 1
88 fi
89 fi
90
91 if [ -v KUBECFG ]; then
92 cat $KUBECFG | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
93 [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME \
94 --config controller-service-type=loadbalancer \
95 --agent-version=$JUJU_AGENT_VERSION
96 else
97 sg ${KUBEGRP} -c "echo ${DEFAULT_IP}-${DEFAULT_IP} | microk8s.enable metallb"
98 sg ${KUBEGRP} -c "microk8s.enable ingress"
99 sg ${KUBEGRP} -c "microk8s.enable storage dns"
100 TIME_TO_WAIT=30
101 start_time="$(date -u +%s)"
102 while true
103 do
104 now="$(date -u +%s)"
105 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
106 echo "Microk8s storage failed to enable"
107 sg ${KUBEGRP} -c "microk8s.status"
108 exit 1
109 fi
110 storage_status=`sg ${KUBEGRP} -c "microk8s.status -a storage"`
111 if [[ $storage_status == "enabled" ]]; then
112 break
113 fi
114 sleep 1
115 done
116
117 [ ! -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
118 [ -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c \
119 "juju bootstrap microk8s $CONTROLLER_NAME --config controller-service-type=loadbalancer --agent-version=$JUJU_AGENT_VERSION" \
120 && K8S_CLOUD_NAME=microk8s
121 fi
122
123 if [ -v LXD_CLOUD ]; then
124 if [ ! -v LXD_CREDENTIALS ]; then
125 echo "The installer needs the LXD server certificate if the LXD is external"
126 exit 1
127 fi
128 else
129 LXDENDPOINT=$DEFAULT_IP
130 LXD_CLOUD=~/.osm/lxd-cloud.yaml
131 LXD_CREDENTIALS=~/.osm/lxd-credentials.yaml
132 # Apply sysctl production values for optimal performance
133 sudo cp /usr/share/osm-devops/installers/60-lxd-production.conf /etc/sysctl.d/60-lxd-production.conf
134 sudo sysctl --system
135 # Install LXD snap
136 sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client
137 sudo snap install lxd
138 # Configure LXD
139 sudo usermod -a -G lxd `whoami`
140 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"
141 sg lxd -c "lxd waitready"
142 DEFAULT_MTU=$(ip addr show $DEFAULT_IF | perl -ne 'if (/mtu\s(\d+)/) {print $1;}')
143 sg lxd -c "lxc profile device set default eth0 mtu $DEFAULT_MTU"
144 sg lxd -c "lxc network set lxdbr0 bridge.mtu $DEFAULT_MTU"
145
146 cat << EOF > $LXD_CLOUD
147 clouds:
148 lxd-cloud:
149 type: lxd
150 auth-types: [certificate]
151 endpoint: "https://$LXDENDPOINT:8443"
152 config:
153 ssl-hostname-verification: false
154 EOF
155 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"
156 local server_cert=`cat /var/snap/lxd/common/lxd/server.crt | sed 's/^/ /'`
157 local client_cert=`cat ~/.osm/client.crt | sed 's/^/ /'`
158 local client_key=`cat ~/.osm/client.key | sed 's/^/ /'`
159
160 cat << EOF > $LXD_CREDENTIALS
161 credentials:
162 lxd-cloud:
163 lxd-cloud:
164 auth-type: certificate
165 server-cert: |
166 $server_cert
167 client-cert: |
168 $client_cert
169 client-key: |
170 $client_key
171 EOF
172 lxc config trust add local: ~/.osm/client.crt
173 fi
174
175 juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force
176 juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS
177 sg lxd -c "lxd waitready"
178 juju controller-config features=[k8s-operators]
179 }
180
181 function wait_for_port(){
182 SERVICE=$1
183 INDEX=$2
184 TIME_TO_WAIT=30
185 start_time="$(date -u +%s)"
186 while true
187 do
188 now="$(date -u +%s)"
189 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
190 echo "Failed to expose external ${SERVICE} interface port"
191 exit 1
192 fi
193
194 if [ $(sg ${KUBEGRP} -c "${KUBECTL} get ingresses.networking -n osm -o json | jq -r '.items[$INDEX].metadata.name'") == ${SERVICE} ] ; then
195 break
196 fi
197 sleep 1
198 done
199 }
200
201 function deploy_charmed_osm(){
202 if [ -v REGISTRY_INFO ] ; then
203 registry_parts=(${REGISTRY_INFO//@/ })
204 if [ ${#registry_parts[@]} -eq 1 ] ; then
205 # No credentials supplied
206 REGISTRY_USERNAME=""
207 REGISTRY_PASSWORD=""
208 REGISTRY_URL=${registry_parts[0]}
209 else
210 credentials=${registry_parts[0]}
211 credential_parts=(${credentials//:/ })
212 REGISTRY_USERNAME=${credential_parts[0]}
213 REGISTRY_PASSWORD=${credential_parts[1]}
214 REGISTRY_URL=${registry_parts[1]}
215 fi
216 # Ensure the URL ends with a /
217 case $REGISTRY_URL in
218 */) ;;
219 *) REGISTRY_URL=${REGISTRY_URL}/
220 esac
221 fi
222
223 create_overlay
224 echo "Creating OSM model"
225 if [ -v KUBECFG ]; then
226 juju add-model osm $K8S_CLOUD_NAME
227 else
228 sg ${KUBEGRP} -c "juju add-model osm $K8S_CLOUD_NAME"
229 fi
230 echo "Deploying OSM with charms"
231 images_overlay=""
232 if [ -v REGISTRY_URL ]; then
233 [ ! -v TAG ] && TAG='latest'
234 fi
235 [ -v TAG ] && generate_images_overlay && images_overlay="--overlay $IMAGES_OVERLAY_FILE"
236
237 if [ -v BUNDLE ]; then
238 juju deploy $BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay
239 else
240 juju deploy cs:osm --overlay ~/.osm/vca-overlay.yaml $images_overlay
241 fi
242
243 echo "Waiting for deployment to finish..."
244 check_osm_deployed
245 echo "OSM with charms deployed"
246 if [ ! -v KUBECFG ]; then
247 API_SERVER=${DEFAULT_IP}
248 else
249 API_SERVER=$(kubectl config view --minify | grep server | cut -f 2- -d ":" | tr -d " ")
250 proto="$(echo $API_SERVER | grep :// | sed -e's,^\(.*://\).*,\1,g')"
251 url="$(echo ${API_SERVER/$proto/})"
252 user="$(echo $url | grep @ | cut -d@ -f1)"
253 hostport="$(echo ${url/$user@/} | cut -d/ -f1)"
254 API_SERVER="$(echo $hostport | sed -e 's,:.*,,g')"
255 fi
256
257 # Expose OSM services
258 # Expose Grafana
259 juju config grafana-k8s juju-external-hostname=grafana.${API_SERVER}.xip.io
260 juju expose grafana-k8s
261 wait_for_port grafana-k8s 0
262
263 # Expose NBI
264 juju config nbi-k8s juju-external-hostname=nbi.${API_SERVER}.xip.io
265 juju expose nbi-k8s
266 wait_for_port nbi-k8s 1
267
268 # Expose NG UI
269 juju config ng-ui juju-external-hostname=ui.${API_SERVER}.xip.io
270 juju expose ng-ui
271 wait_for_port ng-ui 2
272
273 # Expose Prometheus
274 juju config prometheus-k8s juju-external-hostname=prometheus.${API_SERVER}.xip.io
275 juju expose prometheus-k8s
276 wait_for_port prometheus-k8s 3
277
278 # Apply annotations
279 sg ${KUBEGRP} -c "${KUBECTL} annotate ingresses.networking nginx.ingress.kubernetes.io/backend-protocol=HTTPS -n osm -l juju-app=nbi-k8s"
280 sg ${KUBEGRP} -c "${KUBECTL} annotate ingresses.networking nginx.ingress.kubernetes.io/proxy-body-size=0 -n osm -l juju-app=nbi-k8s"
281 sg ${KUBEGRP} -c "${KUBECTL} annotate ingresses.networking nginx.ingress.kubernetes.io/proxy-body-size=0 -n osm -l juju-app=ng-ui"
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 | 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 apt install python3-pip -y
312 python3 -m pip install yq
313 PATH=$PATH:$HOME/.local/bin # make yq command available
314 local HOME=/home/$USER
315 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME].user')
316 local vca_password=$(cat $HOME/.local/share/juju/accounts.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME].password')
317 local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME]["api-endpoints"][0]' --raw-output | cut -d ":" -f 1)
318 local vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME]["api-endpoints"][0]' --raw-output | 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 --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME]["ca-cert"]' --raw-output | 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-k8s:
331 options:
332 vca_user: $vca_user
333 vca_password: $vca_password
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-k8s:
341 options:
342 vca_user: $vca_user
343 vca_password: $vca_password
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 if [ ! -z "$REGISTRY_USERNAME" ] ; then
353 REGISTRY_CREDENTIALS=$(cat <<EOF
354
355 image_username: $REGISTRY_USERNAME
356 image_password: $REGISTRY_PASSWORD
357 EOF
358 );
359 fi
360
361 cat << EOF > /tmp/images-overlay.yaml
362 applications:
363 lcm-k8s:
364 options:
365 image: ${REGISTRY_URL}opensourcemano/lcm:$TAG ${REGISTRY_CREDENTIALS}
366 mon-k8s:
367 options:
368 image: ${REGISTRY_URL}opensourcemano/mon:$TAG ${REGISTRY_CREDENTIALS}
369 ro-k8s:
370 options:
371 image: ${REGISTRY_URL}opensourcemano/ro:$TAG ${REGISTRY_CREDENTIALS}
372 nbi-k8s:
373 options:
374 image: ${REGISTRY_URL}opensourcemano/nbi:$TAG ${REGISTRY_CREDENTIALS}
375 pol-k8s:
376 options:
377 image: ${REGISTRY_URL}opensourcemano/pol:$TAG ${REGISTRY_CREDENTIALS}
378 pla:
379 options:
380 image: ${REGISTRY_URL}opensourcemano/pla:$TAG ${REGISTRY_CREDENTIALS}
381 ng-ui:
382 options:
383 image: ${REGISTRY_URL}opensourcemano/ng-ui:$TAG ${REGISTRY_CREDENTIALS}
384 keystone:
385 options:
386 image: ${REGISTRY_URL}opensourcemano/keystone:$TAG ${REGISTRY_CREDENTIALS}
387 EOF
388 mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE
389 }
390
391 function install_osmclient() {
392 sudo snap install osmclient
393 sudo snap alias osmclient.osm osm
394 }
395
396
397 function install_microstack() {
398 sudo snap install microstack --classic --beta
399 sudo microstack.init --auto
400 wget https://cloud-images.ubuntu.com/releases/16.04/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img -P ~/.osm/
401 microstack.openstack image create \
402 --public \
403 --disk-format qcow2 \
404 --container-format bare \
405 --file ~/.osm/ubuntu-16.04-server-cloudimg-amd64-disk1.img \
406 ubuntu1604
407 ssh-keygen -t rsa -N "" -f ~/.ssh/microstack
408 microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack
409 export OSM_HOSTNAME=`juju status --format json | jq -rc '.applications."nbi-k8s".address'`
410 osm vim-create --name microstack-site \
411 --user admin \
412 --password keystone \
413 --auth_url http://10.20.20.1:5000/v3 \
414 --tenant admin \
415 --account_type openstack \
416 --config='{security_groups: default,
417 keypair: microstack,
418 project_name: admin,
419 user_domain_name: default,
420 region_name: microstack,
421 insecure: True,
422 availability_zone: nova,
423 version: 3}'
424 }
425
426 DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
427 DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
428
429 check_arguments $@
430 mkdir -p ~/.osm
431 install_snaps
432 bootstrap_k8s_lxd
433 deploy_charmed_osm
434 install_osmclient
435 if [ -v MICROSTACK ]; then
436 install_microstack
437 fi
438
439 OSM_HOSTNAME=$(juju config nbi-k8s juju-external-hostname):443
440
441 echo "Your installation is now complete, follow these steps for configuring the osmclient:"
442 echo
443 echo "1. Create the OSM_HOSTNAME environment variable with the NBI IP"
444 echo
445 echo "export OSM_HOSTNAME=$OSM_HOSTNAME"
446 echo
447 echo "2. Add the previous command to your .bashrc for other Shell sessions"
448 echo
449 echo "echo \"export OSM_HOSTNAME=$OSM_HOSTNAME\" >> ~/.bashrc"
450 echo
451 echo "DONE"