From 07b20288bce93b3b7407a94e2642880404076ce3 Mon Sep 17 00:00:00 2001
From: Nathan Rodet <nrodet@grandlyon.com>
Date: Fri, 13 Jan 2023 12:01:15 +0100
Subject: [PATCH] Working scripts configuration

---
 .gitlab-ci.yml                            |  1 +
 README.md                                 | 25 +++++++++++++++++++++--
 instance-scripts/config-files/atrium.yaml |  2 +-
 instance-scripts/scripts/atrium.sh        |  1 +
 terraform/main.tf                         |  1 +
 terraform/variables-gitlab.tf             |  6 ++++++
 6 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 6eac312..9f5fffb 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -15,6 +15,7 @@ before_script:
   - export TF_VAR_INSTANCES_COUNT=${TF_VAR_INSTANCES_COUNT}
   - export TF_VAR_USER_PASSWORD=${TF_VAR_USER_PASSWORD}
   - export TF_VAR_LETSENCRYPT_EMAIL=${TF_VAR_LETSENCRYPT_EMAIL}
+  - export TF_VAR_PROJECT_DOMAIN=${TF_VAR_PROJECT_DOMAIN}
   - export TF_VAR_ENVIRONMENT=${ENVIRONMENT}
 
 stages:
diff --git a/README.md b/README.md
index 823bd90..1ded645 100644
--- a/README.md
+++ b/README.md
@@ -38,12 +38,14 @@ After deployment, each resource can be accessed though HTTPS depending on their
 - Username: abc
 - Password: <Gitlab Stored>
 
-## Cloud-init - file creation
+## How to setup configuration file for services
+
+### cloud-init : file creation
 
 In this example, you will find how to create a file to a defined path with cloud-init.
 Content inside `%` are meant to be replaced with sed command to be able to use environment variables or user-data.
 
-**cloud-init :**
+**example:**
 ```yaml
 write_files:
   - content: |
@@ -76,6 +78,25 @@ sed -i "s/%atrium_letsencrypt_email%/$(scw-userdata atrium_letsencrypt_email)/g"
 sed -i "s/%atrium_count_index%/$(scw-userdata atrium_count_index)/g" /root/atrium.yaml
 ```
 
+## Downloading file
+
+The example show how to curl a configuration file from repository to a defined path with cloud-init.
+
+**example :**
+```yaml
+runcmd:
+#
+# cloud-init execute cmd before HOME and USER are set.
+#
+  - "export HOME=/root"
+  - "export USER=root"
+#
+# Install Atrium
+#
+  - "curl https://forge.grandlyon.com/systemes-dinformation/alpha/projects/labo-rust/-/raw/develop/instance-scripts/config-files/atrium.yaml -o root/atrium.yaml"
+  - "curl https://forge.grandlyon.com/systemes-dinformation/alpha/projects/labo-rust/-/raw/develop/instance-scripts/config-files/atrium.sh | bash"
+```
+
 ## Use Gitlab variables in instances
 
 ### Example
diff --git a/instance-scripts/config-files/atrium.yaml b/instance-scripts/config-files/atrium.yaml
index 56c601f..4aff5a3 100644
--- a/instance-scripts/config-files/atrium.yaml
+++ b/instance-scripts/config-files/atrium.yaml
@@ -1,5 +1,5 @@
 # The content of this file is recreated by cloud init.
-hostname: %atrium_hostname%.daag.alpha.grandlyon.com
+hostname: %atrium_hostname%.%project_domain%
 debug_mode: false
 letsencrypt_email: %atrium_letsencrypt_email%
 tls_mode: Auto
diff --git a/instance-scripts/scripts/atrium.sh b/instance-scripts/scripts/atrium.sh
index a99ed7e..53ccdcc 100644
--- a/instance-scripts/scripts/atrium.sh
+++ b/instance-scripts/scripts/atrium.sh
@@ -6,6 +6,7 @@
 
 mkdir -p /root/atrium/data && mkdir -p /root/atrium/letsencrypt_cache
 sed -i "s/%atrium_hostname%/$(scw-userdata atrium_hostname)/g" /root/atrium.yaml
+sed -i "s/%project_domain%/$(scw-userdata project_domain)/g" /root/atrium.yaml
 sed -i "s/%atrium_letsencrypt_email%/$(scw-userdata atrium_letsencrypt_email)/g" /root/atrium.yaml
 sed -i "s/%atrium_count_index%/$(scw-userdata atrium_count_index)/g" /root/atrium.yaml
 mv /root/atrium.yaml /root/atrium/atrium.yaml
diff --git a/terraform/main.tf b/terraform/main.tf
index 70d8fdf..ed0bee7 100644
--- a/terraform/main.tf
+++ b/terraform/main.tf
@@ -65,6 +65,7 @@ resource "scaleway_instance_server" "user_instance_server" {
   security_group_id = scaleway_instance_security_group.www.id
 
   user_data = {
+    project_domain = var.PROJECT_DOMAIN
     atrium_count_index = count.index
     atrium_hostname = scaleway_domain_record.subdomain_record[count.index].name
     atrium_letsencrypt_email = var.LETSENCRYPT_EMAIL
diff --git a/terraform/variables-gitlab.tf b/terraform/variables-gitlab.tf
index f324f23..b8ac5e8 100644
--- a/terraform/variables-gitlab.tf
+++ b/terraform/variables-gitlab.tf
@@ -18,6 +18,12 @@ variable "SCW_SECRET_KEY" {
   description = "Scaleway secret key"
 }
 
+variable "PROJECT_DOMAIN" {
+  type = string
+  sensitive = true
+  description = "External domain from Scaleway"
+}
+
 variable "LETSENCRYPT_EMAIL" {
   type        = string
   sensitive   = true
-- 
GitLab