#!/bin/bash

rm -f cluster.yml
rm -f kube_config_cluster.yml
rm -f rke
rm -f config

wget https://github.com/rancher/rke/releases/download/v0.2.1/rke_linux-amd64
mv rke_linux-amd64 rke
chmod 754 rke


usage () {
  echo "Usage:"
  echo "   ./$(basename $0) -c \"cNode1 cNode2 ... cnodeN\" -w \"wNode1 wnode2 ... wnodeN\""
  echo "Note: controllers hostnames and workers hostnames are to be enclosed in \"\""
  exit 0
}

if [[ ( $# == "--help") ||  $# == "-h" ]] 
        then 
                usage
                exit 0
fi 

if [ "$#" -lt 2 ]; then
  echo "Missing Kubernetes control and worker nodes"
  usage
fi

echo "# An example of an HA Kubernetes cluster for ONAP" >> cluster.yml
echo "nodes:" >> cluster.yml

while getopts c:w: option
do
case "${option}"
in
c) CONTROLLERS=${OPTARG};;
w) WORKERS=${OPTARG};;
esac
done

IFS=' ' read -ra C <<< "$CONTROLLERS"
IFS=' ' read -ra W <<< "$WORKERS"

echo "Testing node availability. This might take some time"
for i in "${C[@]}"; do
while ! ping -c 1 -n -w 1 $i &> /dev/null
do
    printf "%c" "."
done

echo "127.0.0.1 localhost" > hosts
echo "`ping $i -c 1 | grep \"PING\" | grep '('|awk '{gsub(/[()]/,\"\"); print $3}'` ${i}" >> hosts

scp hosts root@$i:/etc/hosts
done

for i in "${W[@]}"; do
while ! ping -c 1 -n -w 1 $i &> /dev/null
do
    printf "%c" "."
done
echo "127.0.0.1 localhost" > hosts
echo "`ping $i -c 1 | grep \"PING\" | grep '('|awk '{gsub(/[()]/,\"\"); print $3}'` ${i}" >> hosts
scp hosts root@$i:/etc/hosts
done
echo "Availability check successful"

for i in "${C[@]}"; do
   echo "- address: `ping $i -c 1 | grep \"PING\" | grep '('|awk '{gsub(/[()]/,\"\"); print $3}'`" >> cluster.yml
   echo '  port: "22"' >> cluster.yml
   echo "  role:" >> cluster.yml
   echo "  - controlplane" >> cluster.yml
   echo "  - etcd" >> cluster.yml
   echo "  hostname_override: `ping $i -c 1 | grep 'PING' | awk '{print $2}' | awk -F . '{print $1}'`" >> cluster.yml
   echo "  user: root" >> cluster.yml
   echo "  ssh_key_path: '~/.ssh/id_rsa'" >> cluster.yml
done

echo "# worker nodes start " >> cluster.yml

for i in "${W[@]}"; do
   echo "- address: `ping $i -c 1 | grep \"PING\" | grep '('|awk '{gsub(/[()]/,\"\"); print $3}'`" >> cluster.yml
   echo '  port: "22"' >> cluster.yml
   echo "  role:" >> cluster.yml
   echo "  - worker" >> cluster.yml
   echo "  hostname_override: `ping $i -c 1 | grep 'PING' | awk '{print $2}' | awk -F . '{print $1}'`" >> cluster.yml
   echo "  user: root" >> cluster.yml
   echo "  ssh_key_path: '~/.ssh/id_rsa'" >> cluster.yml
done

echo 'services:
  kube-api:
    service_cluster_ip_range: 10.43.0.0/16
    pod_security_policy: false
    always_pull_images: false
  kube-controller:
    cluster_cidr: 10.42.0.0/16
    service_cluster_ip_range: 10.43.0.0/16
  kubelet:
    cluster_domain: cluster.local
    cluster_dns_server: 10.43.0.10
    fail_swap_on: false
network:
  plugin: canal
authentication:
  strategy: x509
ssh_key_path: "~/.ssh/id_rsa"
ssh_agent_auth: false
authorization:
  mode: rbac
ignore_docker_version: false
kubernetes_version: "v1.13.5-rancher1-2"
private_registries:
- url: nexus3.onap.org:10001
  user: docker
  password: docker
  is_default: true
cluster_name: "onap"
restore:
  restore: false
  snapshot_name: ""' >> cluster.yml

./rke up

for i in "${C[@]}"; do
scp kube_config_cluster.yml root@$i:~/.kube/config
done

exit 0

