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