Skip to content

Commit 8fc6986

Browse files
committed
Update 1.test terraform configuration
1 parent 3a5cb15 commit 8fc6986

File tree

8 files changed

+138
-129
lines changed

8 files changed

+138
-129
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@
44
**/terraform.tfvars
55
**/terraform.tfstate*
66
**/.terraform
7-
1.test/main.tf
8-
2.prod/main.tf
7+
**/conf/*

1.test/main.tf.sample

-30
This file was deleted.

1.test/openrc.sh.sample

-8
This file was deleted.

1.test/script-up

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
ll
2-
. openrc.sh
3-
nova list
4-
python ../ovh-tools/show-servers-attached-test.py
51
vi main.tf # show 2 modules
62
terraform get
3+
terraform init
74
terraform apply -target openstack_compute_keypair_v2.gw -target module.app
85
vi terraform-modules/app/main.tf # show variables, openstack ressources, instances with yaml, count, flavor, metadata
96
vi terraform-modules/app/frontweb.yaml # show add/del scripts, metadata, runcmd
@@ -13,4 +10,3 @@ vi terraform-modules/stress/main.tf # show variables, block_device
1310
vi terraform-modules/stress/master.yaml # mount
1411
terraform-modules/stress/injector.yaml # ssh
1512
nova list
16-
python ../ovh-tools/show-servers-attached-test.py

terraform-modules/app/backend.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ runcmd:
2828
- service nfs-kernel-server restart
2929
- wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -O /usr/local/bin/wp
3030
- chmod +x /usr/local/bin/wp
31-
- cd /srv/www/html/ && /usr/local/bin/wp --allow-root core download --path=/srv/www/html/
31+
- cd /srv/www/html/ && /usr/local/bin/wp --allow-root core download --path=/srv/www/html/ --version="4.8.1"
3232
- cd /srv/www/html/ && /usr/local/bin/wp --allow-root core config --dbname=wordpress --dbuser=v9bK6weA --dbhost=10.1.254.254 --dbpass=$p
3333
- cd /srv/www/html/ && /usr/local/bin/wp --allow-root db create
34-
- cd /srv/www/html/ && /usr/local/bin/wp --allow-root core install --url=test-infra-as-code.pilgrimstack.com --title="Infrastructure As Code" --admin_user=wpcli --admin_password=$p --admin_email=support@ovh.net
35-
- chown -R www-data:www-data /srv/www
34+
- chown -R www-data:www-data /srv/www
35+
- apt-get purge -y apache2

terraform-modules/app/frontweb.yaml

+13-62
Original file line numberDiff line numberDiff line change
@@ -18,95 +18,46 @@ packages:
1818
- mariadb-client
1919
- php5-curl
2020
- nfs-common
21-
- python-pip
22-
- python-dev
2321
- curl
2422
- jq
2523

2624
write_files:
2725
- content: |
28-
#!/usr/bin/python
29-
import ovh
30-
from netifaces import interfaces, ifaddresses, AF_INET
31-
client = ovh.Client()
32-
params = {}
33-
params['address'] = ifaddresses('eth0').setdefault(AF_INET, [{'addr':'No IP addr'}] )[0]['addr']
34-
params['status'] = 'active'
35-
result = client.post('/ipLoadbalancing/--IPLB--/server', **params)
36-
params = {}
37-
params['serverId'] = result['id']
38-
params['probe'] = 'true'
39-
result = client.post('/ipLoadbalancing/--IPLB--/backend/--BACKEND--/server', **params)
40-
params = {}
41-
params['zone'] = 'gra'
42-
result = client.post('/ipLoadbalancing/--IPLB--/refresh', **params)
43-
path: /root/ovh-add-in-iplb.py
26+
#!/bin/bash
27+
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@--IPLB-- "echo ' server $(hostname) --MYIP--:80 maxconn 32' >> /etc/haproxy/haproxy.cfg; service haproxy restart"
28+
path: /root/ovh-add-in-iplb
4429
owner: root:root
4530
permissions: '0755'
4631
- content: |
47-
#!/usr/bin/python
48-
import ovh
49-
from netifaces import interfaces, ifaddresses, AF_INET
50-
client = ovh.Client()
51-
params = {}
52-
params['address'] = ifaddresses('eth0').setdefault(AF_INET, [{'addr':'No IP addr'}] )[0]['addr']
53-
result = client.get('/ipLoadbalancing/--IPLB--/server', **params)
54-
id = result[0]
55-
params = {}
56-
result = client.get('/ipLoadbalancing/--IPLB--/backend/--BACKEND--/server', **params)
57-
for i in result:
58-
params = {}
59-
result_to_delete = client.get('/ipLoadbalancing/--IPLB--/backend/--BACKEND--/server/' + str(i), **params)
60-
if result_to_delete['serverId'] == id:
61-
params = {}
62-
client.delete('/ipLoadbalancing/--IPLB--/backend/--BACKEND--/server/' + str(i), **params)
63-
client.delete('/ipLoadbalancing/--IPLB--/server/' + str(result_to_delete['serverId']), **params)
64-
params = {}
65-
params['zone'] = 'gra'
66-
result = client.post('/ipLoadbalancing/--IPLB--/refresh', **params)
67-
path: /root/ovh-del-in-iplb.py
32+
#!/bin/bash
33+
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@--IPLB-- "sed -i -re /$(hostname)/d /etc/haproxy/haproxy.cfg; service haproxy restart"
34+
path: /root/ovh-del-in-iplb
6835
owner: root:root
6936
permissions: '0755'
7037
- content: |
7138
[Unit]
72-
Description=Subscribe to the IPLB at OVH with the local public IP on eth0
39+
Description=Subscribe to the loadbalancer with the local public IP on eth0
7340
Wants=cloud-init.service
7441
7542
[Service]
7643
Type=oneshot
7744
RemainAfterExit=yes
78-
ExecStart=/root/ovh-add-in-iplb.py
79-
ExecStop=/root/ovh-del-in-iplb.py
45+
ExecStart=/root/ovh-add-in-iplb
46+
ExecStop=/root/ovh-del-in-iplb
8047
8148
[Install]
8249
WantedBy=multi-user.target
8350
path: /etc/systemd/system/ovh-iplb-subscription.service
8451
owner: root:root
8552
permissions: '0644'
86-
- content: |
87-
#!/bin/bash
88-
cat > /etc/ovh.conf <<EOF
89-
[default]
90-
endpoint=ovh-eu
91-
92-
[ovh-eu]
93-
EOF
94-
echo "application_key=$(curl -s http://169.254.169.254/openstack/latest/meta_data.json | jq .meta.application_key | sed s'/\"//g')" >> /etc/ovh.conf
95-
echo "application_secret=$(curl -s http://169.254.169.254/openstack/latest/meta_data.json | jq .meta.application_secret| sed s'/\"//g')" >> /etc/ovh.conf
96-
echo "consumer_key=$(curl -s http://169.254.169.254/openstack/latest/meta_data.json | jq .meta.consumer_key | sed s'/\"//g')" >> /etc/ovh.conf
97-
path: /root/ovh-api-confgen
98-
owner: root:root
99-
permissions: '0755'
10053
10154
runcmd:
102-
- pip install ovh
103-
- pip install netifaces
104-
- /root/ovh-api-confgen
10555
- iplb=$(curl -s http://169.254.169.254/openstack/latest/meta_data.json | jq .meta.iplb | sed s'/\"//g')
106-
- iplbbackend=$(curl -s http://169.254.169.254/openstack/latest/meta_data.json | jq .meta.iplbbackend | sed s'/\"//g')
107-
- sed -i -re "s/--IPLB--/$iplb/g" /root/ovh-del-in-iplb.py /root/ovh-add-in-iplb.py
108-
- sed -i -re "s/--BACKEND--/$iplbbackend/g" /root/ovh-del-in-iplb.py /root/ovh-add-in-iplb.py
56+
- myip=$(curl ifconfig.me)
57+
- sed -i -re "s/--IPLB--/$iplb/g" /root/ovh-del-in-iplb /root/ovh-add-in-iplb
58+
- sed -i -re "s/--MYIP--/$myip/g" /root/ovh-del-in-iplb /root/ovh-add-in-iplb
10959
- systemctl enable ovh-iplb-subscription.service
11060
- systemctl start ovh-iplb-subscription.service
11161
- service apache2 restart
11262
- rm -f /var/www/html/index.html
63+
- mount -a
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#cloud-config
2+
3+
apt_update: true
4+
5+
packages:
6+
- haproxy
7+
8+
write_files:
9+
- content: |
10+
global
11+
log /dev/log local0
12+
log /dev/log local1 notice
13+
chroot /var/lib/haproxy
14+
stats socket /run/haproxy/admin.sock mode 660 level admin
15+
stats timeout 30s
16+
user haproxy
17+
group haproxy
18+
daemon
19+
ca-base /etc/ssl/certs
20+
crt-base /etc/ssl/private
21+
ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS
22+
ssl-default-bind-options no-sslv3
23+
defaults
24+
log global
25+
mode http
26+
option httplog
27+
option dontlognull
28+
timeout connect 5000
29+
timeout client 50000
30+
timeout server 50000
31+
errorfile 400 /etc/haproxy/errors/400.http
32+
errorfile 403 /etc/haproxy/errors/403.http
33+
errorfile 408 /etc/haproxy/errors/408.http
34+
errorfile 500 /etc/haproxy/errors/500.http
35+
errorfile 502 /etc/haproxy/errors/502.http
36+
errorfile 503 /etc/haproxy/errors/503.http
37+
errorfile 504 /etc/haproxy/errors/504.http
38+
listen http-in
39+
bind *:80
40+
stats enable
41+
stats show-node
42+
stats uri /stats
43+
path: /etc/haproxy/haproxy.cfg
44+
owner: root:root
45+
permissions: '0644'
46+
47+
runcmd:
48+
- service haproxy restart

terraform-modules/app/main.tf

+72-19
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
variable "iplb" {}
2-
variable "iplbbackend" {}
3-
variable "application_key" {}
4-
variable "application_secret" {}
5-
variable "consumer_key" {}
6-
variable "vlan_id" {}
71
variable "count" {
82
default = 1
93
}
104

5+
resource "openstack_networking_network_v2" "privatenet-test" {
6+
name = "privatenet-test"
7+
admin_state_up = "true"
8+
}
9+
1110
resource "openstack_networking_subnet_v2" "internal" {
12-
network_id = "${var.vlan_id}"
11+
network_id = "${openstack_networking_network_v2.privatenet-test.id}"
1312
cidr = "10.0.0.0/8"
1413
allocation_pools {
1514
start = "10.0.0.2"
@@ -18,47 +17,101 @@ resource "openstack_networking_subnet_v2" "internal" {
1817
}
1918

2019
resource "openstack_compute_instance_v2" "backend" {
21-
region = "GRA1"
2220
name = "backend"
2321
image_name = "Debian 8"
24-
flavor_name = "sp-60-ssd"
22+
flavor_name = "b2-15"
2523
key_pair = "gw"
2624
security_groups = ["default"]
2725
network {
2826
name = "Ext-Net"
2927
access_network = true
3028
}
3129
network {
32-
name = "VLAN"
30+
name = "${openstack_networking_network_v2.privatenet-test.name}"
3331
fixed_ip_v4 = "10.1.254.254"
3432
}
3533
user_data = "${file("${path.module}/backend.yaml")}"
3634
}
3735

36+
resource "openstack_compute_instance_v2" "loadbalancer" {
37+
name = "loadbalancer"
38+
image_name = "Debian 8"
39+
flavor_name = "s1-2"
40+
key_pair = "gw"
41+
security_groups = ["default"]
42+
network {
43+
name = "Ext-Net"
44+
access_network = true
45+
}
46+
provisioner "local-exec" {
47+
command = "rm -f conf/shared_key && ssh-keygen -t rsa -N '' -f conf/shared_key -q"
48+
}
49+
provisioner "file" {
50+
source = "conf/shared_key.pub"
51+
destination = "/home/debian/authorized_keys"
52+
connection {
53+
type = "ssh"
54+
user = "debian"
55+
private_key = "${file("~/.ssh/id_rsa")}"
56+
}
57+
}
58+
provisioner "remote-exec" {
59+
inline = [
60+
"sudo mv /home/debian/authorized_keys /root/.ssh/",
61+
"sudo chmod 600 /root/.ssh/authorized_keys",
62+
"sudo chown root:root /root/.ssh/authorized_keys",
63+
]
64+
connection {
65+
type = "ssh"
66+
user = "debian"
67+
private_key = "${file("~/.ssh/id_rsa")}"
68+
}
69+
}
70+
user_data = "${file("${path.module}/loadbalancer.yaml")}"
71+
}
3872

3973
resource "openstack_compute_instance_v2" "frontweb" {
40-
depends_on = ["openstack_compute_instance_v2.backend"]
74+
depends_on = [
75+
"openstack_compute_instance_v2.backend",
76+
"openstack_compute_instance_v2.loadbalancer",
77+
]
4178
count = "${var.count}"
4279
stop_before_destroy = true
43-
region = "GRA1"
4480
name = "${format("frontweb-%02d", count.index+1)}"
4581
image_name = "Debian 8"
46-
flavor_name = "eg-7-ssd"
82+
flavor_name = "b2-7"
4783
key_pair = "gw"
4884
security_groups = ["default"]
4985
network {
5086
name = "Ext-Net"
5187
access_network = true
5288
}
5389
network {
54-
name = "VLAN"
90+
name = "${openstack_networking_network_v2.privatenet-test.name}"
91+
}
92+
provisioner "file" {
93+
source = "conf/shared_key"
94+
destination = "/home/debian/id_rsa"
95+
connection {
96+
type = "ssh"
97+
user = "debian"
98+
private_key = "${file("~/.ssh/id_rsa")}"
99+
}
100+
}
101+
provisioner "remote-exec" {
102+
inline = [
103+
"sudo mv /home/debian/id_rsa /root/.ssh/",
104+
"sudo chmod 600 /root/.ssh/id_rsa",
105+
"sudo chown root:root /root/.ssh/id_rsa",
106+
]
107+
connection {
108+
type = "ssh"
109+
user = "debian"
110+
private_key = "${file("~/.ssh/id_rsa")}"
111+
}
55112
}
56113
user_data = "${file("${path.module}/frontweb.yaml")}"
57114
metadata {
58-
application_key = "${var.application_key}"
59-
application_secret = "${var.application_secret}"
60-
consumer_key = "${var.consumer_key}"
61-
iplb = "${var.iplb}"
62-
iplbbackend = "${var.iplbbackend}"
115+
iplb = "${openstack_compute_instance_v2.loadbalancer.access_ip_v4}"
63116
}
64117
}

0 commit comments

Comments
 (0)