From cf0e1f240f4804c9138f7ab0b89ec9e5bd209d4b Mon Sep 17 00:00:00 2001
From: "tom.ryser" <tom.ryser@etu.hesge.ch>
Date: Mon, 3 Mar 2025 13:37:44 +0100
Subject: [PATCH] pull last version (not used)

---
 .../Terraform/conf/cloud-init.packages.yaml   | 13 ------
 Jenkins/Terraform/conf/cloud-init.users.yaml  | 30 ++++++++++++++
 Jenkins/Terraform/main.tf                     | 33 ++++++++-------
 Jenkins/Terraform/outputs.tf                  | 17 ++++++++
 Jenkins/Terraform/varables.tf                 | 41 +++++++++++++++----
 5 files changed, 95 insertions(+), 39 deletions(-)
 create mode 100644 Jenkins/Terraform/outputs.tf

diff --git a/Jenkins/Terraform/conf/cloud-init.packages.yaml b/Jenkins/Terraform/conf/cloud-init.packages.yaml
index 62c3a501..97b22c9d 100644
--- a/Jenkins/Terraform/conf/cloud-init.packages.yaml
+++ b/Jenkins/Terraform/conf/cloud-init.packages.yaml
@@ -13,19 +13,6 @@ system_info:
 
 # add any basic packages here:
 packages:
-  - curl
-  - nano
-  - ripgrep
-  - docker.io
   - bash-completion
 
 # SH commands to install jenkns
-runcmd:
-  - sudo yum update -y  # updates the package list and upgrades installed packages on the system
-  - sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo  #downloads the Jenkins repository configuration file and saves it to /etc/yum.repos.d/jenkins.repo
-  - sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key  #imports the GPG key for the Jenkins repository. This key is used to verify the authenticity of the Jenkins packages
-  - sudo yum upgrade -y #  upgrades packages again, which might be necessary to ensure that any new dependencies required by Jenkins are installed
-  - sudo dnf install java-11-amazon-corretto -y  # installs Amazon Corretto 11, which is a required dependency for Jenkins.
-  - sudo yum install jenkins -y  #installs Jenkins itself
-  - sudo systemctl enable jenkins  #enables the Jenkins service to start automatically at boot time
-  - sudo systemctl start jenkins   #starts the Jenkins service immediately
diff --git a/Jenkins/Terraform/conf/cloud-init.users.yaml b/Jenkins/Terraform/conf/cloud-init.users.yaml
index 201c6c0f..1e19ccea 100644
--- a/Jenkins/Terraform/conf/cloud-init.users.yaml
+++ b/Jenkins/Terraform/conf/cloud-init.users.yaml
@@ -13,3 +13,33 @@ system_info:
     sudo: ALL=(ALL) NOPASSWD:ALL
     ssh_authorized_keys:
       - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICd2om++e154/EKtD66CaRELfJ/lbzum44EqLbRKjjuQ terraform@TF-lab
+
+runcmd:
+  # Met à jour la liste des paquets
+  - sudo apt update
+
+  # Met à jour tous les paquets sans prompt
+  - sudo DEBIAN_FRONTEND=noninteractive apt upgrade -y
+
+  # Installe OpenSSH sans demander d’interaction
+  - sudo DEBIAN_FRONTEND=noninteractive apt install -y openssh-server
+
+  # Redémarre SSH pour s'assurer qu'il fonctionne
+  - sudo systemctl restart ssh
+
+  # Installe Java en premier (nécessaire pour Jenkins)
+  - sudo apt install -y openjdk-17-jre fontconfig
+
+  # Ajoute la clé et le dépôt Jenkins
+  - curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
+  - echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
+
+  # Met à jour la liste des paquets après ajout du repo Jenkins
+  - sudo apt update
+
+  # Installe Jenkins
+  - sudo apt install -y jenkins
+
+  # Active et démarre Jenkins
+  - sudo systemctl enable jenkins
+  - sudo systemctl start jenkins
\ No newline at end of file
diff --git a/Jenkins/Terraform/main.tf b/Jenkins/Terraform/main.tf
index 2a2903da..de79f449 100644
--- a/Jenkins/Terraform/main.tf
+++ b/Jenkins/Terraform/main.tf
@@ -23,34 +23,39 @@ provider "openstack" {
   cloud = "engines"
 }
 
+# SSH Key generation
 resource "tls_private_key" "my_generated_key" {
   algorithm = "RSA"
   rsa_bits  = 2048
 }
 
 resource "openstack_compute_keypair_v2" "my_keypair" {
-  name       = "my-keypair"
+  name       = "${var.key_name}-keypair"
   public_key = tls_private_key.my_generated_key.public_key_openssh
 }
 
 resource "local_file" "private_key" {
   content  = tls_private_key.my_generated_key.private_key_pem
-  filename = "${path.module}/private_key.pem"
+  filename = "${pathexpand(var.private_key_path)}/${var.key_name}-keypair.pem"
+
+  provisioner "local-exec" {
+    command = "chmod 600 ${self.filename}"
+  }
 }
 
 resource "openstack_compute_instance_v2" "app_server" {
-  name        = var.instance_name
+  name        = "${var.instance_name}-instance"
   image_id    = "654bf798-579b-47aa-a7f7-8a8798c9779d"
   flavor_name = "m1.medium"
   key_pair    = openstack_compute_keypair_v2.my_keypair.name
 
-  security_groups = ["default", "secgrp_1"]
+  security_groups = ["default", "${var.secgrp_name}-group"]
   user_data = data.cloudinit_config.my_config.rendered
 }
 
 # Network configuration
 resource "openstack_networking_secgroup_v2" "secgrp_1" {
-  name = "secgrp_1"
+  name = "${var.secgrp_name}-group"
 }
 
 resource "openstack_networking_secgroup_rule_v2" "secgroup_rule_1" {
@@ -93,7 +98,7 @@ resource "openstack_networking_secgroup_rule_v2" "secgroup_rule_4" {
 
 resource "openstack_networking_floatingip_v2" "fip_1" {
   pool        = "public"
-  description = "TF-Lab"
+  description = var.project_name
 }
 
 data "openstack_networking_port_v2" "port" {
@@ -106,6 +111,7 @@ resource "openstack_networking_floatingip_associate_v2" "fip_associate" {
   port_id     = data.openstack_networking_port_v2.port.id
 }
 
+# Cloud-init configuration
 data "cloudinit_config" "my_config" {
   gzip          = false
   base64_encode = false
@@ -116,16 +122,9 @@ data "cloudinit_config" "my_config" {
     content      = file("conf/cloud-init.users.yaml")
     merge_type   = "list(append)+dict(no_replace,recurse_list)"
   }
-
-  part {
-    filename     = "file-2"
-    content_type = "text/cloud-config"
-    content      = file("conf/cloud-init.packages.yaml")
-    merge_type   = "list(append)+dict(no_replace,recurse_list)"
-  }
 }
 
-# Configuration du provider AWS pour OpenStack Swift (S3-compatible)
+# AWS S3-compatible provider for OpenStack Swift
 provider "aws" {
   region                      = "us-east-1"
   skip_credentials_validation = true
@@ -138,9 +137,9 @@ provider "aws" {
   }
 }
 
-# Création d'un conteneur (équivalent à un bucket S3)
+# Bucket and object creation in OpenStack Swift
 resource "aws_s3_bucket" "bucket" {
-  bucket = var.container_name
+  bucket = "${var.bucket_name}-bucket"
 }
 
 resource "aws_s3_bucket_policy" "public_policy" {
@@ -161,4 +160,4 @@ resource "aws_s3_bucket_policy" "public_policy" {
       }
     ]
   })
-}
\ No newline at end of file
+}
diff --git a/Jenkins/Terraform/outputs.tf b/Jenkins/Terraform/outputs.tf
new file mode 100644
index 00000000..9d1ef984
--- /dev/null
+++ b/Jenkins/Terraform/outputs.tf
@@ -0,0 +1,17 @@
+# outputs.tf
+output "instance_id" {
+  description = "ID of the instance"
+  value       = openstack_compute_instance_v2.app_server.id
+}
+
+output "instance_public_ip" {
+  description = "Public IP address of instance"
+  value       = openstack_networking_floatingip_v2.fip_1.address
+}
+
+
+output "private_key" {
+  description = "Private key for SSH access"
+  value       = tls_private_key.my_generated_key.private_key_pem
+  sensitive   = true
+}
\ No newline at end of file
diff --git a/Jenkins/Terraform/varables.tf b/Jenkins/Terraform/varables.tf
index 3100dfec..6ad8ecad 100644
--- a/Jenkins/Terraform/varables.tf
+++ b/Jenkins/Terraform/varables.tf
@@ -1,22 +1,45 @@
+variable "os_access_key" {
+  description = "OpenStack Swift Access Key"
+  type        = string
+}
+
+variable "os_secret_key" {
+  description = "OpenStack Swift Secret Key"
+  type        = string
+}
+
+variable "project_name" {
+  description = "Name of the project"
+  type        = string
+  default     = "jenkins-qcm"
+}
+
 variable "instance_name" {
-  description = "Value of the instance's name tag"
+  description = "Name of the instance"
+  type        = string
+  default     = "jenkins-qcm"
+}
+
+variable "secgrp_name" {
+  description = "Name of the security group"
   type        = string
-  default     = "Jenkins"
+  default     = "jenkins-qcm"
 }
 
-variable "container_name" {
+variable "bucket_name" {
   description = "Name of the OpenStack Swift container"
   type        = string
-  default     = "Jenkins-Server"
+  default     = "jenkins-qcm"
 }
 
-# Variables nécessaires
-variable "os_access_key" {
-  description = "OpenStack Swift Access Key"
+variable "key_name" {
+  description = "Name of the SSH key"
   type        = string
+  default     = "jenkins-qcm"
 }
 
-variable "os_secret_key" {
-  description = "OpenStack Swift Secret Key"
+variable "private_key_path" {
+  description = "Destination for SSH key"
   type        = string
+  default     = "~/.ssh/"
 }
-- 
GitLab