blob: b7ea2a2dce055d72f997e15ce5d39d0639c3d8fa [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 Garcia5d394b22021-10-05 16:13:08 +020018LXD_VERSION=4.0
David Garciac35943e2021-06-28 16:50:42 +020019JUJU_VERSION=2.9
David Garcia2e918062022-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"
aticig734099e2022-02-04 15:36:26 +030023MICROK8S_VERSION=1.23
David Garcia76ff9082021-06-15 13:14:27 +020024OSMCLIENT_VERSION=10.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}
beierlm18d047f2022-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
beierlm7e4e7d02022-05-24 16:30:21 -040039function track(){
40 ctime=`date +%s`
41 duration=$((ctime - SESSION_ID))
42 url="http://www.woopra.com/track/ce?project=osm.etsi.org&cookie=${SESSION_ID}"
43 event_name="bin"
44 event_name="${event_name}_$1"
45 url="${url}&event=${event_name}&ce_duration=${duration}"
46 wget -q -O /dev/null $url
47}
48
49function FATAL_TRACK(){
50 track
51 exit 1
52}
53
David Garciaef349d92020-12-10 21:16:12 +010054MODEL_NAME=osm
55
David Garciaf049ed52022-02-23 17:00:31 +010056
57OSM_BUNDLE=ch:osm
58OSM_HA_BUNDLE=ch:osm-ha
David Garcia2882e3e2022-03-11 17:23:25 +010059CHARMHUB_CHANNEL=10.0/candidate
David Garciaf049ed52022-02-23 17:00:31 +010060unset TAG
David Garciaab11f842020-12-16 17:25:15 +010061
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010062function check_arguments(){
63 while [ $# -gt 0 ] ; do
David Garcia42375212020-04-27 19:07:49 +020064 case $1 in
65 --bundle) BUNDLE="$2" ;;
David Garcia4bd2bf02021-05-05 19:01:43 +020066 --overlay) OVERLAY="$2" ;;
Dominik Fleischmann7a97a4c2020-06-04 10:52:05 +020067 --k8s) KUBECFG="$2" ;;
68 --vca) CONTROLLER="$2" ;;
69 --lxd) LXD_CLOUD="$2" ;;
70 --lxd-cred) LXD_CREDENTIALS="$2" ;;
David Garcia69388c22020-05-07 12:14:19 +020071 --microstack) MICROSTACK=y ;;
David Garciaab11f842020-12-16 17:25:15 +010072 --ha) BUNDLE=$OSM_HA_BUNDLE ;;
David Garcia69388c22020-05-07 12:14:19 +020073 --tag) TAG="$2" ;;
beierlmf2782c52020-11-05 17:04:05 -050074 --registry) REGISTRY_INFO="$2" ;;
David Garcia1bc71022021-05-03 18:27:18 +020075 --only-vca) ONLY_VCA=y ;;
David Garcia42375212020-04-27 19:07:49 +020076 esac
77 shift
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010078 done
79
David Garcia42375212020-04-27 19:07:49 +020080 # echo $BUNDLE $KUBECONFIG $LXDENDPOINT
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010081}
beierlma4a37f72020-06-26 12:55:01 -040082
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010083function install_snaps(){
beierlma4a37f72020-06-26 12:55:01 -040084 if [ ! -v KUBECFG ]; then
David Garciaf049ed52022-02-23 17:00:31 +010085 KUBEGRP="microk8s"
beierlm18d047f2022-04-13 08:19:40 -040086 sudo snap install microk8s --classic --channel=${MICROK8S_VERSION}/stable ||
87 FATAL_TRACK k8scluster "snap install microk8s ${MICROK8S_VERSION}/stable failed"
David Garciaf049ed52022-02-23 17:00:31 +010088 sudo usermod -a -G microk8s `whoami`
David Garcia9a59e3d2021-02-08 15:18:27 +010089 sudo cat /var/snap/microk8s/current/args/kube-apiserver | grep advertise-address || (
David Garcia107010b2021-01-15 12:58:59 +010090 echo "--advertise-address $DEFAULT_IP" | sudo tee -a /var/snap/microk8s/current/args/kube-apiserver
David Garciaf049ed52022-02-23 17:00:31 +010091 sg ${KUBEGRP} -c microk8s.stop
92 sg ${KUBEGRP} -c microk8s.start
David Garcia107010b2021-01-15 12:58:59 +010093 )
beierlma4a37f72020-06-26 12:55:01 -040094 mkdir -p ~/.kube
95 sudo chown -f -R `whoami` ~/.kube
beierlmf2782c52020-11-05 17:04:05 -050096 sg ${KUBEGRP} -c "microk8s status --wait-ready"
beierlmdf6de3d2020-12-16 08:46:40 -050097 KUBECONFIG=~/.osm/microk8s-config.yaml
David Garcia107010b2021-01-15 12:58:59 +010098 sg ${KUBEGRP} -c "microk8s config" | tee ${KUBECONFIG}
beierlm18d047f2022-04-13 08:19:40 -040099 track k8scluster k8scluster_ok
beierlma4a37f72020-06-26 12:55:01 -0400100 else
101 KUBECTL="kubectl"
102 sudo snap install kubectl --classic
103 export KUBECONFIG=${KUBECFG}
104 KUBEGRP=$(id -g -n)
105 fi
beierlm18d047f2022-04-13 08:19:40 -0400106 sudo snap install juju --classic --channel=$JUJU_VERSION/stable ||
107 FATAL_TRACK juju "snap install juju ${JUJU_VERSION}/stable failed"
108 track juju juju_ok
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100109}
110
111function bootstrap_k8s_lxd(){
David Garcia42375212020-04-27 19:07:49 +0200112 [ -v CONTROLLER ] && ADD_K8S_OPTS="--controller ${CONTROLLER}" && CONTROLLER_NAME=$CONTROLLER
beierlma4a37f72020-06-26 12:55:01 -0400113 [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="osm-vca"
114
115 if [ -v BOOTSTRAP_NEEDED ]; then
116 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
117 if [ $CONTROLLER_PRESENT -ge 1 ]; then
118 cat << EOF
119Threre is already a VCA present with the installer reserved name of "${CONTROLLER_NAME}".
120You may either explicitly use this VCA with the "--vca ${CONTROLLER_NAME}" option, or remove it
121using this command:
122
123 juju destroy-controller --release-storage --destroy-all-models -y ${CONTROLLER_NAME}
124
125Please retry the installation once this conflict has been resolved.
126EOF
beierlm18d047f2022-04-13 08:19:40 -0400127 FATAL_TRACK bootstrap_k8s "VCA already present"
beierlma4a37f72020-06-26 12:55:01 -0400128 fi
beierlm9afb0ef2020-10-16 12:53:51 -0400129 else
130 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
131 if [ $CONTROLLER_PRESENT -le 0 ]; then
132 cat << EOF
133Threre is no VCA present with the name "${CONTROLLER_NAME}". Please specify a VCA
134that exists, or remove the --vca ${CONTROLLER_NAME} option.
135
136Please retry the installation with one of the solutions applied.
137EOF
beierlm18d047f2022-04-13 08:19:40 -0400138 FATAL_TRACK bootstrap_k8s "Requested VCA not present"
beierlm9afb0ef2020-10-16 12:53:51 -0400139 fi
beierlma4a37f72020-06-26 12:55:01 -0400140 fi
David Garcia42375212020-04-27 19:07:49 +0200141
142 if [ -v KUBECFG ]; then
143 cat $KUBECFG | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
beierlm2634cd32020-09-15 16:00:34 -0400144 [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME \
145 --config controller-service-type=loadbalancer \
David Garciaa1376012020-10-19 15:42:42 +0200146 --agent-version=$JUJU_AGENT_VERSION
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100147 else
beierlma4a37f72020-06-26 12:55:01 -0400148 sg ${KUBEGRP} -c "echo ${DEFAULT_IP}-${DEFAULT_IP} | microk8s.enable metallb"
beierlm9afb0ef2020-10-16 12:53:51 -0400149 sg ${KUBEGRP} -c "microk8s.enable ingress"
beierlma4a37f72020-06-26 12:55:01 -0400150 sg ${KUBEGRP} -c "microk8s.enable storage dns"
151 TIME_TO_WAIT=30
152 start_time="$(date -u +%s)"
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200153 while true
154 do
beierlma4a37f72020-06-26 12:55:01 -0400155 now="$(date -u +%s)"
156 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
157 echo "Microk8s storage failed to enable"
158 sg ${KUBEGRP} -c "microk8s.status"
beierlm18d047f2022-04-13 08:19:40 -0400159 FATAL_TRACK bootstrap_k8s "Microk8s storage failed to enable"
beierlma4a37f72020-06-26 12:55:01 -0400160 fi
161 storage_status=`sg ${KUBEGRP} -c "microk8s.status -a storage"`
162 if [[ $storage_status == "enabled" ]]; then
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200163 break
164 fi
165 sleep 1
166 done
David Garcia42375212020-04-27 19:07:49 +0200167
beierlma4a37f72020-06-26 12:55:01 -0400168 [ ! -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
beierlm2634cd32020-09-15 16:00:34 -0400169 [ -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c \
David Garciaa1376012020-10-19 15:42:42 +0200170 "juju bootstrap microk8s $CONTROLLER_NAME --config controller-service-type=loadbalancer --agent-version=$JUJU_AGENT_VERSION" \
beierlm2634cd32020-09-15 16:00:34 -0400171 && K8S_CLOUD_NAME=microk8s
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100172 fi
beierlm18d047f2022-04-13 08:19:40 -0400173 track bootstrap_k8s bootstrap_k8s_ok
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100174
David Garcia42375212020-04-27 19:07:49 +0200175 if [ -v LXD_CLOUD ]; then
176 if [ ! -v LXD_CREDENTIALS ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100177 echo "The installer needs the LXD server certificate if the LXD is external"
beierlm18d047f2022-04-13 08:19:40 -0400178 FATAL_TRACK bootstrap_lxd "No LXD certificate supplied"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100179 fi
180 else
181 LXDENDPOINT=$DEFAULT_IP
David Garcia42375212020-04-27 19:07:49 +0200182 LXD_CLOUD=~/.osm/lxd-cloud.yaml
183 LXD_CREDENTIALS=~/.osm/lxd-credentials.yaml
184 # Apply sysctl production values for optimal performance
185 sudo cp /usr/share/osm-devops/installers/60-lxd-production.conf /etc/sysctl.d/60-lxd-production.conf
186 sudo sysctl --system
187 # Install LXD snap
188 sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client
David Garciad9c90a02021-09-09 17:23:42 +0200189 sudo snap install lxd --channel $LXD_VERSION/stable
David Garcia42375212020-04-27 19:07:49 +0200190 # Configure LXD
191 sudo usermod -a -G lxd `whoami`
192 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"
193 sg lxd -c "lxd waitready"
194 DEFAULT_MTU=$(ip addr show $DEFAULT_IF | perl -ne 'if (/mtu\s(\d+)/) {print $1;}')
195 sg lxd -c "lxc profile device set default eth0 mtu $DEFAULT_MTU"
David Garciad00e49c2020-06-19 10:33:37 +0200196 sg lxd -c "lxc network set lxdbr0 bridge.mtu $DEFAULT_MTU"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100197
David Garcia42375212020-04-27 19:07:49 +0200198 cat << EOF > $LXD_CLOUD
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100199clouds:
200 lxd-cloud:
201 type: lxd
202 auth-types: [certificate]
203 endpoint: "https://$LXDENDPOINT:8443"
204 config:
205 ssl-hostname-verification: false
206EOF
David Garcia42375212020-04-27 19:07:49 +0200207 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"
208 local server_cert=`cat /var/snap/lxd/common/lxd/server.crt | sed 's/^/ /'`
209 local client_cert=`cat ~/.osm/client.crt | sed 's/^/ /'`
210 local client_key=`cat ~/.osm/client.key | sed 's/^/ /'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100211
David Garcia42375212020-04-27 19:07:49 +0200212 cat << EOF > $LXD_CREDENTIALS
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100213credentials:
214 lxd-cloud:
David Garcia42375212020-04-27 19:07:49 +0200215 lxd-cloud:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100216 auth-type: certificate
217 server-cert: |
218$server_cert
219 client-cert: |
220$client_cert
221 client-key: |
222$client_key
223EOF
David Garcia42375212020-04-27 19:07:49 +0200224 lxc config trust add local: ~/.osm/client.crt
225 fi
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100226
David Garcia42375212020-04-27 19:07:49 +0200227 juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force
228 juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS
229 sg lxd -c "lxd waitready"
beierlma4a37f72020-06-26 12:55:01 -0400230 juju controller-config features=[k8s-operators]
beierlm18d047f2022-04-13 08:19:40 -0400231 track bootstrap_lxd bootstrap_lxd_ok
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100232}
233
234function deploy_charmed_osm(){
beierlmf2782c52020-11-05 17:04:05 -0500235 if [ -v REGISTRY_INFO ] ; then
236 registry_parts=(${REGISTRY_INFO//@/ })
237 if [ ${#registry_parts[@]} -eq 1 ] ; then
238 # No credentials supplied
239 REGISTRY_USERNAME=""
240 REGISTRY_PASSWORD=""
241 REGISTRY_URL=${registry_parts[0]}
242 else
243 credentials=${registry_parts[0]}
244 credential_parts=(${credentials//:/ })
245 REGISTRY_USERNAME=${credential_parts[0]}
246 REGISTRY_PASSWORD=${credential_parts[1]}
247 REGISTRY_URL=${registry_parts[1]}
248 fi
249 # Ensure the URL ends with a /
250 case $REGISTRY_URL in
251 */) ;;
252 *) REGISTRY_URL=${REGISTRY_URL}/
253 esac
254 fi
255
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100256 echo "Creating OSM model"
David Garcia42375212020-04-27 19:07:49 +0200257 if [ -v KUBECFG ]; then
David Garciaef349d92020-12-10 21:16:12 +0100258 juju add-model $MODEL_NAME $K8S_CLOUD_NAME
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100259 else
David Garciaef349d92020-12-10 21:16:12 +0100260 sg ${KUBEGRP} -c "juju add-model $MODEL_NAME $K8S_CLOUD_NAME"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100261 fi
262 echo "Deploying OSM with charms"
beierlma4a37f72020-06-26 12:55:01 -0400263 images_overlay=""
beierlmf2782c52020-11-05 17:04:05 -0500264 if [ -v REGISTRY_URL ]; then
265 [ ! -v TAG ] && TAG='latest'
266 fi
beierlma4a37f72020-06-26 12:55:01 -0400267 [ -v TAG ] && generate_images_overlay && images_overlay="--overlay $IMAGES_OVERLAY_FILE"
beierlmf2782c52020-11-05 17:04:05 -0500268
David Garcia4bd2bf02021-05-05 19:01:43 +0200269 if [ -v OVERLAY ]; then
270 extra_overlay="--overlay $OVERLAY"
271 fi
272
David Garcia42375212020-04-27 19:07:49 +0200273 if [ -v BUNDLE ]; then
David Garciaf049ed52022-02-23 17:00:31 +0100274 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 +0100275 else
David Garciaf049ed52022-02-23 17:00:31 +0100276 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 +0100277 fi
beierlma4a37f72020-06-26 12:55:01 -0400278
beierlma4a37f72020-06-26 12:55:01 -0400279 if [ ! -v KUBECFG ]; then
beierlma4a37f72020-06-26 12:55:01 -0400280 API_SERVER=${DEFAULT_IP}
281 else
282 API_SERVER=$(kubectl config view --minify | grep server | cut -f 2- -d ":" | tr -d " ")
283 proto="$(echo $API_SERVER | grep :// | sed -e's,^\(.*://\).*,\1,g')"
284 url="$(echo ${API_SERVER/$proto/})"
285 user="$(echo $url | grep @ | cut -d@ -f1)"
286 hostport="$(echo ${url/$user@/} | cut -d/ -f1)"
287 API_SERVER="$(echo $hostport | sed -e 's,:.*,,g')"
288 fi
David Garciade5fc1d2020-09-02 11:40:12 +0200289 # Expose OSM services
David Garciac395a452021-05-05 14:22:26 +0200290 juju config -m $MODEL_NAME nbi site_url=https://nbi.${API_SERVER}.nip.io
291 juju config -m $MODEL_NAME ng-ui site_url=https://ui.${API_SERVER}.nip.io
292 juju config -m $MODEL_NAME grafana site_url=https://grafana.${API_SERVER}.nip.io
293 juju config -m $MODEL_NAME prometheus site_url=https://prometheus.${API_SERVER}.nip.io
David Garciaef349d92020-12-10 21:16:12 +0100294
David Garcia49379ce2021-02-24 13:48:22 +0100295 echo "Waiting for deployment to finish..."
Mark Beierlc617f772022-03-04 07:24:01 -0500296 grafana_leader=`juju status -m $MODEL_NAME grafana | grep "*" | cut -d "*" -f 1`
297 grafana_admin_password=`juju run -m $MODEL_NAME --unit $grafana_leader "echo \\$GF_SECURITY_ADMIN_PASSWORD"`
298 juju config -m $MODEL_NAME mon grafana_password=$grafana_admin_password
David Garcia49379ce2021-02-24 13:48:22 +0100299 check_osm_deployed
300 echo "OSM with charms deployed"
beierlm9afb0ef2020-10-16 12:53:51 -0400301}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100302
303function check_osm_deployed() {
beierlma4a37f72020-06-26 12:55:01 -0400304 TIME_TO_WAIT=600
305 start_time="$(date -u +%s)"
David Garcia02a5eb92020-11-28 14:41:22 +0100306 total_service_count=14
beierlm7e54dfc2020-09-24 15:27:53 -0400307 previous_count=0
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100308 while true
309 do
beierlmf7cb7132022-03-25 12:23:36 -0400310 service_count=$(juju status --format json -m $MODEL_NAME | jq '.applications[]."application-status".current' | grep active | wc -l)
beierlma4a37f72020-06-26 12:55:01 -0400311 echo "$service_count / $total_service_count services active"
312 if [ $service_count -eq $total_service_count ]; then
313 break
314 fi
beierlm7e54dfc2020-09-24 15:27:53 -0400315 if [ $service_count -ne $previous_count ]; then
316 previous_count=$service_count
317 start_time="$(date -u +%s)"
318 fi
beierlma4a37f72020-06-26 12:55:01 -0400319 now="$(date -u +%s)"
320 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
321 echo "Timed out waiting for OSM services to become ready"
beierlm18d047f2022-04-13 08:19:40 -0400322 FATAL_TRACK deploy_osm "Timed out waiting for services to become ready"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100323 fi
324 sleep 10
325 done
326}
327
328function create_overlay() {
David Garcia42375212020-04-27 19:07:49 +0200329 sudo snap install jq
David Garciac8660ad2020-12-16 13:13:20 +0100330 sudo snap install yq
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100331 local HOME=/home/$USER
calvinosanc183ea27a2021-01-04 11:42:18 +0100332 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
David Garciac753dc52021-03-17 15:28:47 +0100333 local vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
calvinosanc183ea27a2021-01-04 11:42:18 +0100334 local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
335 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 +0100336 local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
337 local vca_cloud="lxd-cloud"
338 # Get the VCA Certificate
calvinosanc183ea27a2021-01-04 11:42:18 +0100339 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 +0100340
341 # Calculate the default route of this machine
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200342 local DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100343
344 # Generate a new overlay.yaml, overriding any existing one
David Garcia42375212020-04-27 19:07:49 +0200345 cat << EOF > /tmp/vca-overlay.yaml
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100346applications:
David Garcia49379ce2021-02-24 13:48:22 +0100347 lcm:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100348 options:
349 vca_user: $vca_user
David Garciac753dc52021-03-17 15:28:47 +0100350 vca_secret: $vca_secret
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100351 vca_host: $vca_host
352 vca_port: $vca_port
353 vca_pubkey: $vca_pubkey
354 vca_cacert: $vca_cacert
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100355 vca_cloud: $vca_cloud
beierlma4a37f72020-06-26 12:55:01 -0400356 vca_k8s_cloud: $K8S_CLOUD_NAME
David Garcia49379ce2021-02-24 13:48:22 +0100357 mon:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100358 options:
359 vca_user: $vca_user
David Garciac753dc52021-03-17 15:28:47 +0100360 vca_secret: $vca_secret
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100361 vca_host: $vca_host
362 vca_cacert: $vca_cacert
363EOF
David Garcia42375212020-04-27 19:07:49 +0200364 mv /tmp/vca-overlay.yaml ~/.osm/
365 OSM_VCA_HOST=$vca_host
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100366}
367
David Garcia69388c22020-05-07 12:14:19 +0200368function generate_images_overlay(){
David Garcia49379ce2021-02-24 13:48:22 +0100369 echo "applications:" > /tmp/images-overlay.yaml
beierlmf2782c52020-11-05 17:04:05 -0500370
David Garciaf049ed52022-02-23 17:00:31 +0100371 charms_with_resources="nbi lcm mon pol ng-ui ro pla"
David Garcia49379ce2021-02-24 13:48:22 +0100372 for charm in $charms_with_resources; do
373 cat << EOF > /tmp/${charm}_registry.yaml
374registrypath: ${REGISTRY_URL}opensourcemano/${charm}:$TAG
beierlmf2782c52020-11-05 17:04:05 -0500375EOF
David Garcia49379ce2021-02-24 13:48:22 +0100376 if [ ! -z "$REGISTRY_USERNAME" ] ; then
377 echo username: $REGISTRY_USERNAME >> /tmp/${charm}_registry.yaml
378 echo password: $REGISTRY_PASSWORD >> /tmp/${charm}_registry.yaml
379 fi
beierlmf2782c52020-11-05 17:04:05 -0500380
David Garcia49379ce2021-02-24 13:48:22 +0100381 cat << EOF >> /tmp/images-overlay.yaml
382 ${charm}:
David Garciaef349d92020-12-10 21:16:12 +0100383 resources:
David Garcia49379ce2021-02-24 13:48:22 +0100384 image: /tmp/${charm}_registry.yaml
385
David Garcia69388c22020-05-07 12:14:19 +0200386EOF
David Garcia49379ce2021-02-24 13:48:22 +0100387 done
David Garciaf049ed52022-02-23 17:00:31 +0100388 ch_charms_with_resources="keystone"
389 for charm in $ch_charms_with_resources; do
390 cat << EOF > /tmp/${charm}_registry.yaml
391registrypath: ${REGISTRY_URL}opensourcemano/${charm}:$TAG
392EOF
393 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
397
398 cat << EOF >> /tmp/images-overlay.yaml
399 ${charm}:
400 resources:
401 ${charm}-image: /tmp/${charm}_registry.yaml
402
403EOF
404 done
David Garcia49379ce2021-02-24 13:48:22 +0100405
David Garcia69388c22020-05-07 12:14:19 +0200406 mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE
407}
408
David Garcia107010b2021-01-15 12:58:59 +0100409function refresh_osmclient_snap() {
410 osmclient_snap_install_refresh refresh
411}
412
413function install_osm_client_snap() {
414 osmclient_snap_install_refresh install
415}
416
417function osmclient_snap_install_refresh() {
418 channel_preference="stable candidate beta edge"
419 for channel in $channel_preference; do
420 echo "Trying to install osmclient from channel $OSMCLIENT_VERSION/$channel"
421 sudo snap $1 osmclient --channel $OSMCLIENT_VERSION/$channel 2> /dev/null && echo osmclient snap installed && break
422 done
423}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100424function install_osmclient() {
David Garcia107010b2021-01-15 12:58:59 +0100425 snap info osmclient | grep -E ^installed: && refresh_osmclient_snap || install_osm_client_snap
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100426}
427
beierlmdf6de3d2020-12-16 08:46:40 -0500428function add_local_k8scluster() {
429 osm --all-projects vim-create \
430 --name _system-osm-vim \
431 --account_type dummy \
432 --auth_url http://dummy \
433 --user osm --password osm --tenant osm \
434 --description "dummy" \
435 --config '{management_network_name: mgmt}'
436 tmpfile=$(mktemp --tmpdir=${HOME})
437 cp ${KUBECONFIG} ${tmpfile}
438 osm --all-projects k8scluster-add \
439 --creds ${tmpfile} \
440 --vim _system-osm-vim \
441 --k8s-nets '{"net1": null}' \
442 --version '1.19' \
443 --description "OSM Internal Cluster" \
444 _system-osm-k8s
445 rm -f ${tmpfile}
446}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100447
448function install_microstack() {
beierlm18d047f2022-04-13 08:19:40 -0400449 sudo snap install microstack --beta --devmode
450
451 CHECK=$(microstack.openstack server list)
452 if [ $? -ne 0 ] ; then
453 if [[ $CHECK == *"not initialized"* ]]; then
454 echo "Setting MicroStack dashboard to listen to port 8080"
455 sudo snap set microstack config.network.ports.dashboard=8080
456 echo "Initializing MicroStack. This can take several minutes"
457 sudo microstack.init --auto --control
458 fi
459 fi
460
461 sudo snap alias microstack.openstack openstack
462
463 echo "Updating default security group in MicroStack to allow all access"
464
465 for i in $(microstack.openstack security group list | awk '/default/{ print $2 }'); do
466 for PROTO in icmp tcp udp ; do
467 echo " $PROTO ingress"
468 CHECK=$(microstack.openstack security group rule create $i --protocol $PROTO --remote-ip 0.0.0.0/0 2>&1)
469 if [ $? -ne 0 ] ; then
470 if [[ $CHECK != *"409"* ]]; then
471 echo "Error creating ingress rule for $PROTO"
472 echo $CHECK
473 fi
474 fi
475 done
476 done
477
478 microstack.openstack network show osm-ext &>/dev/null
479 if [ $? -ne 0 ]; then
480 echo "Creating osm-ext network with router to bridge to MicroStack external network"
481 microstack.openstack network create --enable --no-share osm-ext
482 microstack.openstack subnet create osm-ext-subnet --network osm-ext --dns-nameserver 8.8.8.8 \
483 --subnet-range 172.30.0.0/24
484 microstack.openstack router create external-router
485 microstack.openstack router add subnet external-router osm-ext-subnet
486 microstack.openstack router set --external-gateway external external-router
487 fi
488
489 microstack.openstack image list | grep ubuntu20.04 &> /dev/null
490 if [ $? -ne 0 ] ; then
491 echo "Fetching Ubuntu 20.04 image and upLoading to MicroStack"
492 wget -q -O- https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img \
493 | microstack.openstack image create --public --container-format=bare \
494 --disk-format=qcow2 ubuntu20.04 | grep status
495 fi
496
497 if [ ! -f ~/.ssh/microstack ]; then
498 ssh-keygen -t rsa -N "" -f ~/.ssh/microstack
499 microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack
500 fi
501
502 echo "Creating VIM microstack-site in OSM"
503 . /var/snap/microstack/common/etc/microstack.rc
504
505 osm vim-create \
506 --name microstack-site \
507 --user "$OS_USERNAME" \
508 --password "$OS_PASSWORD" \
509 --auth_url "$OS_AUTH_URL" \
510 --tenant "$OS_USERNAME" \
511 --account_type openstack \
512 --config='{use_floating_ip: True,
513 insecure: True,
514 keypair: microstack,
515 management_network_name: osm-ext}'
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100516}
517
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200518DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100519DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
520
521check_arguments $@
David Garcia42375212020-04-27 19:07:49 +0200522mkdir -p ~/.osm
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100523install_snaps
524bootstrap_k8s_lxd
David Garcia1bc71022021-05-03 18:27:18 +0200525create_overlay
526if [ -v ONLY_VCA ]; then
527 HOME=/home/$USER
528 vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
529 vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
530 vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
531 vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 2)
532 vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
533 vca_cloud="lxd-cloud"
534 vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.ca-cert - | base64 | tr -d \\n)
535 hostname=`cat /etc/hostname`
beierlmdf6de3d2020-12-16 08:46:40 -0500536
David Garcia1bc71022021-05-03 18:27:18 +0200537 echo "Use the following command to register the installed VCA to your OSM:"
David Garciac35943e2021-06-28 16:50:42 +0200538 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"
beierlm18d047f2022-04-13 08:19:40 -0400539 track deploy_osm deploy_vca_only_ok
David Garcia1bc71022021-05-03 18:27:18 +0200540else
541 deploy_charmed_osm
beierlm18d047f2022-04-13 08:19:40 -0400542 track deploy_osm deploy_osm_services_k8s_ok
David Garcia1bc71022021-05-03 18:27:18 +0200543 install_osmclient
beierlm18d047f2022-04-13 08:19:40 -0400544 track osmclient osmclient_ok
David Garcia1bc71022021-05-03 18:27:18 +0200545 export OSM_HOSTNAME=$(juju config -m $MODEL_NAME nbi site_url | sed "s/http.*\?:\/\///"):443
546 sleep 10
547 add_local_k8scluster
beierlm18d047f2022-04-13 08:19:40 -0400548 track final_ops add_local_k8scluster_ok
David Garcia1bc71022021-05-03 18:27:18 +0200549 if [ -v MICROSTACK ]; then
550 install_microstack
beierlm18d047f2022-04-13 08:19:40 -0400551 track final_ops install_microstack_ok
David Garcia1bc71022021-05-03 18:27:18 +0200552 fi
553
554 echo "Your installation is now complete, follow these steps for configuring the osmclient:"
555 echo
556 echo "1. Create the OSM_HOSTNAME environment variable with the NBI IP"
557 echo
558 echo "export OSM_HOSTNAME=$OSM_HOSTNAME"
559 echo
560 echo "2. Add the previous command to your .bashrc for other Shell sessions"
561 echo
562 echo "echo \"export OSM_HOSTNAME=$OSM_HOSTNAME\" >> ~/.bashrc"
563 echo
564 echo "DONE"
beierlm18d047f2022-04-13 08:19:40 -0400565 track end
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100566fi
beierlma4a37f72020-06-26 12:55:01 -0400567