diff --git a/terraform/bootstrap.sh b/terraform/bootstrap.sh new file mode 100755 index 0000000000000000000000000000000000000000..5bcf1287a6757f63c4411e4a0b184284f89207f9 --- /dev/null +++ b/terraform/bootstrap.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +BASEDIR=$(dirname "$0") + +if ! [ -f "$BASEDIR/secret/login.sh" ]; then + echo "Please create the login.sh file!" + exit 0 +fi +source $BASEDIR/secret/login.sh + +if [ -d "$BASEDIR/.terraform" ]; then + echo ".terraform folder exists." +else + terraform init \ + -backend-config="address=https://git.sch.bme.hu/api/v4/projects/${GITLAB_PROJECT_ID}/terraform/state/${GITLAB_STATE_NAME}" \ + -backend-config="lock_address=https://git.sch.bme.hu/api/v4/projects/${GITLAB_PROJECT_ID}/terraform/state/${GITLAB_STATE_NAME}/lock" \ + -backend-config="unlock_address=https://git.sch.bme.hu/api/v4/projects/${GITLAB_PROJECT_ID}/terraform/state/${GITLAB_STATE_NAME}/lock" \ + -backend-config="username=${GITLAB_USER}" \ + -backend-config="password=${GITLAB_REPO_TOKEN}" \ + -backend-config="lock_method=POST" \ + -backend-config="unlock_method=DELETE" \ + -backend-config="retry_wait_min=5" \ + -reconfigure +fi +echo "Environment has been set up." +# to stay in our comfy virtualenv +exec "${SHELL:bash}" \ No newline at end of file diff --git a/terraform/init.sh b/terraform/init.sh deleted file mode 100755 index 389a4c34e9e0c11a7801aef8a24173616cf7ef76..0000000000000000000000000000000000000000 --- a/terraform/init.sh +++ /dev/null @@ -1,54 +0,0 @@ -#!/bin/bash - -BASEDIR=$(dirname "$0") -PROJ_ID="3594" -STATE_NAME="state" - -if [ -f "$BASEDIR/secret/login.sh" ]; then - echo "This folder is already initialized." - read -r -p "Reinitialize it? [y/N] " response - if ! [[ "$response" =~ ^([yY])$ ]] - then - echo "Doing nothing. Don't forget to source the secret/login.sh file" - exit 0 - fi -fi - -## Get variables - -echo "Gitlab username:" -read GITLAB_USER -echo "Private access token for the Terraform state repo:" -read GITLAB_REPO_PAT -echo "Vsphere user:" -read TF_VAR_vsphere_user -echo "Vsphere password:" -read TF_VAR_vsphere_password - -## Create login.sh - -cat > "$BASEDIR/secret/login.sh" << EOF -# This file is managed with init.sh - -export GITLAB_USER="$GITLAB_USER" -export GITLAB_REPO_PAT="$GITLAB_REPO_PAT" -export TF_VAR_vsphere_user="$TF_VAR_vsphere_user" -export TF_VAR_vsphere_password="$TF_VAR_vsphere_password" -EOF - -chmod +x "$BASEDIR/secret/login.sh" - -## Initialize Terraform - -terraform init \ - -backend-config="address=https://git.sch.bme.hu/api/v4/projects/${PROJ_ID}/terraform/state/${STATE_NAME}" \ - -backend-config="lock_address=https://git.sch.bme.hu/api/v4/projects/${PROJ_ID}/terraform/state/${STATE_NAME}/lock" \ - -backend-config="unlock_address=https://git.sch.bme.hu/api/v4/projects/${PROJ_ID}/terraform/state/${STATE_NAME}/lock" \ - -backend-config="username=${GITLAB_USER}" \ - -backend-config="password=${GITLAB_REPO_PAT}" \ - -backend-config="lock_method=POST" \ - -backend-config="unlock_method=DELETE" \ - -backend-config="retry_wait_min=5" \ - -reconfigure - -echo "Initilized. Don't forget to source the secret/login.sh file" \ No newline at end of file diff --git a/terraform/secret/login.sh.example b/terraform/secret/login.sh.example old mode 100644 new mode 100755 index 78d676e52007f207f863c7a99821cefcb6877a14..7442c2be686eae035610ddf6e039987873e13f1b --- a/terraform/secret/login.sh.example +++ b/terraform/secret/login.sh.example @@ -1,4 +1,14 @@ -export GITLAB_USER="gitsch user" -export GITLAB_REPO_PAT="gitsch repo private access token" -export TF_VAR_vsphere_user="" -export TF_VAR_vsphere_password="" +#!/bin/bash + +# Gitlab Project ID, this will be used as state store +export GITLAB_PROJECT_ID="0" +# If you want to rename the terraform state name, feel free. +export GITLAB_STATE_NAME="state" +# GitLab username +export GITLAB_USER="user" +# Access token with API Acces to the repo +export GITLAB_REPO_TOKEN="acces token" +# vSphere Username +export TF_VAR_vsphere_user="username" +# vSphere Password +export TF_VAR_vsphere_password="pw"