-
Notifications
You must be signed in to change notification settings - Fork 29
/
cron_ssl_renew.py
78 lines (67 loc) · 2.3 KB
/
cron_ssl_renew.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import sys
import requests
import json
if sys.version_info[0] < 3:
import subprocess32 as subprocess
else:
import subprocess
from sciblog.private import (CLOUDFLARE_ZONEID, CLOUDFLARE_APIKEY,
CERTBOT_AUTO_PATH, CLOUDFLARE_FLAG)
from sciblog.settings import EMAIL_ADDRESS
# Variables
HEADERS = {
'Content-type': 'application/json',
'X-Auth-Email': EMAIL_ADDRESS,
'X-Auth-Key': CLOUDFLARE_APIKEY
}
ENDPOINT = 'https://api.cloudflare.com/client/v4/zones/' + CLOUDFLARE_ZONEID
def pause_cloudflare():
"""Pause cloudflare. From documentation:
$ curl -X PATCH "https://api.cloudflare.com/client/v4/zones/0000ZONEID0000" \
-H "X-Auth-Email: [email protected]" \
-H "X-Auth-Key: 0000APIKEY0000" \
-H "Content-Type: application/json" \
--data '{"paused": true}'
"""
data = {'paused': True}
res = requests.patch(ENDPOINT,
data=json.dumps(data), headers=HEADERS)
if res.ok:
print("Cloudflare paused")
else:
print("ERROR when pausing Cloudflare")
print(res.json())
def resume_cloudflare():
"""Resume cloudflare. From documentation:
$ curl -X PATCH "https://api.cloudflare.com/client/v4/zones/0000ZONEID0000" \
-H "X-Auth-Email: [email protected]" \
-H "X-Auth-Key: 0000APIKEY0000" \
-H "Content-Type: application/json" \
--data '{"paused": false}'
"""
data = {'paused': False}
res = requests.patch(ENDPOINT,
data=json.dumps(data), headers=HEADERS)
if res.ok:
print("Cloudflare resumed")
else:
print("ERROR when resuming Cloudflare")
print(res.json())
def renew_ssl_certificate():
try:
print("Renewing certificate...")
command = [CERTBOT_AUTO_PATH, "renew", "--quiet",
"--keep-until-expiring", "--no-self-upgrade"]
out_str = subprocess.run(command, stdout=subprocess.PIPE).stdout
if out_str:
out_str = out_str.decode("utf-8").replace('\r', '')
print(out_str)
except Exception as e:
print(e)
if __name__ == "__main__":
if CLOUDFLARE_FLAG:
pause_cloudflare()
renew_ssl_certificate()
if CLOUDFLARE_FLAG:
resume_cloudflare()
print("Process finished\n")