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