From 58a0fa2541fe23b848f23bc8d185824f60b9b2d3 Mon Sep 17 00:00:00 2001
From: Rafael Laszlo <rlacko99@gmail.com>
Date: Fri, 19 Nov 2021 16:00:57 +0100
Subject: [PATCH] Update terraform template and container password

---
 terraform/bootstrap.sh            | 27 +++++++++++++++++++++++++++
 terraform/{main.tf => config.tf}  |  4 ++++
 terraform/init.sh                 | 17 -----------------
 terraform/router.tf               |  8 ++------
 terraform/runner.tf               |  4 ++--
 terraform/secret/login.sh.example | 18 ++++++++++++++----
 6 files changed, 49 insertions(+), 29 deletions(-)
 create mode 100755 terraform/bootstrap.sh
 rename terraform/{main.tf => config.tf} (89%)
 delete mode 100755 terraform/init.sh

diff --git a/terraform/bootstrap.sh b/terraform/bootstrap.sh
new file mode 100755
index 0000000..5bcf128
--- /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/main.tf b/terraform/config.tf
similarity index 89%
rename from terraform/main.tf
rename to terraform/config.tf
index bca7578..943f65f 100644
--- a/terraform/main.tf
+++ b/terraform/config.tf
@@ -19,3 +19,7 @@ locals {
   ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHXuXr/Mz1ENkvZ+Ntc9dF1r8VK62XaZtsMaV7N+CMZ8 rlacko
   EOF
 }
+
+variable "ct_passwords" {
+  type = string
+}
diff --git a/terraform/init.sh b/terraform/init.sh
deleted file mode 100755
index 3e00303..0000000
--- a/terraform/init.sh
+++ /dev/null
@@ -1,17 +0,0 @@
-#!/bin/bash
-
-source secret/login.sh
-
-PROJ_ID="3419"
-STATE_NAME="maze"
-
-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
diff --git a/terraform/router.tf b/terraform/router.tf
index b6e5bd6..f070bb1 100644
--- a/terraform/router.tf
+++ b/terraform/router.tf
@@ -1,12 +1,8 @@
-variable "router_password" {
-  type = string
-}
-
 resource "proxmox_lxc" "router" {
   target_node  = "maze"
   hostname     = "router"
   ostemplate   = "local:vztmpl/ubuntu-20.04-standard_20.04-1_amd64.tar.gz"
-  password     = var.router_password
+  password     = var.ct_passwords
   vmid         = 9254
   start        = true
   memory       = 4096
@@ -18,7 +14,7 @@ resource "proxmox_lxc" "router" {
   ssh_public_keys = local.ssh_keys
 
   rootfs {
-    storage = "ssd"
+    storage = "z-one"
     size    = "8G"
   }
 
diff --git a/terraform/runner.tf b/terraform/runner.tf
index 5283d09..9047763 100644
--- a/terraform/runner.tf
+++ b/terraform/runner.tf
@@ -17,9 +17,9 @@ resource "proxmox_vm_qemu" "gitlab-runner" {
   os_type = "cloud-init"
 
   disk {
-    size    = "35G"
+    size    = "40G"
     type    = "virtio"
-    storage = "ssd"
+    storage = "z-one"
   }
 
   network {
diff --git a/terraform/secret/login.sh.example b/terraform/secret/login.sh.example
index e590c82..7442c2b 100644
--- a/terraform/secret/login.sh.example
+++ b/terraform/secret/login.sh.example
@@ -1,4 +1,14 @@
-export GITLAB_USER=""
-export GITLAB_REPO_PAT=""
-export PM_USER=""
-export PM_PASS=""
\ No newline at end of file
+#!/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"
-- 
GitLab