blob: 4c5c18f0514a772e87649912cd966f58acb5bc7a [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 Garciaaac52e02021-10-05 16:13:08 +020018LXD_VERSION=4.0
19JUJU_VERSION=2.8
20JUJU_AGENT_VERSION=2.8.11
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 Garciab50dabe2021-01-15 12:58:59 +010024OSMCLIENT_VERSION=9.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 Garciaab11f842020-12-16 17:25:15 +010030OSM_BUNDLE=cs:osm-54
31OSM_HA_BUNDLE=cs:osm-ha-40
David Garciab50dabe2021-01-15 12:58:59 +010032TAG=9
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" ;;
Dominik Fleischmann7a97a4c2020-06-04 10:52:05 +020038 --k8s) KUBECFG="$2" ;;
39 --vca) CONTROLLER="$2" ;;
40 --lxd) LXD_CLOUD="$2" ;;
41 --lxd-cred) LXD_CREDENTIALS="$2" ;;
David Garcia69388c22020-05-07 12:14:19 +020042 --microstack) MICROSTACK=y ;;
David Garciaab11f842020-12-16 17:25:15 +010043 --ha) BUNDLE=$OSM_HA_BUNDLE ;;
David Garcia69388c22020-05-07 12:14:19 +020044 --tag) TAG="$2" ;;
beierlmf2782c52020-11-05 17:04:05 -050045 --registry) REGISTRY_INFO="$2" ;;
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 Garcia9e84fea2021-02-08 15:18:27 +010056 sudo cat /var/snap/microk8s/current/args/kube-apiserver | grep advertise-address || (
David Garciab50dabe2021-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 Garciab50dabe2021-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
David Garciaaac52e02021-10-05 16:13:08 +020074 sudo snap install juju --classic --channel=$JUJU_VERSION/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
David Garciaaac52e02021-10-05 16:13:08 +0200154 sudo snap install lxd --channel $LXD_VERSION/stable
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 create_overlay
241 echo "Creating OSM model"
David Garcia42375212020-04-27 19:07:49 +0200242 if [ -v KUBECFG ]; then
David Garciaef349d92020-12-10 21:16:12 +0100243 juju add-model $MODEL_NAME $K8S_CLOUD_NAME
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100244 else
David Garciaef349d92020-12-10 21:16:12 +0100245 sg ${KUBEGRP} -c "juju add-model $MODEL_NAME $K8S_CLOUD_NAME"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100246 fi
247 echo "Deploying OSM with charms"
beierlma4a37f72020-06-26 12:55:01 -0400248 images_overlay=""
beierlmf2782c52020-11-05 17:04:05 -0500249 if [ -v REGISTRY_URL ]; then
250 [ ! -v TAG ] && TAG='latest'
251 fi
beierlma4a37f72020-06-26 12:55:01 -0400252 [ -v TAG ] && generate_images_overlay && images_overlay="--overlay $IMAGES_OVERLAY_FILE"
beierlmf2782c52020-11-05 17:04:05 -0500253
David Garcia42375212020-04-27 19:07:49 +0200254 if [ -v BUNDLE ]; then
David Garciaef349d92020-12-10 21:16:12 +0100255 juju deploy -m $MODEL_NAME $BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100256 else
David Garciaab11f842020-12-16 17:25:15 +0100257 juju deploy -m $MODEL_NAME $OSM_BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100258 fi
beierlma4a37f72020-06-26 12:55:01 -0400259
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100260 echo "Waiting for deployment to finish..."
beierlma4a37f72020-06-26 12:55:01 -0400261 check_osm_deployed
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100262 echo "OSM with charms deployed"
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
273
David Garciade5fc1d2020-09-02 11:40:12 +0200274 # Expose OSM services
David Garciaef349d92020-12-10 21:16:12 +0100275 # Expose NBI
beierlm1d704d92021-05-06 13:58:24 -0400276 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
David Garciaef349d92020-12-10 21:16:12 +0100278
beierlm9afb0ef2020-10-16 12:53:51 -0400279 # Expose Grafana
beierlm1d704d92021-05-06 13:58:24 -0400280 juju config -m $MODEL_NAME grafana-k8s juju-external-hostname=grafana.${API_SERVER}.nip.io
David Garciaef349d92020-12-10 21:16:12 +0100281 juju expose -m $MODEL_NAME grafana-k8s
beierlm9afb0ef2020-10-16 12:53:51 -0400282 wait_for_port grafana-k8s 0
283
beierlm9afb0ef2020-10-16 12:53:51 -0400284 # Expose Prometheus
beierlm1d704d92021-05-06 13:58:24 -0400285 juju config -m $MODEL_NAME prometheus-k8s juju-external-hostname=prometheus.${API_SERVER}.nip.io
David Garciaef349d92020-12-10 21:16:12 +0100286 juju expose -m $MODEL_NAME prometheus-k8s
287 wait_for_port prometheus-k8s 1
beierlma4a37f72020-06-26 12:55:01 -0400288
David Garciade5fc1d2020-09-02 11:40:12 +0200289 # Apply annotations
beierlm9afb0ef2020-10-16 12:53:51 -0400290 sg ${KUBEGRP} -c "${KUBECTL} annotate ingresses.networking nginx.ingress.kubernetes.io/proxy-body-size=0 -n osm -l juju-app=ng-ui"
beierlm9afb0ef2020-10-16 12:53:51 -0400291}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100292
293function check_osm_deployed() {
beierlma4a37f72020-06-26 12:55:01 -0400294 TIME_TO_WAIT=600
295 start_time="$(date -u +%s)"
David Garcia02a5eb92020-11-28 14:41:22 +0100296 total_service_count=14
beierlm7e54dfc2020-09-24 15:27:53 -0400297 previous_count=0
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100298 while true
299 do
David Garciaef349d92020-12-10 21:16:12 +0100300 service_count=$(juju status -m $MODEL_NAME | grep kubernetes | grep active | wc -l)
beierlma4a37f72020-06-26 12:55:01 -0400301 echo "$service_count / $total_service_count services active"
302 if [ $service_count -eq $total_service_count ]; then
303 break
304 fi
beierlm7e54dfc2020-09-24 15:27:53 -0400305 if [ $service_count -ne $previous_count ]; then
306 previous_count=$service_count
307 start_time="$(date -u +%s)"
308 fi
beierlma4a37f72020-06-26 12:55:01 -0400309 now="$(date -u +%s)"
310 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
311 echo "Timed out waiting for OSM services to become ready"
312 exit 1
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100313 fi
314 sleep 10
315 done
316}
317
318function create_overlay() {
David Garcia42375212020-04-27 19:07:49 +0200319 sudo snap install jq
David Garciac8660ad2020-12-16 13:13:20 +0100320 sudo snap install yq
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100321 local HOME=/home/$USER
calvinosanc183dff822021-01-04 11:50:09 +0100322 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.user - )
323 local vca_password=$(cat $HOME/.local/share/juju/accounts.yaml | yq e .controllers.$CONTROLLER_NAME.password - )
324 local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq e .controllers.$CONTROLLER_NAME.api-endpoints[0] - | cut -d ":" -f 1)
325 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 +0100326 local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
327 local vca_cloud="lxd-cloud"
328 # Get the VCA Certificate
calvinosanc183dff822021-01-04 11:50:09 +0100329 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 +0100330
331 # Calculate the default route of this machine
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200332 local DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100333
334 # Generate a new overlay.yaml, overriding any existing one
David Garcia42375212020-04-27 19:07:49 +0200335 cat << EOF > /tmp/vca-overlay.yaml
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100336applications:
337 lcm-k8s:
338 options:
339 vca_user: $vca_user
340 vca_password: $vca_password
341 vca_host: $vca_host
342 vca_port: $vca_port
343 vca_pubkey: $vca_pubkey
344 vca_cacert: $vca_cacert
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100345 vca_cloud: $vca_cloud
beierlma4a37f72020-06-26 12:55:01 -0400346 vca_k8s_cloud: $K8S_CLOUD_NAME
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100347 mon-k8s:
348 options:
349 vca_user: $vca_user
350 vca_password: $vca_password
351 vca_host: $vca_host
352 vca_cacert: $vca_cacert
353EOF
David Garcia42375212020-04-27 19:07:49 +0200354 mv /tmp/vca-overlay.yaml ~/.osm/
355 OSM_VCA_HOST=$vca_host
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100356}
357
David Garcia69388c22020-05-07 12:14:19 +0200358function generate_images_overlay(){
David Garciaef349d92020-12-10 21:16:12 +0100359 cat << EOF > /tmp/nbi_registry.yaml
360registrypath: ${REGISTRY_URL}opensourcemano/nbi:$TAG
361EOF
362 cat << EOF > /tmp/ng_ui_registry.yaml
363registrypath: ${REGISTRY_URL}opensourcemano/ng-ui:$TAG
364EOF
beierlmf2782c52020-11-05 17:04:05 -0500365 if [ ! -z "$REGISTRY_USERNAME" ] ; then
366 REGISTRY_CREDENTIALS=$(cat <<EOF
367
368 image_username: $REGISTRY_USERNAME
369 image_password: $REGISTRY_PASSWORD
370EOF
371 );
David Garciaef349d92020-12-10 21:16:12 +0100372 echo username: $REGISTRY_USERNAME >> /tmp/nbi_registry.yaml
373 echo password: $REGISTRY_PASSWORD >> /tmp/nbi_registry.yaml
374 echo username: $REGISTRY_USERNAME >> /tmp/ng_ui_registry.yaml
375 echo password: $REGISTRY_PASSWORD >> /tmp/ng_ui_registry.yaml
beierlmf2782c52020-11-05 17:04:05 -0500376fi
377
David Garcia69388c22020-05-07 12:14:19 +0200378 cat << EOF > /tmp/images-overlay.yaml
379applications:
380 lcm-k8s:
381 options:
beierlmf2782c52020-11-05 17:04:05 -0500382 image: ${REGISTRY_URL}opensourcemano/lcm:$TAG ${REGISTRY_CREDENTIALS}
David Garcia69388c22020-05-07 12:14:19 +0200383 mon-k8s:
384 options:
beierlmf2782c52020-11-05 17:04:05 -0500385 image: ${REGISTRY_URL}opensourcemano/mon:$TAG ${REGISTRY_CREDENTIALS}
David Garcia69388c22020-05-07 12:14:19 +0200386 ro-k8s:
387 options:
beierlmf2782c52020-11-05 17:04:05 -0500388 image: ${REGISTRY_URL}opensourcemano/ro:$TAG ${REGISTRY_CREDENTIALS}
David Garciaef349d92020-12-10 21:16:12 +0100389 nbi:
390 resources:
391 image: /tmp/nbi_registry.yaml
David Garcia69388c22020-05-07 12:14:19 +0200392 pol-k8s:
393 options:
beierlmf2782c52020-11-05 17:04:05 -0500394 image: ${REGISTRY_URL}opensourcemano/pol:$TAG ${REGISTRY_CREDENTIALS}
beierlma4a37f72020-06-26 12:55:01 -0400395 pla:
396 options:
David Garcia055ae2d2021-01-27 13:13:10 +0100397 image: ${REGISTRY_URL}opensourcemano/pla:8 ${REGISTRY_CREDENTIALS}
beierlma4a37f72020-06-26 12:55:01 -0400398 ng-ui:
David Garciaef349d92020-12-10 21:16:12 +0100399 resources:
400 image: /tmp/ng_ui_registry.yaml
David Garcia009a5d62020-08-27 16:53:44 +0200401 keystone:
402 options:
beierlmf2782c52020-11-05 17:04:05 -0500403 image: ${REGISTRY_URL}opensourcemano/keystone:$TAG ${REGISTRY_CREDENTIALS}
David Garcia69388c22020-05-07 12:14:19 +0200404EOF
405 mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE
406}
407
David Garciab50dabe2021-01-15 12:58:59 +0100408function refresh_osmclient_snap() {
409 osmclient_snap_install_refresh refresh
410}
411
412function install_osm_client_snap() {
413 osmclient_snap_install_refresh install
414}
415
416function osmclient_snap_install_refresh() {
417 channel_preference="stable candidate beta edge"
418 for channel in $channel_preference; do
419 echo "Trying to install osmclient from channel $OSMCLIENT_VERSION/$channel"
420 sudo snap $1 osmclient --channel $OSMCLIENT_VERSION/$channel 2> /dev/null && echo osmclient snap installed && break
421 done
422}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100423function install_osmclient() {
David Garciab50dabe2021-01-15 12:58:59 +0100424 snap info osmclient | grep -E ^installed: && refresh_osmclient_snap || install_osm_client_snap
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100425}
426
beierlmdf6de3d2020-12-16 08:46:40 -0500427function add_local_k8scluster() {
428 osm --all-projects vim-create \
429 --name _system-osm-vim \
430 --account_type dummy \
431 --auth_url http://dummy \
432 --user osm --password osm --tenant osm \
433 --description "dummy" \
434 --config '{management_network_name: mgmt}'
435 tmpfile=$(mktemp --tmpdir=${HOME})
436 cp ${KUBECONFIG} ${tmpfile}
437 osm --all-projects k8scluster-add \
438 --creds ${tmpfile} \
439 --vim _system-osm-vim \
440 --k8s-nets '{"net1": null}' \
441 --version '1.19' \
442 --description "OSM Internal Cluster" \
443 _system-osm-k8s
444 rm -f ${tmpfile}
445}
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100446
447function install_microstack() {
David Garciaff046ce2021-02-15 13:38:42 +0100448 # Install and init microstack
449 sudo snap install microstack --channel beta --devmode
450 sudo snap set microstack config.network.ports.dashboard=8080
451 sudo microstack.init --auto --control
452
453 # Basic configuration
454 microstack.openstack network create --enable --no-share osm-ext
455 microstack.openstack subnet create osm-ext-subnet --network osm-ext --dns-nameserver 8.8.8.8 --subnet-range 172.16.0.0/24
456 microstack.openstack router create external-router
457 microstack.openstack router add subnet external-router osm-ext-subnet
458 microstack.openstack router set --external-gateway external external-router
459 for i in $(microstack.openstack security group list | awk '/default/{ print $2 }'); do
460 microstack.openstack security group rule create $i --protocol icmp --remote-ip 0.0.0.0/0
461 microstack.openstack security group rule create $i --protocol tcp --remote-ip 0.0.0.0/0
462 done
463 KEYPAIR_PATH=~/.ssh/microstack
464 if ! test -f $KEYPAIR_PATH; then
465 echo "Generating ssh keypair for microstack"
466 ssh-keygen -t rsa -N "" -f $KEYPAIR_PATH
467 fi
468 microstack.openstack keypair create --public-key $KEYPAIR_PATH.pub microstack
469
470 # Add xenial, bionic, and focal images to microstack
471 echo "curl https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img | microstack.openstack image create --public --container-format=bare --disk-format=qcow2 ubuntu16.04"
472 echo "curl https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img | microstack.openstack image create --public --container-format=bare --disk-format=qcow2 ubuntu18.04"
473 echo "curl https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img | microstack.openstack image create --public --container-format=bare --disk-format=qcow2 ubuntu20.04"
474
475 # Load ENV variables
476 . /var/snap/microstack/common/etc/microstack.rc
477 osm vim-create --name microstack \
478 --user "$OS_USERNAME" \
479 --password "$OS_PASSWORD" \
480 --auth_url "$OS_AUTH_URL/v3" \
481 --tenant "$OS_USERNAME" \
482 --account_type openstack \
483 --config='{
484 use_floating_ip: True,
485 management_network_name: osm-ext,
486 keypair: microstack,
487 project_name: admin,
488 user_domain_name: default,
489 region_name: microstack,
490 insecure: True,
491 availability_zone: nova,
492 version: 3
493 }'
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100494}
495
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200496DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100497DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
498
499check_arguments $@
David Garcia42375212020-04-27 19:07:49 +0200500mkdir -p ~/.osm
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100501install_snaps
502bootstrap_k8s_lxd
503deploy_charmed_osm
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100504install_osmclient
David Garciae2dd10f2021-03-02 12:20:14 +0100505export OSM_HOSTNAME=$(juju config nbi site_url | sed "s/http.*\?:\/\///"):443
beierlmdf6de3d2020-12-16 08:46:40 -0500506add_local_k8scluster
507
David Garcia42375212020-04-27 19:07:49 +0200508if [ -v MICROSTACK ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100509 install_microstack
510fi
beierlma4a37f72020-06-26 12:55:01 -0400511
beierlm7e54dfc2020-09-24 15:27:53 -0400512
beierlma4a37f72020-06-26 12:55:01 -0400513echo "Your installation is now complete, follow these steps for configuring the osmclient:"
514echo
515echo "1. Create the OSM_HOSTNAME environment variable with the NBI IP"
516echo
beierlm7e54dfc2020-09-24 15:27:53 -0400517echo "export OSM_HOSTNAME=$OSM_HOSTNAME"
beierlma4a37f72020-06-26 12:55:01 -0400518echo
519echo "2. Add the previous command to your .bashrc for other Shell sessions"
520echo
beierlm7e54dfc2020-09-24 15:27:53 -0400521echo "echo \"export OSM_HOSTNAME=$OSM_HOSTNAME\" >> ~/.bashrc"
beierlma4a37f72020-06-26 12:55:01 -0400522echo
523echo "DONE"