Skip to content

Commit

Permalink
add new param to aws eks init formula
Browse files Browse the repository at this point in the history
  • Loading branch information
luangazin committed Oct 26, 2021
1 parent db7eed3 commit 214dfdb
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 6 deletions.
51 changes: 51 additions & 0 deletions vkpr/aws/eks/init/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,57 @@
{
"name": "gitlab_username",
"type": "CREDENTIAL_GITLAB_USERNAME"
},
{
"name": "eks_cluster_name",
"label": "EKS cluster name: ",
"default": "eks-sample",
"required": true,
"type": "text",
"cache": {
"active": true,
"qty": 8,
"newLabel": "Type another EKS cluster name: "
}
},
{
"label": "K8s version",
"name": "eks_k8s_version",
"type": "text",
"required": true,
"items": [
"1.21",
"1.20",
"1.19",
"1.18",
"1.17"
]

},
{
"label": "Choose nodes instance type: ",
"name": "eks_cluster_node_instance_type",
"type": "text",
"required": true,
"items": [
"t3.small (vCPU:2, Mem: 2Gib)",
"m5.large (vCPU:2, Mem: 8Gib)",
"m5.xlarge (vCPU:4, Mem: 16Gib)",
"t4g.small (vCPU:2, Mem: 2Gib)",
"m6g.large (vCPU:2, Mem: 8Gib)",
"m6g.xlarge (vCPU:4, Mem: 16Gib)"
]
},
{
"name": "eks_cluster_size",
"label": "Desired number of nodes: ",
"required": true,
"type": "text",
"cache": {
"active": true,
"qty": 3,
"newLabel": "Type another EKS cluster name: "
}
}
],
"template": "shell-bat",
Expand Down
85 changes: 79 additions & 6 deletions vkpr/aws/eks/init/src/unix/formula/formula.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,87 @@
#!/bin/sh

PROJECT_ID=$(rawUrlEncode "$GITLAB_USERNAME/aws-eks")

runFormula() {
$VKPR_GLAB auth login -h "gitlab.com" -t "$GITLAB_TOKEN"
$VKPR_GLAB repo fork vkpr/aws-eks
#getting real instance type
EKS_CLUSTER_NODE_INSTANCE_TYPE=${EKS_CLUSTER_NODE_INSTANCE_TYPE// ([^)]*)/}
EKS_CLUSTER_NODE_INSTANCE_TYPE=${EKS_CLUSTER_NODE_INSTANCE_TYPE// /}

checkGlobalConfig $AWS_REGION "us-east-1" "aws.eks.region" "AWS_REGION"
checkGlobalConfig $EKS_CLUSTER_NAME "eks-sample" "aws.eks.cluster_name" "EKS_CLUSTER_NAME"
checkGlobalConfig $EKS_CLUSTER_NODE_INSTANCE_TYPE "t3.small" "aws.eks.nodes.instace_type" "EKS_CLUSTER_NODE_INSTANCE_TYPE"
checkGlobalConfig $EKS_K8S_VERSION "1.21" "aws.eks.version" "EKS_K8S_VERSION"
checkGlobalConfig $EKS_CLUSTER_SIZE "1" "aws.eks.nodes.size" "EKS_CLUSTER_SIZE"

local FORK_RESPONSE_CODE=$(curl -s -i -X POST --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "https://gitlab.com/api/v4/projects/$(rawUrlEncode "vkpr/aws-eks")/fork" | head -n 1 | awk -F' ' '{print $2}')
# echo "FORK_RESPONSE_CODE= $FORK_RESPONSE_CODE"
if [ $FORK_RESPONSE_CODE = 409 ];then
echoColor yellow "Project already forked"
fi

setVariablesGLAB
$VKPR_GLAB ci run -R $GITLAB_USERNAME/aws-eks

createBranch $EKS_CLUSTER_NAME

}

## Set all input into Gitlab environments
setVariablesGLAB() {
$VKPR_GLAB variable set "AWS_ACCESS_KEY" -m -v "$AWS_ACCESS_KEY" -R $GITLAB_USERNAME/aws-eks
$VKPR_GLAB variable set "AWS_SECRET_KEY" -m -v "$AWS_SECRET_KEY" -R $GITLAB_USERNAME/aws-eks
$VKPR_GLAB variable set "AWS_REGION" -m -v "$AWS_REGION" -R $GITLAB_USERNAME/aws-eks
createOrUpdateVariable "AWS_ACCESS_KEY" $AWS_ACCESS_KEY "yes"
createOrUpdateVariable "AWS_SECRET_KEY" $AWS_SECRET_KEY "yes"
createOrUpdateVariable "AWS_REGION" $AWS_REGION "no"
createOrUpdateVariable "EKS_CLUSTER_NAME" $EKS_CLUSTER_NAME "no"
createOrUpdateVariable "EKS_CLUSTER_NODE_INSTANCE_TYPE" $EKS_CLUSTER_NODE_INSTANCE_TYPE "no"
createOrUpdateVariable "EKS_K8S_VERSION" "$EKS_K8S_VERSION" "no"
createOrUpdateVariable "EKS_CLUSTER_SIZE" "$EKS_CLUSTER_SIZE" "no"
}

## Create a new variable setting cluster-name as environment
## If var already exist, just update your value
createOrUpdateVariable(){
# echo "VARIABLE $1 = $2"
local VARIABLE_RESPONSE_CODE=$(curl -s -i --request POST --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "https://gitlab.com/api/v4/projects/${PROJECT_ID}/variables" \
--form "key=$1" --form "value=$2" --form "masked=$3" --form "environment_scope=$EKS_CLUSTER_NAME" | head -n 1 | awk -F' ' '{print $2}')
# echo "VARIABLE_RESPONSE_CODE = $VARIABLE_RESPONSE_CODE"

if [ $VARIABLE_RESPONSE_CODE = 201 ];then
echoColor yellow "Variable $1 created into ${EKS_CLUSTER_NAME} environment"
elif [ $VARIABLE_RESPONSE_CODE = 400 ];then
# echoColor yellow "Variable $1 already exists, updating..."
updateVariable $@
else
echoColor red "Something wrong while saving $1"
fi

}

## Update gitlab variable
updateVariable(){
local UPDATE_CODE=$(curl -s -i --request PUT --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "https://gitlab.com/api/v4/projects/${PROJECT_ID}/variables/${1}?filter\[environment_scope\]=${EKS_CLUSTER_NAME}" \
--form "value=$2" --form "masked=$3" | head -n 1 | awk -F' ' '{print $2}')
# echo "UPDATE_CODE= $UPDATE_CODE"
if [ $UPDATE_CODE = 200 ];then
echoColor green "$1 updated"
else
echoColor red "error while updating $1, $UPDATE_CODE"
fi
}

## Create a new branch using eks-cluster-name as branch's name, or just start a new pipeline
createBranch(){
echoColor green "Creating branch named $1 or justing starting a new pipeline"
# echo "https://gitlab.com/api/v4/projects/${PROJECT_ID}/repository/branches?branch=$1&ref=master"
local CREATE_BRANCH_CODE=$(curl -s -i --request POST --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "https://gitlab.com/api/v4/projects/${PROJECT_ID}/repository/branches?branch=$1&ref=master" | head -n 1 | awk -F' ' '{print $2}')
# echo "CREATE_BRANCH_CODE: $CREATE_BRANCH_CODE"

if [ $CREATE_BRANCH_CODE = 400 ];then
createPipeline $1
fi
}

## create a new pipeline
createPipeline(){
RESPONSE_PIPE=$(curl -s --request POST --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "https://gitlab.com/api/v4/projects/${PROJECT_ID}/pipeline?ref=$1")
# echo "RESPONSE_PIPE: $RESPONSE_PIPE"
echoColor green "Pipeline url: $(echo $RESPONSE_PIPE | $VKPR_JQ -r '.web_url')"
}
19 changes: 19 additions & 0 deletions vkpr/init/src/utils/helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,23 @@ registerHelmRepository(){
echoColor green "Repository ${REPO_NAME} already configured."
fi

}

##Encode text
rawUrlEncode() {
local string="${1}"
local strlen=${#string}
local encoded=""
local pos c o

for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}" # You can either set a return variable (FASTER)
REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p
}

0 comments on commit 214dfdb

Please sign in to comment.