Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

247 add dev env #45

Merged
merged 12 commits into from
Dec 4, 2020
1 change: 1 addition & 0 deletions templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func main() {

r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
log.Printf("Hello, %q", html.EscapeString(r.URL.Path))
})

serverAddress := fmt.Sprintf("0.0.0.0:%s", os.Getenv("SERVER_PORT"))
Expand Down
116 changes: 116 additions & 0 deletions templates/start-dev-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/bin/bash

#
# This script is to create a dev namespace on Staging environment
#
PROJECT_NAME=<% .Name %>
ENVIRONMENT=stage
ACCOUNT_ID=<% index .Params `accountId` %>
REGION=<% index .Params `region` %>

# common functions
function usage() {
echo
echo "Usage:"
echo " $0 <project id>"
echo " - project id: can be 001, 002, or whatever id without space"
exit 1
}

function command_exist() {
command -v ${1} >& /dev/null
}

function error_exit() {
echo "ERROR : $1"
exit 2
}

function can_i() {
commands=$1
IFS=',' read -r -a array <<< "$commands"
err=0
for command in "${array[@]}"
do
kubectl --context ${CLUSTER_CONTEXT} auth can-i $command >& /dev/null || (echo "No permission to '$command'" && let "err+=1")
done

[[ $err -gt 0 ]] && error_exit "Found $err permission errors. Please check with your administrator."
}

# Start
# Validate current iam user
MY_USERNAME=$(aws sts get-caller-identity --output json | jq -r .Arn | cut -d/ -f2)
DEV_USERS=$(aws iam get-group --group-name ${PROJECT_NAME}-developer-${ENVIRONMENT} | jq -r .Users[].UserName)
bmonkman marked this conversation as resolved.
Show resolved Hide resolved
[[ "${DEV_USERS[@]}" =~ "${MY_USERNAME}" ]] || error_exit "You (${MY_USERNAME}) are not in ${DEV_USERS}"
sshi100 marked this conversation as resolved.
Show resolved Hide resolved

DEV_PROJECT_ID=${1:-"001"}
sshi100 marked this conversation as resolved.
Show resolved Hide resolved

echo '[Dev Environment]'

# Validate cluster
CLUSTER_CONTEXT=${PROJECT_NAME}-${ENVIRONMENT}-${REGION}
echo " Cluster context: ${CLUSTER_CONTEXT}"

# Validate secret
NAMESPACE=${PROJECT_NAME}
SECRET_NAME=${PROJECT_NAME}
DEV_SECRET_NAME=devenv${PROJECT_NAME}
DEV_SECRET_JSON=$(kubectl --context ${CLUSTER_CONTEXT} get secret ${DEV_SECRET_NAME} -n ${NAMESPACE} -o json)
[[ -z "${DEV_SECRET_JSON}" ]] && error_exit "The secret ${DEV_SECRET_NAME} is not existing in namespace '${NAMESPACE}'."

# Check installations
if ! command_exist kustomize || ! command_exist telepresence; then
if ! command_exist kustomize; then
error_exit "command 'kustomize' not found: please visit https://kubectl.docs.kubernetes.io/installation/kustomize/"
fi
if ! command_exist kubectl; then
error_exit "command 'telepresence' not found. You can download it at https://www.telepresence.io/reference/install"
fi
fi

# Setup dev namepsace
DEV_NAMESPACE=${MY_USERNAME}-${DEV_PROJECT_ID}
kubectl --context ${CLUSTER_CONTEXT} get namespace ${DEV_NAMESPACE} >& /dev/null || \
(can_i "create namespace,create deployment,create ingress,create service,create secret,create configmap" && \
kubectl --context ${CLUSTER_CONTEXT} create namespace ${DEV_NAMESPACE})
echo " Namespace: ${DEV_NAMESPACE}"

# Setup dev secret from pre-configed one
kubectl --context ${CLUSTER_CONTEXT} get secret ${SECRET_NAME} -n ${DEV_NAMESPACE} >& /dev/null || \
echo ${DEV_SECRET_JSON} | jq 'del(.metadata["namespace","creationTimestamp","resourceVersion","selfLink","uid"])' | sed "s/${DEV_SECRET_NAME}/${SECRET_NAME}/g" | kubectl --context ${CLUSTER_CONTEXT} apply -n ${DEV_NAMESPACE} -f -
echo " Secret: ${SECRET_NAME}"

# Setup dev service account from pre-configured one
SERVICE_ACCOUNT=backend-service
kubectl --context ${CLUSTER_CONTEXT} get sa ${SERVICE_ACCOUNT} -n ${DEV_NAMESPACE} >& /dev/null || \
kubectl --context ${CLUSTER_CONTEXT} get sa ${SERVICE_ACCOUNT} -n ${NAMESPACE} -o json | jq 'del(.metadata["namespace","creationTimestamp","resourceVersion","selfLink","uid"])' | kubectl --context ${CLUSTER_CONTEXT} apply -n ${DEV_NAMESPACE} -f -

# Setup dev k8s manifests, configuration, docker login etc
CONFIG_ENVIRONMENT="staging"
EXT_HOSTNAME=<% index .Params `stagingBackendSubdomain` %><% index .Params `stagingHostRoot` %>
MY_EXT_HOSTNAME=${DEV_NAMESPACE}-${EXT_HOSTNAME}
ECR_REPO=${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/${PROJECT_NAME}
VERSION_TAG=latest
DATABASE_NAME=<% index .Params `databaseName` %>
DEV_DATABASE_NAME=$(echo "dev${MY_USERNAME}" | tr -dc 'A-Za-z0-9')
echo " Domain: ${MY_EXT_HOSTNAME}"
echo " Database Name: ${DEV_DATABASE_NAME}"

# Apply manifests
(cd kubenetes/overlay/${CONFIG_ENVIRONMENT} && \
kustomize build . | \
sed "s|image: fake-image|image: ${ECR_REPO}:${VERSION_TAG}|g" | \
sshi100 marked this conversation as resolved.
Show resolved Hide resolved
sed "s|${EXT_HOSTNAME}|${MY_EXT_HOSTNAME}|g" | \
sed "s|DATABASE_NAME=${DATABASE_NAME}|DATABASE_NAME=${DEV_DATABASE_NAME}|g" | \
kubectl --context ${} -n ${DEV_NAMESPACE} apply -f - ) || error_exit "Failed to apply kubernetes manifests"

# Starting dev environment with telepresence shell
echo
telepresence --swap-deployment ${PROJECT_NAME} --namespace ${DEV_NAMESPACE} --expose 80 --run-shell

# Ending dev environment
echo
kubectl --context ${CLUSTER_CONTEXT} delete namespaces/${DEV_NAMESPACE}
echo "Your dev environment on Staging has been deleted completely"
echo