blob: 4cc1cfb8e0ab26a70def79f57e90d7116baf3b96 [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 Garciaa60d7112022-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"
aticig8eff33a2022-02-02 12:09:05 +030023MICROK8S_VERSION=1.23
David Garciad05db442021-11-15 13:35:48 +010024OSMCLIENT_VERSION=11.0
David Garcia69388c22020-05-07 12:14:19 +020025IMAGES_OVERLAY_FILE=~/.osm/images-overlay.yaml
beierlmf2782c52020-11-05 17:04:05 -050026PATH=/snap/bin:${PATH}
beierlmf8c3ce52022-04-13 08:19:40 -040027OSM_DEVOPS="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/.. &> /dev/null && pwd )"
28if [ -f ${OSM_DEVOPS}/common/all_funcs ] ; then
29 source ${OSM_DEVOPS}/common/all_funcs
30else
31 function track(){
32 true
33 }
34 function FATAL_TRACK(){
35 exit 1
36 }
37fi
beierlmf2782c52020-11-05 17:04:05 -050038
David Garciaef349d92020-12-10 21:16:12 +010039MODEL_NAME=osm
40
David Garcia044f3652022-02-22 20:14:13 +010041OSM_BUNDLE=ch:osm
42OSM_HA_BUNDLE=ch:osm-ha
43CHARMHUB_CHANNEL=11.0/edge
44unset TAG
David Garciaab11f842020-12-16 17:25:15 +010045
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010046function check_arguments(){
47 while [ $# -gt 0 ] ; do
David Garcia42375212020-04-27 19:07:49 +020048 case $1 in
49 --bundle) BUNDLE="$2" ;;
David Garcia4bd2bf02021-05-05 19:01:43 +020050 --overlay) OVERLAY="$2" ;;
Dominik Fleischmann7a97a4c2020-06-04 10:52:05 +020051 --k8s) KUBECFG="$2" ;;
52 --vca) CONTROLLER="$2" ;;
53 --lxd) LXD_CLOUD="$2" ;;
54 --lxd-cred) LXD_CREDENTIALS="$2" ;;
David Garcia69388c22020-05-07 12:14:19 +020055 --microstack) MICROSTACK=y ;;
David Garciaab11f842020-12-16 17:25:15 +010056 --ha) BUNDLE=$OSM_HA_BUNDLE ;;
David Garcia69388c22020-05-07 12:14:19 +020057 --tag) TAG="$2" ;;
beierlmf2782c52020-11-05 17:04:05 -050058 --registry) REGISTRY_INFO="$2" ;;
David Garcia1bc71022021-05-03 18:27:18 +020059 --only-vca) ONLY_VCA=y ;;
David Garcia42375212020-04-27 19:07:49 +020060 esac
61 shift
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010062 done
63
David Garcia42375212020-04-27 19:07:49 +020064 # echo $BUNDLE $KUBECONFIG $LXDENDPOINT
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010065}
beierlma4a37f72020-06-26 12:55:01 -040066
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010067function install_snaps(){
beierlma4a37f72020-06-26 12:55:01 -040068 if [ ! -v KUBECFG ]; then
David Garcia044f3652022-02-22 20:14:13 +010069 KUBEGRP="microk8s"
beierlmf8c3ce52022-04-13 08:19:40 -040070 sudo snap install microk8s --classic --channel=${MICROK8S_VERSION}/stable ||
71 FATAL_TRACK k8scluster "snap install microk8s ${MICROK8S_VERSION}/stable failed"
David Garcia044f3652022-02-22 20:14:13 +010072 sudo usermod -a -G microk8s `whoami`
David Garcia9a59e3d2021-02-08 15:18:27 +010073 sudo cat /var/snap/microk8s/current/args/kube-apiserver | grep advertise-address || (
David Garcia107010b2021-01-15 12:58:59 +010074 echo "--advertise-address $DEFAULT_IP" | sudo tee -a /var/snap/microk8s/current/args/kube-apiserver
David Garcia044f3652022-02-22 20:14:13 +010075 sg ${KUBEGRP} -c microk8s.stop
76 sg ${KUBEGRP} -c microk8s.start
David Garcia107010b2021-01-15 12:58:59 +010077 )
beierlma4a37f72020-06-26 12:55:01 -040078 mkdir -p ~/.kube
79 sudo chown -f -R `whoami` ~/.kube
beierlmf2782c52020-11-05 17:04:05 -050080 sg ${KUBEGRP} -c "microk8s status --wait-ready"
beierlmdf6de3d2020-12-16 08:46:40 -050081 KUBECONFIG=~/.osm/microk8s-config.yaml
David Garcia107010b2021-01-15 12:58:59 +010082 sg ${KUBEGRP} -c "microk8s config" | tee ${KUBECONFIG}
beierlmf8c3ce52022-04-13 08:19:40 -040083 track k8scluster k8scluster_ok
beierlma4a37f72020-06-26 12:55:01 -040084 else
85 KUBECTL="kubectl"
86 sudo snap install kubectl --classic
87 export KUBECONFIG=${KUBECFG}
88 KUBEGRP=$(id -g -n)
89 fi
beierlmf8c3ce52022-04-13 08:19:40 -040090 sudo snap install juju --classic --channel=$JUJU_VERSION/stable ||
91 FATAL_TRACK juju "snap install juju ${JUJU_VERSION}/stable failed"
92 track juju juju_ok
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010093}
94
95function bootstrap_k8s_lxd(){
David Garcia42375212020-04-27 19:07:49 +020096 [ -v CONTROLLER ] && ADD_K8S_OPTS="--controller ${CONTROLLER}" && CONTROLLER_NAME=$CONTROLLER
beierlma4a37f72020-06-26 12:55:01 -040097 [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="osm-vca"
98
99 if [ -v BOOTSTRAP_NEEDED ]; then
100 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
101 if [ $CONTROLLER_PRESENT -ge 1 ]; then
102 cat << EOF
103Threre is already a VCA present with the installer reserved name of "${CONTROLLER_NAME}".
104You may either explicitly use this VCA with the "--vca ${CONTROLLER_NAME}" option, or remove it
105using this command:
106
107 juju destroy-controller --release-storage --destroy-all-models -y ${CONTROLLER_NAME}
108
109Please retry the installation once this conflict has been resolved.
110EOF
beierlmf8c3ce52022-04-13 08:19:40 -0400111 FATAL_TRACK bootstrap_k8s "VCA already present"
beierlma4a37f72020-06-26 12:55:01 -0400112 fi
beierlm9afb0ef2020-10-16 12:53:51 -0400113 else
114 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
115 if [ $CONTROLLER_PRESENT -le 0 ]; then
116 cat << EOF
117Threre is no VCA present with the name "${CONTROLLER_NAME}". Please specify a VCA
118that exists, or remove the --vca ${CONTROLLER_NAME} option.
119
120Please retry the installation with one of the solutions applied.
121EOF
beierlmf8c3ce52022-04-13 08:19:40 -0400122 FATAL_TRACK bootstrap_k8s "Requested VCA not present"
beierlm9afb0ef2020-10-16 12:53:51 -0400123 fi
beierlma4a37f72020-06-26 12:55:01 -0400124 fi
David Garcia42375212020-04-27 19:07:49 +0200125
126 if [ -v KUBECFG ]; then
127 cat $KUBECFG | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
beierlm2634cd32020-09-15 16:00:34 -0400128 [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME \
129 --config controller-service-type=loadbalancer \
David Garciaa1376012020-10-19 15:42:42 +0200130 --agent-version=$JUJU_AGENT_VERSION
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100131 else
beierlma4a37f72020-06-26 12:55:01 -0400132 sg ${KUBEGRP} -c "echo ${DEFAULT_IP}-${DEFAULT_IP} | microk8s.enable metallb"
beierlm9afb0ef2020-10-16 12:53:51 -0400133 sg ${KUBEGRP} -c "microk8s.enable ingress"
beierlma4a37f72020-06-26 12:55:01 -0400134 sg ${KUBEGRP} -c "microk8s.enable storage dns"
135 TIME_TO_WAIT=30
136 start_time="$(date -u +%s)"
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200137 while true
138 do
beierlma4a37f72020-06-26 12:55:01 -0400139 now="$(date -u +%s)"
140 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
141 echo "Microk8s storage failed to enable"
142 sg ${KUBEGRP} -c "microk8s.status"
beierlmf8c3ce52022-04-13 08:19:40 -0400143 FATAL_TRACK bootstrap_k8s "Microk8s storage failed to enable"
beierlma4a37f72020-06-26 12:55:01 -0400144 fi
145 storage_status=`sg ${KUBEGRP} -c "microk8s.status -a storage"`
146 if [[ $storage_status == "enabled" ]]; then
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200147 break
148 fi
149 sleep 1
150 done
David Garcia42375212020-04-27 19:07:49 +0200151
beierlma4a37f72020-06-26 12:55:01 -0400152 [ ! -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
beierlm2634cd32020-09-15 16:00:34 -0400153 [ -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c \
David Garciaa1376012020-10-19 15:42:42 +0200154 "juju bootstrap microk8s $CONTROLLER_NAME --config controller-service-type=loadbalancer --agent-version=$JUJU_AGENT_VERSION" \
beierlm2634cd32020-09-15 16:00:34 -0400155 && K8S_CLOUD_NAME=microk8s
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100156 fi
beierlmf8c3ce52022-04-13 08:19:40 -0400157 track bootstrap_k8s bootstrap_k8s_ok
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100158
David Garcia42375212020-04-27 19:07:49 +0200159 if [ -v LXD_CLOUD ]; then
160 if [ ! -v LXD_CREDENTIALS ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100161 echo "The installer needs the LXD server certificate if the LXD is external"
beierlmf8c3ce52022-04-13 08:19:40 -0400162 FATAL_TRACK bootstrap_lxd "No LXD certificate supplied"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100163 fi
164 else
165 LXDENDPOINT=$DEFAULT_IP
David Garcia42375212020-04-27 19:07:49 +0200166 LXD_CLOUD=~/.osm/lxd-cloud.yaml
167 LXD_CREDENTIALS=~/.osm/lxd-credentials.yaml
168 # Apply sysctl production values for optimal performance
169 sudo cp /usr/share/osm-devops/installers/60-lxd-production.conf /etc/sysctl.d/60-lxd-production.conf
170 sudo sysctl --system
171 # Install LXD snap
172 sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client
David Garcia956fcb72021-09-09 17:23:42 +0200173 sudo snap install lxd --channel $LXD_VERSION/stable
David Garcia42375212020-04-27 19:07:49 +0200174 # Configure LXD
175 sudo usermod -a -G lxd `whoami`
176 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"
177 sg lxd -c "lxd waitready"
178 DEFAULT_MTU=$(ip addr show $DEFAULT_IF | perl -ne 'if (/mtu\s(\d+)/) {print $1;}')
179 sg lxd -c "lxc profile device set default eth0 mtu $DEFAULT_MTU"
David Garciad00e49c2020-06-19 10:33:37 +0200180 sg lxd -c "lxc network set lxdbr0 bridge.mtu $DEFAULT_MTU"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100181
David Garcia42375212020-04-27 19:07:49 +0200182 cat << EOF > $LXD_CLOUD
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100183clouds:
184 lxd-cloud:
185 type: lxd
186 auth-types: [certificate]
187 endpoint: "https://$LXDENDPOINT:8443"
188 config:
189 ssl-hostname-verification: false
190EOF
David Garcia42375212020-04-27 19:07:49 +0200191 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"
192 local server_cert=`cat /var/snap/lxd/common/lxd/server.crt | sed 's/^/ /'`
193 local client_cert=`cat ~/.osm/client.crt | sed 's/^/ /'`
194 local client_key=`cat ~/.osm/client.key | sed 's/^/ /'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100195
David Garcia42375212020-04-27 19:07:49 +0200196 cat << EOF > $LXD_CREDENTIALS
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100197credentials:
198 lxd-cloud:
David Garcia42375212020-04-27 19:07:49 +0200199 lxd-cloud:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100200 auth-type: certificate
201 server-cert: |
202$server_cert
203 client-cert: |
204$client_cert
205 client-key: |
206$client_key
207EOF
David Garcia42375212020-04-27 19:07:49 +0200208 lxc config trust add local: ~/.osm/client.crt
209 fi
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100210
David Garcia42375212020-04-27 19:07:49 +0200211 juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force
212 juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS
213 sg lxd -c "lxd waitready"
beierlma4a37f72020-06-26 12:55:01 -0400214 juju controller-config features=[k8s-operators]
beierlmf8c3ce52022-04-13 08:19:40 -0400215 track bootstrap_lxd bootstrap_lxd_ok
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100216}
217
218function deploy_charmed_osm(){
beierlmf2782c52020-11-05 17:04:05 -0500219 if [ -v REGISTRY_INFO ] ; then
220 registry_parts=(${REGISTRY_INFO//@/ })
221 if [ ${#registry_parts[@]} -eq 1 ] ; then
222 # No credentials supplied
223 REGISTRY_USERNAME=""
224 REGISTRY_PASSWORD=""
225 REGISTRY_URL=${registry_parts[0]}
226 else
227 credentials=${registry_parts[0]}
228 credential_parts=(${credentials//:/ })
229 REGISTRY_USERNAME=${credential_parts[0]}
230 REGISTRY_PASSWORD=${credential_parts[1]}
231 REGISTRY_URL=${registry_parts[1]}
232 fi
233 # Ensure the URL ends with a /
234 case $REGISTRY_URL in
235 */) ;;
236 *) REGISTRY_URL=${REGISTRY_URL}/
237 esac
238 fi
239
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100240 echo "Creating OSM model"
David Garcia42375212020-04-27 19:07:49 +0200241 if [ -v KUBECFG ]; then
David Garciaef349d92020-12-10 21:16:12 +0100242 juju add-model $MODEL_NAME $K8S_CLOUD_NAME
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100243 else
David Garciaef349d92020-12-10 21:16:12 +0100244 sg ${KUBEGRP} -c "juju add-model $MODEL_NAME $K8S_CLOUD_NAME"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100245 fi
246 echo "Deploying OSM with charms"
beierlma4a37f72020-06-26 12:55:01 -0400247 images_overlay=""
beierlmf2782c52020-11-05 17:04:05 -0500248 if [ -v REGISTRY_URL ]; then
249 [ ! -v TAG ] && TAG='latest'
250 fi
beierlma4a37f72020-06-26 12:55:01 -0400251 [ -v TAG ] && generate_images_overlay && images_overlay="--overlay $IMAGES_OVERLAY_FILE"
beierlmf2782c52020-11-05 17:04:05 -0500252
David Garcia4bd2bf02021-05-05 19:01:43 +0200253 if [ -v OVERLAY ]; then
254 extra_overlay="--overlay $OVERLAY"
255 fi
256
David Garcia42375212020-04-27 19:07:49 +0200257 if [ -v BUNDLE ]; then
David Garcia044f3652022-02-22 20:14:13 +0100258 juju deploy --trust --channel $CHARMHUB_CHANNEL -m $MODEL_NAME $BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay $extra_overlay
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100259 else
David Garcia044f3652022-02-22 20:14:13 +0100260 juju deploy --trust --channel $CHARMHUB_CHANNEL -m $MODEL_NAME $OSM_BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay $extra_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 Garcia72ac65c2021-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
beierlm8ddd03b2022-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"
beierlmf8c3ce52022-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
313function create_overlay() {
David Garcia42375212020-04-27 19:07:49 +0200314 sudo snap install jq
David Garciac8660ad2020-12-16 13:13:20 +0100315 sudo snap install yq
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100316 local HOME=/home/$USER
calvinosanc183ea27a2021-01-04 11:42:18 +0100317 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
David Garciac753dc52021-03-17 15:28:47 +0100318 local vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
calvinosanc183ea27a2021-01-04 11:42:18 +0100319 local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
320 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 +0100321 local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
322 local vca_cloud="lxd-cloud"
323 # Get the VCA Certificate
calvinosanc183ea27a2021-01-04 11:42:18 +0100324 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 +0100325
326 # Calculate the default route of this machine
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200327 local DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100328
329 # Generate a new overlay.yaml, overriding any existing one
David Garcia42375212020-04-27 19:07:49 +0200330 cat << EOF > /tmp/vca-overlay.yaml
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100331applications:
David Garcia49379ce2021-02-24 13:48:22 +0100332 lcm:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100333 options:
334 vca_user: $vca_user
David Garciac753dc52021-03-17 15:28:47 +0100335 vca_secret: $vca_secret
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100336 vca_host: $vca_host
337 vca_port: $vca_port
338 vca_pubkey: $vca_pubkey
339 vca_cacert: $vca_cacert
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100340 vca_cloud: $vca_cloud
beierlma4a37f72020-06-26 12:55:01 -0400341 vca_k8s_cloud: $K8S_CLOUD_NAME
David Garcia49379ce2021-02-24 13:48:22 +0100342 mon:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100343 options:
344 vca_user: $vca_user
David Garciac753dc52021-03-17 15:28:47 +0100345 vca_secret: $vca_secret
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100346 vca_host: $vca_host
347 vca_cacert: $vca_cacert
348EOF
David Garcia42375212020-04-27 19:07:49 +0200349 mv /tmp/vca-overlay.yaml ~/.osm/
350 OSM_VCA_HOST=$vca_host
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100351}
352
David Garcia69388c22020-05-07 12:14:19 +0200353function generate_images_overlay(){
David Garcia49379ce2021-02-24 13:48:22 +0100354 echo "applications:" > /tmp/images-overlay.yaml
beierlmf2782c52020-11-05 17:04:05 -0500355
David Garcia044f3652022-02-22 20:14:13 +0100356 charms_with_resources="nbi lcm mon pol ng-ui ro pla"
David Garcia49379ce2021-02-24 13:48:22 +0100357 for charm in $charms_with_resources; do
358 cat << EOF > /tmp/${charm}_registry.yaml
359registrypath: ${REGISTRY_URL}opensourcemano/${charm}:$TAG
beierlmf2782c52020-11-05 17:04:05 -0500360EOF
David Garcia49379ce2021-02-24 13:48:22 +0100361 if [ ! -z "$REGISTRY_USERNAME" ] ; then
362 echo username: $REGISTRY_USERNAME >> /tmp/${charm}_registry.yaml
363 echo password: $REGISTRY_PASSWORD >> /tmp/${charm}_registry.yaml
364 fi
beierlmf2782c52020-11-05 17:04:05 -0500365
David Garcia49379ce2021-02-24 13:48:22 +0100366 cat << EOF >> /tmp/images-overlay.yaml
367 ${charm}:
David Garciaef349d92020-12-10 21:16:12 +0100368 resources:
David Garcia49379ce2021-02-24 13:48:22 +0100369 image: /tmp/${charm}_registry.yaml
370
David Garcia69388c22020-05-07 12:14:19 +0200371EOF
David Garcia49379ce2021-02-24 13:48:22 +0100372 done
David Garcia044f3652022-02-22 20:14:13 +0100373 ch_charms_with_resources="keystone"
374 for charm in $ch_charms_with_resources; do
375 cat << EOF > /tmp/${charm}_registry.yaml
376registrypath: ${REGISTRY_URL}opensourcemano/${charm}:$TAG
377EOF
378 if [ ! -z "$REGISTRY_USERNAME" ] ; then
379 echo username: $REGISTRY_USERNAME >> /tmp/${charm}_registry.yaml
380 echo password: $REGISTRY_PASSWORD >> /tmp/${charm}_registry.yaml
381 fi
382
383 cat << EOF >> /tmp/images-overlay.yaml
384 ${charm}:
385 resources:
386 ${charm}-image: /tmp/${charm}_registry.yaml
387
388EOF
389 done
David Garcia49379ce2021-02-24 13:48:22 +0100390
David Garcia69388c22020-05-07 12:14:19 +0200391 mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE
392}
393
David Garcia107010b2021-01-15 12:58:59 +0100394function refresh_osmclient_snap() {
395 osmclient_snap_install_refresh refresh
396}
397
398function install_osm_client_snap() {
399 osmclient_snap_install_refresh install
400}
401
402function osmclient_snap_install_refresh() {
403 channel_preference="stable candidate beta edge"
404 for channel in $channel_preference; do
405 echo "Trying to install osmclient from channel $OSMCLIENT_VERSION/$channel"
406 sudo snap $1 osmclient --channel $OSMCLIENT_VERSION/$channel 2> /dev/null && echo osmclient snap installed && break
407 done
408}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100409function install_osmclient() {
David Garcia107010b2021-01-15 12:58:59 +0100410 snap info osmclient | grep -E ^installed: && refresh_osmclient_snap || install_osm_client_snap
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100411}
412
beierlmdf6de3d2020-12-16 08:46:40 -0500413function add_local_k8scluster() {
414 osm --all-projects vim-create \
415 --name _system-osm-vim \
416 --account_type dummy \
417 --auth_url http://dummy \
418 --user osm --password osm --tenant osm \
419 --description "dummy" \
420 --config '{management_network_name: mgmt}'
421 tmpfile=$(mktemp --tmpdir=${HOME})
422 cp ${KUBECONFIG} ${tmpfile}
423 osm --all-projects k8scluster-add \
424 --creds ${tmpfile} \
425 --vim _system-osm-vim \
426 --k8s-nets '{"net1": null}' \
427 --version '1.19' \
428 --description "OSM Internal Cluster" \
429 _system-osm-k8s
430 rm -f ${tmpfile}
431}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100432
433function install_microstack() {
beierlmf8c3ce52022-04-13 08:19:40 -0400434 sudo snap install microstack --beta --devmode
435
436 CHECK=$(microstack.openstack server list)
437 if [ $? -ne 0 ] ; then
438 if [[ $CHECK == *"not initialized"* ]]; then
439 echo "Setting MicroStack dashboard to listen to port 8080"
440 sudo snap set microstack config.network.ports.dashboard=8080
441 echo "Initializing MicroStack. This can take several minutes"
442 sudo microstack.init --auto --control
443 fi
444 fi
445
446 sudo snap alias microstack.openstack openstack
447
448 echo "Updating default security group in MicroStack to allow all access"
449
450 for i in $(microstack.openstack security group list | awk '/default/{ print $2 }'); do
451 for PROTO in icmp tcp udp ; do
452 echo " $PROTO ingress"
453 CHECK=$(microstack.openstack security group rule create $i --protocol $PROTO --remote-ip 0.0.0.0/0 2>&1)
454 if [ $? -ne 0 ] ; then
455 if [[ $CHECK != *"409"* ]]; then
456 echo "Error creating ingress rule for $PROTO"
457 echo $CHECK
458 fi
459 fi
460 done
461 done
462
463 microstack.openstack network show osm-ext &>/dev/null
464 if [ $? -ne 0 ]; then
465 echo "Creating osm-ext network with router to bridge to MicroStack external network"
466 microstack.openstack network create --enable --no-share osm-ext
467 microstack.openstack subnet create osm-ext-subnet --network osm-ext --dns-nameserver 8.8.8.8 \
468 --subnet-range 172.30.0.0/24
469 microstack.openstack router create external-router
470 microstack.openstack router add subnet external-router osm-ext-subnet
471 microstack.openstack router set --external-gateway external external-router
472 fi
473
474 microstack.openstack image list | grep ubuntu20.04 &> /dev/null
475 if [ $? -ne 0 ] ; then
476 echo "Fetching Ubuntu 20.04 image and upLoading to MicroStack"
477 wget -q -O- https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img \
478 | microstack.openstack image create --public --container-format=bare \
479 --disk-format=qcow2 ubuntu20.04 | grep status
480 fi
481
482 if [ ! -f ~/.ssh/microstack ]; then
483 ssh-keygen -t rsa -N "" -f ~/.ssh/microstack
484 microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack
485 fi
486
487 echo "Creating VIM microstack-site in OSM"
488 . /var/snap/microstack/common/etc/microstack.rc
489
490 osm vim-create \
491 --name microstack-site \
492 --user "$OS_USERNAME" \
493 --password "$OS_PASSWORD" \
494 --auth_url "$OS_AUTH_URL" \
495 --tenant "$OS_USERNAME" \
496 --account_type openstack \
497 --config='{use_floating_ip: True,
498 insecure: True,
499 keypair: microstack,
500 management_network_name: osm-ext}'
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100501}
502
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200503DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100504DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
505
506check_arguments $@
David Garcia42375212020-04-27 19:07:49 +0200507mkdir -p ~/.osm
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100508install_snaps
509bootstrap_k8s_lxd
David Garcia1bc71022021-05-03 18:27:18 +0200510create_overlay
511if [ -v ONLY_VCA ]; then
512 HOME=/home/$USER
513 vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
514 vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
515 vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
516 vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 2)
517 vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
518 vca_cloud="lxd-cloud"
519 vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.ca-cert - | base64 | tr -d \\n)
520 hostname=`cat /etc/hostname`
beierlmdf6de3d2020-12-16 08:46:40 -0500521
David Garcia1bc71022021-05-03 18:27:18 +0200522 echo "Use the following command to register the installed VCA to your OSM:"
David Garciad68e0b42021-06-28 16:50:42 +0200523 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"
beierlmf8c3ce52022-04-13 08:19:40 -0400524 track deploy_osm deploy_vca_only_ok
David Garcia1bc71022021-05-03 18:27:18 +0200525else
526 deploy_charmed_osm
beierlmf8c3ce52022-04-13 08:19:40 -0400527 track deploy_osm deploy_osm_services_k8s_ok
David Garcia1bc71022021-05-03 18:27:18 +0200528 install_osmclient
beierlmf8c3ce52022-04-13 08:19:40 -0400529 track osmclient osmclient_ok
David Garcia1bc71022021-05-03 18:27:18 +0200530 export OSM_HOSTNAME=$(juju config -m $MODEL_NAME nbi site_url | sed "s/http.*\?:\/\///"):443
531 sleep 10
532 add_local_k8scluster
beierlmf8c3ce52022-04-13 08:19:40 -0400533 track final_ops add_local_k8scluster_ok
David Garcia1bc71022021-05-03 18:27:18 +0200534 if [ -v MICROSTACK ]; then
535 install_microstack
beierlmf8c3ce52022-04-13 08:19:40 -0400536 track final_ops install_microstack_ok
David Garcia1bc71022021-05-03 18:27:18 +0200537 fi
538
539 echo "Your installation is now complete, follow these steps for configuring the osmclient:"
540 echo
541 echo "1. Create the OSM_HOSTNAME environment variable with the NBI IP"
542 echo
543 echo "export OSM_HOSTNAME=$OSM_HOSTNAME"
544 echo
545 echo "2. Add the previous command to your .bashrc for other Shell sessions"
546 echo
547 echo "echo \"export OSM_HOSTNAME=$OSM_HOSTNAME\" >> ~/.bashrc"
548 echo
549 echo "DONE"
beierlmf8c3ce52022-04-13 08:19:40 -0400550 track end
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100551fi
beierlma4a37f72020-06-26 12:55:01 -0400552