From 18357eb01233b75b601d73b527165b131ec0f5a0 Mon Sep 17 00:00:00 2001
From: Nathan Rodet <nrodet@grandlyon.com>
Date: Wed, 21 Dec 2022 13:46:45 +0100
Subject: [PATCH] Changes to cloud-init config

---
 create_lab.sh                 | 35 ---------------------------------
 delete_lab.sh                 |  3 ---
 terraform/cloud-init.yml      | 15 +++++++++++++-
 terraform/main.tf             | 37 ++---------------------------------
 terraform/variables-gitlab.tf |  6 ++++++
 5 files changed, 22 insertions(+), 74 deletions(-)
 delete mode 100755 create_lab.sh
 delete mode 100755 delete_lab.sh

diff --git a/create_lab.sh b/create_lab.sh
deleted file mode 100755
index 42d1817..0000000
--- a/create_lab.sh
+++ /dev/null
@@ -1,35 +0,0 @@
-#!/bin/bash
-
-echo "|------------------------|"
-echo "| INITIALISATION DU LABO |"
-echo "|------------------------|"
-
-if [ ! -f ".env" ]; then
-    echo "Créez un fichier .env de la forme suivante :"
-    echo "SCW_ACCESS_KEY="
-    echo "SCW_SECRET_KEY="
-    echo "TF_VAR_PROJECT_ID="
-    echo "TF_VAR_INSTANCES_COUNT=2"
-    exit 1
-fi
-
-export $(cat .env | xargs)
-# Move to Terraform directory
-cd ./terraform
-# Initialize a Terraform working directory
-terraform init
-# Generate and show the execution plan
-terraform plan
-# Build the infrastructure
-terraform apply
-# Echo Terraform outputs
-terraform output
-# Move to default directory
-cd ..
-
-echo "|--------------|"
-echo "| LABO DÉMARRÉ |"
-echo "|--------------|"
-
-echo "Se connecter sur les IPs, avec le port 80 pour webtop, et 8080 pour code-server."
-echo "Le mot de passe des instances est yfB4W23G."
diff --git a/delete_lab.sh b/delete_lab.sh
deleted file mode 100755
index 47aaecf..0000000
--- a/delete_lab.sh
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/bash
-export $(cat .env | xargs)
-terraform destroy
diff --git a/terraform/cloud-init.yml b/terraform/cloud-init.yml
index ab41958..edc4ce5 100644
--- a/terraform/cloud-init.yml
+++ b/terraform/cloud-init.yml
@@ -1,18 +1,31 @@
 #cloud-config
 
+# System update
 package_update: true
 package_upgrade: true
 
+# Install Packages
 packages:
   - curl
   - jq
   - git
 
 runcmd:
+# Check env variables access
+  - "echo $USER_PASSWORD > test.txt"
+# Install Rust
   - "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y"
   - "source \"$HOME/.cargo/env\""
+# Install Docker
+  - "curl -fsSL https://get.docker.com -o get-docker.sh | sh"
+# Install atrium
+  - "git clone https://github.com/nicolaspernoud/atrium.git"
+# Run Webtop Container
+  - "docker stop webtop"
+  - "docker rm webtop"
+  - "docker run -d --name=webtop --security-opt seccomp=unconfined -e PUID=0 -e PGID=0 -e TZ=Europe/Paris -e SUBFOLDER=/ -e KEYBOARD=fr-fr-azerty -e AUTO_LOGIN=false -p 80:3000 -v /root/webtop_data/:/config -v /var/run/docker.sock:/var/run/docker.sock --shm-size=8gb --cap-add=NET_ADMIN --privileged --restart unless-stopped --sysctl net.ipv6.conf.all.disable_ipv6=0 lscr.io/linuxserver/webtop:ubuntu-xfce"
+# Install Code Server
   - "curl -fsSL https://code-server.dev/install.sh | sh"
   - "mkdir -p  /root/.config/code-server/"
   - "echo 'bind-addr: 0.0.0.0:8080\nauth: password\npassword: yfB4W23G\ncert: false\n' > /root/.config/code-server/config.yaml"
   - "systemctl enable --now code-server@$USER"
-
diff --git a/terraform/main.tf b/terraform/main.tf
index 20540c5..30429af 100644
--- a/terraform/main.tf
+++ b/terraform/main.tf
@@ -83,6 +83,7 @@ resource "scaleway_instance_server" "user_instance_server" {
   security_group_id = scaleway_instance_security_group.www.id
 
   user_data = {
+    USER_PASSWORD = var.USER_PASSWORD
     cloud-init = file("cloud-init.yml")
   }
 
@@ -90,38 +91,4 @@ resource "scaleway_instance_server" "user_instance_server" {
     scaleway_account_ssh_key.ssh_key,
     scaleway_instance_security_group.www
   ]
-}
-
-#resource "null_resource" "provisioner" {
-#  count      = var.INSTANCES_COUNT
-#  depends_on = [
-#    scaleway_instance_server.user_instance_server
-#  ]
-#
-#  connection {
-#    host        = element(scaleway_instance_ip.public_ip.*.address, count.index)
-#    type        = "ssh"
-#    user        = "root"
-#    private_key = var.SCW_SSH_PRIVATE_KEY
-#    timeout     = "2m"
-#  }
-#
-#  // Install Rust
-#  provisioner "remote-exec" {
-#    inline = [
-#      "apt-get update -y'",
-#      "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y'",
-#      "source \"$HOME/.cargo/env\"",
-#    ]
-#  }
-#
-#  // Install Code-server
-#  provisioner "remote-exec" {
-#    inline = [
-#      "curl -fsSL https://code-server.dev/install.sh | sh",
-#      "mkdir -p  /root/.config/code-server/",
-#      "echo 'bind-addr: 0.0.0.0:8080\nauth: password\npassword: yfB4W23G\ncert: false\n' > /root/.config/code-server/config.yaml",
-#      "systemctl enable --now code-server@$USER"
-#    ]
-#  }
-#}
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/terraform/variables-gitlab.tf b/terraform/variables-gitlab.tf
index 5648602..5ef03b6 100644
--- a/terraform/variables-gitlab.tf
+++ b/terraform/variables-gitlab.tf
@@ -30,6 +30,12 @@ variable "SCW_SSH_PRIVATE_KEY" {
   description = "SSH private key from Gitlab agent for remote-exec"
 }
 
+variable "USER_PASSWORD" {
+  type        = string
+  sensitive   = true
+  description = "User password for accessing services"
+}
+
 variable "INSTANCES_COUNT" {
   type        = number
   description = "Number of instances to create"
-- 
GitLab