blob: e8eda63d46ade2528fdf043f31256ec96dec6f84 [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
18K8S_CLOUD_NAME="k8s-cloud"
beierlmdfd42882020-07-02 14:21:09 -040019KUBECTL="microk8s.kubectl"
David Garcia69388c22020-05-07 12:14:19 +020020IMAGES_OVERLAY_FILE=~/.osm/images-overlay.yaml
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010021function check_arguments(){
22 while [ $# -gt 0 ] ; do
David Garcia42375212020-04-27 19:07:49 +020023 case $1 in
24 --bundle) BUNDLE="$2" ;;
Dominik Fleischmann7a97a4c2020-06-04 10:52:05 +020025 --k8s) KUBECFG="$2" ;;
26 --vca) CONTROLLER="$2" ;;
27 --lxd) LXD_CLOUD="$2" ;;
28 --lxd-cred) LXD_CREDENTIALS="$2" ;;
David Garcia69388c22020-05-07 12:14:19 +020029 --microstack) MICROSTACK=y ;;
Dominik Fleischmannc57296f2020-06-09 11:45:08 +020030 --ha) BUNDLE="osm-ha" ;;
David Garcia69388c22020-05-07 12:14:19 +020031 --tag) TAG="$2" ;;
David Garcia42375212020-04-27 19:07:49 +020032 esac
33 shift
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010034 done
35
David Garcia42375212020-04-27 19:07:49 +020036 # echo $BUNDLE $KUBECONFIG $LXDENDPOINT
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010037}
38function install_snaps(){
Dominik Fleischmannc57296f2020-06-09 11:45:08 +020039 [ ! -v KUBECFG ] && sudo snap install microk8s --classic && sudo usermod -a -G microk8s `whoami` && mkdir -p ~/.kube && sudo chown -f -R `whoami` ~/.kube
Dominik Fleischmann71997c12020-06-30 14:25:19 +020040 sudo snap install juju --classic --channel=2.8/stable
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010041}
42
43function bootstrap_k8s_lxd(){
David Garcia42375212020-04-27 19:07:49 +020044 [ -v CONTROLLER ] && ADD_K8S_OPTS="--controller ${CONTROLLER}" && CONTROLLER_NAME=$CONTROLLER
beierlm9425dd22020-07-16 16:57:09 -040045 [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="osm-vca"
46
47 if [ -v BOOTSTRAP_NEEDED ]; then
48 CONTROLLER_PRESENT=$(juju controllers 2>/dev/null| grep ${CONTROLLER_NAME} | wc -l)
49 if [ $CONTROLLER_PRESENT -ge 1 ]; then
50 cat << EOF
51Threre is already a VCA present with the installer reserved name of "${CONTROLLER_NAME}".
52You may either explicitly use this VCA with the "--vca ${CONTROLLER_NAME}" option, or remove it
53using this command:
54
55 juju destroy-controller --release-storage --destroy-all-models -y ${CONTROLLER_NAME}
56
57Please retry the installation once this conflict has been resolved.
58EOF
59 exit 1
60 fi
61 fi
David Garcia42375212020-04-27 19:07:49 +020062
63 if [ -v KUBECFG ]; then
64 cat $KUBECFG | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
beierlmdfd42882020-07-02 14:21:09 -040065 [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME --config controller-service-type=loadbalancer
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010066 else
beierlmdfd42882020-07-02 14:21:09 -040067 sg microk8s -c "echo ${DEFAULT_IP}-${DEFAULT_IP} | microk8s.enable metallb"
David Garcia42375212020-04-27 19:07:49 +020068 sg microk8s -c "microk8s.enable storage dns"
beierlm9425dd22020-07-16 16:57:09 -040069 TIME_TO_WAIT=30
70 start_time="$(date -u +%s)"
Dominik Fleischmannc57296f2020-06-09 11:45:08 +020071 while true
72 do
beierlm9425dd22020-07-16 16:57:09 -040073 now="$(date -u +%s)"
74 if [[ $(( now - start_time )) -gt $TIME_TO_WAIT ]];then
75 echo "Microk8s storage failed to enable"
76 sg microk8s -c "microk8s.status"
77 exit 1
78 fi
Dominik Fleischmannc57296f2020-06-09 11:45:08 +020079 sg microk8s -c "microk8s.status" | grep 'storage: enabled'
80 if [ $? -eq 0 ]; then
81 break
82 fi
83 sleep 1
84 done
David Garcia42375212020-04-27 19:07:49 +020085
86 [ ! -v BOOTSTRAP_NEEDED ] && sg microk8s -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
beierlmdfd42882020-07-02 14:21:09 -040087 [ -v BOOTSTRAP_NEEDED ] && sg microk8s -c "juju bootstrap microk8s $CONTROLLER_NAME --config controller-service-type=loadbalancer" && K8S_CLOUD_NAME=microk8s
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010088 fi
89
David Garcia42375212020-04-27 19:07:49 +020090 if [ -v LXD_CLOUD ]; then
91 if [ ! -v LXD_CREDENTIALS ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010092 echo "The installer needs the LXD server certificate if the LXD is external"
93 exit 1
94 fi
95 else
96 LXDENDPOINT=$DEFAULT_IP
David Garcia42375212020-04-27 19:07:49 +020097 LXD_CLOUD=~/.osm/lxd-cloud.yaml
98 LXD_CREDENTIALS=~/.osm/lxd-credentials.yaml
99 # Apply sysctl production values for optimal performance
100 sudo cp /usr/share/osm-devops/installers/60-lxd-production.conf /etc/sysctl.d/60-lxd-production.conf
101 sudo sysctl --system
102 # Install LXD snap
103 sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client
104 sudo snap install lxd
105 sudo apt-get install zfsutils-linux -y
106 # Configure LXD
107 sudo usermod -a -G lxd `whoami`
108 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"
109 sg lxd -c "lxd waitready"
110 DEFAULT_MTU=$(ip addr show $DEFAULT_IF | perl -ne 'if (/mtu\s(\d+)/) {print $1;}')
111 sg lxd -c "lxc profile device set default eth0 mtu $DEFAULT_MTU"
David Garciad00e49c2020-06-19 10:33:37 +0200112 sg lxd -c "lxc network set lxdbr0 bridge.mtu $DEFAULT_MTU"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100113
David Garcia42375212020-04-27 19:07:49 +0200114 cat << EOF > $LXD_CLOUD
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100115clouds:
116 lxd-cloud:
117 type: lxd
118 auth-types: [certificate]
119 endpoint: "https://$LXDENDPOINT:8443"
120 config:
121 ssl-hostname-verification: false
122EOF
David Garcia42375212020-04-27 19:07:49 +0200123 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"
124 local server_cert=`cat /var/snap/lxd/common/lxd/server.crt | sed 's/^/ /'`
125 local client_cert=`cat ~/.osm/client.crt | sed 's/^/ /'`
126 local client_key=`cat ~/.osm/client.key | sed 's/^/ /'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100127
David Garcia42375212020-04-27 19:07:49 +0200128 cat << EOF > $LXD_CREDENTIALS
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100129credentials:
130 lxd-cloud:
David Garcia42375212020-04-27 19:07:49 +0200131 lxd-cloud:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100132 auth-type: certificate
133 server-cert: |
134$server_cert
135 client-cert: |
136$client_cert
137 client-key: |
138$client_key
139EOF
David Garcia42375212020-04-27 19:07:49 +0200140 lxc config trust add local: ~/.osm/client.crt
141 fi
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100142
David Garcia42375212020-04-27 19:07:49 +0200143 juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force
144 juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS
145 sg lxd -c "lxd waitready"
beierlm9425dd22020-07-16 16:57:09 -0400146 #juju add-model test lxd-cloud || true
Dominik Fleischmann71997c12020-06-30 14:25:19 +0200147 juju controller-config features=[k8s-operators]
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100148}
149
150function deploy_charmed_osm(){
151 create_overlay
152 echo "Creating OSM model"
David Garcia42375212020-04-27 19:07:49 +0200153 if [ -v KUBECFG ]; then
154 juju add-model osm $K8S_CLOUD_NAME
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100155 else
David Garcia42375212020-04-27 19:07:49 +0200156 sg microk8s -c "juju add-model osm $K8S_CLOUD_NAME"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100157 fi
158 echo "Deploying OSM with charms"
beierlmb5332cf2020-07-08 11:43:59 -0400159 images_overlay=""
160 [ -v TAG ] && generate_images_overlay && images_overlay="--overlay $IMAGES_OVERLAY_FILE"
David Garcia42375212020-04-27 19:07:49 +0200161 if [ -v BUNDLE ]; then
beierlmb5332cf2020-07-08 11:43:59 -0400162 juju deploy $BUNDLE --overlay ~/.osm/vca-overlay.yaml $images_overlay
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100163 else
David Garcia69388c22020-05-07 12:14:19 +0200164 juju deploy osm --overlay ~/.osm/vca-overlay.yaml $images_overlay
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100165 fi
166 echo "Waiting for deployment to finish..."
167 check_osm_deployed &> /dev/null
168 echo "OSM with charms deployed"
David Garcia42375212020-04-27 19:07:49 +0200169 sg microk8s -c "microk8s.enable ingress"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100170 juju config ui-k8s juju-external-hostname=osm.$DEFAULT_IP.xip.io
171 juju expose ui-k8s
172}
173
174function check_osm_deployed() {
175 while true
176 do
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200177 pod_name=`sg microk8s -c "microk8s.kubectl -n osm get pods | grep ui-k8s | grep -v operator" | awk '{print $1; exit}'`
178
David Garcia42375212020-04-27 19:07:49 +0200179 if [[ `sg microk8s -c "microk8s.kubectl -n osm wait pod $pod_name --for condition=Ready"` ]]; then
180 if [[ `sg microk8s -c "microk8s.kubectl -n osm wait pod lcm-k8s-0 --for condition=Ready"` ]]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100181 break
182 fi
183 fi
184 sleep 10
185 done
186}
187
188function create_overlay() {
David Garcia42375212020-04-27 19:07:49 +0200189 sudo snap install jq
190 sudo apt install python3-pip -y
191 python3 -m pip install yq
192 PATH=$PATH:$HOME/.local/bin # make yq command available
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100193 local HOME=/home/$USER
David Garcia42375212020-04-27 19:07:49 +0200194 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME].user')
195 local vca_password=$(cat $HOME/.local/share/juju/accounts.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME].password')
beierlmdfd42882020-07-02 14:21:09 -0400196 local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME]["api-endpoints"][0]' --raw-output | cut -d ":" -f 1)
197 local vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME]["api-endpoints"][0]' --raw-output | cut -d ":" -f 2)
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100198 local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
199 local vca_cloud="lxd-cloud"
200 # Get the VCA Certificate
beierlmdfd42882020-07-02 14:21:09 -0400201 local vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME]["ca-cert"]' --raw-output | base64 | tr -d \\n)
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100202
203 # Calculate the default route of this machine
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200204 local DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100205
206 # Generate a new overlay.yaml, overriding any existing one
David Garcia42375212020-04-27 19:07:49 +0200207 cat << EOF > /tmp/vca-overlay.yaml
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100208applications:
209 lcm-k8s:
210 options:
211 vca_user: $vca_user
212 vca_password: $vca_password
213 vca_host: $vca_host
214 vca_port: $vca_port
215 vca_pubkey: $vca_pubkey
216 vca_cacert: $vca_cacert
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100217 vca_cloud: $vca_cloud
Dominik Fleischmann71997c12020-06-30 14:25:19 +0200218 vca_k8s_cloud: $K8S_CLOUD_NAME
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100219 mon-k8s:
220 options:
221 vca_user: $vca_user
222 vca_password: $vca_password
223 vca_host: $vca_host
224 vca_cacert: $vca_cacert
225EOF
David Garcia42375212020-04-27 19:07:49 +0200226 mv /tmp/vca-overlay.yaml ~/.osm/
227 OSM_VCA_HOST=$vca_host
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100228}
229
David Garcia69388c22020-05-07 12:14:19 +0200230function generate_images_overlay(){
231 cat << EOF > /tmp/images-overlay.yaml
232applications:
233 lcm-k8s:
234 options:
235 image: opensourcemano/lcm:$TAG
236 mon-k8s:
237 options:
238 image: opensourcemano/mon:$TAG
239 ro-k8s:
240 options:
241 image: opensourcemano/ro:$TAG
242 nbi-k8s:
243 options:
244 image: opensourcemano/nbi:$TAG
245 pol-k8s:
246 options:
247 image: opensourcemano/pol:$TAG
248 ui-k8s:
249 options:
250 image: opensourcemano/light-ui:$TAG
David Garcia5863d3e2020-07-09 13:14:13 +0200251 pla:
252 options:
253 image: opensourcemano/pla:$TAG
David Garciafa55bd72020-07-07 11:14:19 +0200254 ng-ui:
255 options:
256 image: opensourcemano/ng-ui:$TAG
David Garcia69388c22020-05-07 12:14:19 +0200257
258EOF
259 mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE
260}
261
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100262function install_osmclient() {
263 sudo snap install osmclient
264 sudo snap alias osmclient.osm osm
265}
266
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100267
268function install_microstack() {
269 sudo snap install microstack --classic --beta
270 sudo microstack.init --auto
271 wget https://cloud-images.ubuntu.com/releases/16.04/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img -P ~/.osm/
272 microstack.openstack image create \
David Garcia42375212020-04-27 19:07:49 +0200273 --public \
274 --disk-format qcow2 \
275 --container-format bare \
276 --file ~/.osm/ubuntu-16.04-server-cloudimg-amd64-disk1.img \
277 ubuntu1604
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100278 ssh-keygen -t rsa -N "" -f ~/.ssh/microstack
279 microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200280 export OSM_HOSTNAME=`juju status --format json | jq -rc '.applications."nbi-k8s".address'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100281 osm vim-create --name microstack-site \
David Garcia42375212020-04-27 19:07:49 +0200282 --user admin \
283 --password keystone \
284 --auth_url http://10.20.20.1:5000/v3 \
285 --tenant admin \
286 --account_type openstack \
287 --config='{security_groups: default,
288 keypair: microstack,
289 project_name: admin,
290 user_domain_name: default,
291 region_name: microstack,
292 insecure: True,
293 availability_zone: nova,
294 version: 3}'
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100295}
296
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200297DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100298DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
299
300check_arguments $@
David Garcia42375212020-04-27 19:07:49 +0200301mkdir -p ~/.osm
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100302install_snaps
beierlmdfd42882020-07-02 14:21:09 -0400303sleep 5
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100304bootstrap_k8s_lxd
305deploy_charmed_osm
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100306install_osmclient
David Garcia42375212020-04-27 19:07:49 +0200307if [ -v MICROSTACK ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100308 install_microstack
309fi
beierlm9425dd22020-07-16 16:57:09 -0400310
311echo "Your installation is now complete, follow these steps for configuring the osmclient:"
312echo
313echo "1. Get the NBI IP with the following command:"
314echo
315echo NBI_IP='`juju status --format json | jq -rc '"'"'.applications."nbi-k8s".address'"'"'`'
316echo
317echo "2. Create the OSM_HOSTNAME environment variable with the NBI IP"
318echo
319echo "export OSM_HOSTNAME=\$NBI_IP"
320echo
321echo "3. Add the previous command to your .bashrc for other Shell sessions"
322echo
323echo "echo \"export OSM_HOSTNAME=\$NBI_IP\" >> ~/.bashrc"
324echo
325echo "DONE"