blob: 24272a17e2a8fd0391c41b2814f81f3a0c6a29b7 [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
David Garcia49a203d2022-05-06 13:17:42 +020020JUJU_AGENT_VERSION=2.9.29
David Garcia42375212020-04-27 19:07:49 +020021K8S_CLOUD_NAME="k8s-cloud"
beierlm3749e312020-07-02 14:21:09 -040022KUBECTL="microk8s.kubectl"
aticig1f2e2e92022-02-03 16:42:39 +030023MICROK8S_VERSION=1.23
David Garcia53a09d82022-02-01 16:39:44 +010024OSMCLIENT_VERSION=latest
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
44CHARMHUB_CHANNEL=latest/edge
45unset 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" ;;
54 --lxd) LXD_CLOUD="$2" ;;
55 --lxd-cred) LXD_CREDENTIALS="$2" ;;
David Garcia69388c22020-05-07 12:14:19 +020056 --microstack) MICROSTACK=y ;;
David Garciaab11f842020-12-16 17:25:15 +010057 --ha) BUNDLE=$OSM_HA_BUNDLE ;;
David Garcia69388c22020-05-07 12:14:19 +020058 --tag) TAG="$2" ;;
beierlmf2782c52020-11-05 17:04:05 -050059 --registry) REGISTRY_INFO="$2" ;;
David Garcia1bc71022021-05-03 18:27:18 +020060 --only-vca) ONLY_VCA=y ;;
David Garcia42375212020-04-27 19:07:49 +020061 esac
62 shift
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010063 done
64
David Garcia42375212020-04-27 19:07:49 +020065 # echo $BUNDLE $KUBECONFIG $LXDENDPOINT
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010066}
beierlma4a37f72020-06-26 12:55:01 -040067
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010068function install_snaps(){
beierlma4a37f72020-06-26 12:55:01 -040069 if [ ! -v KUBECFG ]; then
beierlmce7b4602022-04-13 08:19:40 -040070 KUBEGRP="microk8s"
71 sudo snap install microk8s --classic --channel=${MICROK8S_VERSION}/stable ||
72 FATAL_TRACK k8scluster "snap install microk8s ${MICROK8S_VERSION}/stable failed"
73 sudo usermod -a -G microk8s `whoami`
David Garcia9a59e3d2021-02-08 15:18:27 +010074 sudo cat /var/snap/microk8s/current/args/kube-apiserver | grep advertise-address || (
David Garcia107010b2021-01-15 12:58:59 +010075 echo "--advertise-address $DEFAULT_IP" | sudo tee -a /var/snap/microk8s/current/args/kube-apiserver
beierlmce7b4602022-04-13 08:19:40 -040076 sg ${KUBEGRP} -c microk8s.stop
77 sg ${KUBEGRP} -c microk8s.start
David Garcia107010b2021-01-15 12:58:59 +010078 )
beierlma4a37f72020-06-26 12:55:01 -040079 mkdir -p ~/.kube
80 sudo chown -f -R `whoami` ~/.kube
beierlmf2782c52020-11-05 17:04:05 -050081 sg ${KUBEGRP} -c "microk8s status --wait-ready"
beierlmdf6de3d2020-12-16 08:46:40 -050082 KUBECONFIG=~/.osm/microk8s-config.yaml
David Garcia107010b2021-01-15 12:58:59 +010083 sg ${KUBEGRP} -c "microk8s config" | tee ${KUBECONFIG}
beierlmce7b4602022-04-13 08:19:40 -040084 track k8scluster k8scluster_ok
beierlma4a37f72020-06-26 12:55:01 -040085 else
86 KUBECTL="kubectl"
87 sudo snap install kubectl --classic
88 export KUBECONFIG=${KUBECFG}
89 KUBEGRP=$(id -g -n)
90 fi
beierlmce7b4602022-04-13 08:19:40 -040091 sudo snap install juju --classic --channel=$JUJU_VERSION/stable ||
92 FATAL_TRACK juju "snap install juju ${JUJU_VERSION}/stable failed"
93 track juju juju_ok
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010094}
95
96function bootstrap_k8s_lxd(){
David Garcia42375212020-04-27 19:07:49 +020097 [ -v CONTROLLER ] && ADD_K8S_OPTS="--controller ${CONTROLLER}" && CONTROLLER_NAME=$CONTROLLER
beierlma4a37f72020-06-26 12:55:01 -040098 [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="osm-vca"
99
100 if [ -v BOOTSTRAP_NEEDED ]; then
101 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
102 if [ $CONTROLLER_PRESENT -ge 1 ]; then
103 cat << EOF
104Threre is already a VCA present with the installer reserved name of "${CONTROLLER_NAME}".
105You may either explicitly use this VCA with the "--vca ${CONTROLLER_NAME}" option, or remove it
106using this command:
107
108 juju destroy-controller --release-storage --destroy-all-models -y ${CONTROLLER_NAME}
109
110Please retry the installation once this conflict has been resolved.
111EOF
beierlmce7b4602022-04-13 08:19:40 -0400112 FATAL_TRACK bootstrap_k8s "VCA already present"
beierlma4a37f72020-06-26 12:55:01 -0400113 fi
beierlm9afb0ef2020-10-16 12:53:51 -0400114 else
115 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
116 if [ $CONTROLLER_PRESENT -le 0 ]; then
117 cat << EOF
118Threre is no VCA present with the name "${CONTROLLER_NAME}". Please specify a VCA
119that exists, or remove the --vca ${CONTROLLER_NAME} option.
120
121Please retry the installation with one of the solutions applied.
122EOF
beierlmce7b4602022-04-13 08:19:40 -0400123 FATAL_TRACK bootstrap_k8s "Requested VCA not present"
beierlm9afb0ef2020-10-16 12:53:51 -0400124 fi
beierlma4a37f72020-06-26 12:55:01 -0400125 fi
David Garcia42375212020-04-27 19:07:49 +0200126
127 if [ -v KUBECFG ]; then
128 cat $KUBECFG | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
beierlm2634cd32020-09-15 16:00:34 -0400129 [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME \
130 --config controller-service-type=loadbalancer \
David Garciaa1376012020-10-19 15:42:42 +0200131 --agent-version=$JUJU_AGENT_VERSION
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100132 else
beierlma4a37f72020-06-26 12:55:01 -0400133 sg ${KUBEGRP} -c "echo ${DEFAULT_IP}-${DEFAULT_IP} | microk8s.enable metallb"
beierlm9afb0ef2020-10-16 12:53:51 -0400134 sg ${KUBEGRP} -c "microk8s.enable ingress"
beierlma4a37f72020-06-26 12:55:01 -0400135 sg ${KUBEGRP} -c "microk8s.enable storage dns"
136 TIME_TO_WAIT=30
137 start_time="$(date -u +%s)"
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200138 while true
139 do
beierlma4a37f72020-06-26 12:55:01 -0400140 now="$(date -u +%s)"
141 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
142 echo "Microk8s storage failed to enable"
143 sg ${KUBEGRP} -c "microk8s.status"
beierlmce7b4602022-04-13 08:19:40 -0400144 FATAL_TRACK bootstrap_k8s "Microk8s storage failed to enable"
beierlma4a37f72020-06-26 12:55:01 -0400145 fi
146 storage_status=`sg ${KUBEGRP} -c "microk8s.status -a storage"`
147 if [[ $storage_status == "enabled" ]]; then
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200148 break
149 fi
150 sleep 1
151 done
David Garcia42375212020-04-27 19:07:49 +0200152
beierlma4a37f72020-06-26 12:55:01 -0400153 [ ! -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
beierlm2634cd32020-09-15 16:00:34 -0400154 [ -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c \
David Garciaa1376012020-10-19 15:42:42 +0200155 "juju bootstrap microk8s $CONTROLLER_NAME --config controller-service-type=loadbalancer --agent-version=$JUJU_AGENT_VERSION" \
beierlm2634cd32020-09-15 16:00:34 -0400156 && K8S_CLOUD_NAME=microk8s
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100157 fi
beierlmce7b4602022-04-13 08:19:40 -0400158 track bootstrap_k8s bootstrap_k8s_ok
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100159
David Garcia42375212020-04-27 19:07:49 +0200160 if [ -v LXD_CLOUD ]; then
161 if [ ! -v LXD_CREDENTIALS ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100162 echo "The installer needs the LXD server certificate if the LXD is external"
beierlmce7b4602022-04-13 08:19:40 -0400163 FATAL_TRACK bootstrap_lxd "No LXD certificate supplied"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100164 fi
165 else
166 LXDENDPOINT=$DEFAULT_IP
David Garcia42375212020-04-27 19:07:49 +0200167 LXD_CLOUD=~/.osm/lxd-cloud.yaml
168 LXD_CREDENTIALS=~/.osm/lxd-credentials.yaml
169 # Apply sysctl production values for optimal performance
170 sudo cp /usr/share/osm-devops/installers/60-lxd-production.conf /etc/sysctl.d/60-lxd-production.conf
171 sudo sysctl --system
172 # Install LXD snap
173 sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client
David Garcia956fcb72021-09-09 17:23:42 +0200174 sudo snap install lxd --channel $LXD_VERSION/stable
David Garcia42375212020-04-27 19:07:49 +0200175 # Configure LXD
176 sudo usermod -a -G lxd `whoami`
177 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"
178 sg lxd -c "lxd waitready"
179 DEFAULT_MTU=$(ip addr show $DEFAULT_IF | perl -ne 'if (/mtu\s(\d+)/) {print $1;}')
180 sg lxd -c "lxc profile device set default eth0 mtu $DEFAULT_MTU"
David Garciad00e49c2020-06-19 10:33:37 +0200181 sg lxd -c "lxc network set lxdbr0 bridge.mtu $DEFAULT_MTU"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100182
David Garcia42375212020-04-27 19:07:49 +0200183 cat << EOF > $LXD_CLOUD
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100184clouds:
185 lxd-cloud:
186 type: lxd
187 auth-types: [certificate]
188 endpoint: "https://$LXDENDPOINT:8443"
189 config:
190 ssl-hostname-verification: false
191EOF
David Garcia42375212020-04-27 19:07:49 +0200192 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"
193 local server_cert=`cat /var/snap/lxd/common/lxd/server.crt | sed 's/^/ /'`
194 local client_cert=`cat ~/.osm/client.crt | sed 's/^/ /'`
195 local client_key=`cat ~/.osm/client.key | sed 's/^/ /'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100196
David Garcia42375212020-04-27 19:07:49 +0200197 cat << EOF > $LXD_CREDENTIALS
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100198credentials:
199 lxd-cloud:
David Garcia42375212020-04-27 19:07:49 +0200200 lxd-cloud:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100201 auth-type: certificate
202 server-cert: |
203$server_cert
204 client-cert: |
205$client_cert
206 client-key: |
207$client_key
208EOF
David Garcia42375212020-04-27 19:07:49 +0200209 lxc config trust add local: ~/.osm/client.crt
210 fi
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100211
David Garcia42375212020-04-27 19:07:49 +0200212 juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force
213 juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS
214 sg lxd -c "lxd waitready"
beierlma4a37f72020-06-26 12:55:01 -0400215 juju controller-config features=[k8s-operators]
beierlmce7b4602022-04-13 08:19:40 -0400216 track bootstrap_lxd bootstrap_lxd_ok
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100217}
218
219function deploy_charmed_osm(){
beierlmf2782c52020-11-05 17:04:05 -0500220 if [ -v REGISTRY_INFO ] ; then
221 registry_parts=(${REGISTRY_INFO//@/ })
222 if [ ${#registry_parts[@]} -eq 1 ] ; then
223 # No credentials supplied
224 REGISTRY_USERNAME=""
225 REGISTRY_PASSWORD=""
226 REGISTRY_URL=${registry_parts[0]}
227 else
228 credentials=${registry_parts[0]}
229 credential_parts=(${credentials//:/ })
230 REGISTRY_USERNAME=${credential_parts[0]}
231 REGISTRY_PASSWORD=${credential_parts[1]}
232 REGISTRY_URL=${registry_parts[1]}
233 fi
234 # Ensure the URL ends with a /
235 case $REGISTRY_URL in
236 */) ;;
237 *) REGISTRY_URL=${REGISTRY_URL}/
238 esac
239 fi
240
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100241 echo "Creating OSM model"
David Garcia42375212020-04-27 19:07:49 +0200242 if [ -v KUBECFG ]; then
David Garciaef349d92020-12-10 21:16:12 +0100243 juju add-model $MODEL_NAME $K8S_CLOUD_NAME
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100244 else
David Garciaef349d92020-12-10 21:16:12 +0100245 sg ${KUBEGRP} -c "juju add-model $MODEL_NAME $K8S_CLOUD_NAME"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100246 fi
247 echo "Deploying OSM with charms"
beierlma4a37f72020-06-26 12:55:01 -0400248 images_overlay=""
beierlmf2782c52020-11-05 17:04:05 -0500249 if [ -v REGISTRY_URL ]; then
250 [ ! -v TAG ] && TAG='latest'
251 fi
beierlma4a37f72020-06-26 12:55:01 -0400252 [ -v TAG ] && generate_images_overlay && images_overlay="--overlay $IMAGES_OVERLAY_FILE"
beierlmf2782c52020-11-05 17:04:05 -0500253
David Garcia4bd2bf02021-05-05 19:01:43 +0200254 if [ -v OVERLAY ]; then
255 extra_overlay="--overlay $OVERLAY"
256 fi
aticig6eb39282022-02-24 00:34:52 +0300257 echo "Creating Password Overlay"
258
259 generate_password_overlay && secret_overlay="--overlay $PASSWORD_OVERLAY_FILE"
David Garcia4bd2bf02021-05-05 19:01:43 +0200260
David Garcia42375212020-04-27 19:07:49 +0200261 if [ -v BUNDLE ]; then
aticig6eb39282022-02-24 00:34:52 +0300262 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 +0100263 else
aticig6eb39282022-02-24 00:34:52 +0300264 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 +0100265 fi
beierlma4a37f72020-06-26 12:55:01 -0400266
beierlma4a37f72020-06-26 12:55:01 -0400267 if [ ! -v KUBECFG ]; then
beierlma4a37f72020-06-26 12:55:01 -0400268 API_SERVER=${DEFAULT_IP}
269 else
270 API_SERVER=$(kubectl config view --minify | grep server | cut -f 2- -d ":" | tr -d " ")
271 proto="$(echo $API_SERVER | grep :// | sed -e's,^\(.*://\).*,\1,g')"
272 url="$(echo ${API_SERVER/$proto/})"
273 user="$(echo $url | grep @ | cut -d@ -f1)"
274 hostport="$(echo ${url/$user@/} | cut -d/ -f1)"
275 API_SERVER="$(echo $hostport | sed -e 's,:.*,,g')"
276 fi
David Garciade5fc1d2020-09-02 11:40:12 +0200277 # Expose OSM services
David Garciac395a452021-05-05 14:22:26 +0200278 juju config -m $MODEL_NAME nbi site_url=https://nbi.${API_SERVER}.nip.io
279 juju config -m $MODEL_NAME ng-ui site_url=https://ui.${API_SERVER}.nip.io
280 juju config -m $MODEL_NAME grafana site_url=https://grafana.${API_SERVER}.nip.io
281 juju config -m $MODEL_NAME prometheus site_url=https://prometheus.${API_SERVER}.nip.io
David Garciaef349d92020-12-10 21:16:12 +0100282
David Garcia49379ce2021-02-24 13:48:22 +0100283 echo "Waiting for deployment to finish..."
284 check_osm_deployed
David Garciac537fa62021-11-09 08:45:49 +0100285 grafana_leader=`juju status -m $MODEL_NAME grafana | grep "*" | cut -d "*" -f 1`
286 grafana_admin_password=`juju run -m $MODEL_NAME --unit $grafana_leader "echo \\$GF_SECURITY_ADMIN_PASSWORD"`
287 juju config -m $MODEL_NAME mon grafana_password=$grafana_admin_password
288 check_osm_deployed
David Garcia49379ce2021-02-24 13:48:22 +0100289 echo "OSM with charms deployed"
beierlm9afb0ef2020-10-16 12:53:51 -0400290}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100291
292function check_osm_deployed() {
beierlma4a37f72020-06-26 12:55:01 -0400293 TIME_TO_WAIT=600
294 start_time="$(date -u +%s)"
David Garcia02a5eb92020-11-28 14:41:22 +0100295 total_service_count=14
beierlm7e54dfc2020-09-24 15:27:53 -0400296 previous_count=0
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100297 while true
298 do
beierlm67a34472022-03-25 12:23:36 -0400299 service_count=$(juju status --format json -m $MODEL_NAME | jq '.applications[]."application-status".current' | grep active | wc -l)
beierlma4a37f72020-06-26 12:55:01 -0400300 echo "$service_count / $total_service_count services active"
301 if [ $service_count -eq $total_service_count ]; then
302 break
303 fi
beierlm7e54dfc2020-09-24 15:27:53 -0400304 if [ $service_count -ne $previous_count ]; then
305 previous_count=$service_count
306 start_time="$(date -u +%s)"
307 fi
beierlma4a37f72020-06-26 12:55:01 -0400308 now="$(date -u +%s)"
309 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
310 echo "Timed out waiting for OSM services to become ready"
beierlmce7b4602022-04-13 08:19:40 -0400311 FATAL_TRACK deploy_osm "Timed out waiting for services to become ready"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100312 fi
313 sleep 10
314 done
315}
316
aticig6eb39282022-02-24 00:34:52 +0300317function generate_password_overlay() {
318 # prometheus
319 web_config_password=`openssl rand -base64 16`
320 # keystone
321 keystone_db_password=`openssl rand -base64 16`
322 keystone_admin_password=`openssl rand -base64 16`
323 keystone_service_password=`openssl rand -base64 16`
324 # mariadb
325 mariadb_password=`openssl rand -hex 16`
326 mariadb_root_password=`openssl rand -hex 16`
327 cat << EOF > /tmp/password-overlay.yaml
328applications:
329 prometheus:
330 options:
331 web_config_password: $web_config_password
332 keystone:
333 options:
334 keystone-db-password: $keystone_db_password
335 admin-password: $keystone_admin_password
336 service-password: $keystone_service_password
337 mariadb:
338 options:
339 password: $mariadb_password
340 root_password: $mariadb_root_password
341EOF
342 mv /tmp/password-overlay.yaml $PASSWORD_OVERLAY_FILE
343}
344
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100345function create_overlay() {
David Garcia42375212020-04-27 19:07:49 +0200346 sudo snap install jq
David Garciac8660ad2020-12-16 13:13:20 +0100347 sudo snap install yq
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100348 local HOME=/home/$USER
calvinosanc183ea27a2021-01-04 11:42:18 +0100349 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
David Garciac753dc52021-03-17 15:28:47 +0100350 local vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
calvinosanc183ea27a2021-01-04 11:42:18 +0100351 local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
352 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 +0100353 local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
354 local vca_cloud="lxd-cloud"
355 # Get the VCA Certificate
calvinosanc183ea27a2021-01-04 11:42:18 +0100356 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 +0100357
358 # Calculate the default route of this machine
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200359 local DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100360
361 # Generate a new overlay.yaml, overriding any existing one
David Garcia42375212020-04-27 19:07:49 +0200362 cat << EOF > /tmp/vca-overlay.yaml
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100363applications:
David Garcia49379ce2021-02-24 13:48:22 +0100364 lcm:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100365 options:
366 vca_user: $vca_user
David Garciac753dc52021-03-17 15:28:47 +0100367 vca_secret: $vca_secret
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100368 vca_host: $vca_host
369 vca_port: $vca_port
370 vca_pubkey: $vca_pubkey
371 vca_cacert: $vca_cacert
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100372 vca_cloud: $vca_cloud
beierlma4a37f72020-06-26 12:55:01 -0400373 vca_k8s_cloud: $K8S_CLOUD_NAME
David Garcia49379ce2021-02-24 13:48:22 +0100374 mon:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100375 options:
376 vca_user: $vca_user
David Garciac753dc52021-03-17 15:28:47 +0100377 vca_secret: $vca_secret
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100378 vca_host: $vca_host
379 vca_cacert: $vca_cacert
380EOF
David Garcia42375212020-04-27 19:07:49 +0200381 mv /tmp/vca-overlay.yaml ~/.osm/
382 OSM_VCA_HOST=$vca_host
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100383}
384
David Garcia69388c22020-05-07 12:14:19 +0200385function generate_images_overlay(){
David Garcia49379ce2021-02-24 13:48:22 +0100386 echo "applications:" > /tmp/images-overlay.yaml
beierlmf2782c52020-11-05 17:04:05 -0500387
David Garcia53a09d82022-02-01 16:39:44 +0100388 charms_with_resources="nbi lcm mon pol ng-ui ro pla"
David Garcia49379ce2021-02-24 13:48:22 +0100389 for charm in $charms_with_resources; do
390 cat << EOF > /tmp/${charm}_registry.yaml
391registrypath: ${REGISTRY_URL}opensourcemano/${charm}:$TAG
beierlmf2782c52020-11-05 17:04:05 -0500392EOF
David Garcia49379ce2021-02-24 13:48:22 +0100393 if [ ! -z "$REGISTRY_USERNAME" ] ; then
394 echo username: $REGISTRY_USERNAME >> /tmp/${charm}_registry.yaml
395 echo password: $REGISTRY_PASSWORD >> /tmp/${charm}_registry.yaml
396 fi
beierlmf2782c52020-11-05 17:04:05 -0500397
David Garcia49379ce2021-02-24 13:48:22 +0100398 cat << EOF >> /tmp/images-overlay.yaml
399 ${charm}:
David Garciaef349d92020-12-10 21:16:12 +0100400 resources:
David Garcia49379ce2021-02-24 13:48:22 +0100401 image: /tmp/${charm}_registry.yaml
402
David Garcia69388c22020-05-07 12:14:19 +0200403EOF
David Garcia49379ce2021-02-24 13:48:22 +0100404 done
David Garcia53a09d82022-02-01 16:39:44 +0100405 ch_charms_with_resources="keystone"
406 for charm in $ch_charms_with_resources; do
407 cat << EOF > /tmp/${charm}_registry.yaml
408registrypath: ${REGISTRY_URL}opensourcemano/${charm}:$TAG
409EOF
410 if [ ! -z "$REGISTRY_USERNAME" ] ; then
411 echo username: $REGISTRY_USERNAME >> /tmp/${charm}_registry.yaml
412 echo password: $REGISTRY_PASSWORD >> /tmp/${charm}_registry.yaml
413 fi
414
415 cat << EOF >> /tmp/images-overlay.yaml
416 ${charm}:
417 resources:
418 ${charm}-image: /tmp/${charm}_registry.yaml
419
420EOF
421 done
David Garcia49379ce2021-02-24 13:48:22 +0100422
David Garcia69388c22020-05-07 12:14:19 +0200423 mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE
424}
425
David Garcia107010b2021-01-15 12:58:59 +0100426function refresh_osmclient_snap() {
427 osmclient_snap_install_refresh refresh
428}
429
430function install_osm_client_snap() {
431 osmclient_snap_install_refresh install
432}
433
434function osmclient_snap_install_refresh() {
435 channel_preference="stable candidate beta edge"
436 for channel in $channel_preference; do
437 echo "Trying to install osmclient from channel $OSMCLIENT_VERSION/$channel"
438 sudo snap $1 osmclient --channel $OSMCLIENT_VERSION/$channel 2> /dev/null && echo osmclient snap installed && break
439 done
440}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100441function install_osmclient() {
David Garcia107010b2021-01-15 12:58:59 +0100442 snap info osmclient | grep -E ^installed: && refresh_osmclient_snap || install_osm_client_snap
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100443}
444
beierlmdf6de3d2020-12-16 08:46:40 -0500445function add_local_k8scluster() {
446 osm --all-projects vim-create \
447 --name _system-osm-vim \
448 --account_type dummy \
449 --auth_url http://dummy \
450 --user osm --password osm --tenant osm \
451 --description "dummy" \
452 --config '{management_network_name: mgmt}'
453 tmpfile=$(mktemp --tmpdir=${HOME})
454 cp ${KUBECONFIG} ${tmpfile}
455 osm --all-projects k8scluster-add \
456 --creds ${tmpfile} \
457 --vim _system-osm-vim \
458 --k8s-nets '{"net1": null}' \
459 --version '1.19' \
460 --description "OSM Internal Cluster" \
461 _system-osm-k8s
462 rm -f ${tmpfile}
463}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100464
465function install_microstack() {
beierlmb403f062021-12-09 08:47:59 -0500466 sudo snap install microstack --beta --devmode
467
468 CHECK=$(microstack.openstack server list)
469 if [ $? -ne 0 ] ; then
470 if [[ $CHECK == *"not initialized"* ]]; then
471 echo "Setting MicroStack dashboard to listen to port 8080"
472 sudo snap set microstack config.network.ports.dashboard=8080
473 echo "Initializing MicroStack. This can take several minutes"
474 sudo microstack.init --auto --control
475 fi
476 fi
477
478 sudo snap alias microstack.openstack openstack
479
480 echo "Updating default security group in MicroStack to allow all access"
481
482 for i in $(microstack.openstack security group list | awk '/default/{ print $2 }'); do
483 for PROTO in icmp tcp udp ; do
484 echo " $PROTO ingress"
485 CHECK=$(microstack.openstack security group rule create $i --protocol $PROTO --remote-ip 0.0.0.0/0 2>&1)
486 if [ $? -ne 0 ] ; then
487 if [[ $CHECK != *"409"* ]]; then
488 echo "Error creating ingress rule for $PROTO"
489 echo $CHECK
490 fi
491 fi
492 done
493 done
494
495 microstack.openstack network show osm-ext &>/dev/null
496 if [ $? -ne 0 ]; then
497 echo "Creating osm-ext network with router to bridge to MicroStack external network"
498 microstack.openstack network create --enable --no-share osm-ext
499 microstack.openstack subnet create osm-ext-subnet --network osm-ext --dns-nameserver 8.8.8.8 \
500 --subnet-range 172.30.0.0/24
501 microstack.openstack router create external-router
502 microstack.openstack router add subnet external-router osm-ext-subnet
503 microstack.openstack router set --external-gateway external external-router
504 fi
505
506 microstack.openstack image list | grep ubuntu20.04 &> /dev/null
507 if [ $? -ne 0 ] ; then
508 echo "Fetching Ubuntu 20.04 image and upLoading to MicroStack"
509 wget -q -O- https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img \
510 | microstack.openstack image create --public --container-format=bare \
511 --disk-format=qcow2 ubuntu20.04 | grep status
512 fi
513
514 if [ ! -f ~/.ssh/microstack ]; then
515 ssh-keygen -t rsa -N "" -f ~/.ssh/microstack
516 microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack
517 fi
518
519 echo "Creating VIM microstack-site in OSM"
520 . /var/snap/microstack/common/etc/microstack.rc
521
522 osm vim-create \
523 --name microstack-site \
524 --user "$OS_USERNAME" \
525 --password "$OS_PASSWORD" \
526 --auth_url "$OS_AUTH_URL" \
527 --tenant "$OS_USERNAME" \
528 --account_type openstack \
529 --config='{use_floating_ip: True,
530 insecure: True,
531 keypair: microstack,
532 management_network_name: osm-ext}'
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100533}
534
beierlm4aad47b2021-12-07 16:15:53 -0500535DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5; exit}'`
536DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]; exit}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100537
538check_arguments $@
David Garcia42375212020-04-27 19:07:49 +0200539mkdir -p ~/.osm
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100540install_snaps
541bootstrap_k8s_lxd
David Garcia1bc71022021-05-03 18:27:18 +0200542create_overlay
543if [ -v ONLY_VCA ]; then
544 HOME=/home/$USER
545 vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
546 vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
547 vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
548 vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 2)
549 vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
550 vca_cloud="lxd-cloud"
551 vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.ca-cert - | base64 | tr -d \\n)
552 hostname=`cat /etc/hostname`
beierlmdf6de3d2020-12-16 08:46:40 -0500553
David Garcia1bc71022021-05-03 18:27:18 +0200554 echo "Use the following command to register the installed VCA to your OSM:"
David Garciad68e0b42021-06-28 16:50:42 +0200555 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 -0400556 track deploy_osm deploy_vca_only_ok
David Garcia1bc71022021-05-03 18:27:18 +0200557else
558 deploy_charmed_osm
beierlmce7b4602022-04-13 08:19:40 -0400559 track deploy_osm deploy_osm_services_k8s_ok
David Garcia1bc71022021-05-03 18:27:18 +0200560 install_osmclient
beierlmce7b4602022-04-13 08:19:40 -0400561 track osmclient osmclient_ok
David Garcia1bc71022021-05-03 18:27:18 +0200562 export OSM_HOSTNAME=$(juju config -m $MODEL_NAME nbi site_url | sed "s/http.*\?:\/\///"):443
aticig6eb39282022-02-24 00:34:52 +0300563 export OSM_PASSWORD=$keystone_admin_password
David Garcia1bc71022021-05-03 18:27:18 +0200564 sleep 10
565 add_local_k8scluster
beierlmce7b4602022-04-13 08:19:40 -0400566 track final_ops add_local_k8scluster_ok
David Garcia1bc71022021-05-03 18:27:18 +0200567 if [ -v MICROSTACK ]; then
568 install_microstack
beierlmce7b4602022-04-13 08:19:40 -0400569 track final_ops install_microstack_ok
David Garcia1bc71022021-05-03 18:27:18 +0200570 fi
571
572 echo "Your installation is now complete, follow these steps for configuring the osmclient:"
573 echo
574 echo "1. Create the OSM_HOSTNAME environment variable with the NBI IP"
575 echo
576 echo "export OSM_HOSTNAME=$OSM_HOSTNAME"
aticig6eb39282022-02-24 00:34:52 +0300577 echo "export OSM_PASSWORD=$OSM_PASSWORD"
David Garcia1bc71022021-05-03 18:27:18 +0200578 echo
aticig6eb39282022-02-24 00:34:52 +0300579 echo "2. Add the previous commands to your .bashrc for other Shell sessions"
David Garcia1bc71022021-05-03 18:27:18 +0200580 echo
581 echo "echo \"export OSM_HOSTNAME=$OSM_HOSTNAME\" >> ~/.bashrc"
aticig6eb39282022-02-24 00:34:52 +0300582 echo "echo \"export OSM_PASSWORD=$OSM_PASSWORD\" >> ~/.bashrc"
583 echo
584 echo "3. Login OSM GUI by using admin password: $OSM_PASSWORD"
David Garcia1bc71022021-05-03 18:27:18 +0200585 echo
586 echo "DONE"
beierlmce7b4602022-04-13 08:19:40 -0400587 track end
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100588fi
beierlma4a37f72020-06-26 12:55:01 -0400589