#!/usr/bin/env bash

#export get_ip_address
. /opt/adb/openshift/get_ip_address

# Prepare, configure and start OpenShift

set -o pipefail
set -o nounset

export ORIGIN_DIR="/var/lib/openshift"
export OPENSHIFT_DIR=${ORIGIN_DIR}/openshift.local.config/master
export KUBECONFIG=${OPENSHIFT_DIR}/admin.kubeconfig

# Function to check openshift availability
wait_for_openshift_api() {
    # Wait for container to show up
    ATTEMPT=0
    until docker inspect openshift > /dev/null 2>&1 || [ $ATTEMPT -eq 10 ]; do
      sleep 1
      ((ATTEMPT++))
    done

    # Wait for API to be accessible
    ATTEMPT=0
    until curl -ksSf https://127.0.0.1:8443/api > /dev/null 2>&1 || [ $ATTEMPT -eq 30 ]; do
      sleep 1
      ((ATTEMPT++))
    done
}

wait_for_openshift_api

# Copy OpenShift CLI tools to the VM
# Binaries are copied every time in case there is a version change
# Note: oc and oadm are symlinks to openshift
binaries=(openshift oc oadm)
for n in ${binaries[@]}; do
  echo "[INFO] Copying ${n} binary to VM"
  docker cp openshift:/usr/bin/${n} /usr/bin/${n}
done

# Create Docker Registry
if [ ! -f ${ORIGIN_DIR}/configured.registry ]; then
  echo "[INFO] Configuring Docker Registry"
  oadm registry --create --credentials=${OPENSHIFT_DIR}/openshift-registry.kubeconfig || exit 1
  oadm policy add-scc-to-group anyuid system:authenticated || exit 1
  touch ${ORIGIN_DIR}/configured.registry
fi

# For router, we have to create service account first and then use it for router creation.
if [ ! -f ${ORIGIN_DIR}/configured.router ]; then
  echo "[INFO] Configuring HAProxy router"
  echo '{"kind":"ServiceAccount","apiVersion":"v1","metadata":{"name":"router"}}' \
    | oc create -f -
  oc get scc privileged -o json \
    | sed '/\"users\"/a \"system:serviceaccount:default:router\",'  \
    | oc replace scc privileged -f -
  oadm router --create \
    --expose-metrics=true \
    --credentials=${OPENSHIFT_DIR}/openshift-router.kubeconfig \
    --service-account=router
  touch ${ORIGIN_DIR}/configured.router

  # Get machine IP address
  ip=$(get_ip_address)
  registry_host_name="hub.openshift.${ip}.xip.io"
  oc expose service docker-registry --hostname ${registry_host_name}
fi

# Installing templates into OpenShift
if [ ! -f ${ORIGIN_DIR}/configured.templates ]; then
  echo "[INFO] Installing OpenShift templates"

  # TODO - These list must be verified and completed for a official release
  # Currently templates are sources from three main repositories
  # - openshift/origin
  # - openshift/nodejs-ex
  # - jboss-openshift/application-templates
  ose_tag=ose-v1.2.0
  template_list=(
    # Image streams
    https://raw.githubusercontent.com/openshift/origin/master/examples/image-streams/image-streams-rhel7.json
    https://raw.githubusercontent.com/jboss-openshift/application-templates/${ose_tag}/jboss-image-streams.json
    # DB templates
    https://raw.githubusercontent.com/openshift/origin/master/examples/db-templates/mongodb-ephemeral-template.json
    https://raw.githubusercontent.com/openshift/origin/master/examples/db-templates/mongodb-persistent-template.json
    https://raw.githubusercontent.com/openshift/origin/master/examples/db-templates/mysql-ephemeral-template.json
    https://raw.githubusercontent.com/openshift/origin/master/examples/db-templates/mysql-persistent-template.json
    https://raw.githubusercontent.com/openshift/origin/master/examples/db-templates/postgresql-ephemeral-template.json
    https://raw.githubusercontent.com/openshift/origin/master/examples/db-templates/postgresql-persistent-template.json
    # Jenkins
    https://raw.githubusercontent.com/openshift/origin/master/examples/jenkins/jenkins-ephemeral-template.json
    https://raw.githubusercontent.com/openshift/origin/master/examples/jenkins/jenkins-persistent-template.json
    # Node.js
    https://raw.githubusercontent.com/openshift/nodejs-ex/master/openshift/templates/nodejs-mongodb.json
    https://raw.githubusercontent.com/openshift/nodejs-ex/master/openshift/templates/nodejs.json
    # EAP
    https://raw.githubusercontent.com/jboss-openshift/application-templates/${ose_tag}/eap/eap64-amq-persistent-s2i.json
    https://raw.githubusercontent.com/jboss-openshift/application-templates/${ose_tag}/eap/eap64-amq-s2i.json
    https://raw.githubusercontent.com/jboss-openshift/application-templates/${ose_tag}/eap/eap64-basic-s2i.json
    https://raw.githubusercontent.com/jboss-openshift/application-templates/${ose_tag}/eap/eap64-https-s2i.json
    https://raw.githubusercontent.com/jboss-openshift/application-templates/${ose_tag}/eap/eap64-mongodb-persistent-s2i.json
    https://raw.githubusercontent.com/jboss-openshift/application-templates/${ose_tag}/eap/eap64-mongodb-s2i.json
    https://raw.githubusercontent.com/jboss-openshift/application-templates/${ose_tag}/eap/eap64-mysql-persistent-s2i.json
    https://raw.githubusercontent.com/jboss-openshift/application-templates/${ose_tag}/eap/eap64-mysql-s2i.json
    https://raw.githubusercontent.com/jboss-openshift/application-templates/${ose_tag}/eap/eap64-postgresql-persistent-s2i.json
    https://raw.githubusercontent.com/jboss-openshift/application-templates/${ose_tag}/eap/eap64-postgresql-s2i.json
  )

  for template in ${template_list[@]}; do
    echo "[INFO] Importing template ${template}"
    oc create -f $template -n openshift >/dev/null
  done
  touch ${ORIGIN_DIR}/configured.templates
fi

# Configuring a openshift-dev and admin user
if [ ! -f ${OPENSHIFT_DIR}/user.htpasswd ]; then
  echo "[INFO] Creating 'test-admin' user and 'test' project ..."
  oadm policy add-role-to-user basic-user openshift-dev --config=${OPENSHIFT_DIR}/admin.kubeconfig
  oadm policy add-cluster-role-to-user cluster-admin admin --config=${OPENSHIFT_DIR}/admin.kubeconfig
fi
