blob: c2108d6d22959c4d0272ad717d333214f1467e32 [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"
David Garcia69388c22020-05-07 12:14:19 +020019IMAGES_OVERLAY_FILE=~/.osm/images-overlay.yaml
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010020function check_arguments(){
21 while [ $# -gt 0 ] ; do
David Garcia42375212020-04-27 19:07:49 +020022 case $1 in
23 --bundle) BUNDLE="$2" ;;
Dominik Fleischmann7a97a4c2020-06-04 10:52:05 +020024 --k8s) KUBECFG="$2" ;;
25 --vca) CONTROLLER="$2" ;;
26 --lxd) LXD_CLOUD="$2" ;;
27 --lxd-cred) LXD_CREDENTIALS="$2" ;;
David Garcia69388c22020-05-07 12:14:19 +020028 --microstack) MICROSTACK=y ;;
Dominik Fleischmannc57296f2020-06-09 11:45:08 +020029 --ha) BUNDLE="osm-ha" ;;
David Garcia69388c22020-05-07 12:14:19 +020030 --tag) TAG="$2" ;;
David Garcia42375212020-04-27 19:07:49 +020031 esac
32 shift
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010033 done
34
David Garcia42375212020-04-27 19:07:49 +020035 # echo $BUNDLE $KUBECONFIG $LXDENDPOINT
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010036}
37function install_snaps(){
Dominik Fleischmannc57296f2020-06-09 11:45:08 +020038 sudo snap install juju --classic --channel=2.7/stable
39 [ ! -v KUBECFG ] && sudo snap install microk8s --classic && sudo usermod -a -G microk8s `whoami` && mkdir -p ~/.kube && sudo chown -f -R `whoami` ~/.kube
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010040}
41
42function bootstrap_k8s_lxd(){
David Garcia42375212020-04-27 19:07:49 +020043 [ -v CONTROLLER ] && ADD_K8S_OPTS="--controller ${CONTROLLER}" && CONTROLLER_NAME=$CONTROLLER
44 [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="controller"
45
46 if [ -v KUBECFG ]; then
47 cat $KUBECFG | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
48 [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010049 else
David Garcia42375212020-04-27 19:07:49 +020050 sg microk8s -c "microk8s.enable storage dns"
Dominik Fleischmannc57296f2020-06-09 11:45:08 +020051 while true
52 do
53 sg microk8s -c "microk8s.status" | grep 'storage: enabled'
54 if [ $? -eq 0 ]; then
55 break
56 fi
57 sleep 1
58 done
David Garcia42375212020-04-27 19:07:49 +020059
60 [ ! -v BOOTSTRAP_NEEDED ] && sg microk8s -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
61 [ -v BOOTSTRAP_NEEDED ] && sg microk8s -c "juju bootstrap microk8s $CONTROLLER_NAME" && K8S_CLOUD_NAME=microk8s
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010062 fi
63
David Garcia42375212020-04-27 19:07:49 +020064 if [ -v LXD_CLOUD ]; then
65 if [ ! -v LXD_CREDENTIALS ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010066 echo "The installer needs the LXD server certificate if the LXD is external"
67 exit 1
68 fi
69 else
70 LXDENDPOINT=$DEFAULT_IP
David Garcia42375212020-04-27 19:07:49 +020071 LXD_CLOUD=~/.osm/lxd-cloud.yaml
72 LXD_CREDENTIALS=~/.osm/lxd-credentials.yaml
73 # Apply sysctl production values for optimal performance
74 sudo cp /usr/share/osm-devops/installers/60-lxd-production.conf /etc/sysctl.d/60-lxd-production.conf
75 sudo sysctl --system
76 # Install LXD snap
77 sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client
78 sudo snap install lxd
79 sudo apt-get install zfsutils-linux -y
80 # Configure LXD
81 sudo usermod -a -G lxd `whoami`
82 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"
83 sg lxd -c "lxd waitready"
84 DEFAULT_MTU=$(ip addr show $DEFAULT_IF | perl -ne 'if (/mtu\s(\d+)/) {print $1;}')
85 sg lxd -c "lxc profile device set default eth0 mtu $DEFAULT_MTU"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010086
David Garcia42375212020-04-27 19:07:49 +020087 cat << EOF > $LXD_CLOUD
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010088clouds:
89 lxd-cloud:
90 type: lxd
91 auth-types: [certificate]
92 endpoint: "https://$LXDENDPOINT:8443"
93 config:
94 ssl-hostname-verification: false
95EOF
David Garcia42375212020-04-27 19:07:49 +020096 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"
97 local server_cert=`cat /var/snap/lxd/common/lxd/server.crt | sed 's/^/ /'`
98 local client_cert=`cat ~/.osm/client.crt | sed 's/^/ /'`
99 local client_key=`cat ~/.osm/client.key | sed 's/^/ /'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100100
David Garcia42375212020-04-27 19:07:49 +0200101 cat << EOF > $LXD_CREDENTIALS
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100102credentials:
103 lxd-cloud:
David Garcia42375212020-04-27 19:07:49 +0200104 lxd-cloud:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100105 auth-type: certificate
106 server-cert: |
107$server_cert
108 client-cert: |
109$client_cert
110 client-key: |
111$client_key
112EOF
David Garcia42375212020-04-27 19:07:49 +0200113 lxc config trust add local: ~/.osm/client.crt
114 fi
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100115
David Garcia42375212020-04-27 19:07:49 +0200116 juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force
117 juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS
118 sg lxd -c "lxd waitready"
119 juju add-model test lxd-cloud || true
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100120}
121
122function deploy_charmed_osm(){
123 create_overlay
124 echo "Creating OSM model"
David Garcia42375212020-04-27 19:07:49 +0200125 if [ -v KUBECFG ]; then
126 juju add-model osm $K8S_CLOUD_NAME
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100127 else
David Garcia42375212020-04-27 19:07:49 +0200128 sg microk8s -c "juju add-model osm $K8S_CLOUD_NAME"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100129 fi
130 echo "Deploying OSM with charms"
David Garcia42375212020-04-27 19:07:49 +0200131 # echo $BUNDLE
132 if [ -v BUNDLE ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100133 juju deploy $BUNDLE --overlay ~/.osm/vca-overlay.yaml
134 else
David Garcia69388c22020-05-07 12:14:19 +0200135 images_overlay=""
136 [ -v TAG ] && generate_images_overlay && images_overlay="--overlay $IMAGES_OVERLAY_FILE"
137 juju deploy osm --overlay ~/.osm/vca-overlay.yaml $images_overlay
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100138 fi
139 echo "Waiting for deployment to finish..."
140 check_osm_deployed &> /dev/null
141 echo "OSM with charms deployed"
David Garcia42375212020-04-27 19:07:49 +0200142 sg microk8s -c "microk8s.enable ingress"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100143 juju config ui-k8s juju-external-hostname=osm.$DEFAULT_IP.xip.io
144 juju expose ui-k8s
145}
146
147function check_osm_deployed() {
148 while true
149 do
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200150 pod_name=`sg microk8s -c "microk8s.kubectl -n osm get pods | grep ui-k8s | grep -v operator" | awk '{print $1; exit}'`
151
David Garcia42375212020-04-27 19:07:49 +0200152 if [[ `sg microk8s -c "microk8s.kubectl -n osm wait pod $pod_name --for condition=Ready"` ]]; then
153 if [[ `sg microk8s -c "microk8s.kubectl -n osm wait pod lcm-k8s-0 --for condition=Ready"` ]]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100154 break
155 fi
156 fi
157 sleep 10
158 done
159}
160
161function create_overlay() {
David Garcia42375212020-04-27 19:07:49 +0200162 sudo snap install jq
163 sudo apt install python3-pip -y
164 python3 -m pip install yq
165 PATH=$PATH:$HOME/.local/bin # make yq command available
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100166 local HOME=/home/$USER
David Garcia42375212020-04-27 19:07:49 +0200167 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME].user')
168 local vca_password=$(cat $HOME/.local/share/juju/accounts.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME].password')
169 local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME]["api-endpoints"][0]' | cut -d ":" -f 1 | cut -d "\"" -f 2)
170 local vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME]["api-endpoints"][0]' | cut -d ":" -f 2 | cut -d "\"" -f 1)
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100171 local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
172 local vca_cloud="lxd-cloud"
173 # Get the VCA Certificate
David Garcia42375212020-04-27 19:07:49 +0200174 local vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME]["ca-cert"]' | base64 | tr -d \\n)
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100175
176 # Calculate the default route of this machine
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200177 local DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100178 local vca_apiproxy=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
179
180 # Generate a new overlay.yaml, overriding any existing one
David Garcia42375212020-04-27 19:07:49 +0200181 cat << EOF > /tmp/vca-overlay.yaml
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100182applications:
183 lcm-k8s:
184 options:
185 vca_user: $vca_user
186 vca_password: $vca_password
187 vca_host: $vca_host
188 vca_port: $vca_port
189 vca_pubkey: $vca_pubkey
190 vca_cacert: $vca_cacert
191 vca_apiproxy: $vca_apiproxy
192 vca_cloud: $vca_cloud
193 mon-k8s:
194 options:
195 vca_user: $vca_user
196 vca_password: $vca_password
197 vca_host: $vca_host
198 vca_cacert: $vca_cacert
199EOF
David Garcia42375212020-04-27 19:07:49 +0200200 mv /tmp/vca-overlay.yaml ~/.osm/
201 OSM_VCA_HOST=$vca_host
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100202}
203
David Garcia69388c22020-05-07 12:14:19 +0200204function generate_images_overlay(){
205 cat << EOF > /tmp/images-overlay.yaml
206applications:
207 lcm-k8s:
208 options:
209 image: opensourcemano/lcm:$TAG
210 mon-k8s:
211 options:
212 image: opensourcemano/mon:$TAG
213 ro-k8s:
214 options:
215 image: opensourcemano/ro:$TAG
216 nbi-k8s:
217 options:
218 image: opensourcemano/nbi:$TAG
219 pol-k8s:
220 options:
221 image: opensourcemano/pol:$TAG
222 ui-k8s:
223 options:
224 image: opensourcemano/light-ui:$TAG
225
226EOF
227 mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE
228}
229
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100230function install_osmclient() {
231 sudo snap install osmclient
232 sudo snap alias osmclient.osm osm
233}
234
235function create_iptables() {
236 check_install_iptables_persistent
237
238 if ! sudo iptables -t nat -C PREROUTING -p tcp -m tcp -d $DEFAULT_IP --dport 17070 -j DNAT --to-destination $OSM_VCA_HOST; then
239 sudo iptables -t nat -A PREROUTING -p tcp -m tcp -d $DEFAULT_IP --dport 17070 -j DNAT --to-destination $OSM_VCA_HOST
240 sudo netfilter-persistent save
241 fi
242}
243
244function check_install_iptables_persistent(){
245 echo -e "\nChecking required packages: iptables-persistent"
246 if ! dpkg -l iptables-persistent &>/dev/null; then
247 echo -e " Not installed.\nInstalling iptables-persistent requires root privileges"
248 echo iptables-persistent iptables-persistent/autosave_v4 boolean true | sudo debconf-set-selections
249 echo iptables-persistent iptables-persistent/autosave_v6 boolean true | sudo debconf-set-selections
250 sudo apt-get -yq install iptables-persistent
251 fi
252}
253
254function install_microstack() {
255 sudo snap install microstack --classic --beta
256 sudo microstack.init --auto
257 wget https://cloud-images.ubuntu.com/releases/16.04/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img -P ~/.osm/
258 microstack.openstack image create \
David Garcia42375212020-04-27 19:07:49 +0200259 --public \
260 --disk-format qcow2 \
261 --container-format bare \
262 --file ~/.osm/ubuntu-16.04-server-cloudimg-amd64-disk1.img \
263 ubuntu1604
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100264 ssh-keygen -t rsa -N "" -f ~/.ssh/microstack
265 microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200266 export OSM_HOSTNAME=`juju status --format json | jq -rc '.applications."nbi-k8s".address'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100267 osm vim-create --name microstack-site \
David Garcia42375212020-04-27 19:07:49 +0200268 --user admin \
269 --password keystone \
270 --auth_url http://10.20.20.1:5000/v3 \
271 --tenant admin \
272 --account_type openstack \
273 --config='{security_groups: default,
274 keypair: microstack,
275 project_name: admin,
276 user_domain_name: default,
277 region_name: microstack,
278 insecure: True,
279 availability_zone: nova,
280 version: 3}'
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100281}
282
Dominik Fleischmannc57296f2020-06-09 11:45:08 +0200283DEFAULT_IF=`ip route list match 0.0.0.0 | awk '{print $5}'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100284DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
285
286check_arguments $@
David Garcia42375212020-04-27 19:07:49 +0200287mkdir -p ~/.osm
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100288install_snaps
289bootstrap_k8s_lxd
290deploy_charmed_osm
David Garcia42375212020-04-27 19:07:49 +0200291[ ! -v CONTROLLER ] && create_iptables
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100292install_osmclient
David Garcia42375212020-04-27 19:07:49 +0200293if [ -v MICROSTACK ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100294 install_microstack
295fi