Open sherl0cks opened 7 years ago
@mdanter @oybed FYI
Perhaps simplify to just use a BASH script
#!/usr/bin/env bash
set -e
set -x ## unccomment for debugging
## Check for the existence of the project-names.json file to get the project name prefix
if [[ -f vars/project-names.json ]]; then
export PROJECT_NAME_PREFIX=$(jq -r .project_name_prefix < vars/project-names.json)
else
## If the file does not exist, ask the user to enter it
printf "Project name prefix: "
read PROJECT_NAME_PREFIX
fi
## Check for the existence of the openshift login information and import if it exists
if [[ -f vars/openshift-vars.sh ]]; then
source vars/openshift-vars.sh
else
## If the file doesn't exist, ask the user to input the OpenShift login details
printf "OpenShift API base URL: "
read OPENSHIFT_URL
printf "OpenShift API username: "
read OPENSHIFT_USERNAME
printf "OpenShift API password: "
read OPENSHIFT_PASSWORD
fi
## Log on to the OpenShift cluster
oc login ${OPENSHIFT_URL} --insecure-skip-tls-verify=true -u ${OPENSHIFT_USERNAME} -p ${OPENSHIFT_PASSWORD}
## Convert the project name prefix to all caps for use in display names
export PROJECT_NAME_CAPS=$(tr [:lower:] [:upper:] <<< "${PROJECT_NAME_PREFIX}")
## Read in the vars JSON and perform template replacement on the placeholders
export STARTER_VARS=$(cat templates/ci-cd-starter-vars.json | jq -c .)
export STARTER_VARS=$(sed "s@{{project_name_prefix|capitalize}}@${PROJECT_NAME_CAPS}@g" <<< "${STARTER_VARS}")
export STARTER_VARS=$(sed "s@{{project_name_prefix}}@${PROJECT_NAME_PREFIX}@g" <<< "${STARTER_VARS}")
## Iterate over the projects in the JSON data
IFS=$'\n'; for PROJECT in $(jq -c '.openshift_clusters[0].openshift_resources.projects[]' <<< "${STARTER_VARS}")
do
## Extract the project namespace and display name from the JSON data
PROJECT_NAMESPACE=$(jq -r '.name' <<< "${PROJECT}")
PROJECT_DISPLAY_NAME=$(jq -r '.display_name' <<< "${PROJECT}")
## Create the new project
oc new-project ${PROJECT_NAMESPACE} --display-name "${PROJECT_DISPLAY_NAME}"
## Iterate over the templates in the current project and create new applications from those templates
for TEMPLATE in $(echo ${PROJECT} | jq -c '.templates[]')
do
TEMPLATE_FILE=$(echo ${TEMPLATE} | jq -r '.filename')
TEMPLATE_NAME=$(echo "${TEMPLATE}" | jq -r '.name')
if [[ "null" -eq "$TEMPLATE_FILE" ]]; then ## IF TEMPLATE_FILE is null, assume we're using a remote template
OC_PROCESS_COMMAND="oc process ${TEMPLATE_NAME}"
else ## Otherwise process as a local/http template
OC_PROCESS_COMMAND="oc process -f ${TEMPLATE_FILE}"
fi
for PARAM in $(echo ${TEMPLATE} | jq -c '.parameters' | jq -r 'keys[]')
do
PARAM_VALUE=$(echo ${TEMPLATE} | jq -r ".parameters.${PARAM}")
OC_PROCESS_COMMAND="${OC_PROCESS_COMMAND} -p ${PARAM}=${PARAM_VALUE}"
done
eval "${OC_PROCESS_COMMAND}" | oc apply -f - -n ${PROJECT_NAMESPACE}
done
done
per our discussion today, we'll move this into 3 roles:
Separate it from
create-openshift-resources
and decouple it from the automation api