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