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