Skip to content

Commit 7e23553

Browse files
Merge pull request #197 from stfc/grafana_certbot
ENH: Add a certbot role to setup let's encrypt certificates
2 parents 6cc7d74 + 74fa5b0 commit 7e23553

File tree

4 files changed

+109
-15
lines changed

4 files changed

+109
-15
lines changed

grafana_monitoring/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ An Ansible playbook to deploy our staging / production Grafana instances. It ins
77
To replace the Aquilon configuration...
88

99
## How?
10+
11+
#### Note: You will need to have a floating IP with DNS and ports 80, 443 open. You must set up a load balancer and add the VM as a member to both pools beforehand.
12+
1013
1. Clone this repository
1114
```shell
1215
git clone https://github.com/stfc/SCD-OpenStack-Utils
@@ -23,16 +26,13 @@ To replace the Aquilon configuration...
2326
https://<your-domain>:443/login/generic_oauth
2427
```
2528
4. Fill in the staging or production inventory with the credentials
26-
5. (Optional): Change the `grafana` group inventory hosts to whatever IP they are running on
27-
6. Copy your SSL certificate with name format `<your-domain>.crt` to `roles/haproxy/files/` and make sure the key is prepended to the top.
28-
7. Run the ansible playbook
29+
5. Change the `grafana` group inventory hosts to whatever IP the machine will be running on.
30+
6. Run the ansible playbook
2931
```shell
30-
ansible-playbook site.yaml --inventory staging/production
32+
ansible-playbook site.yaml --inventory <staging | production>
3133
```
32-
8. If you need to make changes to either the Grafana or HAProxy config you can run each role separately with their tags
34+
7. If you need to make changes to any of the services' config you can run each role separately with their tags
3335
```shell
34-
ansible-playbook site.yaml --inventory staging/production --tags grafana
35-
# or
36-
ansible-playbook site.yaml --inventory staging/production --tags haproxy
36+
ansible-playbook site.yaml --inventory <staging | production> --tags <grafana | haproxy | certbot>
3737
```
3838
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
- name: Install prerequisite packages
3+
become: true
4+
ansible.builtin.apt:
5+
pkg:
6+
- python3-venv
7+
- libaugeas0
8+
update_cache: true
9+
10+
- name: Install certbot
11+
become: true
12+
ansible.builtin.pip:
13+
name: certbot
14+
virtualenv: /opt/certbot
15+
virtualenv_command: python3 -m venv
16+
17+
- name: Create a symbolic link for certbot
18+
become: true
19+
ansible.builtin.file:
20+
src: /opt/certbot/bin/certbot
21+
dest: /usr/bin/certbot
22+
owner: root
23+
group: root
24+
state: link
25+
26+
- name: Stop HAProxy
27+
become: true
28+
ansible.builtin.systemd_service:
29+
state: stopped
30+
name: haproxy.service
31+
32+
- name: Check if certificate exists
33+
become: true
34+
ansible.builtin.stat:
35+
path: /etc/haproxy/{{ domain }}.crt
36+
register: certificate_file
37+
38+
- name: Generate the certificate for the first time
39+
become: true
40+
ansible.builtin.shell: "certbot certonly --standalone --non-interactive --agree-tos --domains {{ domain }} -m cloud-support@stfc.ac.uk"
41+
when: not certificate_file.stat.exists
42+
43+
- name: Copy certificate for the first time
44+
become: true
45+
ansible.builtin.shell: "cat /etc/letsencrypt/live/{{ domain }}/privkey.pem /etc/letsencrypt/live/{{ domain }}/fullchain.pem > /etc/haproxy/{{ domain }}.crt"
46+
when: not certificate_file.stat.exists
47+
48+
- name: Create a cron job for the renewal of certificates
49+
become: true
50+
become_user: root
51+
ansible.builtin.cron:
52+
name: "Renew Let's Encrypt certificates"
53+
minute: "0"
54+
hour: "0,12"
55+
day: "*"
56+
job: "/opt/certbot/bin/python -c 'import random; import time; time.sleep(random.random() * 3600)' && sudo certbot renew -q"
57+
58+
- name: Create a cron job for the upgrade of certbot
59+
become: true
60+
become_user: root
61+
ansible.builtin.cron:
62+
name: "Upgrade Certbot"
63+
month: "*"
64+
job: "sudo /opt/certbot/bin/pip install --upgrade certbot"
65+
66+
- name: Create a cron job to copy certificate to haproxy directory
67+
become: true
68+
become_user: root
69+
ansible.builtin.cron:
70+
name: "Copy certificate"
71+
minute: "1"
72+
hour: "0,12"
73+
day: "*"
74+
job: "cat /etc/letsencrypt/live/{{ domain }}/privkey.pem /etc/letsencrypt/live/{{ domain }}/fullchain.pem > /etc/haproxy/{{ domain }}.crt"
75+
76+
- name: Create a cron job to restart HAProxy to pick up new certificate
77+
become: true
78+
become_user: root
79+
ansible.builtin.cron:
80+
name: "Restart HAProxy to pick up new certificate"
81+
minute: "3"
82+
hour: "0,12"
83+
day: "*"
84+
job: "systemctl restart haproxy.service"
85+
86+
- name: Restart HAProxy
87+
become: true
88+
ansible.builtin.systemd_service:
89+
state: restarted
90+
name: haproxy.service
91+
92+
93+

grafana_monitoring/roles/haproxy/tasks/main.yml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@
1414
group: haproxy
1515
mode: '0644'
1616

17-
- name: Copy certificate
17+
- name: Check if certificate exists
1818
become: true
19-
ansible.builtin.copy:
20-
src: "{{ domain }}.crt"
21-
dest: "/etc/haproxy/{{ domain }}.crt"
22-
owner: root
23-
group: haproxy
24-
mode: '0644'
19+
ansible.builtin.stat:
20+
path: /etc/haproxy/{{ domain }}.crt
21+
register: certificate_file
2522

2623
- name: Make sure haproxy.service is running
2724
become: true
2825
ansible.builtin.systemd_service:
2926
state: restarted
3027
name: haproxy.service
28+
when: certificate_file.stat.exists

grafana_monitoring/site.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@
99
- role: haproxy
1010
tags:
1111
- haproxy
12+
- role: certbot
13+
tags:
14+
- certbot

0 commit comments

Comments
 (0)