blob: dd26c57de334e91cb1671ddf315381d108695de6 [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"
19
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" ;;
28 --microstack) MICROSTACK=y
29 esac
30 shift
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010031 done
32
David Garcia42375212020-04-27 19:07:49 +020033 # echo $BUNDLE $KUBECONFIG $LXDENDPOINT
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010034}
35function install_snaps(){
36 sudo snap install juju --classic
David Garcia42375212020-04-27 19:07:49 +020037 [ ! -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 +010038}
39
40function bootstrap_k8s_lxd(){
David Garcia42375212020-04-27 19:07:49 +020041 [ -v CONTROLLER ] && ADD_K8S_OPTS="--controller ${CONTROLLER}" && CONTROLLER_NAME=$CONTROLLER
42 [ ! -v CONTROLLER ] && ADD_K8S_OPTS="--client" && BOOTSTRAP_NEEDED="yes" && CONTROLLER_NAME="controller"
43
44 if [ -v KUBECFG ]; then
45 cat $KUBECFG | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
46 [ -v BOOTSTRAP_NEEDED ] && juju bootstrap $K8S_CLOUD_NAME $CONTROLLER_NAME
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010047 else
David Garcia42375212020-04-27 19:07:49 +020048 sg microk8s -c "microk8s.enable storage dns"
49
50 [ ! -v BOOTSTRAP_NEEDED ] && sg microk8s -c "microk8s.config" | juju add-k8s $K8S_CLOUD_NAME $ADD_K8S_OPTS
51 [ -v BOOTSTRAP_NEEDED ] && sg microk8s -c "juju bootstrap microk8s $CONTROLLER_NAME" && K8S_CLOUD_NAME=microk8s
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010052 fi
53
David Garcia42375212020-04-27 19:07:49 +020054 if [ -v LXD_CLOUD ]; then
55 if [ ! -v LXD_CREDENTIALS ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010056 echo "The installer needs the LXD server certificate if the LXD is external"
57 exit 1
58 fi
59 else
60 LXDENDPOINT=$DEFAULT_IP
David Garcia42375212020-04-27 19:07:49 +020061 LXD_CLOUD=~/.osm/lxd-cloud.yaml
62 LXD_CREDENTIALS=~/.osm/lxd-credentials.yaml
63 # Apply sysctl production values for optimal performance
64 sudo cp /usr/share/osm-devops/installers/60-lxd-production.conf /etc/sysctl.d/60-lxd-production.conf
65 sudo sysctl --system
66 # Install LXD snap
67 sudo apt-get remove --purge -y liblxc1 lxc-common lxcfs lxd lxd-client
68 sudo snap install lxd
69 sudo apt-get install zfsutils-linux -y
70 # Configure LXD
71 sudo usermod -a -G lxd `whoami`
72 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"
73 sg lxd -c "lxd waitready"
74 DEFAULT_MTU=$(ip addr show $DEFAULT_IF | perl -ne 'if (/mtu\s(\d+)/) {print $1;}')
75 sg lxd -c "lxc profile device set default eth0 mtu $DEFAULT_MTU"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010076
David Garcia42375212020-04-27 19:07:49 +020077 cat << EOF > $LXD_CLOUD
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010078clouds:
79 lxd-cloud:
80 type: lxd
81 auth-types: [certificate]
82 endpoint: "https://$LXDENDPOINT:8443"
83 config:
84 ssl-hostname-verification: false
85EOF
David Garcia42375212020-04-27 19:07:49 +020086 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"
87 local server_cert=`cat /var/snap/lxd/common/lxd/server.crt | sed 's/^/ /'`
88 local client_cert=`cat ~/.osm/client.crt | sed 's/^/ /'`
89 local client_key=`cat ~/.osm/client.key | sed 's/^/ /'`
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010090
David Garcia42375212020-04-27 19:07:49 +020091 cat << EOF > $LXD_CREDENTIALS
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010092credentials:
93 lxd-cloud:
David Garcia42375212020-04-27 19:07:49 +020094 lxd-cloud:
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +010095 auth-type: certificate
96 server-cert: |
97$server_cert
98 client-cert: |
99$client_cert
100 client-key: |
101$client_key
102EOF
David Garcia42375212020-04-27 19:07:49 +0200103 lxc config trust add local: ~/.osm/client.crt
104 fi
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100105
David Garcia42375212020-04-27 19:07:49 +0200106 juju add-cloud -c $CONTROLLER_NAME lxd-cloud $LXD_CLOUD --force
107 juju add-credential -c $CONTROLLER_NAME lxd-cloud -f $LXD_CREDENTIALS
108 sg lxd -c "lxd waitready"
109 juju add-model test lxd-cloud || true
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100110}
111
112function deploy_charmed_osm(){
113 create_overlay
114 echo "Creating OSM model"
David Garcia42375212020-04-27 19:07:49 +0200115 if [ -v KUBECFG ]; then
116 juju add-model osm $K8S_CLOUD_NAME
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100117 else
David Garcia42375212020-04-27 19:07:49 +0200118 sg microk8s -c "juju add-model osm $K8S_CLOUD_NAME"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100119 fi
120 echo "Deploying OSM with charms"
David Garcia42375212020-04-27 19:07:49 +0200121 # echo $BUNDLE
122 if [ -v BUNDLE ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100123 juju deploy $BUNDLE --overlay ~/.osm/vca-overlay.yaml
124 else
125 juju deploy osm --overlay ~/.osm/vca-overlay.yaml
126 fi
127 echo "Waiting for deployment to finish..."
128 check_osm_deployed &> /dev/null
129 echo "OSM with charms deployed"
David Garcia42375212020-04-27 19:07:49 +0200130 sg microk8s -c "microk8s.enable ingress"
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100131 juju config ui-k8s juju-external-hostname=osm.$DEFAULT_IP.xip.io
132 juju expose ui-k8s
133}
134
135function check_osm_deployed() {
136 while true
137 do
David Garcia42375212020-04-27 19:07:49 +0200138 pod_name=`sg microk8s -c "microk8s.kubectl -n osm get pods | grep ui-k8s | grep -v operator" | awk '{print $1}'`
139 if [[ `sg microk8s -c "microk8s.kubectl -n osm wait pod $pod_name --for condition=Ready"` ]]; then
140 if [[ `sg microk8s -c "microk8s.kubectl -n osm wait pod lcm-k8s-0 --for condition=Ready"` ]]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100141 break
142 fi
143 fi
144 sleep 10
145 done
146}
147
148function create_overlay() {
David Garcia42375212020-04-27 19:07:49 +0200149 sudo snap install jq
150 sudo apt install python3-pip -y
151 python3 -m pip install yq
152 PATH=$PATH:$HOME/.local/bin # make yq command available
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100153 local HOME=/home/$USER
David Garcia42375212020-04-27 19:07:49 +0200154 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME].user')
155 local vca_password=$(cat $HOME/.local/share/juju/accounts.yaml | yq --arg CONTROLLER_NAME $CONTROLLER_NAME '.controllers[$CONTROLLER_NAME].password')
156 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)
157 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 +0100158 local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
159 local vca_cloud="lxd-cloud"
160 # Get the VCA Certificate
David Garcia42375212020-04-27 19:07:49 +0200161 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 +0100162
163 # Calculate the default route of this machine
164 local DEFAULT_IF=`route -n |awk '$1~/^0.0.0.0/ {print $8}'`
165 local vca_apiproxy=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
166
167 # Generate a new overlay.yaml, overriding any existing one
David Garcia42375212020-04-27 19:07:49 +0200168 cat << EOF > /tmp/vca-overlay.yaml
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100169applications:
170 lcm-k8s:
171 options:
172 vca_user: $vca_user
173 vca_password: $vca_password
174 vca_host: $vca_host
175 vca_port: $vca_port
176 vca_pubkey: $vca_pubkey
177 vca_cacert: $vca_cacert
178 vca_apiproxy: $vca_apiproxy
179 vca_cloud: $vca_cloud
180 mon-k8s:
181 options:
182 vca_user: $vca_user
183 vca_password: $vca_password
184 vca_host: $vca_host
185 vca_cacert: $vca_cacert
186EOF
David Garcia42375212020-04-27 19:07:49 +0200187 mv /tmp/vca-overlay.yaml ~/.osm/
188 OSM_VCA_HOST=$vca_host
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100189}
190
191function install_osmclient() {
192 sudo snap install osmclient
193 sudo snap alias osmclient.osm osm
194}
195
196function create_iptables() {
197 check_install_iptables_persistent
198
199 if ! sudo iptables -t nat -C PREROUTING -p tcp -m tcp -d $DEFAULT_IP --dport 17070 -j DNAT --to-destination $OSM_VCA_HOST; then
200 sudo iptables -t nat -A PREROUTING -p tcp -m tcp -d $DEFAULT_IP --dport 17070 -j DNAT --to-destination $OSM_VCA_HOST
201 sudo netfilter-persistent save
202 fi
203}
204
205function check_install_iptables_persistent(){
206 echo -e "\nChecking required packages: iptables-persistent"
207 if ! dpkg -l iptables-persistent &>/dev/null; then
208 echo -e " Not installed.\nInstalling iptables-persistent requires root privileges"
209 echo iptables-persistent iptables-persistent/autosave_v4 boolean true | sudo debconf-set-selections
210 echo iptables-persistent iptables-persistent/autosave_v6 boolean true | sudo debconf-set-selections
211 sudo apt-get -yq install iptables-persistent
212 fi
213}
214
215function install_microstack() {
216 sudo snap install microstack --classic --beta
217 sudo microstack.init --auto
218 wget https://cloud-images.ubuntu.com/releases/16.04/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img -P ~/.osm/
219 microstack.openstack image create \
David Garcia42375212020-04-27 19:07:49 +0200220 --public \
221 --disk-format qcow2 \
222 --container-format bare \
223 --file ~/.osm/ubuntu-16.04-server-cloudimg-amd64-disk1.img \
224 ubuntu1604
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100225 ssh-keygen -t rsa -N "" -f ~/.ssh/microstack
226 microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack
227 export OSM_HOSTNAME=`juju status --format yaml | yq r - applications.nbi-k8s.address`
228 osm vim-create --name microstack-site \
David Garcia42375212020-04-27 19:07:49 +0200229 --user admin \
230 --password keystone \
231 --auth_url http://10.20.20.1:5000/v3 \
232 --tenant admin \
233 --account_type openstack \
234 --config='{security_groups: default,
235 keypair: microstack,
236 project_name: admin,
237 user_domain_name: default,
238 region_name: microstack,
239 insecure: True,
240 availability_zone: nova,
241 version: 3}'
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100242}
243
244DEFAULT_IF=`route -n |awk '$1~/^0.0.0.0/ {print $8}'`
245DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
246
247check_arguments $@
David Garcia42375212020-04-27 19:07:49 +0200248mkdir -p ~/.osm
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100249install_snaps
250bootstrap_k8s_lxd
251deploy_charmed_osm
David Garcia42375212020-04-27 19:07:49 +0200252[ ! -v CONTROLLER ] && create_iptables
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100253install_osmclient
David Garcia42375212020-04-27 19:07:49 +0200254if [ -v MICROSTACK ]; then
Dominik Fleischmann5e4a7512020-03-06 14:05:06 +0100255 install_microstack
256fi