| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 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 | # |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 15 | |
| 16 | # set -eux |
| 17 | |
| 18 | K8S_CLOUD_NAME="k8s-cloud" |
| 19 | |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 20 | function check_arguments(){ |
| 21 | while [ $# -gt 0 ] ; do |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 22 | 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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 31 | done |
| 32 | |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 33 | # echo $BUNDLE $KUBECONFIG $LXDENDPOINT |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 34 | } |
| 35 | function install_snaps(){ |
| 36 | sudo snap install juju --classic |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 37 | [ ! -v KUBECFG ] && sudo snap install microk8s --classic && sudo usermod -a -G microk8s ubuntu && mkdir -p ~/.kube && sudo chown -f -R `whoami` ~/.kube |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | function bootstrap_k8s_lxd(){ |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 41 | [ -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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 47 | else |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 48 | 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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 52 | fi |
| 53 | |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 54 | if [ -v LXD_CLOUD ]; then |
| 55 | if [ ! -v LXD_CREDENTIALS ]; then |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 56 | 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 Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 61 | 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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 76 | |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 77 | cat << EOF > $LXD_CLOUD |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 78 | clouds: |
| 79 | lxd-cloud: |
| 80 | type: lxd |
| 81 | auth-types: [certificate] |
| 82 | endpoint: "https://$LXDENDPOINT:8443" |
| 83 | config: |
| 84 | ssl-hostname-verification: false |
| 85 | EOF |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 86 | 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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 90 | |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 91 | cat << EOF > $LXD_CREDENTIALS |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 92 | credentials: |
| 93 | lxd-cloud: |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 94 | lxd-cloud: |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 95 | auth-type: certificate |
| 96 | server-cert: | |
| 97 | $server_cert |
| 98 | client-cert: | |
| 99 | $client_cert |
| 100 | client-key: | |
| 101 | $client_key |
| 102 | EOF |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 103 | lxc config trust add local: ~/.osm/client.crt |
| 104 | fi |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 105 | |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 106 | 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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | function deploy_charmed_osm(){ |
| 113 | create_overlay |
| 114 | echo "Creating OSM model" |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 115 | if [ -v KUBECFG ]; then |
| 116 | juju add-model osm $K8S_CLOUD_NAME |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 117 | else |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 118 | sg microk8s -c "juju add-model osm $K8S_CLOUD_NAME" |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 119 | fi |
| 120 | echo "Deploying OSM with charms" |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 121 | # echo $BUNDLE |
| 122 | if [ -v BUNDLE ]; then |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 123 | 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 Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 130 | sg microk8s -c "microk8s.enable ingress" |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 131 | juju config ui-k8s juju-external-hostname=osm.$DEFAULT_IP.xip.io |
| 132 | juju expose ui-k8s |
| 133 | } |
| 134 | |
| 135 | function check_osm_deployed() { |
| 136 | while true |
| 137 | do |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 138 | 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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 141 | break |
| 142 | fi |
| 143 | fi |
| 144 | sleep 10 |
| 145 | done |
| 146 | } |
| 147 | |
| 148 | function create_overlay() { |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 149 | 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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 153 | local HOME=/home/$USER |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 154 | 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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 158 | 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 Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 161 | 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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 162 | |
| 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 Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 168 | cat << EOF > /tmp/vca-overlay.yaml |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 169 | applications: |
| 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 |
| 186 | EOF |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 187 | mv /tmp/vca-overlay.yaml ~/.osm/ |
| 188 | OSM_VCA_HOST=$vca_host |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | function install_osmclient() { |
| 192 | sudo snap install osmclient |
| 193 | sudo snap alias osmclient.osm osm |
| 194 | } |
| 195 | |
| 196 | function 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 | |
| 205 | function 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 | |
| 215 | function 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 Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 220 | --public \ |
| 221 | --disk-format qcow2 \ |
| 222 | --container-format bare \ |
| 223 | --file ~/.osm/ubuntu-16.04-server-cloudimg-amd64-disk1.img \ |
| 224 | ubuntu1604 |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 225 | 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 Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 229 | --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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | DEFAULT_IF=`route -n |awk '$1~/^0.0.0.0/ {print $8}'` |
| 245 | DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'` |
| 246 | |
| 247 | check_arguments $@ |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 248 | mkdir -p ~/.osm |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 249 | install_snaps |
| 250 | bootstrap_k8s_lxd |
| 251 | deploy_charmed_osm |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 252 | [ ! -v CONTROLLER ] && create_iptables |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 253 | install_osmclient |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 254 | if [ -v MICROSTACK ]; then |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 255 | install_microstack |
| 256 | fi |