blob: 8a517085ec293222ecca0af80a7df8e08a9a9092 [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 Garcia49379ce2021-02-24 13:48:22 +010018JUJU_AGENT_VERSION=2.8.9
David Garcia42375212020-04-27 19:07:49 +020019K8S_CLOUD_NAME="k8s-cloud"
beierlm3749e312020-07-02 14:21:09 -040020KUBECTL="microk8s.kubectl"
beierlm481ae7d2020-12-14 09:59:19 -050021MICROK8S_VERSION=1.19
David Garcia107010b2021-01-15 12:58:59 +010022OSMCLIENT_VERSION=9.0
David Garcia69388c22020-05-07 12:14:19 +020023IMAGES_OVERLAY_FILE=~/.osm/images-overlay.yaml
beierlmf2782c52020-11-05 17:04:05 -050024PATH=/snap/bin:${PATH}
25
David Garciaef349d92020-12-10 21:16:12 +010026MODEL_NAME=osm
27
David Garcia27bfcfd2021-05-19 18:04:31 +020028OSM_BUNDLE=cs:osm-64
29OSM_HA_BUNDLE=cs:osm-ha-49
David Garcia49379ce2021-02-24 13:48:22 +010030TAG=testing-daily
David Garciaab11f842020-12-16 17:25:15 +010031
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010032function check_arguments(){
33 while [ $# -gt 0 ] ; do
David Garcia42375212020-04-27 19:07:49 +020034 case $1 in
35 --bundle) BUNDLE="$2" ;;
David Garcia4bd2bf02021-05-05 19:01:43 +020036 --overlay) OVERLAY="$2" ;;
Dominik Fleischmann7a97a4c2020-06-04 10:52:05 +020037 --k8s) KUBECFG="$2" ;;
38 --vca) CONTROLLER="$2" ;;
39 --lxd) LXD_CLOUD="$2" ;;
40 --lxd-cred) LXD_CREDENTIALS="$2" ;;
David Garcia69388c22020-05-07 12:14:19 +020041 --microstack) MICROSTACK=y ;;
David Garciaab11f842020-12-16 17:25:15 +010042 --ha) BUNDLE=$OSM_HA_BUNDLE ;;
David Garcia69388c22020-05-07 12:14:19 +020043 --tag) TAG="$2" ;;
beierlmf2782c52020-11-05 17:04:05 -050044 --registry) REGISTRY_INFO="$2" ;;
David Garcia1bc71022021-05-03 18:27:18 +020045 --only-vca) ONLY_VCA=y ;;
David Garcia42375212020-04-27 19:07:49 +020046 esac
47 shift
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010048 done
49
David Garcia42375212020-04-27 19:07:49 +020050 # echo $BUNDLE $KUBECONFIG $LXDENDPOINT
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010051}
beierlma4a37f72020-06-26 12:55:01 -040052
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010053function install_snaps(){
beierlma4a37f72020-06-26 12:55:01 -040054 if [ ! -v KUBECFG ]; then
beierlm481ae7d2020-12-14 09:59:19 -050055 sudo snap install microk8s --classic --channel=${MICROK8S_VERSION}/stable
David Garcia9a59e3d2021-02-08 15:18:27 +010056 sudo cat /var/snap/microk8s/current/args/kube-apiserver | grep advertise-address || (
David Garcia107010b2021-01-15 12:58:59 +010057 echo "--advertise-address $DEFAULT_IP" | sudo tee -a /var/snap/microk8s/current/args/kube-apiserver
58 microk8s.stop
59 microk8s.start
60 )
beierlma4a37f72020-06-26 12:55:01 -040061 sudo usermod -a -G microk8s `whoami`
62 mkdir -p ~/.kube
63 sudo chown -f -R `whoami` ~/.kube
64 KUBEGRP="microk8s"
beierlmf2782c52020-11-05 17:04:05 -050065 sg ${KUBEGRP} -c "microk8s status --wait-ready"
beierlmdf6de3d2020-12-16 08:46:40 -050066 KUBECONFIG=~/.osm/microk8s-config.yaml
David Garcia107010b2021-01-15 12:58:59 +010067 sg ${KUBEGRP} -c "microk8s config" | tee ${KUBECONFIG}
beierlma4a37f72020-06-26 12:55:01 -040068 else
69 KUBECTL="kubectl"
70 sudo snap install kubectl --classic
71 export KUBECONFIG=${KUBECFG}
72 KUBEGRP=$(id -g -n)
73 fi
74 sudo snap install juju --classic --channel=2.8/stable
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010075}
76
77function bootstrap_k8s_lxd(){
David Garcia42375212020-04-27 19:07:49 +020078 [ -v CONTROLLER ] && ADD_K8S_OPTS="--controller ${CONTROLLER}" && CONTROLLER_NAME=$CONTROLLER
beierlma4a37f72020-06-26 12:55:01 -040079 [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="osm-vca"
80
81 if [ -v BOOTSTRAP_NEEDED ]; then
82 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
83 if [ $CONTROLLER_PRESENT -ge 1 ]; then
84 cat << EOF
85Threre is already a VCA present with the installer reserved name of "${CONTROLLER_NAME}".
86You may either explicitly use this VCA with the "--vca ${CONTROLLER_NAME}" option, or remove it
87using this command:
88
89 juju destroy-controller --release-storage --destroy-all-models -y ${CONTROLLER_NAME}
90
91Please retry the installation once this conflict has been resolved.
92EOF
93 exit 1
94 fi
beierlm9afb0ef2020-10-16 12:53:51 -040095 else
96 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
97 if [ $CONTROLLER_PRESENT -le 0 ]; then
98 cat << EOF
99Threre is no VCA present with the name "${CONTROLLER_NAME}". Please specify a VCA
100that exists, or remove the --vca ${CONTROLLER_NAME} option.
101
102Please retry the installation with one of the solutions applied.
103EOF
104 exit 1
105 fi
beierlma4a37f72020-06-26 12:55:01 -0400106 fi
David Garcia42375212020-04-27 19:07:49 +0200107
108 if [ -v KUBECFG ]; then
109 cat $KUBECFG | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
beierlm2634cd32020-09-15 16:00:34 -0400110 [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME \
111 --config controller-service-type=loadbalancer \
David Garciaa1376012020-10-19 15:42:42 +0200112 --agent-version=$JUJU_AGENT_VERSION
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100113 else
beierlma4a37f72020-06-26 12:55:01 -0400114 sg ${KUBEGRP} -c "echo ${DEFAULT_IP}-${DEFAULT_IP} | microk8s.enable metallb"
beierlm9afb0ef2020-10-16 12:53:51 -0400115 sg ${KUBEGRP} -c "microk8s.enable ingress"
beierlma4a37f72020-06-26 12:55:01 -0400116 sg ${KUBEGRP} -c "microk8s.enable storage dns"
117 TIME_TO_WAIT=30
118 start_time="$(date -u +%s)"
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200119 while true
120 do
beierlma4a37f72020-06-26 12:55:01 -0400121 now="$(date -u +%s)"
122 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
123 echo "Microk8s storage failed to enable"
124 sg ${KUBEGRP} -c "microk8s.status"
125 exit 1
126 fi
127 storage_status=`sg ${KUBEGRP} -c "microk8s.status -a storage"`
128 if [[ $storage_status == "enabled" ]]; then
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200129 break
130 fi
131 sleep 1
132 done
David Garcia42375212020-04-27 19:07:49 +0200133
beierlma4a37f72020-06-26 12:55:01 -0400134 [ ! -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
beierlm2634cd32020-09-15 16:00:34 -0400135 [ -v BOOTSTRAP_NEEDED ] && sg ${KUBEGRP} -c \
David Garciaa1376012020-10-19 15:42:42 +0200136 "juju bootstrap microk8s $CONTROLLER_NAME --config controller-service-type=loadbalancer --agent-version=$JUJU_AGENT_VERSION" \
beierlm2634cd32020-09-15 16:00:34 -0400137 && K8S_CLOUD_NAME=microk8s
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100138 fi
139
David Garcia42375212020-04-27 19:07:49 +0200140 if [ -v LXD_CLOUD ]; then
141 if [ ! -v LXD_CREDENTIALS ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100142 echo "The installer needs the LXD server certificate if the LXD is external"
143 exit 1
144 fi
145 else
146 LXDENDPOINT=$DEFAULT_IP
David Garcia42375212020-04-27 19:07:49 +0200147 LXD_CLOUD=~/.osm/lxd-cloud.yaml
148 LXD_CREDENTIALS=~/.osm/lxd-credentials.yaml
149 # Apply sysctl production values for optimal performance
150 sudo cp /usr/share/osm-devops/installers/60-lxd-production.conf /etc/sysctl.d/60-lxd-production.conf
151 sudo sysctl --system
152 # Install LXD snap
153 sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client
154 sudo snap install lxd
David Garcia42375212020-04-27 19:07:49 +0200155 # Configure LXD
156 sudo usermod -a -G lxd `whoami`
157 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"
158 sg lxd -c "lxd waitready"
159 DEFAULT_MTU=$(ip addr show $DEFAULT_IF | perl -ne 'if (/mtu\s(\d+)/) {print $1;}')
160 sg lxd -c "lxc profile device set default eth0 mtu $DEFAULT_MTU"
David Garciad00e49c2020-06-19 10:33:37 +0200161 sg lxd -c "lxc network set lxdbr0 bridge.mtu $DEFAULT_MTU"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100162
David Garcia42375212020-04-27 19:07:49 +0200163 cat << EOF > $LXD_CLOUD
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100164clouds:
165 lxd-cloud:
166 type: lxd
167 auth-types: [certificate]
168 endpoint: "https://$LXDENDPOINT:8443"
169 config:
170 ssl-hostname-verification: false
171EOF
David Garcia42375212020-04-27 19:07:49 +0200172 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"
173 local server_cert=`cat /var/snap/lxd/common/lxd/server.crt | sed 's/^/ /'`
174 local client_cert=`cat ~/.osm/client.crt | sed 's/^/ /'`
175 local client_key=`cat ~/.osm/client.key | sed 's/^/ /'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100176
David Garcia42375212020-04-27 19:07:49 +0200177 cat << EOF > $LXD_CREDENTIALS
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100178credentials:
179 lxd-cloud:
David Garcia42375212020-04-27 19:07:49 +0200180 lxd-cloud:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100181 auth-type: certificate
182 server-cert: |
183$server_cert
184 client-cert: |
185$client_cert
186 client-key: |
187$client_key
188EOF
David Garcia42375212020-04-27 19:07:49 +0200189 lxc config trust add local: ~/.osm/client.crt
190 fi
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100191
David Garcia42375212020-04-27 19:07:49 +0200192 juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force
193 juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS
194 sg lxd -c "lxd waitready"
beierlma4a37f72020-06-26 12:55:01 -0400195 juju controller-config features=[k8s-operators]
196}
197
198function wait_for_port(){
199 SERVICE=$1
200 INDEX=$2
201 TIME_TO_WAIT=30
202 start_time="$(date -u +%s)"
203 while true
204 do
205 now="$(date -u +%s)"
206 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
207 echo "Failed to expose external ${SERVICE} interface port"
208 exit 1
209 fi
210
beierlm9afb0ef2020-10-16 12:53:51 -0400211 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 -0400212 break
213 fi
214 sleep 1
215 done
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 Garcia4bd2bf02021-05-05 19:01:43 +0200258 juju deploy -m $MODEL_NAME $BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay $extra_overlay
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100259 else
David Garcia4bd2bf02021-05-05 19:01:43 +0200260 juju deploy -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
281 echo "OSM with charms deployed"
beierlm9afb0ef2020-10-16 12:53:51 -0400282}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100283
284function check_osm_deployed() {
beierlma4a37f72020-06-26 12:55:01 -0400285 TIME_TO_WAIT=600
286 start_time="$(date -u +%s)"
David Garcia02a5eb92020-11-28 14:41:22 +0100287 total_service_count=14
beierlm7e54dfc2020-09-24 15:27:53 -0400288 previous_count=0
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100289 while true
290 do
David Garciaef349d92020-12-10 21:16:12 +0100291 service_count=$(juju status -m $MODEL_NAME | grep kubernetes | grep active | wc -l)
beierlma4a37f72020-06-26 12:55:01 -0400292 echo "$service_count / $total_service_count services active"
293 if [ $service_count -eq $total_service_count ]; then
294 break
295 fi
beierlm7e54dfc2020-09-24 15:27:53 -0400296 if [ $service_count -ne $previous_count ]; then
297 previous_count=$service_count
298 start_time="$(date -u +%s)"
299 fi
beierlma4a37f72020-06-26 12:55:01 -0400300 now="$(date -u +%s)"
301 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
302 echo "Timed out waiting for OSM services to become ready"
303 exit 1
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100304 fi
305 sleep 10
306 done
307}
308
309function create_overlay() {
David Garcia42375212020-04-27 19:07:49 +0200310 sudo snap install jq
David Garciac8660ad2020-12-16 13:13:20 +0100311 sudo snap install yq
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100312 local HOME=/home/$USER
calvinosanc183ea27a2021-01-04 11:42:18 +0100313 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
David Garciac753dc52021-03-17 15:28:47 +0100314 local vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
calvinosanc183ea27a2021-01-04 11:42:18 +0100315 local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
316 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 +0100317 local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
318 local vca_cloud="lxd-cloud"
319 # Get the VCA Certificate
calvinosanc183ea27a2021-01-04 11:42:18 +0100320 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 +0100321
322 # Calculate the default route of this machine
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200323 local DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100324
325 # Generate a new overlay.yaml, overriding any existing one
David Garcia42375212020-04-27 19:07:49 +0200326 cat << EOF > /tmp/vca-overlay.yaml
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100327applications:
David Garcia49379ce2021-02-24 13:48:22 +0100328 lcm:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100329 options:
330 vca_user: $vca_user
David Garciac753dc52021-03-17 15:28:47 +0100331 vca_secret: $vca_secret
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100332 vca_host: $vca_host
333 vca_port: $vca_port
334 vca_pubkey: $vca_pubkey
335 vca_cacert: $vca_cacert
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100336 vca_cloud: $vca_cloud
beierlma4a37f72020-06-26 12:55:01 -0400337 vca_k8s_cloud: $K8S_CLOUD_NAME
David Garcia49379ce2021-02-24 13:48:22 +0100338 mon:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100339 options:
340 vca_user: $vca_user
David Garciac753dc52021-03-17 15:28:47 +0100341 vca_secret: $vca_secret
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100342 vca_host: $vca_host
343 vca_cacert: $vca_cacert
344EOF
David Garcia42375212020-04-27 19:07:49 +0200345 mv /tmp/vca-overlay.yaml ~/.osm/
346 OSM_VCA_HOST=$vca_host
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100347}
348
David Garcia69388c22020-05-07 12:14:19 +0200349function generate_images_overlay(){
David Garcia49379ce2021-02-24 13:48:22 +0100350 echo "applications:" > /tmp/images-overlay.yaml
beierlmf2782c52020-11-05 17:04:05 -0500351
David Garcia49379ce2021-02-24 13:48:22 +0100352 charms_with_resources="nbi lcm mon pol ng-ui ro pla keystone"
353 for charm in $charms_with_resources; do
354 cat << EOF > /tmp/${charm}_registry.yaml
355registrypath: ${REGISTRY_URL}opensourcemano/${charm}:$TAG
beierlmf2782c52020-11-05 17:04:05 -0500356EOF
David Garcia49379ce2021-02-24 13:48:22 +0100357 if [ ! -z "$REGISTRY_USERNAME" ] ; then
358 echo username: $REGISTRY_USERNAME >> /tmp/${charm}_registry.yaml
359 echo password: $REGISTRY_PASSWORD >> /tmp/${charm}_registry.yaml
360 fi
beierlmf2782c52020-11-05 17:04:05 -0500361
David Garcia49379ce2021-02-24 13:48:22 +0100362 cat << EOF >> /tmp/images-overlay.yaml
363 ${charm}:
David Garciaef349d92020-12-10 21:16:12 +0100364 resources:
David Garcia49379ce2021-02-24 13:48:22 +0100365 image: /tmp/${charm}_registry.yaml
366
David Garcia69388c22020-05-07 12:14:19 +0200367EOF
David Garcia49379ce2021-02-24 13:48:22 +0100368 done
369
David Garcia69388c22020-05-07 12:14:19 +0200370 mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE
371}
372
David Garcia107010b2021-01-15 12:58:59 +0100373function refresh_osmclient_snap() {
374 osmclient_snap_install_refresh refresh
375}
376
377function install_osm_client_snap() {
378 osmclient_snap_install_refresh install
379}
380
381function osmclient_snap_install_refresh() {
382 channel_preference="stable candidate beta edge"
383 for channel in $channel_preference; do
384 echo "Trying to install osmclient from channel $OSMCLIENT_VERSION/$channel"
385 sudo snap $1 osmclient --channel $OSMCLIENT_VERSION/$channel 2> /dev/null && echo osmclient snap installed && break
386 done
387}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100388function install_osmclient() {
David Garcia107010b2021-01-15 12:58:59 +0100389 snap info osmclient | grep -E ^installed: && refresh_osmclient_snap || install_osm_client_snap
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100390}
391
beierlmdf6de3d2020-12-16 08:46:40 -0500392function add_local_k8scluster() {
393 osm --all-projects vim-create \
394 --name _system-osm-vim \
395 --account_type dummy \
396 --auth_url http://dummy \
397 --user osm --password osm --tenant osm \
398 --description "dummy" \
399 --config '{management_network_name: mgmt}'
400 tmpfile=$(mktemp --tmpdir=${HOME})
401 cp ${KUBECONFIG} ${tmpfile}
402 osm --all-projects k8scluster-add \
403 --creds ${tmpfile} \
404 --vim _system-osm-vim \
405 --k8s-nets '{"net1": null}' \
406 --version '1.19' \
407 --description "OSM Internal Cluster" \
408 _system-osm-k8s
409 rm -f ${tmpfile}
410}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100411
412function install_microstack() {
413 sudo snap install microstack --classic --beta
414 sudo microstack.init --auto
415 wget https://cloud-images.ubuntu.com/releases/16.04/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img -P ~/.osm/
416 microstack.openstack image create \
David Garcia42375212020-04-27 19:07:49 +0200417 --public \
418 --disk-format qcow2 \
419 --container-format bare \
420 --file ~/.osm/ubuntu-16.04-server-cloudimg-amd64-disk1.img \
421 ubuntu1604
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100422 ssh-keygen -t rsa -N "" -f ~/.ssh/microstack
423 microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack
David Garcia49379ce2021-02-24 13:48:22 +0100424 export OSM_HOSTNAME=`juju status -m $MODEL_NAME --format json | jq -rc '.applications."nbi".address'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100425 osm vim-create --name microstack-site \
David Garcia42375212020-04-27 19:07:49 +0200426 --user admin \
427 --password keystone \
428 --auth_url http://10.20.20.1:5000/v3 \
429 --tenant admin \
430 --account_type openstack \
431 --config='{security_groups: default,
432 keypair: microstack,
433 project_name: admin,
434 user_domain_name: default,
435 region_name: microstack,
436 insecure: True,
437 availability_zone: nova,
438 version: 3}'
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100439}
440
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200441DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100442DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
443
444check_arguments $@
David Garcia42375212020-04-27 19:07:49 +0200445mkdir -p ~/.osm
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100446install_snaps
447bootstrap_k8s_lxd
David Garcia1bc71022021-05-03 18:27:18 +0200448create_overlay
449if [ -v ONLY_VCA ]; then
450 HOME=/home/$USER
451 vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
452 vca_secret=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
453 vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
454 vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 2)
455 vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
456 vca_cloud="lxd-cloud"
457 vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.ca-cert - | base64 | tr -d \\n)
458 hostname=`cat /etc/hostname`
beierlmdf6de3d2020-12-16 08:46:40 -0500459
David Garcia1bc71022021-05-03 18:27:18 +0200460 echo "Use the following command to register the installed VCA to your OSM:"
461 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"
462else
463 deploy_charmed_osm
464 install_osmclient
465 export OSM_HOSTNAME=$(juju config -m $MODEL_NAME nbi site_url | sed "s/http.*\?:\/\///"):443
466 sleep 10
467 add_local_k8scluster
468 if [ -v MICROSTACK ]; then
469 install_microstack
470 fi
471
472 echo "Your installation is now complete, follow these steps for configuring the osmclient:"
473 echo
474 echo "1. Create the OSM_HOSTNAME environment variable with the NBI IP"
475 echo
476 echo "export OSM_HOSTNAME=$OSM_HOSTNAME"
477 echo
478 echo "2. Add the previous command to your .bashrc for other Shell sessions"
479 echo
480 echo "echo \"export OSM_HOSTNAME=$OSM_HOSTNAME\" >> ~/.bashrc"
481 echo
482 echo "DONE"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100483fi
beierlma4a37f72020-06-26 12:55:01 -0400484