From b64a6db779674d738b7aa9388d11e5e7afe409ce Mon Sep 17 00:00:00 2001
From: integration_test_api <project7619_bot1@noreply.git.code.tecnalia.com>
Date: Fri, 30 Dec 2022 14:12:07 +0100
Subject: [PATCH] Create testfile

---
 config.yaml                                   |   6 +
 nginx/config.yaml                             |   8 +
 nginx/inventory.j2                            |   9 ++
 nginx/main.yml                                |  44 +++++
 nginx/ssh_key.j2                              |   1 +
 piacere_monitoring/ansible.cfg                |   7 +
 piacere_monitoring/ansible_requirements.yml   |   8 +
 piacere_monitoring/config.yaml                |   8 +
 piacere_monitoring/hosts.yaml                 |   4 +
 .../install_playbook_requirements.sh          |  33 ++++
 piacere_monitoring/inventory.j2               |   9 ++
 piacere_monitoring/main.yml                   |  22 +++
 piacere_monitoring/run-playbook.sh            |  33 ++++
 piacere_monitoring/site.yaml                  |  30 ++++
 piacere_monitoring/site_requirements.yaml     |   9 ++
 piacere_monitoring/ssh_key.j2                 |   1 +
 piacere_monitoring/vars/main.yaml             |  27 ++++
 terraform/config.yaml                         |  16 ++
 terraform/main.tf                             | 151 ++++++++++++++++++
 terraform/output.tf                           |  14 ++
 20 files changed, 440 insertions(+)
 create mode 100644 config.yaml
 create mode 100644 nginx/config.yaml
 create mode 100644 nginx/inventory.j2
 create mode 100644 nginx/main.yml
 create mode 100644 nginx/ssh_key.j2
 create mode 100644 piacere_monitoring/ansible.cfg
 create mode 100644 piacere_monitoring/ansible_requirements.yml
 create mode 100644 piacere_monitoring/config.yaml
 create mode 100644 piacere_monitoring/hosts.yaml
 create mode 100644 piacere_monitoring/install_playbook_requirements.sh
 create mode 100644 piacere_monitoring/inventory.j2
 create mode 100644 piacere_monitoring/main.yml
 create mode 100644 piacere_monitoring/run-playbook.sh
 create mode 100644 piacere_monitoring/site.yaml
 create mode 100644 piacere_monitoring/site_requirements.yaml
 create mode 100644 piacere_monitoring/ssh_key.j2
 create mode 100644 piacere_monitoring/vars/main.yaml
 create mode 100644 terraform/config.yaml
 create mode 100644 terraform/main.tf
 create mode 100644 terraform/output.tf

diff --git a/config.yaml b/config.yaml
new file mode 100644
index 0000000..082e5e5
--- /dev/null
+++ b/config.yaml
@@ -0,0 +1,6 @@
+---
+iac:
+- terraform
+- piacere_monitoring
+- nginx
+...
\ No newline at end of file
diff --git a/nginx/config.yaml b/nginx/config.yaml
new file mode 100644
index 0000000..8b7284e
--- /dev/null
+++ b/nginx/config.yaml
@@ -0,0 +1,8 @@
+
+---
+input:
+  - instance_ip_vm1
+  - instance_server_private_key_user1
+output: []
+engine: ansible
+...
diff --git a/nginx/inventory.j2 b/nginx/inventory.j2
new file mode 100644
index 0000000..c869825
--- /dev/null
+++ b/nginx/inventory.j2
@@ -0,0 +1,9 @@
+
+
+[servers_for_nginx]
+{{ instance_ip_vm1 }}
+
+[servers_for_nginx:vars]
+ansible_connection=ssh
+ansible_user=ubuntu
+ansible_ssh_private_key_file=ssh_key
diff --git a/nginx/main.yml b/nginx/main.yml
new file mode 100644
index 0000000..7cd932f
--- /dev/null
+++ b/nginx/main.yml
@@ -0,0 +1,44 @@
+
+
+---
+- hosts: servers_for_nginx
+  gather_facts: no
+  become: yes
+  tasks:
+    - name: Update repositories
+      apt:
+        update_cache: yes
+
+    - name: Install nginx
+      package:
+        name: nginx
+
+    - name: Start nginx
+      service:
+        name: nginx
+        enabled: yes
+        state: started
+
+    - name: Set attributes
+      set_stats:
+        data:
+          site_config_dir: /etc/nginx/conf.d
+
+    - name: Install sample site
+      copy:
+        dest: "{{ item }}"
+        content: |
+          <!doctype html>
+          <html lang="en">
+          <head>
+            <title>Hello World!</title>
+          </head>
+          <body>
+            <h1>Sample web page</h1>
+            <p>With little content ;)</p>
+          </body>
+          </html>
+      with_items:
+        - /var/www/html/index.html
+        - /usr/share/nginx/html/index.html
+
diff --git a/nginx/ssh_key.j2 b/nginx/ssh_key.j2
new file mode 100644
index 0000000..1431f67
--- /dev/null
+++ b/nginx/ssh_key.j2
@@ -0,0 +1 @@
+{{ instance_server_private_key_user1 }}
diff --git a/piacere_monitoring/ansible.cfg b/piacere_monitoring/ansible.cfg
new file mode 100644
index 0000000..660a5eb
--- /dev/null
+++ b/piacere_monitoring/ansible.cfg
@@ -0,0 +1,7 @@
+# https://docs.ansible.com/ansible/latest/reference_appendices/config.html
+[defaults]
+host_key_checking = False
+inventory = {{CWD}}/hosts.yaml  ; This points to the file that lists your hosts
+remote_user = esilab
+deprecation_warnings=False ; to remove the python version depretation warning
+display_skipped_hosts = no 
\ No newline at end of file
diff --git a/piacere_monitoring/ansible_requirements.yml b/piacere_monitoring/ansible_requirements.yml
new file mode 100644
index 0000000..58c0cb3
--- /dev/null
+++ b/piacere_monitoring/ansible_requirements.yml
@@ -0,0 +1,8 @@
+roles:
+#  - name: dj-wasabi.telegraf
+#    version: 0.13.2
+#    source: https://galaxy.ansible.com
+  - name: dj-wasabi.telegraf
+    src: https://github.com/dj-wasabi/ansible-telegraf.git
+    scm: git
+    version: 0.13.2
diff --git a/piacere_monitoring/config.yaml b/piacere_monitoring/config.yaml
new file mode 100644
index 0000000..8b7284e
--- /dev/null
+++ b/piacere_monitoring/config.yaml
@@ -0,0 +1,8 @@
+
+---
+input:
+  - instance_ip_vm1
+  - instance_server_private_key_user1
+output: []
+engine: ansible
+...
diff --git a/piacere_monitoring/hosts.yaml b/piacere_monitoring/hosts.yaml
new file mode 100644
index 0000000..b9cbfc6
--- /dev/null
+++ b/piacere_monitoring/hosts.yaml
@@ -0,0 +1,4 @@
+all:
+  hosts:
+    localhost:
+      ansible_connection: local
diff --git a/piacere_monitoring/install_playbook_requirements.sh b/piacere_monitoring/install_playbook_requirements.sh
new file mode 100644
index 0000000..843bf3b
--- /dev/null
+++ b/piacere_monitoring/install_playbook_requirements.sh
@@ -0,0 +1,33 @@
+#!/bin/bash
+set -e
+
+SCRIPT_DIR=$(dirname "$0")
+
+# to avoid the being run in a world writable directory we explicitly assign the ANSIBLE_CONFIG variable 
+if [[ -f ./ansible.cfg ]]
+then
+    export ANSIBLE_CONFIG=./ansible.cfg
+else 
+    if [[ -f $SCRIPT_DIR/ansible.cfg ]]
+    then
+        export ANSIBLE_CONFIG=$SCRIPT_DIR/ansible.cfg
+    fi
+fi
+
+if [[ -z "$ANSIBLE_CONFIG" ]]
+then 
+    echo ANSIBLE_CONFIG to assigned using default https://docs.ansible.com/ansible/latest/reference_appendices/config.html
+else 
+    echo ANSIBLE_CONFIG=$ANSIBLE_CONFIG
+fi
+
+if [[ -z "$1" ]]
+then 
+    # echo without params 
+    echo ansible-playbook $SCRIPT_DIR/site_requirements.yaml
+    ansible-playbook $SCRIPT_DIR/site_requirements.yaml
+else 
+    # echo with params
+    echo ansible-playbook $SCRIPT_DIR/site_requirements.yaml --extra-vars "$1"
+    ansible-playbook $SCRIPT_DIR/site_requirements.yaml --extra-vars "$1"
+fi
diff --git a/piacere_monitoring/inventory.j2 b/piacere_monitoring/inventory.j2
new file mode 100644
index 0000000..fda17b3
--- /dev/null
+++ b/piacere_monitoring/inventory.j2
@@ -0,0 +1,9 @@
+
+
+[servers_for_piacere_monitoring]
+{{ instance_ip_vm1 }}
+
+[servers_for_piacere_monitoring:vars]
+ansible_connection=ssh
+ansible_user=ubuntu
+ansible_ssh_private_key_file=ssh_key
diff --git a/piacere_monitoring/main.yml b/piacere_monitoring/main.yml
new file mode 100644
index 0000000..a0edc4f
--- /dev/null
+++ b/piacere_monitoring/main.yml
@@ -0,0 +1,22 @@
+---
+- hosts: localhost
+  tasks:
+    - name: print disclamer
+      debug:
+        msg: this can also be done with "ansible-galaxy install -r requirements"
+    - name: install telegraf from galaxy
+      community.general.ansible_galaxy_install:
+        type: role
+        requirements_file: ansible_requirements.yml
+
+- hosts: all
+  pre_tasks:
+    - name: Ensure gnupg package
+      package:
+        name: gnupg
+        state: present
+      become: true
+  vars_files:
+    - vars/main.yaml
+  roles:
+    - dj-wasabi.telegraf
diff --git a/piacere_monitoring/run-playbook.sh b/piacere_monitoring/run-playbook.sh
new file mode 100644
index 0000000..f2bba22
--- /dev/null
+++ b/piacere_monitoring/run-playbook.sh
@@ -0,0 +1,33 @@
+#!/bin/bash
+set -e
+
+SCRIPT_DIR=$(dirname "$0")
+
+# to avoid the being run in a world writable directory we explicitly assign the ANSIBLE_CONFIG variable 
+if [[ -f ./ansible.cfg ]]
+then
+    export ANSIBLE_CONFIG=./ansible.cfg
+else 
+    if [[ -f $SCRIPT_DIR/ansible.cfg ]]
+    then
+        export ANSIBLE_CONFIG=$SCRIPT_DIR/ansible.cfg
+    fi
+fi
+
+if [[ -z "$ANSIBLE_CONFIG" ]]
+then 
+    echo ANSIBLE_CONFIG to assigned using default https://docs.ansible.com/ansible/latest/reference_appendices/config.html
+else 
+    echo ANSIBLE_CONFIG=$ANSIBLE_CONFIG
+fi
+
+if [[ -z "$1" ]]
+then 
+    # echo without params 
+    echo ansible-playbook $SCRIPT_DIR/site.yaml
+    ansible-playbook $SCRIPT_DIR/site.yaml
+else 
+    # echo with params
+    echo ansible-playbook $SCRIPT_DIR/site.yaml --extra-vars "$1"
+    ansible-playbook $SCRIPT_DIR/site.yaml --extra-vars "$1"
+fi
diff --git a/piacere_monitoring/site.yaml b/piacere_monitoring/site.yaml
new file mode 100644
index 0000000..531dbf5
--- /dev/null
+++ b/piacere_monitoring/site.yaml
@@ -0,0 +1,30 @@
+- hosts: all
+  pre_tasks:
+    - name: Check parameters
+      fail:
+        msg: 'variable {{item}} not defined'
+      when: item is not defined
+      with_items:
+        - pma_deployment_id
+        - pma_influxdb_bucket
+        - pma_influxdb_token
+        - pma_influxdb_org
+        - pma_influxdb_addr
+    - name: Print parameters
+      debug:
+        msg: 
+          - "pma_deployment_id: {{ pma_deployment_id }}"
+          - "pma_influxdb_bucket: {{ pma_influxdb_bucket }}"
+          - "pma_influxdb_token: {{ pma_influxdb_token }}"
+          - "pma_influxdb_org: {{ pma_influxdb_org }}"
+          - "pma_influxdb_addr: {{ pma_influxdb_addr }}"
+    - name: Ensure gnupg package
+      package:
+        name: gnupg
+        state: present
+      become: true
+      
+  vars_files:
+    - vars/main.yaml
+  roles:
+    - dj-wasabi.telegraf
diff --git a/piacere_monitoring/site_requirements.yaml b/piacere_monitoring/site_requirements.yaml
new file mode 100644
index 0000000..3e7665d
--- /dev/null
+++ b/piacere_monitoring/site_requirements.yaml
@@ -0,0 +1,9 @@
+- hosts: localhost
+  tasks:
+    - name: print disclamer
+      debug:
+        msg: this can also be done with "ansible-galaxy install -r requirements"
+    - name: install telegraf from galaxy
+      community.general.ansible_galaxy_install:
+        type: role
+        requirements_file: ansible_requirements.yml
diff --git a/piacere_monitoring/ssh_key.j2 b/piacere_monitoring/ssh_key.j2
new file mode 100644
index 0000000..1431f67
--- /dev/null
+++ b/piacere_monitoring/ssh_key.j2
@@ -0,0 +1 @@
+{{ instance_server_private_key_user1 }}
diff --git a/piacere_monitoring/vars/main.yaml b/piacere_monitoring/vars/main.yaml
new file mode 100644
index 0000000..861faf3
--- /dev/null
+++ b/piacere_monitoring/vars/main.yaml
@@ -0,0 +1,27 @@
+pma_deployment_id: "123e4567-e89b-12d3-a456-426614174002"
+pma_influxdb_bucket: "bucket"
+pma_influxdb_token: "piacerePassword"
+pma_influxdb_org: "piacere"
+pma_influxdb_addr: "https://influxdb.pm.ci.piacere.digital.tecnalia.dev"
+
+telegraf_agent_package_state: latest
+
+telegraf_agent_output:
+  - type: influxdb_v2
+    config:
+      - urls = ["{{ pma_influxdb_addr }}"]
+      - token = "{{ pma_influxdb_token }}"
+      - organization = "{{ pma_influxdb_org }}"
+      - bucket = "{{ pma_influxdb_bucket }}"
+      - insecure_skip_verify = true
+
+telegraf_global_tags:
+  - tag_name: deployment_id
+    tag_value: "{{ pma_deployment_id }}"
+
+telegraf_plugins_default:
+  - plugin: cpu
+  - plugin: mem
+  - plugin: processes
+  - plugin: disk
+  - plugin: net
\ No newline at end of file
diff --git a/terraform/config.yaml b/terraform/config.yaml
new file mode 100644
index 0000000..783f272
--- /dev/null
+++ b/terraform/config.yaml
@@ -0,0 +1,16 @@
+
+
+---
+engine: terraform
+input:
+  - OS_USERNAME
+  - OS_PASSWORD
+  - OS_AUTH_URL
+  - OS_PROJECT_NAME
+output:
+
+  - instance_server_public_key_user1
+  - instance_server_private_key_user1
+  - instance_ip_vm1
+
+...
diff --git a/terraform/main.tf b/terraform/main.tf
new file mode 100644
index 0000000..5129742
--- /dev/null
+++ b/terraform/main.tf
@@ -0,0 +1,151 @@
+
+
+terraform {
+required_version = ">= 0.14.0"
+  required_providers {
+    openstack = {
+      source  = "terraform-provider-openstack/openstack"
+      version = "~> 1.35.0"
+    }
+  }
+}
+
+# Configure the OpenStack Provider
+provider "openstack" {
+  insecure    = true
+}
+
+# Retrieve data
+data "openstack_networking_network_v2" "external" {
+  name = "external"
+}
+
+
+# Create virtual machine
+resource "openstack_compute_instance_v2" "vm1" {
+  name        = "nginx_host"
+  image_name  = "Ubuntu-Focal-20.04-Daily-2022-04-19"
+  flavor_name = "small"
+  key_pair    = openstack_compute_keypair_v2.user1.name
+  network { 
+    port = openstack_networking_port_v2.net1_subnet_networking_port.id
+    
+  }
+}
+
+# Create floating ip
+resource "openstack_networking_floatingip_v2" "vm1_floating_ip" {
+  pool = "external"
+  # fixed_ip = ""
+}
+
+# Attach floating ip to instance
+resource "openstack_compute_floatingip_associate_v2" "vm1_floating_ip_association" {
+  floating_ip = openstack_networking_floatingip_v2.vm1_floating_ip.address
+  instance_id = openstack_compute_instance_v2.vm1.id
+}
+
+# Router interface configuration
+
+resource "openstack_networking_router_interface_v2" "net1_subnet_router_interface" {
+  router_id = openstack_networking_router_v2.router.id
+  subnet_id = openstack_networking_subnet_v2.net1_subnet_subnet.id
+}
+
+
+## Network
+
+# Create Network
+resource "openstack_networking_network_v2" "ostack2" {
+  name = "concrete_net"
+}
+
+# Subnet
+resource "openstack_networking_subnet_v2" "net1_subnet_subnet" {
+  name            = "net1_subnet_subnet"
+  network_id      = openstack_networking_network_v2.ostack2.id
+  cidr            = "16.0.0.0/24"
+  dns_nameservers = ["8.8.8.8", "8.8.8.4"]
+}
+# Attach networking port
+resource "openstack_networking_port_v2" "net1_subnet_networking_port" {
+  name           = "concrete_net"
+  network_id     = openstack_networking_network_v2.ostack2.id
+  admin_state_up = true
+  security_group_ids = [
+  openstack_compute_secgroup_v2.icmp.id,
+  openstack_compute_secgroup_v2.http.id,
+  openstack_compute_secgroup_v2.https.id,
+  openstack_compute_secgroup_v2.ssh.id,
+  
+  ]
+  fixed_ip {
+   subnet_id = openstack_networking_subnet_v2.net1_subnet_subnet.id
+  }
+}
+
+
+
+# Create router
+resource "openstack_networking_router_v2" "router" { ## 1router, not parametric
+  name                = "router"
+  external_network_id = data.openstack_networking_network_v2.external.id    #External network id
+}
+
+
+
+# Create ssh keys
+resource "openstack_compute_keypair_v2" "user1" {
+  name       = "user-test"
+  public_key = ""
+}
+
+
+
+# CREATING SECURITY_GROUP
+  
+resource "openstack_compute_secgroup_v2" "icmp" {
+  name        = "icmp"
+  description  = "Security group rule for port -1"
+  rule {
+    from_port   = -1
+    to_port     = -1
+    ip_protocol = "icmp"
+    cidr        = "0.0.0.0/0"
+  }
+}
+ 
+resource "openstack_compute_secgroup_v2" "http" {
+  name        = "http"
+  description  = "Security group rule for port 80"
+  rule {
+    from_port   = 80
+    to_port     = 80
+    ip_protocol = "tcp"
+    cidr        = "0.0.0.0/0"
+  }
+}
+ 
+resource "openstack_compute_secgroup_v2" "https" {
+  name        = "https"
+  description  = "Security group rule for port 443"
+  rule {
+    from_port   = 443
+    to_port     = 443
+    ip_protocol = "tcp"
+    cidr        = "0.0.0.0/0"
+  }
+}
+ 
+resource "openstack_compute_secgroup_v2" "ssh" {
+  name        = "ssh"
+  description  = "Security group rule for port 22"
+  rule {
+    from_port   = 22
+    to_port     = 22
+    ip_protocol = "tcp"
+    cidr        = "0.0.0.0/0"
+  }
+}
+
+
diff --git a/terraform/output.tf b/terraform/output.tf
new file mode 100644
index 0000000..f0cc70b
--- /dev/null
+++ b/terraform/output.tf
@@ -0,0 +1,14 @@
+
+
+output "instance_server_public_key_user1" {
+  value = openstack_compute_keypair_v2.user1.public_key
+}
+
+output "instance_server_private_key_user1" {
+  value = openstack_compute_keypair_v2.user1.private_key
+}
+
+output "instance_ip_vm1" {
+  value = openstack_compute_floatingip_associate_v2.vm1_floating_ip_association.floating_ip
+}
+
-- 
GitLab