blob: e6b8ffebc2b372abae98ae0458365a9bd10c6dd8 [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
aticig1f2e2e92022-02-03 16:42:39 +030020JUJU_AGENT_VERSION=2.9.22
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}
28
David Garciaef349d92020-12-10 21:16:12 +010029MODEL_NAME=osm
30
David Garcia4a0db7c2022-02-21 11:48:11 +010031OSM_BUNDLE=ch:osm
32OSM_HA_BUNDLE=ch:osm-ha
33CHARMHUB_CHANNEL=latest/edge
34unset TAG
David Garciaab11f842020-12-16 17:25:15 +010035
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010036function check_arguments(){
37 while [ $# -gt 0 ] ; do
David Garcia42375212020-04-27 19:07:49 +020038 case $1 in
39 --bundle) BUNDLE="$2" ;;
David Garcia4bd2bf02021-05-05 19:01:43 +020040 --overlay) OVERLAY="$2" ;;
Dominik Fleischmann7a97a4c2020-06-04 10:52:05 +020041 --k8s) KUBECFG="$2" ;;
42 --vca) CONTROLLER="$2" ;;
43 --lxd) LXD_CLOUD="$2" ;;
44 --lxd-cred) LXD_CREDENTIALS="$2" ;;
David Garcia69388c22020-05-07 12:14:19 +020045 --microstack) MICROSTACK=y ;;
David Garciaab11f842020-12-16 17:25:15 +010046 --ha) BUNDLE=$OSM_HA_BUNDLE ;;
David Garcia69388c22020-05-07 12:14:19 +020047 --tag) TAG="$2" ;;
beierlmf2782c52020-11-05 17:04:05 -050048 --registry) REGISTRY_INFO="$2" ;;
David Garcia1bc71022021-05-03 18:27:18 +020049 --only-vca) ONLY_VCA=y ;;
David Garcia42375212020-04-27 19:07:49 +020050 esac
51 shift
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010052 done
53
David Garcia42375212020-04-27 19:07:49 +020054 # echo $BUNDLE $KUBECONFIG $LXDENDPOINT
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010055}
beierlma4a37f72020-06-26 12:55:01 -040056
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010057function install_snaps(){
beierlma4a37f72020-06-26 12:55:01 -040058 if [ ! -v KUBECFG ]; then
beierlm481ae7d2020-12-14 09:59:19 -050059 sudo snap install microk8s --classic --channel=${MICROK8S_VERSION}/stable
David Garcia9a59e3d2021-02-08 15:18:27 +010060 sudo cat /var/snap/microk8s/current/args/kube-apiserver | grep advertise-address || (
David Garcia107010b2021-01-15 12:58:59 +010061 echo "--advertise-address $DEFAULT_IP" | sudo tee -a /var/snap/microk8s/current/args/kube-apiserver
62 microk8s.stop
63 microk8s.start
64 )
beierlma4a37f72020-06-26 12:55:01 -040065 sudo usermod -a -G microk8s `whoami`
66 mkdir -p ~/.kube
67 sudo chown -f -R `whoami` ~/.kube
68 KUBEGRP="microk8s"
beierlmf2782c52020-11-05 17:04:05 -050069 sg ${KUBEGRP} -c "microk8s status --wait-ready"
beierlmdf6de3d2020-12-16 08:46:40 -050070 KUBECONFIG=~/.osm/microk8s-config.yaml
David Garcia107010b2021-01-15 12:58:59 +010071 sg ${KUBEGRP} -c "microk8s config" | tee ${KUBECONFIG}
beierlma4a37f72020-06-26 12:55:01 -040072 else
73 KUBECTL="kubectl"
74 sudo snap install kubectl --classic
75 export KUBECONFIG=${KUBECFG}
76 KUBEGRP=$(id -g -n)
77 fi
David Garciad68e0b42021-06-28 16:50:42 +020078 sudo snap install juju --classic --channel=$JUJU_VERSION/stable
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010079}
80
81function bootstrap_k8s_lxd(){
David Garcia42375212020-04-27 19:07:49 +020082 [ -v CONTROLLER ] && ADD_K8S_OPTS="--controller ${CONTROLLER}" && CONTROLLER_NAME=$CONTROLLER
beierlma4a37f72020-06-26 12:55:01 -040083 [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="osm-vca"
84
85 if [ -v BOOTSTRAP_NEEDED ]; then
86 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
87 if [ $CONTROLLER_PRESENT -ge 1 ]; then
88 cat << EOF
89Threre is already a VCA present with the installer reserved name of "${CONTROLLER_NAME}".
90You may either explicitly use this VCA with the "--vca ${CONTROLLER_NAME}" option, or remove it
91using this command:
92
93 juju destroy-controller --release-storage --destroy-all-models -y ${CONTROLLER_NAME}
94
95Please retry the installation once this conflict has been resolved.
96EOF
97 exit 1
98 fi
beierlm9afb0ef2020-10-16 12:53:51 -040099 else
100 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
101 if [ $CONTROLLER_PRESENT -le 0 ]; then
102 cat << EOF
103Threre is no VCA present with the name "${CONTROLLER_NAME}". Please specify a VCA
104that exists, or remove the --vca ${CONTROLLER_NAME} option.
105
106Please retry the installation with one of the solutions applied.
107EOF
108 exit 1
109 fi
beierlma4a37f72020-06-26 12:55:01 -0400110 fi
David Garcia42375212020-04-27 19:07:49 +0200111
112 if [ -v KUBECFG ]; then
113 cat $KUBECFG | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
beierlm2634cd32020-09-15 16:00:34 -0400114 [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME \
115 --config controller-service-type=loadbalancer \
David Garciaa1376012020-10-19 15:42:42 +0200116 --agent-version=$JUJU_AGENT_VERSION
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100117 else
beierlma4a37f72020-06-26 12:55:01 -0400118 sg ${KUBEGRP} -c "echo ${DEFAULT_IP}-${DEFAULT_IP} | microk8s.enable metallb"
beierlm9afb0ef2020-10-16 12:53:51 -0400119 sg ${KUBEGRP} -c "microk8s.enable ingress"
beierlma4a37f72020-06-26 12:55:01 -0400120 sg ${KUBEGRP} -c "microk8s.enable storage dns"
121 TIME_TO_WAIT=30
122 start_time="$(date -u +%s)"
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200123 while true
124 do
beierlma4a37f72020-06-26 12:55:01 -0400125 now="$(date -u +%s)"
126 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
127 echo "Microk8s storage failed to enable"
128 sg ${KUBEGRP} -c "microk8s.status"
129 exit 1
130 fi
131 storage_status=`sg ${KUBEGRP} -c "microk8s.status -a storage"`
132 if [[ $storage_status == "enabled" ]]; then
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200133 break
134 fi
135 sleep 1
136 done
David Garcia42375212020-04-27 19:07:49 +0200137
beierlma4a37f72020-06-26 12:55:01 -0400138 [ ! -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
beierlm2634cd32020-09-15 16:00:34 -0400139 [ -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c \
David Garciaa1376012020-10-19 15:42:42 +0200140 "juju bootstrap microk8s $CONTROLLER_NAME --config controller-service-type=loadbalancer --agent-version=$JUJU_AGENT_VERSION" \
beierlm2634cd32020-09-15 16:00:34 -0400141 && K8S_CLOUD_NAME=microk8s
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100142 fi
143
David Garcia42375212020-04-27 19:07:49 +0200144 if [ -v LXD_CLOUD ]; then
145 if [ ! -v LXD_CREDENTIALS ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100146 echo "The installer needs the LXD server certificate if the LXD is external"
147 exit 1
148 fi
149 else
150 LXDENDPOINT=$DEFAULT_IP
David Garcia42375212020-04-27 19:07:49 +0200151 LXD_CLOUD=~/.osm/lxd-cloud.yaml
152 LXD_CREDENTIALS=~/.osm/lxd-credentials.yaml
153 # Apply sysctl production values for optimal performance
154 sudo cp /usr/share/osm-devops/installers/60-lxd-production.conf /etc/sysctl.d/60-lxd-production.conf
155 sudo sysctl --system
156 # Install LXD snap
157 sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client
David Garcia956fcb72021-09-09 17:23:42 +0200158 sudo snap install lxd --channel $LXD_VERSION/stable
David Garcia42375212020-04-27 19:07:49 +0200159 # Configure LXD
160 sudo usermod -a -G lxd `whoami`
161 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"
162 sg lxd -c "lxd waitready"
163 DEFAULT_MTU=$(ip addr show $DEFAULT_IF | perl -ne 'if (/mtu\s(\d+)/) {print $1;}')
164 sg lxd -c "lxc profile device set default eth0 mtu $DEFAULT_MTU"
David Garciad00e49c2020-06-19 10:33:37 +0200165 sg lxd -c "lxc network set lxdbr0 bridge.mtu $DEFAULT_MTU"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100166
David Garcia42375212020-04-27 19:07:49 +0200167 cat << EOF > $LXD_CLOUD
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100168clouds:
169 lxd-cloud:
170 type: lxd
171 auth-types: [certificate]
172 endpoint: "https://$LXDENDPOINT:8443"
173 config:
174 ssl-hostname-verification: false
175EOF
David Garcia42375212020-04-27 19:07:49 +0200176 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"
177 local server_cert=`cat /var/snap/lxd/common/lxd/server.crt | sed 's/^/ /'`
178 local client_cert=`cat ~/.osm/client.crt | sed 's/^/ /'`
179 local client_key=`cat ~/.osm/client.key | sed 's/^/ /'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100180
David Garcia42375212020-04-27 19:07:49 +0200181 cat << EOF > $LXD_CREDENTIALS
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100182credentials:
183 lxd-cloud:
David Garcia42375212020-04-27 19:07:49 +0200184 lxd-cloud:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100185 auth-type: certificate
186 server-cert: |
187$server_cert
188 client-cert: |
189$client_cert
190 client-key: |
191$client_key
192EOF
David Garcia42375212020-04-27 19:07:49 +0200193 lxc config trust add local: ~/.osm/client.crt
194 fi
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100195
David Garcia42375212020-04-27 19:07:49 +0200196 juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force
197 juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS
198 sg lxd -c "lxd waitready"
beierlma4a37f72020-06-26 12:55:01 -0400199 juju controller-config features=[k8s-operators]
200}
201
202function wait_for_port(){
203 SERVICE=$1
204 INDEX=$2
205 TIME_TO_WAIT=30
206 start_time="$(date -u +%s)"
207 while true
208 do
209 now="$(date -u +%s)"
210 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
211 echo "Failed to expose external ${SERVICE} interface port"
212 exit 1
213 fi
214
beierlm9afb0ef2020-10-16 12:53:51 -0400215 if [ $(sg ${KUBEGRP} -c "${KUBECTL} get ingresses.networking -n osm -o json | jq -r '.items[$INDEX].metadata.name'") == ${SERVICE} ] ; then
beierlma4a37f72020-06-26 12:55:01 -0400216 break
217 fi
218 sleep 1
219 done
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100220}
221
222function deploy_charmed_osm(){
beierlmf2782c52020-11-05 17:04:05 -0500223 if [ -v REGISTRY_INFO ] ; then
224 registry_parts=(${REGISTRY_INFO//@/ })
225 if [ ${#registry_parts[@]} -eq 1 ] ; then
226 # No credentials supplied
227 REGISTRY_USERNAME=""
228 REGISTRY_PASSWORD=""
229 REGISTRY_URL=${registry_parts[0]}
230 else
231 credentials=${registry_parts[0]}
232 credential_parts=(${credentials//:/ })
233 REGISTRY_USERNAME=${credential_parts[0]}
234 REGISTRY_PASSWORD=${credential_parts[1]}
235 REGISTRY_URL=${registry_parts[1]}
236 fi
237 # Ensure the URL ends with a /
238 case $REGISTRY_URL in
239 */) ;;
240 *) REGISTRY_URL=${REGISTRY_URL}/
241 esac
242 fi
243
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100244 echo "Creating OSM model"
David Garcia42375212020-04-27 19:07:49 +0200245 if [ -v KUBECFG ]; then
David Garciaef349d92020-12-10 21:16:12 +0100246 juju add-model $MODEL_NAME $K8S_CLOUD_NAME
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100247 else
David Garciaef349d92020-12-10 21:16:12 +0100248 sg ${KUBEGRP} -c "juju add-model $MODEL_NAME $K8S_CLOUD_NAME"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100249 fi
250 echo "Deploying OSM with charms"
beierlma4a37f72020-06-26 12:55:01 -0400251 images_overlay=""
beierlmf2782c52020-11-05 17:04:05 -0500252 if [ -v REGISTRY_URL ]; then
253 [ ! -v TAG ] && TAG='latest'
254 fi
beierlma4a37f72020-06-26 12:55:01 -0400255 [ -v TAG ] && generate_images_overlay && images_overlay="--overlay $IMAGES_OVERLAY_FILE"
beierlmf2782c52020-11-05 17:04:05 -0500256
David Garcia4bd2bf02021-05-05 19:01:43 +0200257 if [ -v OVERLAY ]; then
258 extra_overlay="--overlay $OVERLAY"
259 fi
aticig6eb39282022-02-24 00:34:52 +0300260 echo "Creating Password Overlay"
261
262 generate_password_overlay && secret_overlay="--overlay $PASSWORD_OVERLAY_FILE"
David Garcia4bd2bf02021-05-05 19:01:43 +0200263
David Garcia42375212020-04-27 19:07:49 +0200264 if [ -v BUNDLE ]; then
aticig6eb39282022-02-24 00:34:52 +0300265 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 +0100266 else
aticig6eb39282022-02-24 00:34:52 +0300267 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 +0100268 fi
beierlma4a37f72020-06-26 12:55:01 -0400269
beierlma4a37f72020-06-26 12:55:01 -0400270 if [ ! -v KUBECFG ]; then
beierlma4a37f72020-06-26 12:55:01 -0400271 API_SERVER=${DEFAULT_IP}
272 else
273 API_SERVER=$(kubectl config view --minify | grep server | cut -f 2- -d ":" | tr -d " ")
274 proto="$(echo $API_SERVER | grep :// | sed -e's,^\(.*://\).*,\1,g')"
275 url="$(echo ${API_SERVER/$proto/})"
276 user="$(echo $url | grep @ | cut -d@ -f1)"
277 hostport="$(echo ${url/$user@/} | cut -d/ -f1)"
278 API_SERVER="$(echo $hostport | sed -e 's,:.*,,g')"
279 fi
David Garciade5fc1d2020-09-02 11:40:12 +0200280 # Expose OSM services
David Garciac395a452021-05-05 14:22:26 +0200281 juju config -m $MODEL_NAME nbi site_url=https://nbi.${API_SERVER}.nip.io
282 juju config -m $MODEL_NAME ng-ui site_url=https://ui.${API_SERVER}.nip.io
283 juju config -m $MODEL_NAME grafana site_url=https://grafana.${API_SERVER}.nip.io
284 juju config -m $MODEL_NAME prometheus site_url=https://prometheus.${API_SERVER}.nip.io
David Garciaef349d92020-12-10 21:16:12 +0100285
David Garcia49379ce2021-02-24 13:48:22 +0100286 echo "Waiting for deployment to finish..."
287 check_osm_deployed
David Garciac537fa62021-11-09 08:45:49 +0100288 grafana_leader=`juju status -m $MODEL_NAME grafana | grep "*" | cut -d "*" -f 1`
289 grafana_admin_password=`juju run -m $MODEL_NAME --unit $grafana_leader "echo \\$GF_SECURITY_ADMIN_PASSWORD"`
290 juju config -m $MODEL_NAME mon grafana_password=$grafana_admin_password
291 check_osm_deployed
David Garcia49379ce2021-02-24 13:48:22 +0100292 echo "OSM with charms deployed"
beierlm9afb0ef2020-10-16 12:53:51 -0400293}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100294
295function check_osm_deployed() {
beierlma4a37f72020-06-26 12:55:01 -0400296 TIME_TO_WAIT=600
297 start_time="$(date -u +%s)"
David Garcia02a5eb92020-11-28 14:41:22 +0100298 total_service_count=14
beierlm7e54dfc2020-09-24 15:27:53 -0400299 previous_count=0
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100300 while true
301 do
David Garciaef349d92020-12-10 21:16:12 +0100302 service_count=$(juju status -m $MODEL_NAME | grep kubernetes | grep active | wc -l)
beierlma4a37f72020-06-26 12:55:01 -0400303 echo "$service_count / $total_service_count services active"
304 if [ $service_count -eq $total_service_count ]; then
305 break
306 fi
beierlm7e54dfc2020-09-24 15:27:53 -0400307 if [ $service_count -ne $previous_count ]; then
308 previous_count=$service_count
309 start_time="$(date -u +%s)"
310 fi
beierlma4a37f72020-06-26 12:55:01 -0400311 now="$(date -u +%s)"
312 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
313 echo "Timed out waiting for OSM services to become ready"
314 exit 1
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100315 fi
316 sleep 10
317 done
318}
319
aticig6eb39282022-02-24 00:34:52 +0300320function generate_password_overlay() {
321 # prometheus
322 web_config_password=`openssl rand -base64 16`
323 # keystone
324 keystone_db_password=`openssl rand -base64 16`
325 keystone_admin_password=`openssl rand -base64 16`
326 keystone_service_password=`openssl rand -base64 16`
327 # mariadb
328 mariadb_password=`openssl rand -hex 16`
329 mariadb_root_password=`openssl rand -hex 16`
330 cat << EOF > /tmp/password-overlay.yaml
331applications:
332 prometheus:
333 options:
334 web_config_password: $web_config_password
335 keystone:
336 options:
337 keystone-db-password: $keystone_db_password
338 admin-password: $keystone_admin_password
339 service-password: $keystone_service_password
340 mariadb:
341 options:
342 password: $mariadb_password
343 root_password: $mariadb_root_password
344EOF
345 mv /tmp/password-overlay.yaml $PASSWORD_OVERLAY_FILE
346}
347
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100348function create_overlay() {
David Garcia42375212020-04-27 19:07:49 +0200349 sudo snap install jq
David Garciac8660ad2020-12-16 13:13:20 +0100350 sudo snap install yq
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100351 local HOME=/home/$USER
calvinosanc183ea27a2021-01-04 11:42:18 +0100352 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
David Garciac753dc52021-03-17 15:28:47 +0100353 local vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
calvinosanc183ea27a2021-01-04 11:42:18 +0100354 local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
355 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 +0100356 local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
357 local vca_cloud="lxd-cloud"
358 # Get the VCA Certificate
calvinosanc183ea27a2021-01-04 11:42:18 +0100359 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 +0100360
361 # Calculate the default route of this machine
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200362 local DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100363
364 # Generate a new overlay.yaml, overriding any existing one
David Garcia42375212020-04-27 19:07:49 +0200365 cat << EOF > /tmp/vca-overlay.yaml
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100366applications:
David Garcia49379ce2021-02-24 13:48:22 +0100367 lcm:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100368 options:
369 vca_user: $vca_user
David Garciac753dc52021-03-17 15:28:47 +0100370 vca_secret: $vca_secret
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100371 vca_host: $vca_host
372 vca_port: $vca_port
373 vca_pubkey: $vca_pubkey
374 vca_cacert: $vca_cacert
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100375 vca_cloud: $vca_cloud
beierlma4a37f72020-06-26 12:55:01 -0400376 vca_k8s_cloud: $K8S_CLOUD_NAME
David Garcia49379ce2021-02-24 13:48:22 +0100377 mon:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100378 options:
379 vca_user: $vca_user
David Garciac753dc52021-03-17 15:28:47 +0100380 vca_secret: $vca_secret
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100381 vca_host: $vca_host
382 vca_cacert: $vca_cacert
383EOF
David Garcia42375212020-04-27 19:07:49 +0200384 mv /tmp/vca-overlay.yaml ~/.osm/
385 OSM_VCA_HOST=$vca_host
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100386}
387
David Garcia69388c22020-05-07 12:14:19 +0200388function generate_images_overlay(){
David Garcia49379ce2021-02-24 13:48:22 +0100389 echo "applications:" > /tmp/images-overlay.yaml
beierlmf2782c52020-11-05 17:04:05 -0500390
David Garcia53a09d82022-02-01 16:39:44 +0100391 charms_with_resources="nbi lcm mon pol ng-ui ro pla"
David Garcia49379ce2021-02-24 13:48:22 +0100392 for charm in $charms_with_resources; do
393 cat << EOF > /tmp/${charm}_registry.yaml
394registrypath: ${REGISTRY_URL}opensourcemano/${charm}:$TAG
beierlmf2782c52020-11-05 17:04:05 -0500395EOF
David Garcia49379ce2021-02-24 13:48:22 +0100396 if [ ! -z "$REGISTRY_USERNAME" ] ; then
397 echo username: $REGISTRY_USERNAME >> /tmp/${charm}_registry.yaml
398 echo password: $REGISTRY_PASSWORD >> /tmp/${charm}_registry.yaml
399 fi
beierlmf2782c52020-11-05 17:04:05 -0500400
David Garcia49379ce2021-02-24 13:48:22 +0100401 cat << EOF >> /tmp/images-overlay.yaml
402 ${charm}:
David Garciaef349d92020-12-10 21:16:12 +0100403 resources:
David Garcia49379ce2021-02-24 13:48:22 +0100404 image: /tmp/${charm}_registry.yaml
405
David Garcia69388c22020-05-07 12:14:19 +0200406EOF
David Garcia49379ce2021-02-24 13:48:22 +0100407 done
David Garcia53a09d82022-02-01 16:39:44 +0100408 ch_charms_with_resources="keystone"
409 for charm in $ch_charms_with_resources; do
410 cat << EOF > /tmp/${charm}_registry.yaml
411registrypath: ${REGISTRY_URL}opensourcemano/${charm}:$TAG
412EOF
413 if [ ! -z "$REGISTRY_USERNAME" ] ; then
414 echo username: $REGISTRY_USERNAME >> /tmp/${charm}_registry.yaml
415 echo password: $REGISTRY_PASSWORD >> /tmp/${charm}_registry.yaml
416 fi
417
418 cat << EOF >> /tmp/images-overlay.yaml
419 ${charm}:
420 resources:
421 ${charm}-image: /tmp/${charm}_registry.yaml
422
423EOF
424 done
David Garcia49379ce2021-02-24 13:48:22 +0100425
David Garcia69388c22020-05-07 12:14:19 +0200426 mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE
427}
428
David Garcia107010b2021-01-15 12:58:59 +0100429function refresh_osmclient_snap() {
430 osmclient_snap_install_refresh refresh
431}
432
433function install_osm_client_snap() {
434 osmclient_snap_install_refresh install
435}
436
437function osmclient_snap_install_refresh() {
438 channel_preference="stable candidate beta edge"
439 for channel in $channel_preference; do
440 echo "Trying to install osmclient from channel $OSMCLIENT_VERSION/$channel"
441 sudo snap $1 osmclient --channel $OSMCLIENT_VERSION/$channel 2> /dev/null && echo osmclient snap installed && break
442 done
443}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100444function install_osmclient() {
David Garcia107010b2021-01-15 12:58:59 +0100445 snap info osmclient | grep -E ^installed: && refresh_osmclient_snap || install_osm_client_snap
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100446}
447
beierlmdf6de3d2020-12-16 08:46:40 -0500448function add_local_k8scluster() {
449 osm --all-projects vim-create \
450 --name _system-osm-vim \
451 --account_type dummy \
452 --auth_url http://dummy \
453 --user osm --password osm --tenant osm \
454 --description "dummy" \
455 --config '{management_network_name: mgmt}'
456 tmpfile=$(mktemp --tmpdir=${HOME})
457 cp ${KUBECONFIG} ${tmpfile}
458 osm --all-projects k8scluster-add \
459 --creds ${tmpfile} \
460 --vim _system-osm-vim \
461 --k8s-nets '{"net1": null}' \
462 --version '1.19' \
463 --description "OSM Internal Cluster" \
464 _system-osm-k8s
465 rm -f ${tmpfile}
466}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100467
468function install_microstack() {
beierlmb403f062021-12-09 08:47:59 -0500469
470 sudo snap install microstack --beta --devmode
471
472 CHECK=$(microstack.openstack server list)
473 if [ $? -ne 0 ] ; then
474 if [[ $CHECK == *"not initialized"* ]]; then
475 echo "Setting MicroStack dashboard to listen to port 8080"
476 sudo snap set microstack config.network.ports.dashboard=8080
477 echo "Initializing MicroStack. This can take several minutes"
478 sudo microstack.init --auto --control
479 fi
480 fi
481
482 sudo snap alias microstack.openstack openstack
483
484 echo "Updating default security group in MicroStack to allow all access"
485
486 for i in $(microstack.openstack security group list | awk '/default/{ print $2 }'); do
487 for PROTO in icmp tcp udp ; do
488 echo " $PROTO ingress"
489 CHECK=$(microstack.openstack security group rule create $i --protocol $PROTO --remote-ip 0.0.0.0/0 2>&1)
490 if [ $? -ne 0 ] ; then
491 if [[ $CHECK != *"409"* ]]; then
492 echo "Error creating ingress rule for $PROTO"
493 echo $CHECK
494 fi
495 fi
496 done
497 done
498
499 microstack.openstack network show osm-ext &>/dev/null
500 if [ $? -ne 0 ]; then
501 echo "Creating osm-ext network with router to bridge to MicroStack external network"
502 microstack.openstack network create --enable --no-share osm-ext
503 microstack.openstack subnet create osm-ext-subnet --network osm-ext --dns-nameserver 8.8.8.8 \
504 --subnet-range 172.30.0.0/24
505 microstack.openstack router create external-router
506 microstack.openstack router add subnet external-router osm-ext-subnet
507 microstack.openstack router set --external-gateway external external-router
508 fi
509
510 microstack.openstack image list | grep ubuntu20.04 &> /dev/null
511 if [ $? -ne 0 ] ; then
512 echo "Fetching Ubuntu 20.04 image and upLoading to MicroStack"
513 wget -q -O- https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img \
514 | microstack.openstack image create --public --container-format=bare \
515 --disk-format=qcow2 ubuntu20.04 | grep status
516 fi
517
518 if [ ! -f ~/.ssh/microstack ]; then
519 ssh-keygen -t rsa -N "" -f ~/.ssh/microstack
520 microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack
521 fi
522
523 echo "Creating VIM microstack-site in OSM"
524 . /var/snap/microstack/common/etc/microstack.rc
525
526 osm vim-create \
527 --name microstack-site \
528 --user "$OS_USERNAME" \
529 --password "$OS_PASSWORD" \
530 --auth_url "$OS_AUTH_URL" \
531 --tenant "$OS_USERNAME" \
532 --account_type openstack \
533 --config='{use_floating_ip: True,
534 insecure: True,
535 keypair: microstack,
536 management_network_name: osm-ext}'
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100537}
538
beierlm4aad47b2021-12-07 16:15:53 -0500539DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5; exit}'`
540DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]; exit}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100541
542check_arguments $@
David Garcia42375212020-04-27 19:07:49 +0200543mkdir -p ~/.osm
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100544install_snaps
545bootstrap_k8s_lxd
David Garcia1bc71022021-05-03 18:27:18 +0200546create_overlay
547if [ -v ONLY_VCA ]; then
548 HOME=/home/$USER
549 vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
550 vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
551 vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
552 vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 2)
553 vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
554 vca_cloud="lxd-cloud"
555 vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.ca-cert - | base64 | tr -d \\n)
556 hostname=`cat /etc/hostname`
beierlmdf6de3d2020-12-16 08:46:40 -0500557
David Garcia1bc71022021-05-03 18:27:18 +0200558 echo "Use the following command to register the installed VCA to your OSM:"
David Garciad68e0b42021-06-28 16:50:42 +0200559 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"
David Garcia1bc71022021-05-03 18:27:18 +0200560else
561 deploy_charmed_osm
562 install_osmclient
563 export OSM_HOSTNAME=$(juju config -m $MODEL_NAME nbi site_url | sed "s/http.*\?:\/\///"):443
aticig6eb39282022-02-24 00:34:52 +0300564 export OSM_PASSWORD=$keystone_admin_password
David Garcia1bc71022021-05-03 18:27:18 +0200565 sleep 10
566 add_local_k8scluster
567 if [ -v MICROSTACK ]; then
568 install_microstack
569 fi
570
571 echo "Your installation is now complete, follow these steps for configuring the osmclient:"
572 echo
573 echo "1. Create the OSM_HOSTNAME environment variable with the NBI IP"
574 echo
575 echo "export OSM_HOSTNAME=$OSM_HOSTNAME"
aticig6eb39282022-02-24 00:34:52 +0300576 echo "export OSM_PASSWORD=$OSM_PASSWORD"
David Garcia1bc71022021-05-03 18:27:18 +0200577 echo
aticig6eb39282022-02-24 00:34:52 +0300578 echo "2. Add the previous commands to your .bashrc for other Shell sessions"
David Garcia1bc71022021-05-03 18:27:18 +0200579 echo
580 echo "echo \"export OSM_HOSTNAME=$OSM_HOSTNAME\" >> ~/.bashrc"
aticig6eb39282022-02-24 00:34:52 +0300581 echo "echo \"export OSM_PASSWORD=$OSM_PASSWORD\" >> ~/.bashrc"
582 echo
583 echo "3. Login OSM GUI by using admin password: $OSM_PASSWORD"
David Garcia1bc71022021-05-03 18:27:18 +0200584 echo
585 echo "DONE"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100586fi
beierlma4a37f72020-06-26 12:55:01 -0400587