blob: b89855f2e8d1e3c15381c4e8910b84c106e30c8c [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" ;;
24 --kubeconfig) KUBECFG="$2" ;;
25 --controller) CONTROLLER="$2" ;;
26 --lxd-cloud) LXD_CLOUD="$2" ;;
27 --lxd-credentials) LXD_CREDENTIALS="$2" ;;
David Garcia69388c22020-05-07 12:14:19 +020028 --microstack) MICROSTACK=y ;;
29 --tag) TAG="$2" ;;
David Garcia42375212020-04-27 19:07:49 +020030 esac
31 shift
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010032 done
33
David Garcia42375212020-04-27 19:07:49 +020034 # echo $BUNDLE $KUBECONFIG $LXDENDPOINT
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010035}
36function install_snaps(){
37 sudo snap install juju --classic
David Garcia42375212020-04-27 19:07:49 +020038 [ ! -v KUBECFG ] && sudo snap install microk8s --classic && sudo usermod -a -G microk8s ubuntu && mkdir -p ~/.kube && sudo chown -f -R `whoami` ~/.kube
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010039}
40
41function bootstrap_k8s_lxd(){
David Garcia42375212020-04-27 19:07:49 +020042 [ -v CONTROLLER ] && ADD_K8S_OPTS="--controller ${CONTROLLER}" && CONTROLLER_NAME=$CONTROLLER
43 [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="controller"
44
45 if [ -v KUBECFG ]; then
46 cat $KUBECFG | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
47 [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010048 else
David Garcia42375212020-04-27 19:07:49 +020049 sg microk8s -c "microk8s.enable storage dns"
50
51 [ ! -v BOOTSTRAP_NEEDED ] && sg microk8s -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
52 [ -v BOOTSTRAP_NEEDED ] && sg microk8s -c "juju bootstrap microk8s $CONTROLLER_NAME" && K8S_CLOUD_NAME=microk8s
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010053 fi
54
David Garcia42375212020-04-27 19:07:49 +020055 if [ -v LXD_CLOUD ]; then
56 if [ ! -v LXD_CREDENTIALS ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010057 echo "The installer needs the LXD server certificate if the LXD is external"
58 exit 1
59 fi
60 else
61 LXDENDPOINT=$DEFAULT_IP
David Garcia42375212020-04-27 19:07:49 +020062 LXD_CLOUD=~/.osm/lxd-cloud.yaml
63 LXD_CREDENTIALS=~/.osm/lxd-credentials.yaml
64 # Apply sysctl production values for optimal performance
65 sudo cp /usr/share/osm-devops/installers/60-lxd-production.conf /etc/sysctl.d/60-lxd-production.conf
66 sudo sysctl --system
67 # Install LXD snap
68 sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client
69 sudo snap install lxd
70 sudo apt-get install zfsutils-linux -y
71 # Configure LXD
72 sudo usermod -a -G lxd `whoami`
73 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"
74 sg lxd -c "lxd waitready"
75 DEFAULT_MTU=$(ip addr show $DEFAULT_IF | perl -ne 'if (/mtu\s(\d+)/) {print $1;}')
76 sg lxd -c "lxc profile device set default eth0 mtu $DEFAULT_MTU"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010077
David Garcia42375212020-04-27 19:07:49 +020078 cat << EOF > $LXD_CLOUD
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010079clouds:
80 lxd-cloud:
81 type: lxd
82 auth-types: [certificate]
83 endpoint: "https://$LXDENDPOINT:8443"
84 config:
85 ssl-hostname-verification: false
86EOF
David Garcia42375212020-04-27 19:07:49 +020087 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"
88 local server_cert=`cat /var/snap/lxd/common/lxd/server.crt | sed 's/^/ /'`
89 local client_cert=`cat ~/.osm/client.crt | sed 's/^/ /'`
90 local client_key=`cat ~/.osm/client.key | sed 's/^/ /'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010091
David Garcia42375212020-04-27 19:07:49 +020092 cat << EOF > $LXD_CREDENTIALS
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010093credentials:
94 lxd-cloud:
David Garcia42375212020-04-27 19:07:49 +020095 lxd-cloud:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010096 auth-type: certificate
97 server-cert: |
98$server_cert
99 client-cert: |
100$client_cert
101 client-key: |
102$client_key
103EOF
David Garcia42375212020-04-27 19:07:49 +0200104 lxc config trust add local: ~/.osm/client.crt
105 fi
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100106
David Garcia42375212020-04-27 19:07:49 +0200107 juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force
108 juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS
109 sg lxd -c "lxd waitready"
110 juju add-model test lxd-cloud || true
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100111}
112
113function deploy_charmed_osm(){
114 create_overlay
115 echo "Creating OSM model"
David Garcia42375212020-04-27 19:07:49 +0200116 if [ -v KUBECFG ]; then
117 juju add-model osm $K8S_CLOUD_NAME
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100118 else
David Garcia42375212020-04-27 19:07:49 +0200119 sg microk8s -c "juju add-model osm $K8S_CLOUD_NAME"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100120 fi
121 echo "Deploying OSM with charms"
David Garcia42375212020-04-27 19:07:49 +0200122 # echo $BUNDLE
123 if [ -v BUNDLE ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100124 juju deploy $BUNDLE --overlay ~/.osm/vca-overlay.yaml
125 else
David Garcia69388c22020-05-07 12:14:19 +0200126 images_overlay=""
127 [ -v TAG ] && generate_images_overlay && images_overlay="--overlay $IMAGES_OVERLAY_FILE"
128 juju deploy osm --overlay ~/.osm/vca-overlay.yaml $images_overlay
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100129 fi
130 echo "Waiting for deployment to finish..."
131 check_osm_deployed &> /dev/null
132 echo "OSM with charms deployed"
David Garcia42375212020-04-27 19:07:49 +0200133 sg microk8s -c "microk8s.enable ingress"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100134 juju config ui-k8s juju-external-hostname=osm.$DEFAULT_IP.xip.io
135 juju expose ui-k8s
136}
137
138function check_osm_deployed() {
139 while true
140 do
David Garcia42375212020-04-27 19:07:49 +0200141 pod_name=`sg microk8s -c "microk8s.kubectl -n osm get pods | grep ui-k8s | grep -v operator" | awk '{print $1}'`
142 if [[ `sg microk8s -c "microk8s.kubectl -n osm wait pod $pod_name --for condition=Ready"` ]]; then
143 if [[ `sg microk8s -c "microk8s.kubectl -n osm wait pod lcm-k8s-0 --for condition=Ready"` ]]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100144 break
145 fi
146 fi
147 sleep 10
148 done
149}
150
151function create_overlay() {
David Garcia42375212020-04-27 19:07:49 +0200152 sudo snap install jq
153 sudo apt install python3-pip -y
154 python3 -m pip install yq
155 PATH=$PATH:$HOME/.local/bin # make yq command available
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100156 local HOME=/home/$USER
David Garcia42375212020-04-27 19:07:49 +0200157 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME].user')
158 local vca_password=$(cat $HOME/.local/share/juju/accounts.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME].password')
159 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)
160 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 +0100161 local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
162 local vca_cloud="lxd-cloud"
163 # Get the VCA Certificate
David Garcia42375212020-04-27 19:07:49 +0200164 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 +0100165
166 # Calculate the default route of this machine
167 local DEFAULT_IF=`route -n |awk '$1~/^0.0.0.0/ {print $8}'`
168 local vca_apiproxy=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
169
170 # Generate a new overlay.yaml, overriding any existing one
David Garcia42375212020-04-27 19:07:49 +0200171 cat << EOF > /tmp/vca-overlay.yaml
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100172applications:
173 lcm-k8s:
174 options:
175 vca_user: $vca_user
176 vca_password: $vca_password
177 vca_host: $vca_host
178 vca_port: $vca_port
179 vca_pubkey: $vca_pubkey
180 vca_cacert: $vca_cacert
181 vca_apiproxy: $vca_apiproxy
182 vca_cloud: $vca_cloud
183 mon-k8s:
184 options:
185 vca_user: $vca_user
186 vca_password: $vca_password
187 vca_host: $vca_host
188 vca_cacert: $vca_cacert
189EOF
David Garcia42375212020-04-27 19:07:49 +0200190 mv /tmp/vca-overlay.yaml ~/.osm/
191 OSM_VCA_HOST=$vca_host
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100192}
193
David Garcia69388c22020-05-07 12:14:19 +0200194function generate_images_overlay(){
195 cat << EOF > /tmp/images-overlay.yaml
196applications:
197 lcm-k8s:
198 options:
199 image: opensourcemano/lcm:$TAG
200 mon-k8s:
201 options:
202 image: opensourcemano/mon:$TAG
203 ro-k8s:
204 options:
205 image: opensourcemano/ro:$TAG
206 nbi-k8s:
207 options:
208 image: opensourcemano/nbi:$TAG
209 pol-k8s:
210 options:
211 image: opensourcemano/pol:$TAG
212 ui-k8s:
213 options:
214 image: opensourcemano/light-ui:$TAG
215
216EOF
217 mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE
218}
219
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100220function install_osmclient() {
221 sudo snap install osmclient
222 sudo snap alias osmclient.osm osm
223}
224
225function create_iptables() {
226 check_install_iptables_persistent
227
228 if ! sudo iptables -t nat -C PREROUTING -p tcp -m tcp -d $DEFAULT_IP --dport 17070 -j DNAT --to-destination $OSM_VCA_HOST; then
229 sudo iptables -t nat -A PREROUTING -p tcp -m tcp -d $DEFAULT_IP --dport 17070 -j DNAT --to-destination $OSM_VCA_HOST
230 sudo netfilter-persistent save
231 fi
232}
233
234function check_install_iptables_persistent(){
235 echo -e "\nChecking required packages: iptables-persistent"
236 if ! dpkg -l iptables-persistent &>/dev/null; then
237 echo -e " Not installed.\nInstalling iptables-persistent requires root privileges"
238 echo iptables-persistent iptables-persistent/autosave_v4 boolean true | sudo debconf-set-selections
239 echo iptables-persistent iptables-persistent/autosave_v6 boolean true | sudo debconf-set-selections
240 sudo apt-get -yq install iptables-persistent
241 fi
242}
243
244function install_microstack() {
245 sudo snap install microstack --classic --beta
246 sudo microstack.init --auto
247 wget https://cloud-images.ubuntu.com/releases/16.04/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img -P ~/.osm/
248 microstack.openstack image create \
David Garcia42375212020-04-27 19:07:49 +0200249 --public \
250 --disk-format qcow2 \
251 --container-format bare \
252 --file ~/.osm/ubuntu-16.04-server-cloudimg-amd64-disk1.img \
253 ubuntu1604
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100254 ssh-keygen -t rsa -N "" -f ~/.ssh/microstack
255 microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack
256 export OSM_HOSTNAME=`juju status --format yaml | yq r - applications.nbi-k8s.address`
257 osm vim-create --name microstack-site \
David Garcia42375212020-04-27 19:07:49 +0200258 --user admin \
259 --password keystone \
260 --auth_url http://10.20.20.1:5000/v3 \
261 --tenant admin \
262 --account_type openstack \
263 --config='{security_groups: default,
264 keypair: microstack,
265 project_name: admin,
266 user_domain_name: default,
267 region_name: microstack,
268 insecure: True,
269 availability_zone: nova,
270 version: 3}'
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100271}
272
273DEFAULT_IF=`route -n |awk '$1~/^0.0.0.0/ {print $8}'`
274DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
275
276check_arguments $@
David Garcia42375212020-04-27 19:07:49 +0200277mkdir -p ~/.osm
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100278install_snaps
279bootstrap_k8s_lxd
280deploy_charmed_osm
David Garcia42375212020-04-27 19:07:49 +0200281[ ! -v CONTROLLER ] && create_iptables
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100282install_osmclient
David Garcia42375212020-04-27 19:07:49 +0200283if [ -v MICROSTACK ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100284 install_microstack
285fi