[CORD-3182]

Pin the version of kubespray to master commit
Check for k8s config file and error if not present

Change-Id: I09a54cb8e03b020adb5bdd3e7ab9d7c92200a8ac
diff --git a/kubespray-installer/.gitignore b/kubespray-installer/.gitignore
index ab99130..b032f2b 100644
--- a/kubespray-installer/.gitignore
+++ b/kubespray-installer/.gitignore
@@ -1,2 +1,3 @@
 kubespray
-*.retry
\ No newline at end of file
+inventories
+*.retry
diff --git a/kubespray-installer/configs/.gitignore b/kubespray-installer/configs/.gitignore
deleted file mode 100644
index c96a04f..0000000
--- a/kubespray-installer/configs/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-*
-!.gitignore
\ No newline at end of file
diff --git a/kubespray-installer/inventories/.gitignore b/kubespray-installer/inventories/.gitignore
deleted file mode 100644
index c96a04f..0000000
--- a/kubespray-installer/inventories/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-*
-!.gitignore
\ No newline at end of file
diff --git a/kubespray-installer/k8s-configs.yaml b/kubespray-installer/k8s-configs.yaml
index b998a06..d975633 100644
--- a/kubespray-installer/k8s-configs.yaml
+++ b/kubespray-installer/k8s-configs.yaml
@@ -19,22 +19,16 @@
     kubespray_remote_ssh_user: "cord"
 
   tasks:
+
     # Edit all.yml variable file
-
-    - name: Add default Python interpreter
+    - name: Add lines to the all.yaml variable file
       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: {{ kubespray_remote_ssh_user }}'
-
-    - name: Give sudoer permissions
-      lineinfile:
-        path: inventories/{{ deployment_name }}/group_vars/all.yml
-        line: 'ansible_become: true'
+        line: "{{ item }}"
+      with_items:
+         - 'ansible_python_interpreter: /usr/bin/python2.7'
+         - 'ansible_user: {{ kubespray_remote_ssh_user }}'
+         - 'ansible_become: true'
 
     # Edit k8s-cluster.yml variable file
     - name: Set Docker DNS servers strict to false
@@ -61,3 +55,16 @@
         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(' ') }}"
+
+    - name: Enable local volume provisioner
+      lineinfile:
+        path: inventories/{{ deployment_name }}/group_vars/k8s-cluster.yml
+        regexp: '^local_volume_provisioner_enabled.*'
+        line: 'local_volume_provisioner_enabled: true'
+
+    - name: Enable persistent volumes
+      lineinfile:
+        path: inventories/{{ deployment_name }}/group_vars/k8s-cluster.yml
+        regexp: '^persistent_volumes_enabled.*'
+        line: 'persistent_volumes_enabled: true'
+
diff --git a/kubespray-installer/setup.sh b/kubespray-installer/setup.sh
index 08fce09..72ed808 100755
--- a/kubespray-installer/setup.sh
+++ b/kubespray-installer/setup.sh
@@ -20,6 +20,8 @@
 
 set -e -u -o pipefail
 
+KS_COMMIT="${KS_COMMIT:-73a2a180061113ac124683e5cc492ba07df33d4c}"
+
 install_kubespray () {
   # Cleanup Old Kubespray Installations
   echo "Cleaning Up Old Kubespray Installation"
@@ -27,12 +29,18 @@
 
   # Download Kubespray
   echo "Downloading Kubespray"
-  git clone https://github.com/kubernetes-incubator/kubespray.git -b "v2.4.0"
+  git clone https://github.com/kubernetes-incubator/kubespray.git
+  pushd kubespray
+  git checkout "$KS_COMMIT"
+  popd
 
   # Generate inventory and var files
   echo "Generating The Inventory File"
+
   rm -rf "inventories/${DEPLOYMENT_NAME}"
-  cp -r kubespray/inventory "inventories/${DEPLOYMENT_NAME}"
+  mkdir -p "inventories/${DEPLOYMENT_NAME}"
+
+  cp -r kubespray/inventory/sample/group_vars "inventories/${DEPLOYMENT_NAME}/group_vars"
   CONFIG_FILE="inventories/${DEPLOYMENT_NAME}/inventory.cfg" python3 kubespray/contrib/inventory_builder/inventory.py "${NODES[@]}"
 
   # Add configuration to inventory
@@ -46,16 +54,27 @@
   echo "Installing Kubespray"
   ansible-playbook -i "inventories/${DEPLOYMENT_NAME}/inventory.cfg" kubespray/cluster.yml -b -v
 
-  # Export the Kubespray Config Location
-  echo "Loading 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
+
+  kubeconfig_path="${PWD}/inventories/${DEPLOYMENT_NAME}/artifacts/admin.conf"
+
+  if [ -f "$kubeconfig_path" ]
+  then
+    # these options are annoying outside of scripts
+    set +e +u +o pipefail
+
+    echo "setting KUBECONFIG=$kubeconfig_path"
+
+    export KUBECONFIG="$kubeconfig_path"
+  else
+    echo "kubernetes admin.conf not found at: '$kubeconfig_path'"
+    exit 1
+  fi
 }
 
 #
@@ -123,8 +142,8 @@
     -s | --source)
         check_pod_name
         source_kubeconfig
-        return 0
-         ;;
+        break
+        ;;
     --) # End of all options
         shift
         break