From f68b0306ab72b198fbc5aecba1e7d19629f5e2c2 Mon Sep 17 00:00:00 2001
From: Pete Birley <pete@port.direct>
Date: Wed, 17 May 2017 10:24:35 -0500
Subject: [PATCH] Gate: CentOS, Fedora and local use support

This PS adds support for CentOS, Fedora and running the gate locally
on a developers machine. It also expands on the original gate scripts
by setting up dns access to the OpenStack cluster from the host's
running OpenStack-Helm using a daemonset.

Change-Id: I99c573a44a73452d558e612fc020e22e7f41ade7
---
 tools/gate/README.md                       | 21 +++++++++++
 tools/gate/basic_launch.sh                 |  7 ++++
 tools/gate/funcs/helm.sh                   | 25 +++++++++---
 tools/gate/funcs/kube.sh                   | 40 +++++++++++++++++---
 tools/gate/funcs/network.sh                | 44 ++++++++++++++++++++++
 tools/gate/kubeadm_aio.sh                  |  2 +
 tools/gate/manifests/resolv-conf-util.yaml | 41 ++++++++++++++++++++
 tools/gate/setup_gate.sh                   | 10 ++++-
 tools/kubeadm-aio/README.md                |  2 +-
 9 files changed, 179 insertions(+), 13 deletions(-)
 create mode 100644 tools/gate/README.md
 create mode 100755 tools/gate/funcs/network.sh
 create mode 100644 tools/gate/manifests/resolv-conf-util.yaml

diff --git a/tools/gate/README.md b/tools/gate/README.md
new file mode 100644
index 0000000000..29d87df5a8
--- /dev/null
+++ b/tools/gate/README.md
@@ -0,0 +1,21 @@
+# Openstack-Helm Gate Scripts
+
+These scripts are used in the OpenStack-Helm Gates and can also be run locally to aid development and for demonstration purposes. Please note that they assume full control of a machine, and may be destructive in nature, so should only be run on a dedicated host.
+
+## Usage
+
+The Gate scripts use the `setup_gate.sh` as an entrypoint and are controlled by environment variables, an example of use to run the basic integration test is below:
+
+``` bash
+export INTEGRATION=aio
+export INTEGRATION_TYPE=basic
+./tools/gate/setup_gate.sh
+```
+
+### Supported Platforms
+
+Currently supported host platforms are:
+ * Ubuntu 16.04
+ * CentOS 7
+
+With some preparation to docker, and disabling of SELinux operation of Fedora 25 is also supported.
diff --git a/tools/gate/basic_launch.sh b/tools/gate/basic_launch.sh
index 9791e02098..40188a59cc 100755
--- a/tools/gate/basic_launch.sh
+++ b/tools/gate/basic_launch.sh
@@ -29,6 +29,13 @@ kube_wait_for_pods openstack 600
 helm install local/keystone --name=keystone --namespace=openstack
 kube_wait_for_pods openstack 240
 
+# NOTE(portdirect): Temp workaround until module loading is supported by
+# OpenStack-Helm in Fedora
+if [ "x$HOST_OS" == "xfedora" ]; then
+  sudo modprobe openvswitch
+  sudo modprobe gre
+  sudo modprobe vxlan
+fi
 helm install local/glance --name=glance --namespace=openstack --values=${WORK_DIR}/tools/overrides/mvp/glance.yaml
 helm install local/nova --name=nova --namespace=openstack --values=${WORK_DIR}/tools/overrides/mvp/nova.yaml --set=conf.nova.libvirt.nova.conf.virt_type=qemu
 helm install local/neutron --name=neutron --namespace=openstack --values=${WORK_DIR}/tools/overrides/mvp/neutron.yaml
diff --git a/tools/gate/funcs/helm.sh b/tools/gate/funcs/helm.sh
index 1583cddd14..d672210a18 100755
--- a/tools/gate/funcs/helm.sh
+++ b/tools/gate/funcs/helm.sh
@@ -15,16 +15,29 @@ set -e
 
 function helm_install {
   TMP_DIR=$(mktemp -d)
-  sudo apt-get update -y
-  sudo apt-get install -y --no-install-recommends -qq \
-    git \
-    make \
-    curl \
-    ca-certificates
+  if [ "x$HOST_OS" == "xubuntu" ]; then
+    sudo apt-get update -y
+    sudo apt-get install -y --no-install-recommends -qq \
+      git \
+      make \
+      curl \
+      ca-certificates
+  elif [ "x$HOST_OS" == "xcentos" ]; then
+    sudo yum install -y \
+      git \
+      make \
+      curl
+  elif [ "x$HOST_OS" == "xfedora" ]; then
+    sudo dnf install -y \
+      git \
+      make \
+      curl
+  fi
 
   # install helm
   curl -sSL https://storage.googleapis.com/kubernetes-helm/helm-${HELM_VERSION}-linux-amd64.tar.gz | tar -zxv --strip-components=1 -C ${TMP_DIR}
   sudo mv ${TMP_DIR}/helm /usr/local/bin/helm
+
   rm -rf ${TMP_DIR}
 }
 
diff --git a/tools/gate/funcs/kube.sh b/tools/gate/funcs/kube.sh
index 8e8f4989e5..17441f0b07 100755
--- a/tools/gate/funcs/kube.sh
+++ b/tools/gate/funcs/kube.sh
@@ -46,15 +46,40 @@ function kube_wait_for_pods {
 
 function kubeadm_aio_reqs_install {
   TMP_DIR=$(mktemp -d)
-  sudo apt-get update -y
-  sudo apt-get install -y --no-install-recommends -qq \
-          docker.io \
-          nfs-common \
-          jq
+  if [ "x$HOST_OS" == "xubuntu" ]; then
+    sudo apt-get update -y
+    sudo apt-get install -y --no-install-recommends -qq \
+            docker.io \
+            nfs-common \
+            jq
+  elif [ "x$HOST_OS" == "xcentos" ]; then
+    sudo yum install -y \
+            epel-release
+    sudo yum install -y \
+            docker \
+            nfs-utils \
+            jq
+    sudo cp -f /usr/lib/systemd/system/docker.service /etc/systemd/system/docker.service
+    sudo sed -i "s|^MountFlags=slave|MountFlags=share|g" /etc/systemd/system/docker.service
+    sudo systemctl daemon-reload
+    sudo systemctl restart docker
+  elif [ "x$HOST_OS" == "xfedora" ]; then
+    sudo dnf install -y \
+            docker-latest \
+            nfs-utils \
+            jq
+    sudo cp -f /usr/lib/systemd/system/docker-latest.service /etc/systemd/system/docker.service
+    sudo sed -i "s|/var/lib/docker-latest|/var/lib/docker|g" /etc/systemd/system/docker.service
+    echo "DOCKER_STORAGE_OPTIONS=--storage-driver=overlay2" | sudo tee /etc/sysconfig/docker-latest-storage
+    sudo systemctl daemon-reload
+    sudo systemctl restart docker
+  fi
 
   curl -sSL https://storage.googleapis.com/kubernetes-release/release/${KUBE_VERSION}/bin/linux/amd64/kubectl -o ${TMP_DIR}/kubectl
   chmod +x ${TMP_DIR}/kubectl
   sudo mv ${TMP_DIR}/kubectl /usr/local/bin/kubectl
+
+  rm -rf ${TMP_DIR}
 }
 
 function kubeadm_aio_build {
@@ -62,6 +87,11 @@ function kubeadm_aio_build {
 }
 
 function kubeadm_aio_launch {
+  if [ "x$HOST_OS" == "xcentos" ]; then
+    sudo setenforce 0 || true
+  elif [ "x$HOST_OS" == "xfedora" ]; then
+    sudo setenforce 0 || true
+  fi
   ${WORK_DIR}/tools/kubeadm-aio/kubeadm-aio-launcher.sh
   mkdir -p ${HOME}/.kube
   cat ${KUBECONFIG} > ${HOME}/.kube/config
diff --git a/tools/gate/funcs/network.sh b/tools/gate/funcs/network.sh
new file mode 100755
index 0000000000..85f724914c
--- /dev/null
+++ b/tools/gate/funcs/network.sh
@@ -0,0 +1,44 @@
+#!/bin/bash
+#
+# 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.
+set -e
+
+function net_resolv_pre_kube {
+  sudo cp -f /etc/resolv.conf /etc/resolv-pre-kube.conf
+  cat << EOF | sudo tee /etc/resolv.conf
+nameserver 8.8.8.8
+EOF
+}
+
+function net_resolv_kube {
+  kubectl get namespace openstack || kubectl create namespace openstack
+  kubectl create --namespace openstack -f ${WORK_DIR}/tools/gate/manifests/resolv-conf-util.yaml
+  kube_wait_for_pods openstack 240
+}
+
+function net_resolv_post_kube {
+  sudo cp -f /etc/resolv-pre-kube.conf /etc/resolv.conf
+}
+
+function net_hosts_pre_kube {
+  sudo cp -f /etc/hosts /etc/hosts-pre-kube
+  HOST_IFACE=$(ip route | grep "^default" | awk '{ print $5 }')
+  HOST_IP=$(ip addr | awk "/inet/ && /${HOST_IFACE}/{sub(/\/.*$/,\"\",\$2); print \$2}")
+
+  sudo sed -i "/$(hostname)/d" /etc/hosts
+  echo "${HOST_IP} $(hostname)" | sudo tee -a /etc/hosts
+}
+
+function net_hosts_post_kube {
+  sudo cp -f /etc/hosts-pre-kube /etc/hosts
+}
diff --git a/tools/gate/kubeadm_aio.sh b/tools/gate/kubeadm_aio.sh
index c8ad203dd2..eb58958aa5 100755
--- a/tools/gate/kubeadm_aio.sh
+++ b/tools/gate/kubeadm_aio.sh
@@ -13,8 +13,10 @@
 # limitations under the License.
 set -ex
 
+source ${WORK_DIR}/tools/gate/funcs/network.sh
 source ${WORK_DIR}/tools/gate/funcs/kube.sh
 
 kubeadm_aio_reqs_install
 #kubeadm_aio_build
 kubeadm_aio_launch
+net_resolv_kube
diff --git a/tools/gate/manifests/resolv-conf-util.yaml b/tools/gate/manifests/resolv-conf-util.yaml
new file mode 100644
index 0000000000..cde8d9b918
--- /dev/null
+++ b/tools/gate/manifests/resolv-conf-util.yaml
@@ -0,0 +1,41 @@
+# Copyright 2017 The Openstack-Helm Authors.
+#
+# 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.
+
+apiVersion: extensions/v1beta1
+kind: DaemonSet
+metadata:
+  name: resolv-conf-util
+spec:
+  template:
+    metadata:
+      labels:
+        version: v0.1.0
+      name: resolv-conf-util
+    spec:
+      containers:
+      - name: resolv-conf-util
+        image: docker.io/ubuntu:16.04
+        command:
+          - /bin/bash
+          - -ecx
+          - |
+            cat /etc/resolv.conf > /host/etc/resolv.conf
+            tail -f /dev/null
+        volumeMounts:
+          - mountPath: /host/etc/resolv.conf
+            name: host-resolv-conf
+      volumes:
+        - name: host-resolv-conf
+          hostPath:
+            path: /etc/resolv.conf
diff --git a/tools/gate/setup_gate.sh b/tools/gate/setup_gate.sh
index 2ae4227534..b34802f49f 100755
--- a/tools/gate/setup_gate.sh
+++ b/tools/gate/setup_gate.sh
@@ -14,13 +14,21 @@
 set -ex
 
 export HELM_VERSION=${2:-v2.3.0}
-export KUBE_VERSION=${3:-v1.6.0}
+export KUBE_VERSION=${3:-v1.6.2}
 export KUBECONFIG=${HOME}/.kubeadm-aio/admin.conf
 export KUBEADM_IMAGE=openstackhelm/kubeadm-aio:v1.6
 
 export WORK_DIR=$(pwd)
+source /etc/os-release
+export HOST_OS=${ID}
+source ${WORK_DIR}/tools/gate/funcs/network.sh
 source ${WORK_DIR}/tools/gate/funcs/helm.sh
 
+# We setup the network for pre kube here, to enable cluster restarts on
+# development machines
+net_resolv_pre_kube
+net_hosts_pre_kube
+
 helm_install
 helm_serve
 helm_lint
diff --git a/tools/kubeadm-aio/README.md b/tools/kubeadm-aio/README.md
index a22819cbed..4888abf315 100644
--- a/tools/kubeadm-aio/README.md
+++ b/tools/kubeadm-aio/README.md
@@ -72,7 +72,7 @@ your host you may run the following, but note that this will wipe any previous
 client configuration you may have.
 
 ``` bash
-mkdir -p  ${HOME}/.kube
+mkdir -p ${HOME}/.kube
 cat ${HOME}/.kubeadm-aio/admin.conf > ${HOME}/.kube/config
 ```