731282b604423ab308be705dbf43d7c1a46f9a49
[osm/devops.git] / installers / charmed_install.sh
1 #! /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 #
15 function check_arguments(){
16 while [ $# -gt 0 ] ; do
17 case $1 in
18 --bundle) BUNDLE="$2" ;;
19 --kubeconfig) KUBECFG="$2" ;;
20 --lxd-endpoint) LXDENDPOINT="$2" ;;
21 --lxd-cert) LXDCERT="$2" ;;
22 --microstack) MICROSTACK=y
23 esac
24 shift
25 done
26
27 echo $BUNDLE $KUBECONFIG $LXDENDPOINT
28 }
29 function install_snaps(){
30 sudo snap install juju --classic
31 if [ -z "$KUBECFG" ]; then sudo snap install microk8s --classic; fi
32 }
33
34 function bootstrap_k8s_lxd(){
35 if [ -n "$KUBECFG" ]; then
36 echo "Using specified K8s"
37 cat $KUBECFG | juju add-k8s k8s-cluster --client
38 juju bootstrap k8s-cluster controller
39 else
40 sudo microk8s.enable storage dns
41 sudo usermod -a -G microk8s ubuntu
42 sg microk8s -c "juju bootstrap microk8s controller"
43 fi
44
45 if [ -n "$LXDENDPOINT" ]; then
46 if [ -n "$LXDCERT" ]; then
47
48 local server_cert=`cat $LXDCERT | sed 's/^/ /'`
49 else
50 echo "The installer needs the LXD server certificate if the LXD is external"
51 exit 1
52 fi
53 else
54 LXDENDPOINT=$DEFAULT_IP
55 lxd init --auto --network-address $LXDENDPOINT
56 lxc network set lxdbr0 ipv6.address none
57
58 local server_cert=`cat /var/lib/lxd/server.crt | sed 's/^/ /'`
59 fi
60
61 sudo cat << EOF > ~/.osm/lxd-cloud.yaml
62 clouds:
63 lxd-cloud:
64 type: lxd
65 auth-types: [certificate]
66 endpoint: "https://$LXDENDPOINT:8443"
67 config:
68 ssl-hostname-verification: false
69 EOF
70 openssl req -nodes -new -x509 -keyout ~/.osm/private.key -out ~/.osm/publickey.crt -days 365 -subj "/C=FR/ST=Nice/L=Nice/O=ETSI/OU=OSM/CN=osm.etsi.org"
71
72
73 local client_cert=`cat ~/.osm/publickey.crt | sed 's/^/ /'`
74 local client_key=`cat ~/.osm/private.key | sed 's/^/ /'`
75
76 sudo cat << EOF > ~/.osm/lxd-credentials.yaml
77 credentials:
78 lxd-cloud:
79 admin:
80 auth-type: certificate
81 server-cert: |
82 $server_cert
83 client-cert: |
84 $client_cert
85 client-key: |
86 $client_key
87 EOF
88
89 lxc config trust add local: ~/.osm/publickey.crt
90 juju add-cloud -c controller lxd-cloud ~/.osm/lxd-cloud.yaml --force
91 juju add-credential -c controller lxd-cloud -f ~/.osm/lxd-credentials.yaml
92 juju add-model test lxd-cloud
93
94 }
95
96 function deploy_charmed_osm(){
97 create_overlay
98 echo "Creating OSM model"
99 if [ -n "$KUBECFG" ]; then
100 juju add-model osm-on-k8s k8s-cluster
101 else
102 sg microk8s -c "juju add-model osm-on-k8s microk8s"
103 fi
104 echo "Deploying OSM with charms"
105 echo $BUNDLE
106 if [ -n "$BUNDLE" ]; then
107 juju deploy $BUNDLE --overlay ~/.osm/vca-overlay.yaml
108 else
109 juju deploy osm --overlay ~/.osm/vca-overlay.yaml
110 fi
111 echo "Waiting for deployment to finish..."
112 check_osm_deployed &> /dev/null
113 echo "OSM with charms deployed"
114 sudo microk8s.enable ingress
115 juju config ui-k8s juju-external-hostname=osm.$DEFAULT_IP.xip.io
116 juju expose ui-k8s
117 }
118
119 function check_osm_deployed() {
120 while true
121 do
122 pod_name=`sg microk8s -c "microk8s.kubectl -n osm-on-k8s get pods | grep ui-k8s | grep -v operator" | awk '{print $1}'
123 `
124
125 if [[ `sg microk8s -c "microk8s.kubectl -n osm-on-k8s wait pod $pod_name --for condition=Ready"` ]]; then
126 if [[ `sg microk8s -c "microk8s.kubectl -n osm-on-k8s wait pod lcm-k8s-0 --for condition=Ready"` ]]; then
127 break
128 fi
129 fi
130 sleep 10
131 done
132 }
133
134 function create_overlay() {
135 sudo snap install yq
136
137 local YQ="$SNAP/bin/yq"
138 local HOME=/home/$USER
139 local vca_user=$(cat $HOME/.local/share/juju/accounts.yaml | yq r - controllers.controller.user)
140 local vca_password=$(cat $HOME/.local/share/juju/accounts.yaml | yq r - controllers.controller.password)
141 local vca_host=$(cat $HOME/.local/share/juju/controllers.yaml | yq r - controllers.controller.api-endpoints[0] | cut -d ":" -f 1)
142 local vca_port=$(cat $HOME/.local/share/juju/controllers.yaml | yq r - controllers.controller.api-endpoints[0] | cut -d ":" -f 2)
143 local vca_pubkey=\"$(cat $HOME/.local/share/juju/ssh/juju_id_rsa.pub)\"
144 local vca_cloud="lxd-cloud"
145 # Get the VCA Certificate
146 local vca_cacert=$(cat $HOME/.local/share/juju/controllers.yaml | yq r - controllers.controller.ca-cert | base64 | tr -d \\n)
147
148 # Calculate the default route of this machine
149 local DEFAULT_IF=`route -n |awk '$1~/^0.0.0.0/ {print $8}'`
150 local vca_apiproxy=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
151
152 # Generate a new overlay.yaml, overriding any existing one
153 sudo cat << EOF > /tmp/vca-overlay.yaml
154 applications:
155 lcm-k8s:
156 options:
157 vca_user: $vca_user
158 vca_password: $vca_password
159 vca_host: $vca_host
160 vca_port: $vca_port
161 vca_pubkey: $vca_pubkey
162 vca_cacert: $vca_cacert
163 vca_apiproxy: $vca_apiproxy
164 vca_cloud: $vca_cloud
165 mon-k8s:
166 options:
167 vca_user: $vca_user
168 vca_password: $vca_password
169 vca_host: $vca_host
170 vca_cacert: $vca_cacert
171 EOF
172 sudo cp /tmp/vca-overlay.yaml ~/.osm/
173 OSM_VCA_HOST=$vca_host
174 }
175
176 function install_osmclient() {
177 sudo snap install osmclient
178 sudo snap alias osmclient.osm osm
179 }
180
181 function create_iptables() {
182 check_install_iptables_persistent
183
184 if ! sudo iptables -t nat -C PREROUTING -p tcp -m tcp -d $DEFAULT_IP --dport 17070 -j DNAT --to-destination $OSM_VCA_HOST; then
185 sudo iptables -t nat -A PREROUTING -p tcp -m tcp -d $DEFAULT_IP --dport 17070 -j DNAT --to-destination $OSM_VCA_HOST
186 sudo netfilter-persistent save
187 fi
188 }
189
190 function check_install_iptables_persistent(){
191 echo -e "\nChecking required packages: iptables-persistent"
192 if ! dpkg -l iptables-persistent &>/dev/null; then
193 echo -e " Not installed.\nInstalling iptables-persistent requires root privileges"
194 echo iptables-persistent iptables-persistent/autosave_v4 boolean true | sudo debconf-set-selections
195 echo iptables-persistent iptables-persistent/autosave_v6 boolean true | sudo debconf-set-selections
196 sudo apt-get -yq install iptables-persistent
197 fi
198 }
199
200 function install_microstack() {
201 sudo snap install microstack --classic --beta
202 sudo microstack.init --auto
203 wget https://cloud-images.ubuntu.com/releases/16.04/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img -P ~/.osm/
204 microstack.openstack image create \
205 --public \
206 --disk-format qcow2 \
207 --container-format bare \
208 --file ~/.osm/ubuntu-16.04-server-cloudimg-amd64-disk1.img \
209 ubuntu1604
210 ssh-keygen -t rsa -N "" -f ~/.ssh/microstack
211 microstack.openstack keypair create --public-key ~/.ssh/microstack.pub microstack
212 export OSM_HOSTNAME=`juju status --format yaml | yq r - applications.nbi-k8s.address`
213 osm vim-create --name microstack-site \
214 --user admin \
215 --password keystone \
216 --auth_url http://10.20.20.1:5000/v3 \
217 --tenant admin \
218 --account_type openstack \
219 --config='{security_groups: default,
220 keypair: microstack,
221 project_name: admin,
222 user_domain_name: default,
223 region_name: microstack,
224 insecure: True,
225 availability_zone: nova,
226 version: 3}'
227 }
228
229 DEFAULT_IF=`route -n |awk '$1~/^0.0.0.0/ {print $8}'`
230 DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'`
231
232 check_arguments $@
233 mkdir ~/.osm
234 install_snaps
235 bootstrap_k8s_lxd
236 deploy_charmed_osm
237 create_iptables
238 install_osmclient
239 if [ -n "$MICROSTACK" ]; then
240 install_microstack
241 fi