From 08c0a0cd544791fcc706566278b2c8f79170db29 Mon Sep 17 00:00:00 2001
From: Nathan Rodet <nrodet@grandlyon.com>
Date: Wed, 21 Dec 2022 10:24:40 +0100
Subject: [PATCH] Replace Remote Exec to cloud-init files

---
 instance_scripts/cloud-init.yml |  9 +++++
 terraform/main.tf               | 71 ++++++++++++++++++---------------
 2 files changed, 47 insertions(+), 33 deletions(-)
 create mode 100644 instance_scripts/cloud-init.yml

diff --git a/instance_scripts/cloud-init.yml b/instance_scripts/cloud-init.yml
new file mode 100644
index 0000000..ed69de7
--- /dev/null
+++ b/instance_scripts/cloud-init.yml
@@ -0,0 +1,9 @@
+#cloud-config
+
+package_update: true
+package_upgrade: true
+
+runcmd:
+  - "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y"
+  - "source \"$HOME/.cargo/env\""
+
diff --git a/terraform/main.tf b/terraform/main.tf
index 2656146..0abafa2 100644
--- a/terraform/main.tf
+++ b/terraform/main.tf
@@ -82,42 +82,47 @@ resource "scaleway_instance_server" "user_instance_server" {
   ip_id             = scaleway_instance_ip.public_ip[count.index].id
   security_group_id = scaleway_instance_security_group.www.id
 
+  user_data = {
+    foo        = "bar"
+    cloud-init = file("../instance_scripts/cloud-init.yml")
+  }
+
   depends_on = [
     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
+#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
-- 
GitLab