blob: 3703ca60277483e6ef982c9dfe4af08d2c427241 [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 Garciad9c90a02021-09-09 17:23:42 +020018LXD_VERSION=4.17
19JUJU_VERSION=2.8
David Garcia49379ce2021-02-24 13:48:22 +010020JUJU_AGENT_VERSION=2.8.9
David Garcia42375212020-04-27 19:07:49 +020021K8S_CLOUD_NAME="k8s-cloud"
beierlm3749e312020-07-02 14:21:09 -040022KUBECTL="microk8s.kubectl"
beierlm481ae7d2020-12-14 09:59:19 -050023MICROK8S_VERSION=1.19
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}
27
David Garciaef349d92020-12-10 21:16:12 +010028MODEL_NAME=osm
29
David Garcia0693b092021-06-10 12:29:52 +020030OSM_BUNDLE=cs:osm-66
31OSM_HA_BUNDLE=cs:osm-ha-51
David Garcia76ff9082021-06-15 13:14:27 +020032TAG=10
David Garciaab11f842020-12-16 17:25:15 +010033
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010034function check_arguments(){
35 while [ $# -gt 0 ] ; do
David Garcia42375212020-04-27 19:07:49 +020036 case $1 in
37 --bundle) BUNDLE="$2" ;;
David Garcia4bd2bf02021-05-05 19:01:43 +020038 --overlay) OVERLAY="$2" ;;
Dominik Fleischmann7a97a4c2020-06-04 10:52:05 +020039 --k8s) KUBECFG="$2" ;;
40 --vca) CONTROLLER="$2" ;;
41 --lxd) LXD_CLOUD="$2" ;;
42 --lxd-cred) LXD_CREDENTIALS="$2" ;;
David Garcia69388c22020-05-07 12:14:19 +020043 --microstack) MICROSTACK=y ;;
David Garciaab11f842020-12-16 17:25:15 +010044 --ha) BUNDLE=$OSM_HA_BUNDLE ;;
David Garcia69388c22020-05-07 12:14:19 +020045 --tag) TAG="$2" ;;
beierlmf2782c52020-11-05 17:04:05 -050046 --registry) REGISTRY_INFO="$2" ;;
David Garcia1bc71022021-05-03 18:27:18 +020047 --only-vca) ONLY_VCA=y ;;
David Garcia42375212020-04-27 19:07:49 +020048 esac
49 shift
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010050 done
51
David Garcia42375212020-04-27 19:07:49 +020052 # echo $BUNDLE $KUBECONFIG $LXDENDPOINT
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010053}
beierlma4a37f72020-06-26 12:55:01 -040054
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010055function install_snaps(){
beierlma4a37f72020-06-26 12:55:01 -040056 if [ ! -v KUBECFG ]; then
beierlm481ae7d2020-12-14 09:59:19 -050057 sudo snap install microk8s --classic --channel=${MICROK8S_VERSION}/stable
David Garcia9a59e3d2021-02-08 15:18:27 +010058 sudo cat /var/snap/microk8s/current/args/kube-apiserver | grep advertise-address || (
David Garcia107010b2021-01-15 12:58:59 +010059 echo "--advertise-address $DEFAULT_IP" | sudo tee -a /var/snap/microk8s/current/args/kube-apiserver
60 microk8s.stop
61 microk8s.start
62 )
beierlma4a37f72020-06-26 12:55:01 -040063 sudo usermod -a -G microk8s `whoami`
64 mkdir -p ~/.kube
65 sudo chown -f -R `whoami` ~/.kube
66 KUBEGRP="microk8s"
beierlmf2782c52020-11-05 17:04:05 -050067 sg ${KUBEGRP} -c "microk8s status --wait-ready"
beierlmdf6de3d2020-12-16 08:46:40 -050068 KUBECONFIG=~/.osm/microk8s-config.yaml
David Garcia107010b2021-01-15 12:58:59 +010069 sg ${KUBEGRP} -c "microk8s config" | tee ${KUBECONFIG}
beierlma4a37f72020-06-26 12:55:01 -040070 else
71 KUBECTL="kubectl"
72 sudo snap install kubectl --classic
73 export KUBECONFIG=${KUBECFG}
74 KUBEGRP=$(id -g -n)
75 fi
David Garciad9c90a02021-09-09 17:23:42 +020076 sudo snap install juju --classic --channel=$JUJU_VERSION/stable
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010077}
78
79function bootstrap_k8s_lxd(){
David Garcia42375212020-04-27 19:07:49 +020080 [ -v CONTROLLER ] && ADD_K8S_OPTS="--controller ${CONTROLLER}" && CONTROLLER_NAME=$CONTROLLER
beierlma4a37f72020-06-26 12:55:01 -040081 [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="osm-vca"
82
83 if [ -v BOOTSTRAP_NEEDED ]; then
84 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
85 if [ $CONTROLLER_PRESENT -ge 1 ]; then
86 cat << EOF
87Threre is already a VCA present with the installer reserved name of "${CONTROLLER_NAME}".
88You may either explicitly use this VCA with the "--vca ${CONTROLLER_NAME}" option, or remove it
89using this command:
90
91 juju destroy-controller --release-storage --destroy-all-models -y ${CONTROLLER_NAME}
92
93Please retry the installation once this conflict has been resolved.
94EOF
95 exit 1
96 fi
beierlm9afb0ef2020-10-16 12:53:51 -040097 else
98 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
99 if [ $CONTROLLER_PRESENT -le 0 ]; then
100 cat << EOF
101Threre is no VCA present with the name "${CONTROLLER_NAME}". Please specify a VCA
102that exists, or remove the --vca ${CONTROLLER_NAME} option.
103
104Please retry the installation with one of the solutions applied.
105EOF
106 exit 1
107 fi
beierlma4a37f72020-06-26 12:55:01 -0400108 fi
David Garcia42375212020-04-27 19:07:49 +0200109
110 if [ -v KUBECFG ]; then
111 cat $KUBECFG | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
beierlm2634cd32020-09-15 16:00:34 -0400112 [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME \
113 --config controller-service-type=loadbalancer \
David Garciaa1376012020-10-19 15:42:42 +0200114 --agent-version=$JUJU_AGENT_VERSION
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100115 else
beierlma4a37f72020-06-26 12:55:01 -0400116 sg ${KUBEGRP} -c "echo ${DEFAULT_IP}-${DEFAULT_IP} | microk8s.enable metallb"
beierlm9afb0ef2020-10-16 12:53:51 -0400117 sg ${KUBEGRP} -c "microk8s.enable ingress"
beierlma4a37f72020-06-26 12:55:01 -0400118 sg ${KUBEGRP} -c "microk8s.enable storage dns"
119 TIME_TO_WAIT=30
120 start_time="$(date -u +%s)"
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200121 while true
122 do
beierlma4a37f72020-06-26 12:55:01 -0400123 now="$(date -u +%s)"
124 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
125 echo "Microk8s storage failed to enable"
126 sg ${KUBEGRP} -c "microk8s.status"
127 exit 1
128 fi
129 storage_status=`sg ${KUBEGRP} -c "microk8s.status -a storage"`
130 if [[ $storage_status == "enabled" ]]; then
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200131 break
132 fi
133 sleep 1
134 done
David Garcia42375212020-04-27 19:07:49 +0200135
beierlma4a37f72020-06-26 12:55:01 -0400136 [ ! -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
beierlm2634cd32020-09-15 16:00:34 -0400137 [ -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c \
David Garciaa1376012020-10-19 15:42:42 +0200138 "juju bootstrap microk8s $CONTROLLER_NAME --config controller-service-type=loadbalancer --agent-version=$JUJU_AGENT_VERSION" \
beierlm2634cd32020-09-15 16:00:34 -0400139 && K8S_CLOUD_NAME=microk8s
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100140 fi
141
David Garcia42375212020-04-27 19:07:49 +0200142 if [ -v LXD_CLOUD ]; then
143 if [ ! -v LXD_CREDENTIALS ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100144 echo "The installer needs the LXD server certificate if the LXD is external"
145 exit 1
146 fi
147 else
148 LXDENDPOINT=$DEFAULT_IP
David Garcia42375212020-04-27 19:07:49 +0200149 LXD_CLOUD=~/.osm/lxd-cloud.yaml
150 LXD_CREDENTIALS=~/.osm/lxd-credentials.yaml
151 # Apply sysctl production values for optimal performance
152 sudo cp /usr/share/osm-devops/installers/60-lxd-production.conf /etc/sysctl.d/60-lxd-production.conf
153 sudo sysctl --system
154 # Install LXD snap
155 sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client
David Garciad9c90a02021-09-09 17:23:42 +0200156 sudo snap install lxd --channel $LXD_VERSION/stable
David Garcia42375212020-04-27 19:07:49 +0200157 # Configure LXD
158 sudo usermod -a -G lxd `whoami`
159 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"
160 sg lxd -c "lxd waitready"
161 DEFAULT_MTU=$(ip addr show $DEFAULT_IF | perl -ne 'if (/mtu\s(\d+)/) {print $1;}')
162 sg lxd -c "lxc profile device set default eth0 mtu $DEFAULT_MTU"
David Garciad00e49c2020-06-19 10:33:37 +0200163 sg lxd -c "lxc network set lxdbr0 bridge.mtu $DEFAULT_MTU"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100164
David Garcia42375212020-04-27 19:07:49 +0200165 cat << EOF > $LXD_CLOUD
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100166clouds:
167 lxd-cloud:
168 type: lxd
169 auth-types: [certificate]
170 endpoint: "https://$LXDENDPOINT:8443"
171 config:
172 ssl-hostname-verification: false
173EOF
David Garcia42375212020-04-27 19:07:49 +0200174 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"
175 local server_cert=`cat /var/snap/lxd/common/lxd/server.crt | sed 's/^/ /'`
176 local client_cert=`cat ~/.osm/client.crt | sed 's/^/ /'`
177 local client_key=`cat ~/.osm/client.key | sed 's/^/ /'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100178
David Garcia42375212020-04-27 19:07:49 +0200179 cat << EOF > $LXD_CREDENTIALS
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100180credentials:
181 lxd-cloud:
David Garcia42375212020-04-27 19:07:49 +0200182 lxd-cloud:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100183 auth-type: certificate
184 server-cert: |
185$server_cert
186 client-cert: |
187$client_cert
188 client-key: |
189$client_key
190EOF
David Garcia42375212020-04-27 19:07:49 +0200191 lxc config trust add local: ~/.osm/client.crt
192 fi
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100193
David Garcia42375212020-04-27 19:07:49 +0200194 juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force
195 juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS
196 sg lxd -c "lxd waitready"
beierlma4a37f72020-06-26 12:55:01 -0400197 juju controller-config features=[k8s-operators]
198}
199
200function wait_for_port(){
201 SERVICE=$1
202 INDEX=$2
203 TIME_TO_WAIT=30
204 start_time="$(date -u +%s)"
205 while true
206 do
207 now="$(date -u +%s)"
208 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
209 echo "Failed to expose external ${SERVICE} interface port"
210 exit 1
211 fi
212
beierlm9afb0ef2020-10-16 12:53:51 -0400213 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 -0400214 break
215 fi
216 sleep 1
217 done
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100218}
219
220function deploy_charmed_osm(){
beierlmf2782c52020-11-05 17:04:05 -0500221 if [ -v REGISTRY_INFO ] ; then
222 registry_parts=(${REGISTRY_INFO//@/ })
223 if [ ${#registry_parts[@]} -eq 1 ] ; then
224 # No credentials supplied
225 REGISTRY_USERNAME=""
226 REGISTRY_PASSWORD=""
227 REGISTRY_URL=${registry_parts[0]}
228 else
229 credentials=${registry_parts[0]}
230 credential_parts=(${credentials//:/ })
231 REGISTRY_USERNAME=${credential_parts[0]}
232 REGISTRY_PASSWORD=${credential_parts[1]}
233 REGISTRY_URL=${registry_parts[1]}
234 fi
235 # Ensure the URL ends with a /
236 case $REGISTRY_URL in
237 */) ;;
238 *) REGISTRY_URL=${REGISTRY_URL}/
239 esac
240 fi
241
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100242 echo "Creating OSM model"
David Garcia42375212020-04-27 19:07:49 +0200243 if [ -v KUBECFG ]; then
David Garciaef349d92020-12-10 21:16:12 +0100244 juju add-model $MODEL_NAME $K8S_CLOUD_NAME
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100245 else
David Garciaef349d92020-12-10 21:16:12 +0100246 sg ${KUBEGRP} -c "juju add-model $MODEL_NAME $K8S_CLOUD_NAME"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100247 fi
248 echo "Deploying OSM with charms"
beierlma4a37f72020-06-26 12:55:01 -0400249 images_overlay=""
beierlmf2782c52020-11-05 17:04:05 -0500250 if [ -v REGISTRY_URL ]; then
251 [ ! -v TAG ] && TAG='latest'
252 fi
beierlma4a37f72020-06-26 12:55:01 -0400253 [ -v TAG ] && generate_images_overlay && images_overlay="--overlay $IMAGES_OVERLAY_FILE"
beierlmf2782c52020-11-05 17:04:05 -0500254
David Garcia4bd2bf02021-05-05 19:01:43 +0200255 if [ -v OVERLAY ]; then
256 extra_overlay="--overlay $OVERLAY"
257 fi
258
David Garcia42375212020-04-27 19:07:49 +0200259 if [ -v BUNDLE ]; then
David Garcia4bd2bf02021-05-05 19:01:43 +0200260 juju deploy -m $MODEL_NAME $BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay $extra_overlay
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100261 else
David Garcia4bd2bf02021-05-05 19:01:43 +0200262 juju deploy -m $MODEL_NAME $OSM_BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay $extra_overlay
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100263 fi
beierlma4a37f72020-06-26 12:55:01 -0400264
beierlma4a37f72020-06-26 12:55:01 -0400265 if [ ! -v KUBECFG ]; then
beierlma4a37f72020-06-26 12:55:01 -0400266 API_SERVER=${DEFAULT_IP}
267 else
268 API_SERVER=$(kubectl config view --minify | grep server | cut -f 2- -d ":" | tr -d " ")
269 proto="$(echo $API_SERVER | grep :// | sed -e's,^\(.*://\).*,\1,g')"
270 url="$(echo ${API_SERVER/$proto/})"
271 user="$(echo $url | grep @ | cut -d@ -f1)"
272 hostport="$(echo ${url/$user@/} | cut -d/ -f1)"
273 API_SERVER="$(echo $hostport | sed -e 's,:.*,,g')"
274 fi
David Garciade5fc1d2020-09-02 11:40:12 +0200275 # Expose OSM services
David Garciac395a452021-05-05 14:22:26 +0200276 juju config -m $MODEL_NAME nbi site_url=https://nbi.${API_SERVER}.nip.io
277 juju config -m $MODEL_NAME ng-ui site_url=https://ui.${API_SERVER}.nip.io
278 juju config -m $MODEL_NAME grafana site_url=https://grafana.${API_SERVER}.nip.io
279 juju config -m $MODEL_NAME prometheus site_url=https://prometheus.${API_SERVER}.nip.io
David Garciaef349d92020-12-10 21:16:12 +0100280
David Garcia49379ce2021-02-24 13:48:22 +0100281 echo "Waiting for deployment to finish..."
282 check_osm_deployed
283 echo "OSM with charms deployed"
beierlm9afb0ef2020-10-16 12:53:51 -0400284}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100285
286function check_osm_deployed() {
beierlma4a37f72020-06-26 12:55:01 -0400287 TIME_TO_WAIT=600
288 start_time="$(date -u +%s)"
David Garcia02a5eb92020-11-28 14:41:22 +0100289 total_service_count=14
beierlm7e54dfc2020-09-24 15:27:53 -0400290 previous_count=0
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100291 while true
292 do
David Garciaef349d92020-12-10 21:16:12 +0100293 service_count=$(juju status -m $MODEL_NAME | grep kubernetes | grep active | wc -l)
beierlma4a37f72020-06-26 12:55:01 -0400294 echo "$service_count / $total_service_count services active"
295 if [ $service_count -eq $total_service_count ]; then
296 break
297 fi
beierlm7e54dfc2020-09-24 15:27:53 -0400298 if [ $service_count -ne $previous_count ]; then
299 previous_count=$service_count
300 start_time="$(date -u +%s)"
301 fi
beierlma4a37f72020-06-26 12:55:01 -0400302 now="$(date -u +%s)"
303 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
304 echo "Timed out waiting for OSM services to become ready"
305 exit 1
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100306 fi
307 sleep 10
308 done
309}
310
311function create_overlay() {
David Garcia42375212020-04-27 19:07:49 +0200312 sudo snap install jq
David Garciac8660ad2020-12-16 13:13:20 +0100313 sudo snap install yq
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100314 local HOME=/home/$USER
calvinosanc183ea27a2021-01-04 11:42:18 +0100315 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
David Garciac753dc52021-03-17 15:28:47 +0100316 local vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
calvinosanc183ea27a2021-01-04 11:42:18 +0100317 local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
318 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 +0100319 local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
320 local vca_cloud="lxd-cloud"
321 # Get the VCA Certificate
calvinosanc183ea27a2021-01-04 11:42:18 +0100322 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 +0100323
324 # Calculate the default route of this machine
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200325 local DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100326
327 # Generate a new overlay.yaml, overriding any existing one
David Garcia42375212020-04-27 19:07:49 +0200328 cat << EOF > /tmp/vca-overlay.yaml
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100329applications:
David Garcia49379ce2021-02-24 13:48:22 +0100330 lcm:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100331 options:
332 vca_user: $vca_user
David Garciac753dc52021-03-17 15:28:47 +0100333 vca_secret: $vca_secret
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100334 vca_host: $vca_host
335 vca_port: $vca_port
336 vca_pubkey: $vca_pubkey
337 vca_cacert: $vca_cacert
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100338 vca_cloud: $vca_cloud
beierlma4a37f72020-06-26 12:55:01 -0400339 vca_k8s_cloud: $K8S_CLOUD_NAME
David Garcia49379ce2021-02-24 13:48:22 +0100340 mon:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100341 options:
342 vca_user: $vca_user
David Garciac753dc52021-03-17 15:28:47 +0100343 vca_secret: $vca_secret
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100344 vca_host: $vca_host
345 vca_cacert: $vca_cacert
346EOF
David Garcia42375212020-04-27 19:07:49 +0200347 mv /tmp/vca-overlay.yaml ~/.osm/
348 OSM_VCA_HOST=$vca_host
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100349}
350
David Garcia69388c22020-05-07 12:14:19 +0200351function generate_images_overlay(){
David Garcia49379ce2021-02-24 13:48:22 +0100352 echo "applications:" > /tmp/images-overlay.yaml
beierlmf2782c52020-11-05 17:04:05 -0500353
David Garcia49379ce2021-02-24 13:48:22 +0100354 charms_with_resources="nbi lcm mon pol ng-ui ro pla keystone"
355 for charm in $charms_with_resources; do
356 cat << EOF > /tmp/${charm}_registry.yaml
357registrypath: ${REGISTRY_URL}opensourcemano/${charm}:$TAG
beierlmf2782c52020-11-05 17:04:05 -0500358EOF
David Garcia49379ce2021-02-24 13:48:22 +0100359 if [ ! -z "$REGISTRY_USERNAME" ] ; then
360 echo username: $REGISTRY_USERNAME >> /tmp/${charm}_registry.yaml
361 echo password: $REGISTRY_PASSWORD >> /tmp/${charm}_registry.yaml
362 fi
beierlmf2782c52020-11-05 17:04:05 -0500363
David Garcia49379ce2021-02-24 13:48:22 +0100364 cat << EOF >> /tmp/images-overlay.yaml
365 ${charm}:
David Garciaef349d92020-12-10 21:16:12 +0100366 resources:
David Garcia49379ce2021-02-24 13:48:22 +0100367 image: /tmp/${charm}_registry.yaml
368
David Garcia69388c22020-05-07 12:14:19 +0200369EOF
David Garcia49379ce2021-02-24 13:48:22 +0100370 done
371
David Garcia69388c22020-05-07 12:14:19 +0200372 mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE
373}
374
David Garcia107010b2021-01-15 12:58:59 +0100375function refresh_osmclient_snap() {
376 osmclient_snap_install_refresh refresh
377}
378
379function install_osm_client_snap() {
380 osmclient_snap_install_refresh install
381}
382
383function osmclient_snap_install_refresh() {
384 channel_preference="stable candidate beta edge"
385 for channel in $channel_preference; do
386 echo "Trying to install osmclient from channel $OSMCLIENT_VERSION/$channel"
387 sudo snap $1 osmclient --channel $OSMCLIENT_VERSION/$channel 2> /dev/null && echo osmclient snap installed && break
388 done
389}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100390function install_osmclient() {
David Garcia107010b2021-01-15 12:58:59 +0100391 snap info osmclient | grep -E ^installed: && refresh_osmclient_snap || install_osm_client_snap
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100392}
393
beierlmdf6de3d2020-12-16 08:46:40 -0500394function add_local_k8scluster() {
395 osm --all-projects vim-create \
396 --name _system-osm-vim \
397 --account_type dummy \
398 --auth_url http://dummy \
399 --user osm --password osm --tenant osm \
400 --description "dummy" \
401 --config '{management_network_name: mgmt}'
402 tmpfile=$(mktemp --tmpdir=${HOME})
403 cp ${KUBECONFIG} ${tmpfile}
404 osm --all-projects k8scluster-add \
405 --creds ${tmpfile} \
406 --vim _system-osm-vim \
407 --k8s-nets '{"net1": null}' \
408 --version '1.19' \
409 --description "OSM Internal Cluster" \
410 _system-osm-k8s
411 rm -f ${tmpfile}
412}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100413
414function install_microstack() {
415 sudo snap install microstack --classic --beta
416 sudo microstack.init --auto
417 wget https://cloud-images.ubuntu.com/releases/16.04/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img -P ~/.osm/
418 microstack.openstack image create \
David Garcia42375212020-04-27 19:07:49 +0200419 --public \
420 --disk-format qcow2 \
421 --container-format bare \
422 --file ~/.osm/ubuntu-16.04-server-cloudimg-amd64-disk1.img \
423 ubuntu1604
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100424 ssh-keygen -t rsa -N "" -f ~/.ssh/microstack
425 microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack
David Garcia49379ce2021-02-24 13:48:22 +0100426 export OSM_HOSTNAME=`juju status -m $MODEL_NAME --format json | jq -rc '.applications."nbi".address'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100427 osm vim-create --name microstack-site \
David Garcia42375212020-04-27 19:07:49 +0200428 --user admin \
429 --password keystone \
430 --auth_url http://10.20.20.1:5000/v3 \
431 --tenant admin \
432 --account_type openstack \
433 --config='{security_groups: default,
434 keypair: microstack,
435 project_name: admin,
436 user_domain_name: default,
437 region_name: microstack,
438 insecure: True,
439 availability_zone: nova,
440 version: 3}'
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100441}
442
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200443DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100444DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
445
446check_arguments $@
David Garcia42375212020-04-27 19:07:49 +0200447mkdir -p ~/.osm
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100448install_snaps
449bootstrap_k8s_lxd
David Garcia1bc71022021-05-03 18:27:18 +0200450create_overlay
451if [ -v ONLY_VCA ]; then
452 HOME=/home/$USER
453 vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
454 vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
455 vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
456 vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 2)
457 vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
458 vca_cloud="lxd-cloud"
459 vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.ca-cert - | base64 | tr -d \\n)
460 hostname=`cat /etc/hostname`
beierlmdf6de3d2020-12-16 08:46:40 -0500461
David Garcia1bc71022021-05-03 18:27:18 +0200462 echo "Use the following command to register the installed VCA to your OSM:"
463 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"
464else
465 deploy_charmed_osm
466 install_osmclient
467 export OSM_HOSTNAME=$(juju config -m $MODEL_NAME nbi site_url | sed "s/http.*\?:\/\///"):443
468 sleep 10
469 add_local_k8scluster
470 if [ -v MICROSTACK ]; then
471 install_microstack
472 fi
473
474 echo "Your installation is now complete, follow these steps for configuring the osmclient:"
475 echo
476 echo "1. Create the OSM_HOSTNAME environment variable with the NBI IP"
477 echo
478 echo "export OSM_HOSTNAME=$OSM_HOSTNAME"
479 echo
480 echo "2. Add the previous command to your .bashrc for other Shell sessions"
481 echo
482 echo "echo \"export OSM_HOSTNAME=$OSM_HOSTNAME\" >> ~/.bashrc"
483 echo
484 echo "DONE"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100485fi
beierlma4a37f72020-06-26 12:55:01 -0400486