blob: fb50a7ebc3681adf928c7c8d9ec3d993d32d04af [file] [log] [blame]
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +01001#! /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#
David Garcia42375212020-04-27 19:07:49 +020015
16# set -eux
17
David Garcia0db6e9f2021-10-05 16:13:08 +020018LXD_VERSION=4.0
David Garciad68e0b42021-06-28 16:50:42 +020019JUJU_VERSION=2.9
Guillermo Calvino2834fe72023-06-22 10:02:53 +020020JUJU_AGENT_VERSION=2.9.43
David Garcia42375212020-04-27 19:07:49 +020021K8S_CLOUD_NAME="k8s-cloud"
beierlm3749e312020-07-02 14:21:09 -040022KUBECTL="microk8s.kubectl"
Patricia Reinoso0788ee12023-03-14 16:06:32 +000023MICROK8S_VERSION=1.26
Mark Beierlb6d802a2022-09-20 13:10:38 -040024OSMCLIENT_VERSION=12.0
David Garcia69388c22020-05-07 12:14:19 +020025IMAGES_OVERLAY_FILE=~/.osm/images-overlay.yaml
aticig6eb39282022-02-24 00:34:52 +030026PASSWORD_OVERLAY_FILE=~/.osm/password-overlay.yaml
beierlmf2782c52020-11-05 17:04:05 -050027PATH=/snap/bin:${PATH}
beierlmce7b4602022-04-13 08:19:40 -040028OSM_DEVOPS="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/.. &> /dev/null && pwd )"
29if [ -f ${OSM_DEVOPS}/common/all_funcs ] ; then
30 source ${OSM_DEVOPS}/common/all_funcs
31else
32 function track(){
33 true
34 }
35 function FATAL_TRACK(){
36 exit 1
37 }
38fi
beierlmf2782c52020-11-05 17:04:05 -050039
David Garciaef349d92020-12-10 21:16:12 +010040MODEL_NAME=osm
41
David Garcia4a0db7c2022-02-21 11:48:11 +010042OSM_BUNDLE=ch:osm
43OSM_HA_BUNDLE=ch:osm-ha
aticig4eb45c92022-08-11 13:10:19 +030044CHARMHUB_CHANNEL=12.0/stable
David Garcia4a0db7c2022-02-21 11:48:11 +010045unset TAG
David Garciaab11f842020-12-16 17:25:15 +010046
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010047function check_arguments(){
48 while [ $# -gt 0 ] ; do
David Garcia42375212020-04-27 19:07:49 +020049 case $1 in
50 --bundle) BUNDLE="$2" ;;
David Garcia4bd2bf02021-05-05 19:01:43 +020051 --overlay) OVERLAY="$2" ;;
Dominik Fleischmann7a97a4c2020-06-04 10:52:05 +020052 --k8s) KUBECFG="$2" ;;
53 --vca) CONTROLLER="$2" ;;
aticig5d19ac22022-05-06 13:46:22 +030054 --small-profile) INSTALL_NOLXD=y;;
Dominik Fleischmann7a97a4c2020-06-04 10:52:05 +020055 --lxd) LXD_CLOUD="$2" ;;
56 --lxd-cred) LXD_CREDENTIALS="$2" ;;
David Garcia69388c22020-05-07 12:14:19 +020057 --microstack) MICROSTACK=y ;;
David Garciaab11f842020-12-16 17:25:15 +010058 --ha) BUNDLE=$OSM_HA_BUNDLE ;;
David Garcia69388c22020-05-07 12:14:19 +020059 --tag) TAG="$2" ;;
beierlmf2782c52020-11-05 17:04:05 -050060 --registry) REGISTRY_INFO="$2" ;;
David Garcia1bc71022021-05-03 18:27:18 +020061 --only-vca) ONLY_VCA=y ;;
David Garcia42375212020-04-27 19:07:49 +020062 esac
63 shift
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010064 done
65
David Garcia42375212020-04-27 19:07:49 +020066 # echo $BUNDLE $KUBECONFIG $LXDENDPOINT
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010067}
beierlma4a37f72020-06-26 12:55:01 -040068
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010069function install_snaps(){
beierlma4a37f72020-06-26 12:55:01 -040070 if [ ! -v KUBECFG ]; then
beierlmce7b4602022-04-13 08:19:40 -040071 KUBEGRP="microk8s"
72 sudo snap install microk8s --classic --channel=${MICROK8S_VERSION}/stable ||
73 FATAL_TRACK k8scluster "snap install microk8s ${MICROK8S_VERSION}/stable failed"
74 sudo usermod -a -G microk8s `whoami`
David Garcia9a59e3d2021-02-08 15:18:27 +010075 sudo cat /var/snap/microk8s/current/args/kube-apiserver | grep advertise-address || (
David Garcia107010b2021-01-15 12:58:59 +010076 echo "--advertise-address $DEFAULT_IP" | sudo tee -a /var/snap/microk8s/current/args/kube-apiserver
beierlmce7b4602022-04-13 08:19:40 -040077 sg ${KUBEGRP} -c microk8s.stop
78 sg ${KUBEGRP} -c microk8s.start
David Garcia107010b2021-01-15 12:58:59 +010079 )
beierlma4a37f72020-06-26 12:55:01 -040080 mkdir -p ~/.kube
81 sudo chown -f -R `whoami` ~/.kube
beierlmf2782c52020-11-05 17:04:05 -050082 sg ${KUBEGRP} -c "microk8s status --wait-ready"
beierlmdf6de3d2020-12-16 08:46:40 -050083 KUBECONFIG=~/.osm/microk8s-config.yaml
David Garcia107010b2021-01-15 12:58:59 +010084 sg ${KUBEGRP} -c "microk8s config" | tee ${KUBECONFIG}
beierlmce7b4602022-04-13 08:19:40 -040085 track k8scluster k8scluster_ok
beierlma4a37f72020-06-26 12:55:01 -040086 else
87 KUBECTL="kubectl"
88 sudo snap install kubectl --classic
89 export KUBECONFIG=${KUBECFG}
90 KUBEGRP=$(id -g -n)
91 fi
beierlmce7b4602022-04-13 08:19:40 -040092 sudo snap install juju --classic --channel=$JUJU_VERSION/stable ||
93 FATAL_TRACK juju "snap install juju ${JUJU_VERSION}/stable failed"
94 track juju juju_ok
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010095}
96
97function bootstrap_k8s_lxd(){
David Garcia42375212020-04-27 19:07:49 +020098 [ -v CONTROLLER ] && ADD_K8S_OPTS="--controller ${CONTROLLER}" && CONTROLLER_NAME=$CONTROLLER
beierlma4a37f72020-06-26 12:55:01 -040099 [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="osm-vca"
100
101 if [ -v BOOTSTRAP_NEEDED ]; then
102 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
103 if [ $CONTROLLER_PRESENT -ge 1 ]; then
104 cat << EOF
105Threre is already a VCA present with the installer reserved name of "${CONTROLLER_NAME}".
106You may either explicitly use this VCA with the "--vca ${CONTROLLER_NAME}" option, or remove it
107using this command:
108
109 juju destroy-controller --release-storage --destroy-all-models -y ${CONTROLLER_NAME}
110
111Please retry the installation once this conflict has been resolved.
112EOF
beierlmce7b4602022-04-13 08:19:40 -0400113 FATAL_TRACK bootstrap_k8s "VCA already present"
beierlma4a37f72020-06-26 12:55:01 -0400114 fi
beierlm9afb0ef2020-10-16 12:53:51 -0400115 else
116 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
117 if [ $CONTROLLER_PRESENT -le 0 ]; then
118 cat << EOF
119Threre is no VCA present with the name "${CONTROLLER_NAME}". Please specify a VCA
120that exists, or remove the --vca ${CONTROLLER_NAME} option.
121
122Please retry the installation with one of the solutions applied.
123EOF
beierlmce7b4602022-04-13 08:19:40 -0400124 FATAL_TRACK bootstrap_k8s "Requested VCA not present"
beierlm9afb0ef2020-10-16 12:53:51 -0400125 fi
beierlma4a37f72020-06-26 12:55:01 -0400126 fi
David Garcia42375212020-04-27 19:07:49 +0200127
128 if [ -v KUBECFG ]; then
129 cat $KUBECFG | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
beierlm2634cd32020-09-15 16:00:34 -0400130 [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME \
131 --config controller-service-type=loadbalancer \
David Garciaa1376012020-10-19 15:42:42 +0200132 --agent-version=$JUJU_AGENT_VERSION
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100133 else
beierlma4a37f72020-06-26 12:55:01 -0400134 sg ${KUBEGRP} -c "echo ${DEFAULT_IP}-${DEFAULT_IP} | microk8s.enable metallb"
beierlm9afb0ef2020-10-16 12:53:51 -0400135 sg ${KUBEGRP} -c "microk8s.enable ingress"
Patricia Reinoso0788ee12023-03-14 16:06:32 +0000136 sg ${KUBEGRP} -c "microk8s.enable hostpath-storage dns"
beierlma4a37f72020-06-26 12:55:01 -0400137 TIME_TO_WAIT=30
138 start_time="$(date -u +%s)"
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200139 while true
140 do
beierlma4a37f72020-06-26 12:55:01 -0400141 now="$(date -u +%s)"
142 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
143 echo "Microk8s storage failed to enable"
144 sg ${KUBEGRP} -c "microk8s.status"
beierlmce7b4602022-04-13 08:19:40 -0400145 FATAL_TRACK bootstrap_k8s "Microk8s storage failed to enable"
beierlma4a37f72020-06-26 12:55:01 -0400146 fi
147 storage_status=`sg ${KUBEGRP} -c "microk8s.status -a storage"`
148 if [[ $storage_status == "enabled" ]]; then
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200149 break
150 fi
151 sleep 1
152 done
David Garcia42375212020-04-27 19:07:49 +0200153
beierlma4a37f72020-06-26 12:55:01 -0400154 [ ! -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
beierlm2634cd32020-09-15 16:00:34 -0400155 [ -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c \
David Garciaa1376012020-10-19 15:42:42 +0200156 "juju bootstrap microk8s $CONTROLLER_NAME --config controller-service-type=loadbalancer --agent-version=$JUJU_AGENT_VERSION" \
beierlm2634cd32020-09-15 16:00:34 -0400157 && K8S_CLOUD_NAME=microk8s
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100158 fi
beierlmce7b4602022-04-13 08:19:40 -0400159 track bootstrap_k8s bootstrap_k8s_ok
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100160
aticig5d19ac22022-05-06 13:46:22 +0300161 if [ ! -v INSTALL_NOLXD ]; then
162 if [ -v LXD_CLOUD ]; then
163 if [ ! -v LXD_CREDENTIALS ]; then
164 echo "The installer needs the LXD server certificate if the LXD is external"
165 FATAL_TRACK bootstrap_lxd "No LXD certificate supplied"
166 fi
167 else
168 LXDENDPOINT=$DEFAULT_IP
169 LXD_CLOUD=~/.osm/lxd-cloud.yaml
170 LXD_CREDENTIALS=~/.osm/lxd-credentials.yaml
171 # Apply sysctl production values for optimal performance
172 sudo cp /usr/share/osm-devops/installers/60-lxd-production.conf /etc/sysctl.d/60-lxd-production.conf
173 sudo sysctl --system
174 # Install LXD snap
175 sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client
176 sudo snap install lxd --channel $LXD_VERSION/stable
177 # Configure LXD
178 sudo usermod -a -G lxd `whoami`
179 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"
180 sg lxd -c "lxd waitready"
181 DEFAULT_MTU=$(ip addr show $DEFAULT_IF | perl -ne 'if (/mtu\s(\d+)/) {print $1;}')
182 sg lxd -c "lxc profile device set default eth0 mtu $DEFAULT_MTU"
183 sg lxd -c "lxc network set lxdbr0 bridge.mtu $DEFAULT_MTU"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100184
aticig5d19ac22022-05-06 13:46:22 +0300185 cat << EOF > $LXD_CLOUD
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100186clouds:
187 lxd-cloud:
188 type: lxd
189 auth-types: [certificate]
190 endpoint: "https://$LXDENDPOINT:8443"
191 config:
192 ssl-hostname-verification: false
193EOF
aticig5d19ac22022-05-06 13:46:22 +0300194 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"
aticig5d19ac22022-05-06 13:46:22 +0300195 cat << EOF > $LXD_CREDENTIALS
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100196credentials:
197 lxd-cloud:
David Garcia42375212020-04-27 19:07:49 +0200198 lxd-cloud:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100199 auth-type: certificate
David Garciaf82897c2022-06-01 13:01:43 +0200200 server-cert: /var/snap/lxd/common/lxd/server.crt
201 client-cert: ~/.osm/client.crt
202 client-key: ~/.osm/client.key
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100203EOF
aticig5d19ac22022-05-06 13:46:22 +0300204 lxc config trust add local: ~/.osm/client.crt
205 fi
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100206
aticig5d19ac22022-05-06 13:46:22 +0300207 juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force
208 juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS
209 sg lxd -c "lxd waitready"
210 juju controller-config features=[k8s-operators]
211 track bootstrap_lxd bootstrap_lxd_ok
212 fi
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100213}
214
215function deploy_charmed_osm(){
beierlmf2782c52020-11-05 17:04:05 -0500216 if [ -v REGISTRY_INFO ] ; then
217 registry_parts=(${REGISTRY_INFO//@/ })
218 if [ ${#registry_parts[@]} -eq 1 ] ; then
219 # No credentials supplied
220 REGISTRY_USERNAME=""
221 REGISTRY_PASSWORD=""
222 REGISTRY_URL=${registry_parts[0]}
223 else
224 credentials=${registry_parts[0]}
225 credential_parts=(${credentials//:/ })
226 REGISTRY_USERNAME=${credential_parts[0]}
227 REGISTRY_PASSWORD=${credential_parts[1]}
228 REGISTRY_URL=${registry_parts[1]}
229 fi
230 # Ensure the URL ends with a /
231 case $REGISTRY_URL in
232 */) ;;
233 *) REGISTRY_URL=${REGISTRY_URL}/
234 esac
235 fi
236
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100237 echo "Creating OSM model"
David Garcia42375212020-04-27 19:07:49 +0200238 if [ -v KUBECFG ]; then
David Garciaef349d92020-12-10 21:16:12 +0100239 juju add-model $MODEL_NAME $K8S_CLOUD_NAME
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100240 else
David Garciaef349d92020-12-10 21:16:12 +0100241 sg ${KUBEGRP} -c "juju add-model $MODEL_NAME $K8S_CLOUD_NAME"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100242 fi
243 echo "Deploying OSM with charms"
beierlma4a37f72020-06-26 12:55:01 -0400244 images_overlay=""
beierlmf2782c52020-11-05 17:04:05 -0500245 if [ -v REGISTRY_URL ]; then
246 [ ! -v TAG ] && TAG='latest'
247 fi
beierlma4a37f72020-06-26 12:55:01 -0400248 [ -v TAG ] && generate_images_overlay && images_overlay="--overlay $IMAGES_OVERLAY_FILE"
beierlmf2782c52020-11-05 17:04:05 -0500249
David Garcia4bd2bf02021-05-05 19:01:43 +0200250 if [ -v OVERLAY ]; then
251 extra_overlay="--overlay $OVERLAY"
252 fi
aticig6eb39282022-02-24 00:34:52 +0300253 echo "Creating Password Overlay"
254
255 generate_password_overlay && secret_overlay="--overlay $PASSWORD_OVERLAY_FILE"
David Garcia4bd2bf02021-05-05 19:01:43 +0200256
David Garcia42375212020-04-27 19:07:49 +0200257 if [ -v BUNDLE ]; then
aticig6eb39282022-02-24 00:34:52 +0300258 juju deploy --trust --channel $CHARMHUB_CHANNEL -m $MODEL_NAME $BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay $extra_overlay $secret_overlay
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100259 else
aticig6eb39282022-02-24 00:34:52 +0300260 juju deploy --trust --channel $CHARMHUB_CHANNEL -m $MODEL_NAME $OSM_BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay $extra_overlay $secret_overlay
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100261 fi
beierlma4a37f72020-06-26 12:55:01 -0400262
beierlma4a37f72020-06-26 12:55:01 -0400263 if [ ! -v KUBECFG ]; then
beierlma4a37f72020-06-26 12:55:01 -0400264 API_SERVER=${DEFAULT_IP}
265 else
266 API_SERVER=$(kubectl config view --minify | grep server | cut -f 2- -d ":" | tr -d " ")
267 proto="$(echo $API_SERVER | grep :// | sed -e's,^\(.*://\).*,\1,g')"
268 url="$(echo ${API_SERVER/$proto/})"
269 user="$(echo $url | grep @ | cut -d@ -f1)"
270 hostport="$(echo ${url/$user@/} | cut -d/ -f1)"
271 API_SERVER="$(echo $hostport | sed -e 's,:.*,,g')"
272 fi
David Garciade5fc1d2020-09-02 11:40:12 +0200273 # Expose OSM services
David Garciac395a452021-05-05 14:22:26 +0200274 juju config -m $MODEL_NAME nbi site_url=https://nbi.${API_SERVER}.nip.io
275 juju config -m $MODEL_NAME ng-ui site_url=https://ui.${API_SERVER}.nip.io
276 juju config -m $MODEL_NAME grafana site_url=https://grafana.${API_SERVER}.nip.io
277 juju config -m $MODEL_NAME prometheus site_url=https://prometheus.${API_SERVER}.nip.io
David Garciaef349d92020-12-10 21:16:12 +0100278
David Garcia49379ce2021-02-24 13:48:22 +0100279 echo "Waiting for deployment to finish..."
280 check_osm_deployed
David Garciac537fa62021-11-09 08:45:49 +0100281 grafana_leader=`juju status -m $MODEL_NAME grafana | grep "*" | cut -d "*" -f 1`
282 grafana_admin_password=`juju run -m $MODEL_NAME --unit $grafana_leader "echo \\$GF_SECURITY_ADMIN_PASSWORD"`
283 juju config -m $MODEL_NAME mon grafana_password=$grafana_admin_password
284 check_osm_deployed
David Garcia49379ce2021-02-24 13:48:22 +0100285 echo "OSM with charms deployed"
beierlm9afb0ef2020-10-16 12:53:51 -0400286}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100287
288function check_osm_deployed() {
beierlma4a37f72020-06-26 12:55:01 -0400289 TIME_TO_WAIT=600
290 start_time="$(date -u +%s)"
David Garcia02a5eb92020-11-28 14:41:22 +0100291 total_service_count=14
beierlm7e54dfc2020-09-24 15:27:53 -0400292 previous_count=0
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100293 while true
294 do
beierlm67a34472022-03-25 12:23:36 -0400295 service_count=$(juju status --format json -m $MODEL_NAME | jq '.applications[]."application-status".current' | grep active | wc -l)
beierlma4a37f72020-06-26 12:55:01 -0400296 echo "$service_count / $total_service_count services active"
297 if [ $service_count -eq $total_service_count ]; then
298 break
299 fi
beierlm7e54dfc2020-09-24 15:27:53 -0400300 if [ $service_count -ne $previous_count ]; then
301 previous_count=$service_count
302 start_time="$(date -u +%s)"
303 fi
beierlma4a37f72020-06-26 12:55:01 -0400304 now="$(date -u +%s)"
305 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
306 echo "Timed out waiting for OSM services to become ready"
beierlmce7b4602022-04-13 08:19:40 -0400307 FATAL_TRACK deploy_osm "Timed out waiting for services to become ready"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100308 fi
309 sleep 10
310 done
311}
312
aticig6eb39282022-02-24 00:34:52 +0300313function generate_password_overlay() {
314 # prometheus
David Garcia8fa78072022-04-27 13:43:42 +0200315 web_config_password=`openssl rand -hex 16`
aticig6eb39282022-02-24 00:34:52 +0300316 # keystone
David Garcia8fa78072022-04-27 13:43:42 +0200317 keystone_db_password=`openssl rand -hex 16`
318 keystone_admin_password=`openssl rand -hex 16`
319 keystone_service_password=`openssl rand -hex 16`
aticig6eb39282022-02-24 00:34:52 +0300320 # mariadb
321 mariadb_password=`openssl rand -hex 16`
322 mariadb_root_password=`openssl rand -hex 16`
323 cat << EOF > /tmp/password-overlay.yaml
324applications:
325 prometheus:
326 options:
327 web_config_password: $web_config_password
328 keystone:
329 options:
330 keystone-db-password: $keystone_db_password
331 admin-password: $keystone_admin_password
332 service-password: $keystone_service_password
333 mariadb:
334 options:
335 password: $mariadb_password
336 root_password: $mariadb_root_password
337EOF
338 mv /tmp/password-overlay.yaml $PASSWORD_OVERLAY_FILE
339}
340
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100341function create_overlay() {
David Garcia42375212020-04-27 19:07:49 +0200342 sudo snap install jq
David Garciac8660ad2020-12-16 13:13:20 +0100343 sudo snap install yq
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100344 local HOME=/home/$USER
calvinosanc183ea27a2021-01-04 11:42:18 +0100345 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
David Garciac753dc52021-03-17 15:28:47 +0100346 local vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
calvinosanc183ea27a2021-01-04 11:42:18 +0100347 local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
348 local vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 2)
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100349 local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
350 local vca_cloud="lxd-cloud"
351 # Get the VCA Certificate
calvinosanc183ea27a2021-01-04 11:42:18 +0100352 local vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.ca-cert - | base64 | tr -d \\n)
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100353
354 # Calculate the default route of this machine
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200355 local DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100356
357 # Generate a new overlay.yaml, overriding any existing one
David Garcia42375212020-04-27 19:07:49 +0200358 cat << EOF > /tmp/vca-overlay.yaml
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100359applications:
David Garcia49379ce2021-02-24 13:48:22 +0100360 lcm:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100361 options:
362 vca_user: $vca_user
David Garciac753dc52021-03-17 15:28:47 +0100363 vca_secret: $vca_secret
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100364 vca_host: $vca_host
365 vca_port: $vca_port
366 vca_pubkey: $vca_pubkey
367 vca_cacert: $vca_cacert
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100368 vca_cloud: $vca_cloud
beierlma4a37f72020-06-26 12:55:01 -0400369 vca_k8s_cloud: $K8S_CLOUD_NAME
David Garcia49379ce2021-02-24 13:48:22 +0100370 mon:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100371 options:
372 vca_user: $vca_user
David Garciac753dc52021-03-17 15:28:47 +0100373 vca_secret: $vca_secret
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100374 vca_host: $vca_host
375 vca_cacert: $vca_cacert
376EOF
David Garcia42375212020-04-27 19:07:49 +0200377 mv /tmp/vca-overlay.yaml ~/.osm/
378 OSM_VCA_HOST=$vca_host
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100379}
380
David Garcia69388c22020-05-07 12:14:19 +0200381function generate_images_overlay(){
David Garcia49379ce2021-02-24 13:48:22 +0100382 echo "applications:" > /tmp/images-overlay.yaml
beierlmf2782c52020-11-05 17:04:05 -0500383
David Garcia53a09d82022-02-01 16:39:44 +0100384 charms_with_resources="nbi lcm mon pol ng-ui ro pla"
David Garcia49379ce2021-02-24 13:48:22 +0100385 for charm in $charms_with_resources; do
386 cat << EOF > /tmp/${charm}_registry.yaml
387registrypath: ${REGISTRY_URL}opensourcemano/${charm}:$TAG
beierlmf2782c52020-11-05 17:04:05 -0500388EOF
David Garcia49379ce2021-02-24 13:48:22 +0100389 if [ ! -z "$REGISTRY_USERNAME" ] ; then
390 echo username: $REGISTRY_USERNAME >> /tmp/${charm}_registry.yaml
391 echo password: $REGISTRY_PASSWORD >> /tmp/${charm}_registry.yaml
392 fi
beierlmf2782c52020-11-05 17:04:05 -0500393
David Garcia49379ce2021-02-24 13:48:22 +0100394 cat << EOF >> /tmp/images-overlay.yaml
395 ${charm}:
David Garciaef349d92020-12-10 21:16:12 +0100396 resources:
David Garcia49379ce2021-02-24 13:48:22 +0100397 image: /tmp/${charm}_registry.yaml
398
David Garcia69388c22020-05-07 12:14:19 +0200399EOF
David Garcia49379ce2021-02-24 13:48:22 +0100400 done
David Garcia53a09d82022-02-01 16:39:44 +0100401 ch_charms_with_resources="keystone"
402 for charm in $ch_charms_with_resources; do
403 cat << EOF > /tmp/${charm}_registry.yaml
404registrypath: ${REGISTRY_URL}opensourcemano/${charm}:$TAG
405EOF
406 if [ ! -z "$REGISTRY_USERNAME" ] ; then
407 echo username: $REGISTRY_USERNAME >> /tmp/${charm}_registry.yaml
408 echo password: $REGISTRY_PASSWORD >> /tmp/${charm}_registry.yaml
409 fi
410
411 cat << EOF >> /tmp/images-overlay.yaml
412 ${charm}:
413 resources:
414 ${charm}-image: /tmp/${charm}_registry.yaml
415
416EOF
417 done
David Garcia49379ce2021-02-24 13:48:22 +0100418
David Garcia69388c22020-05-07 12:14:19 +0200419 mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE
420}
421
David Garcia107010b2021-01-15 12:58:59 +0100422function refresh_osmclient_snap() {
423 osmclient_snap_install_refresh refresh
424}
425
426function install_osm_client_snap() {
427 osmclient_snap_install_refresh install
428}
429
430function osmclient_snap_install_refresh() {
431 channel_preference="stable candidate beta edge"
432 for channel in $channel_preference; do
433 echo "Trying to install osmclient from channel $OSMCLIENT_VERSION/$channel"
434 sudo snap $1 osmclient --channel $OSMCLIENT_VERSION/$channel 2> /dev/null && echo osmclient snap installed && break
435 done
436}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100437function install_osmclient() {
David Garcia107010b2021-01-15 12:58:59 +0100438 snap info osmclient | grep -E ^installed: && refresh_osmclient_snap || install_osm_client_snap
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100439}
440
beierlmdf6de3d2020-12-16 08:46:40 -0500441function add_local_k8scluster() {
442 osm --all-projects vim-create \
443 --name _system-osm-vim \
444 --account_type dummy \
445 --auth_url http://dummy \
446 --user osm --password osm --tenant osm \
447 --description "dummy" \
448 --config '{management_network_name: mgmt}'
449 tmpfile=$(mktemp --tmpdir=${HOME})
450 cp ${KUBECONFIG} ${tmpfile}
451 osm --all-projects k8scluster-add \
452 --creds ${tmpfile} \
453 --vim _system-osm-vim \
454 --k8s-nets '{"net1": null}' \
455 --version '1.19' \
456 --description "OSM Internal Cluster" \
457 _system-osm-k8s
458 rm -f ${tmpfile}
459}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100460
461function install_microstack() {
beierlmb403f062021-12-09 08:47:59 -0500462 sudo snap install microstack --beta --devmode
463
464 CHECK=$(microstack.openstack server list)
465 if [ $? -ne 0 ] ; then
466 if [[ $CHECK == *"not initialized"* ]]; then
467 echo "Setting MicroStack dashboard to listen to port 8080"
468 sudo snap set microstack config.network.ports.dashboard=8080
469 echo "Initializing MicroStack. This can take several minutes"
470 sudo microstack.init --auto --control
471 fi
472 fi
473
474 sudo snap alias microstack.openstack openstack
475
476 echo "Updating default security group in MicroStack to allow all access"
477
478 for i in $(microstack.openstack security group list | awk '/default/{ print $2 }'); do
479 for PROTO in icmp tcp udp ; do
480 echo " $PROTO ingress"
481 CHECK=$(microstack.openstack security group rule create $i --protocol $PROTO --remote-ip 0.0.0.0/0 2>&1)
482 if [ $? -ne 0 ] ; then
483 if [[ $CHECK != *"409"* ]]; then
484 echo "Error creating ingress rule for $PROTO"
485 echo $CHECK
486 fi
487 fi
488 done
489 done
490
491 microstack.openstack network show osm-ext &>/dev/null
492 if [ $? -ne 0 ]; then
493 echo "Creating osm-ext network with router to bridge to MicroStack external network"
494 microstack.openstack network create --enable --no-share osm-ext
495 microstack.openstack subnet create osm-ext-subnet --network osm-ext --dns-nameserver 8.8.8.8 \
496 --subnet-range 172.30.0.0/24
497 microstack.openstack router create external-router
498 microstack.openstack router add subnet external-router osm-ext-subnet
499 microstack.openstack router set --external-gateway external external-router
500 fi
501
502 microstack.openstack image list | grep ubuntu20.04 &> /dev/null
503 if [ $? -ne 0 ] ; then
504 echo "Fetching Ubuntu 20.04 image and upLoading to MicroStack"
505 wget -q -O- https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img \
506 | microstack.openstack image create --public --container-format=bare \
507 --disk-format=qcow2 ubuntu20.04 | grep status
508 fi
509
510 if [ ! -f ~/.ssh/microstack ]; then
511 ssh-keygen -t rsa -N "" -f ~/.ssh/microstack
512 microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack
513 fi
514
515 echo "Creating VIM microstack-site in OSM"
516 . /var/snap/microstack/common/etc/microstack.rc
517
518 osm vim-create \
519 --name microstack-site \
520 --user "$OS_USERNAME" \
521 --password "$OS_PASSWORD" \
522 --auth_url "$OS_AUTH_URL" \
523 --tenant "$OS_USERNAME" \
524 --account_type openstack \
525 --config='{use_floating_ip: True,
526 insecure: True,
527 keypair: microstack,
528 management_network_name: osm-ext}'
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100529}
530
beierlm4aad47b2021-12-07 16:15:53 -0500531DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5; exit}'`
532DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]; exit}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100533
534check_arguments $@
David Garcia42375212020-04-27 19:07:49 +0200535mkdir -p ~/.osm
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100536install_snaps
537bootstrap_k8s_lxd
David Garcia1bc71022021-05-03 18:27:18 +0200538create_overlay
539if [ -v ONLY_VCA ]; then
540 HOME=/home/$USER
541 vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
542 vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
543 vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
544 vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 2)
545 vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
546 vca_cloud="lxd-cloud"
547 vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.ca-cert - | base64 | tr -d \\n)
548 hostname=`cat /etc/hostname`
beierlmdf6de3d2020-12-16 08:46:40 -0500549
David Garcia1bc71022021-05-03 18:27:18 +0200550 echo "Use the following command to register the installed VCA to your OSM:"
David Garciad68e0b42021-06-28 16:50:42 +0200551 echo -e " osm vca-add --endpoints $vca_host:$vca_port \\\n --user $vca_user \\\n --secret $vca_secret \\\n --cacert $vca_cacert \\\n --lxd-cloud lxd-cloud \\\n --lxd-credentials lxd-cloud \\\n --k8s-cloud microk8s \\\n --k8s-credentials microk8s\\\n $hostname-vca"
beierlmce7b4602022-04-13 08:19:40 -0400552 track deploy_osm deploy_vca_only_ok
David Garcia1bc71022021-05-03 18:27:18 +0200553else
554 deploy_charmed_osm
beierlmce7b4602022-04-13 08:19:40 -0400555 track deploy_osm deploy_osm_services_k8s_ok
David Garcia1bc71022021-05-03 18:27:18 +0200556 install_osmclient
beierlmce7b4602022-04-13 08:19:40 -0400557 track osmclient osmclient_ok
David Garcia1bc71022021-05-03 18:27:18 +0200558 export OSM_HOSTNAME=$(juju config -m $MODEL_NAME nbi site_url | sed "s/http.*\?:\/\///"):443
aticig6eb39282022-02-24 00:34:52 +0300559 export OSM_PASSWORD=$keystone_admin_password
David Garcia1bc71022021-05-03 18:27:18 +0200560 sleep 10
561 add_local_k8scluster
beierlmce7b4602022-04-13 08:19:40 -0400562 track final_ops add_local_k8scluster_ok
David Garcia1bc71022021-05-03 18:27:18 +0200563 if [ -v MICROSTACK ]; then
564 install_microstack
beierlmce7b4602022-04-13 08:19:40 -0400565 track final_ops install_microstack_ok
David Garcia1bc71022021-05-03 18:27:18 +0200566 fi
567
568 echo "Your installation is now complete, follow these steps for configuring the osmclient:"
569 echo
570 echo "1. Create the OSM_HOSTNAME environment variable with the NBI IP"
571 echo
572 echo "export OSM_HOSTNAME=$OSM_HOSTNAME"
aticig6eb39282022-02-24 00:34:52 +0300573 echo "export OSM_PASSWORD=$OSM_PASSWORD"
David Garcia1bc71022021-05-03 18:27:18 +0200574 echo
aticig6eb39282022-02-24 00:34:52 +0300575 echo "2. Add the previous commands to your .bashrc for other Shell sessions"
David Garcia1bc71022021-05-03 18:27:18 +0200576 echo
577 echo "echo \"export OSM_HOSTNAME=$OSM_HOSTNAME\" >> ~/.bashrc"
aticig6eb39282022-02-24 00:34:52 +0300578 echo "echo \"export OSM_PASSWORD=$OSM_PASSWORD\" >> ~/.bashrc"
579 echo
580 echo "3. Login OSM GUI by using admin password: $OSM_PASSWORD"
David Garcia1bc71022021-05-03 18:27:18 +0200581 echo
582 echo "DONE"
beierlmce7b4602022-04-13 08:19:40 -0400583 track end
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100584fi
beierlma4a37f72020-06-26 12:55:01 -0400585