| 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" |
| David Garcia | 69388c2 | 2020-05-07 12:14:19 +0200 | [diff] [blame] | 19 | IMAGES_OVERLAY_FILE=~/.osm/images-overlay.yaml |
| 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" ;; |
| David Garcia | 69388c2 | 2020-05-07 12:14:19 +0200 | [diff] [blame] | 28 | --microstack) MICROSTACK=y ;; |
| 29 | --tag) TAG="$2" ;; |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 30 | esac |
| 31 | shift |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 32 | done |
| 33 | |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 34 | # echo $BUNDLE $KUBECONFIG $LXDENDPOINT |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 35 | } |
| 36 | function install_snaps(){ |
| 37 | sudo snap install juju --classic |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 38 | [ ! -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] | 39 | } |
| 40 | |
| 41 | function bootstrap_k8s_lxd(){ |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 42 | [ -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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 48 | else |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 49 | 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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 53 | fi |
| 54 | |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 55 | if [ -v LXD_CLOUD ]; then |
| 56 | if [ ! -v LXD_CREDENTIALS ]; then |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 57 | 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 Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 62 | 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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 77 | |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 78 | cat << EOF > $LXD_CLOUD |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 79 | clouds: |
| 80 | lxd-cloud: |
| 81 | type: lxd |
| 82 | auth-types: [certificate] |
| 83 | endpoint: "https://$LXDENDPOINT:8443" |
| 84 | config: |
| 85 | ssl-hostname-verification: false |
| 86 | EOF |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 87 | 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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 91 | |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 92 | cat << EOF > $LXD_CREDENTIALS |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 93 | credentials: |
| 94 | lxd-cloud: |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 95 | lxd-cloud: |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 96 | auth-type: certificate |
| 97 | server-cert: | |
| 98 | $server_cert |
| 99 | client-cert: | |
| 100 | $client_cert |
| 101 | client-key: | |
| 102 | $client_key |
| 103 | EOF |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 104 | lxc config trust add local: ~/.osm/client.crt |
| 105 | fi |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 106 | |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 107 | 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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | function deploy_charmed_osm(){ |
| 114 | create_overlay |
| 115 | echo "Creating OSM model" |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 116 | if [ -v KUBECFG ]; then |
| 117 | juju add-model osm $K8S_CLOUD_NAME |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 118 | else |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 119 | sg microk8s -c "juju add-model osm $K8S_CLOUD_NAME" |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 120 | fi |
| 121 | echo "Deploying OSM with charms" |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 122 | # echo $BUNDLE |
| 123 | if [ -v BUNDLE ]; then |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 124 | juju deploy $BUNDLE --overlay ~/.osm/vca-overlay.yaml |
| 125 | else |
| David Garcia | 69388c2 | 2020-05-07 12:14:19 +0200 | [diff] [blame] | 126 | 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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 129 | fi |
| 130 | echo "Waiting for deployment to finish..." |
| 131 | check_osm_deployed &> /dev/null |
| 132 | echo "OSM with charms deployed" |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 133 | sg microk8s -c "microk8s.enable ingress" |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 134 | juju config ui-k8s juju-external-hostname=osm.$DEFAULT_IP.xip.io |
| 135 | juju expose ui-k8s |
| 136 | } |
| 137 | |
| 138 | function check_osm_deployed() { |
| 139 | while true |
| 140 | do |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 141 | 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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 144 | break |
| 145 | fi |
| 146 | fi |
| 147 | sleep 10 |
| 148 | done |
| 149 | } |
| 150 | |
| 151 | function create_overlay() { |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 152 | 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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 156 | local HOME=/home/$USER |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 157 | 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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 161 | 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 Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 164 | 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] | 165 | |
| 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 Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 171 | cat << EOF > /tmp/vca-overlay.yaml |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 172 | applications: |
| 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 |
| 189 | EOF |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 190 | mv /tmp/vca-overlay.yaml ~/.osm/ |
| 191 | OSM_VCA_HOST=$vca_host |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| David Garcia | 69388c2 | 2020-05-07 12:14:19 +0200 | [diff] [blame] | 194 | function generate_images_overlay(){ |
| 195 | cat << EOF > /tmp/images-overlay.yaml |
| 196 | applications: |
| 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 | |
| 216 | EOF |
| 217 | mv /tmp/images-overlay.yaml $IMAGES_OVERLAY_FILE |
| 218 | } |
| 219 | |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 220 | function install_osmclient() { |
| 221 | sudo snap install osmclient |
| 222 | sudo snap alias osmclient.osm osm |
| 223 | } |
| 224 | |
| 225 | function 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 | |
| 234 | function 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 | |
| 244 | function 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 Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 249 | --public \ |
| 250 | --disk-format qcow2 \ |
| 251 | --container-format bare \ |
| 252 | --file ~/.osm/ubuntu-16.04-server-cloudimg-amd64-disk1.img \ |
| 253 | ubuntu1604 |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 254 | 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 Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 258 | --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 Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | DEFAULT_IF=`route -n |awk '$1~/^0.0.0.0/ {print $8}'` |
| 274 | DEFAULT_IP=`ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}'` |
| 275 | |
| 276 | check_arguments $@ |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 277 | mkdir -p ~/.osm |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 278 | install_snaps |
| 279 | bootstrap_k8s_lxd |
| 280 | deploy_charmed_osm |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 281 | [ ! -v CONTROLLER ] && create_iptables |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 282 | install_osmclient |
| David Garcia | 4237521 | 2020-04-27 19:07:49 +0200 | [diff] [blame] | 283 | if [ -v MICROSTACK ]; then |
| Dominik Fleischmann | 5e4a751 | 2020-03-06 14:05:06 +0100 | [diff] [blame] | 284 | install_microstack |
| 285 | fi |