-
Notifications
You must be signed in to change notification settings - Fork 11
/
pa_install_webapp_ssl.py
executable file
·61 lines (50 loc) · 2.26 KB
/
pa_install_webapp_ssl.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
#!/usr/bin/python3.7
"""Set the HTTPS certificate and private key for a website to the contents of two files, and reload the site.
Usage:
pa_set_webapp_ssl.py <domain> <certificate-file> <private-key-file> [--suppress-reload]
Options:
<domain> Domain name, eg www.mydomain.com
<certificate-file> The name of the file containing the combined certificate in PEM format (normally
a number of blocks, each one starting "BEGIN CERTIFICATE" and ending "END CERTIFICATE")
<private-key-file> The name of the file containing the private key in PEM format (a file with one block,
starting with something like "BEGIN PRIVATE KEY" and ending with something like
"END PRIVATE KEY")
--suppress-reload The website will need to be reloaded in order to activate the new certificate/key combination
-- this happens by default, use this option to suppress it.
"""
from docopt import docopt
import os
import sys
from pythonanywhere.api.webapp import Webapp
from pythonanywhere.snakesay import snakesay
def main(domain_name, certificate_file, private_key_file, suppress_reload):
if not os.path.exists(certificate_file):
print(f"Could not find certificate file {certificate_file}")
sys.exit(1)
with open(certificate_file, "r") as f:
certificate = f.read()
if not os.path.exists(private_key_file):
print(f"Could not find private key file {private_key_file}")
sys.exit(1)
with open(private_key_file, "r") as f:
private_key = f.read()
webapp = Webapp(domain_name)
webapp.set_ssl(certificate, private_key)
if not suppress_reload:
webapp.reload()
ssl_details = webapp.get_ssl_info()
print(snakesay(
"That's all set up now :-)\n"
"Your new certificate will expire on {expiry:%d %B %Y},\n"
"so shortly before then you should renew it\n"
"and install the new certificate.".format(
expiry=ssl_details["not_after"]
)
))
if __name__ == '__main__':
arguments = docopt(__doc__)
main(
arguments['<domain>'],
arguments['<certificate-file>'], arguments['<private-key-file>'],
suppress_reload=arguments.get('--suppress-reload')
)