diff --git a/ansible/config.yaml b/ansible/config.yaml new file mode 100644 index 0000000000000000000000000000000000000000..fff1792be092b69af3f0ca25b644e39c718d4f4f --- /dev/null +++ b/ansible/config.yaml @@ -0,0 +1,7 @@ +--- +input: + - instance_ip + - instance_server_public_key +output: [] +engine: ansible +... diff --git a/ansible/inventory.j2 b/ansible/inventory.j2 new file mode 100644 index 0000000000000000000000000000000000000000..fb27937d59c516ee15e53689491ad2e3bc68195e --- /dev/null +++ b/ansible/inventory.j2 @@ -0,0 +1,7 @@ +[vms] +{{ instance_ip }} + +[vms:vars] +ansible_connection=ssh +ansible_user=ubuntu #vm user variable potentialy +ansible_ssh_private_key_file=ssh_key \ No newline at end of file diff --git a/ansible/main.yml b/ansible/main.yml new file mode 100644 index 0000000000000000000000000000000000000000..b932d3893c0ab7d733fba01b39c874eb280dc700 --- /dev/null +++ b/ansible/main.yml @@ -0,0 +1,44 @@ +--- +- hosts: all + gather_facts: no + become: yes + vars: + ansible_ssh_private_key_file: "{{instance_server_public_key}}" + ansible_ssh_user: "ubuntu" + 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: "" + 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 \ No newline at end of file diff --git a/ansible/ssh_key.j2 b/ansible/ssh_key.j2 new file mode 100644 index 0000000000000000000000000000000000000000..9d5f53affb626b336ef30061a91c3c2ee0bdb43e --- /dev/null +++ b/ansible/ssh_key.j2 @@ -0,0 +1 @@ +{{ instance_server_public_key }} \ No newline at end of file diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c4ece8fd1e5dc201cf88edcf02b05c2b0882aa5e --- /dev/null +++ b/config.yaml @@ -0,0 +1,6 @@ +--- +iac: + - terraform + - monitoring + - ansible +... \ No newline at end of file diff --git a/monitoring/ansible.cfg b/monitoring/ansible.cfg new file mode 100644 index 0000000000000000000000000000000000000000..660a5ebcbecd0307307b5c2d2d61083e315c4e45 --- /dev/null +++ b/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/monitoring/ansible_requirements.yml b/monitoring/ansible_requirements.yml new file mode 100644 index 0000000000000000000000000000000000000000..58c0cb368b76e9a0313aee87b2296e727877b2d9 --- /dev/null +++ b/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/monitoring/config.yaml b/monitoring/config.yaml new file mode 100644 index 0000000000000000000000000000000000000000..fff1792be092b69af3f0ca25b644e39c718d4f4f --- /dev/null +++ b/monitoring/config.yaml @@ -0,0 +1,7 @@ +--- +input: + - instance_ip + - instance_server_public_key +output: [] +engine: ansible +... diff --git a/monitoring/hosts.yaml b/monitoring/hosts.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b9cbfc6d1be7e249a1f6813793dd228083d669fe --- /dev/null +++ b/monitoring/hosts.yaml @@ -0,0 +1,4 @@ +all: + hosts: + localhost: + ansible_connection: local diff --git a/monitoring/install_playbook_requirements.sh b/monitoring/install_playbook_requirements.sh new file mode 100644 index 0000000000000000000000000000000000000000..843bf3b6e0c4dfb6d6157ae22687cd7585ef3a02 --- /dev/null +++ b/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/monitoring/inventory.j2 b/monitoring/inventory.j2 new file mode 100644 index 0000000000000000000000000000000000000000..fb27937d59c516ee15e53689491ad2e3bc68195e --- /dev/null +++ b/monitoring/inventory.j2 @@ -0,0 +1,7 @@ +[vms] +{{ instance_ip }} + +[vms:vars] +ansible_connection=ssh +ansible_user=ubuntu #vm user variable potentialy +ansible_ssh_private_key_file=ssh_key \ No newline at end of file diff --git a/monitoring/main.yml b/monitoring/main.yml new file mode 100644 index 0000000000000000000000000000000000000000..657a7ea8c4c1376b0437e53ec82d7c4ebe6d0ec2 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/monitoring/run-playbook.sh b/monitoring/run-playbook.sh new file mode 100644 index 0000000000000000000000000000000000000000..f2bba22bc6dcae68d78545cc6b130677bc5083ef --- /dev/null +++ b/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/monitoring/site.yaml b/monitoring/site.yaml new file mode 100644 index 0000000000000000000000000000000000000000..531dbf50d5ca852802196de7eafe0c238b9beb85 --- /dev/null +++ b/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/monitoring/site_requirements.yaml b/monitoring/site_requirements.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3e7665dce4e17365bfd08f90425e9408d0f83045 --- /dev/null +++ b/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/monitoring/ssh_key.j2 b/monitoring/ssh_key.j2 new file mode 100644 index 0000000000000000000000000000000000000000..9d5f53affb626b336ef30061a91c3c2ee0bdb43e --- /dev/null +++ b/monitoring/ssh_key.j2 @@ -0,0 +1 @@ +{{ instance_server_public_key }} \ No newline at end of file diff --git a/monitoring/vars/main.yaml b/monitoring/vars/main.yaml new file mode 100644 index 0000000000000000000000000000000000000000..861faf3e8413d8a708702083d34c7ecd6f9a1fb1 --- /dev/null +++ b/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 0000000000000000000000000000000000000000..3cadda98c5f9e98b9c0d4fb64d16c6402519755e --- /dev/null +++ b/terraform/config.yaml @@ -0,0 +1,8 @@ +--- +engine: terraform +input: [] +output: + - instance_server_public_key + - instance_server_private_key + - instance_ip +... diff --git a/terraform/main.tf b/terraform/main.tf new file mode 100644 index 0000000000000000000000000000000000000000..75b5e2350079dfb376ad6d4bcb25e425ba03cd46 --- /dev/null +++ b/terraform/main.tf @@ -0,0 +1,116 @@ +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" +} + +data "openstack_identity_project_v3" "test_tenant" { + name = "admin" +} + +data "openstack_networking_secgroup_v2" "default" { + name = "default" + tenant_id = data.openstack_identity_project_v3.test_tenant.id +} +# Create virtual machine +resource "openstack_compute_instance_v2" "nginx" { + name = "nginx-host" + image_name = "ubuntu-18.04" + flavor_name = "m1.tiny" + key_pair = openstack_compute_keypair_v2.user_key.name + network { + port = openstack_networking_port_v2.nginx.id + } +} + +# Create ssh keys +resource "openstack_compute_keypair_v2" "user_key" { + name = "user1" +} + +# Create floating ip +resource "openstack_networking_floatingip_v2" "nginx" { + pool = "external" + +} + +# Attach floating ip to instance +resource "openstack_compute_floatingip_associate_v2" "nginx" { + floating_ip = openstack_networking_floatingip_v2.nginx.address + instance_id = openstack_compute_instance_v2.nginx.id +} + +## Network + +# Create Network +resource "openstack_networking_network_v2" "generic" { + name = " " +} + +# Create Subnet +resource "openstack_networking_subnet_v2" "nginx" { + name = "subnet-nginx" + network_id = openstack_networking_network_v2.generic.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" "nginx" { + name = "nginx" + network_id = openstack_networking_network_v2.generic.id + admin_state_up = true + security_group_ids = [ + data.openstack_networking_secgroup_v2.default.id #default flavour id + ] + fixed_ip { + subnet_id = openstack_networking_subnet_v2.nginx.id + } +} + +# Router creation. UUID external gateway +resource "openstack_networking_router_v2" "generic" { + name = "router-generic" + external_network_id = data.openstack_networking_network_v2.external.id #External network id +} +# Router interface configuration +resource "openstack_networking_router_interface_v2" "nginx" { + router_id = openstack_networking_router_v2.generic.id + subnet_id = openstack_networking_subnet_v2.nginx.id +} + +resource "openstack_compute_secgroup_v2" "http" { + name = "http" + description = "Open input http port" + rule { + from_port = 80 + to_port = 80 + ip_protocol = "tcp" + cidr = "0.0.0.0/0" + } +} + +resource "openstack_compute_secgroup_v2" "ssh" { + name = "ssh" + description = "Open input ssh port" + 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 0000000000000000000000000000000000000000..c04554d815f6c58111b6ef18b6792920c37cba73 --- /dev/null +++ b/terraform/output.tf @@ -0,0 +1,11 @@ +output "instance_server_public_key" { + value = openstack_compute_keypair_v2.user_key.public_key +} + +output "instance_server_private_key" { + value = openstack_compute_keypair_v2.user_key.private_key +} + +output "instance_ip" { + value = openstack_compute_floatingip_associate_v2.nginx.floating_ip +} \ No newline at end of file