Adding kubespray installation to automation-tools repository

Change-Id: I9539e842cea1b1030a35d804287fb33789d22de3
diff --git a/kubespray-installer/.gitignore b/kubespray-installer/.gitignore
new file mode 100644
index 0000000..ab99130
--- /dev/null
+++ b/kubespray-installer/.gitignore
@@ -0,0 +1,2 @@
+kubespray
+*.retry
\ No newline at end of file
diff --git a/kubespray-installer/configs/.gitignore b/kubespray-installer/configs/.gitignore
new file mode 100644
index 0000000..c96a04f
--- /dev/null
+++ b/kubespray-installer/configs/.gitignore
@@ -0,0 +1,2 @@
+*
+!.gitignore
\ No newline at end of file
diff --git a/kubespray-installer/copy-ssh-keys.sh b/kubespray-installer/copy-ssh-keys.sh
new file mode 100755
index 0000000..ff1918b
--- /dev/null
+++ b/kubespray-installer/copy-ssh-keys.sh
@@ -0,0 +1,24 @@
+#!/usr/bin/env bash
+#
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+NODES=($@)
+
+for NODE in "${NODES[@]}";
+  do
+    ssh-keygen -R ${NODE}
+    ssh cord@${NODE} 'mkdir -p .ssh'
+    cat ~/.ssh/id_rsa.pub | ssh cord@${NODE} 'cat >> .ssh/authorized_keys'
+  done
diff --git a/kubespray-installer/inventories/.gitignore b/kubespray-installer/inventories/.gitignore
new file mode 100644
index 0000000..c96a04f
--- /dev/null
+++ b/kubespray-installer/inventories/.gitignore
@@ -0,0 +1,2 @@
+*
+!.gitignore
\ No newline at end of file
diff --git a/kubespray-installer/k8s-configs.yaml b/kubespray-installer/k8s-configs.yaml
new file mode 100644
index 0000000..cb8ae78
--- /dev/null
+++ b/kubespray-installer/k8s-configs.yaml
@@ -0,0 +1,67 @@
+---
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+- hosts: localhost
+
+  vars:
+    tryluca:
+      - one
+      - two
+      - three
+
+  tasks:
+    # Edit all.yml variable file
+
+    - name: Add default Python interpreter
+      lineinfile:
+        path: inventories/{{ deployment_name }}/group_vars/all.yml
+        line: 'ansible_python_interpreter: /usr/bin/python2.7'
+
+    - name: Add default Ansible user
+      lineinfile:
+        path: inventories/{{ deployment_name }}/group_vars/all.yml
+        line: 'ansible_user: cord'
+
+    - name: Give sudoer permissions
+      lineinfile:
+        path: inventories/{{ deployment_name }}/group_vars/all.yml
+        line: 'ansible_become: true'
+
+    # Edit k8s-cluster.yml variable file
+    - name: Set Docker DNS servers strict to false
+      lineinfile:
+        path: inventories/{{ deployment_name }}/group_vars/k8s-cluster.yml
+        regexp: '^#\s*docker_dns_servers_strict.*$'
+        line: 'docker_dns_servers_strict: false'
+
+    - name: Set Helm enabled to true
+      lineinfile:
+        path: inventories/{{ deployment_name }}/group_vars/k8s-cluster.yml
+        regexp: '^helm_enabled.*$'
+        line: 'helm_enabled: true'
+
+    - name: Set kubeconfig localhost to true
+      lineinfile:
+        path: inventories/{{ deployment_name }}/group_vars/k8s-cluster.yml
+        regexp: '^#\s*kubeconfig_localhost.*$'
+        line: 'kubeconfig_localhost: true'
+
+    - name: Allow insecure registry access
+      lineinfile:
+        path: inventories/{{ deployment_name }}/group_vars/k8s-cluster.yml
+        regexp: 'docker_options'
+        line: "docker_options: \"{{ item }} --insecure-registry={{ '{{' }} kube_service_addresses {{ '}}' }} --graph={{ '{{' }} docker_daemon_graph {{ '}}' }}  {{ '{{' }} docker_log_opts {{ '}}' }}\""
+      with_items: "{{ k8s_nodes.split(' ') | map('regex_replace', '(.*)', '--insecure-registry=\\1/32') | join(' ') }}"
diff --git a/kubespray-installer/k8s-requirements.yaml b/kubespray-installer/k8s-requirements.yaml
new file mode 100644
index 0000000..a34b68f
--- /dev/null
+++ b/kubespray-installer/k8s-requirements.yaml
@@ -0,0 +1,69 @@
+---
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+- hosts: all
+  gather_facts: False
+
+  tasks:
+    - name: Update apt-get
+      raw: sudo apt-get update -qq
+
+    - name: Install python 2.7
+      raw: sudo apt-get install -qq python2.7
+
+    - name: Install prerequisites for using PPA repos
+      apt:
+        name: "{{ item }}"
+        update_cache: yes
+        cache_valid_time: 3600
+      with_items:
+        - python-pycurl
+        - software-properties-common
+        - python-netaddr
+
+    - name: Add Ansible PPA
+      apt_repository:
+        repo={{ item }}
+      with_items:
+        - "{{ ansible_apt_repo | default('ppa:ansible/ansible') }}"
+      register: result
+      until: result | success
+      retries: 3
+      delay: 10
+
+    - name: Make sure Ansible is newest version
+      apt:
+        name: "ansible"
+        state: latest
+        update_cache: yes
+        cache_valid_time: 3600
+
+    - name: Enable ip forwarding
+      sysctl:
+        name: 'net.ipv4.ip_forward'
+        value: 1
+        sysctl_set: True
+        state: present
+        reload: yes
+
+    - name: Remove swapfile from /etc/fstab
+      mount:
+        name: swap
+        fstype: swap
+        state: absent
+
+    - name: Disable swap
+      command: swapoff -a
\ No newline at end of file
diff --git a/kubespray-installer/setup.sh b/kubespray-installer/setup.sh
new file mode 100755
index 0000000..61902cd
--- /dev/null
+++ b/kubespray-installer/setup.sh
@@ -0,0 +1,159 @@
+#!/usr/bin/env bash
+#
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+################################################
+#                                              #
+# Installs Kubespray on remote target machines #
+#                                              #
+################################################
+
+#
+# Installs Kubespray on remote target machines.
+#
+install_kubespray () {
+  # Cleanup Old Kubespray Installations
+  echo -e "\nCleaning Up Old Kubespray Installation" && \
+  rm -rf kubespray
+
+  # Download Kubespray
+  echo -e "\nDownloading Kubespray" && \
+  git clone https://github.com/kubernetes-incubator/kubespray.git -b v2.4.0 && \
+
+  # Generate inventory and var files
+  echo -e "\nGenerating The Inventory File" && \
+  rm -rf inventories/${DEPLOYMENT_NAME} && \
+  cp -r kubespray/inventory inventories/${DEPLOYMENT_NAME} && \
+  CONFIG_FILE=inventories/${DEPLOYMENT_NAME}/inventory.cfg python3 kubespray/contrib/inventory_builder/inventory.py ${NODES[@]} && \
+
+  # Edit inventory var files
+  NODE_LIST=`echo ${NODES[@]}`
+  ansible-playbook k8s-configs.yaml --extra-vars "deployment_name=${DEPLOYMENT_NAME} k8s_nodes='${NODE_LIST}'"
+
+  # Copy SSH keys
+  echo -e "\nCopying Public SSH Keys To Remote Machines" && \
+  source copy-ssh-keys.sh ${NODES[@]} && \
+
+  # Prepare Target Machines
+  echo -e "\nInstalling Prerequisites On Remote Machines" && \
+  ansible-playbook -i inventories/${DEPLOYMENT_NAME}/inventory.cfg k8s-requirements.yaml && \
+
+  # Install Kubespray
+  echo -e "\nInstalling Kubespray" && \
+  ansible-playbook -i inventories/${DEPLOYMENT_NAME}/inventory.cfg kubespray/cluster.yml -b -v && \
+
+  # Export the Kubespray Config Location
+  echo -e "\nLoading Kubespray Configuration" && \
+  cp kubespray/artifacts/admin.conf configs/${DEPLOYMENT_NAME}.conf
+}
+
+#
+# Exports the Kubespray Config Location
+#
+source_kubeconfig () {
+  export KUBECONFIG=${PWD}/configs/${DEPLOYMENT_NAME}.conf
+}
+
+#
+# Helm init
+#
+helm_init () {
+  echo -e "\nInitializing Helm" && \
+  source_kubeconfig $DEPLOYMENT_NAME && \
+  helm init --upgrade
+}
+
+#
+# Deploy an insecure registry
+#
+deploy_insecure_registry () {
+  echo -e "\nDeploying insecure registry" && \
+  source_kubeconfig $DEPLOYMENT_NAME && \
+  helm install stable/docker-registry --set service.nodePort=30500,service.type=NodePort -n docker-registry
+}
+
+#
+# Checks if an arbitrary pod name is given during specifc
+# operations.
+#
+check_pod_name () {
+  if [ -z $DEPLOYMENT_NAME ]
+    then
+      echo "Missing option: podname" >&2
+      echo " "
+      display_help
+      exit -1
+    fi
+}
+
+#
+# Displays the help menu.
+#
+display_help () {
+  echo "Usage: $0 {--install|--source|--help} [podname] [ip...] " >&2
+  echo " "
+  echo "   -h, --help              Display this help message."
+  echo "   -i, --install           Install Kubespray on <podname>"
+  echo "   -s, --source            Source the Kubectl config for <podname>"
+  echo " "
+  echo "   podname                 An arbitrary name representing the pod"
+  echo "   ip                      The IP address of a remote node"
+  echo " "
+  echo "Example usages:"
+  echo "   ./setup.sh -i podname 192.168.10.100 192.168.10.101 192.168.10.102"
+  echo "   ./setup.sh -i podname (detault is 10.90.0.101 10.90.0.102 10.90.0.103)"
+  echo "   source setup.sh -s podname"
+}
+
+#
+# Init
+#
+CLI_OPT=$1
+DEPLOYMENT_NAME=$2
+shift 2
+DEFAULT_NODES="10.90.0.101 10.90.0.102 10.90.0.103"
+NODES=(${@:-$DEFAULT_NODES})
+
+while :
+do
+  case $CLI_OPT in
+    -i | --install)
+        check_pod_name
+        install_kubespray $DEPLOYMENT_NAME $NODES
+        helm_init $DEPLOYMENT_NAME
+        deploy_insecure_registry $DEPLOYMENT_NAME
+        exit 0
+        ;;
+    -h | --help)
+        display_help
+        exit 0
+        ;;
+    -s | --source)
+        check_pod_name
+        source_kubeconfig $DEPLOYMENT_NAME
+        return 0
+         ;;
+    --) # End of all options
+        shift
+        break
+        ;;
+    *)
+        echo Error: Unknown option: $CLI_OPT >&2
+        echo " "
+        display_help
+        exit -1
+        ;;
+  esac
+done