django-orchestra-test/orchestra/contrib/letsencrypt/backends.py

55 lines
1.9 KiB
Python
Raw Permalink Normal View History

2016-03-11 12:19:34 +00:00
import os
import textwrap
from orchestra.contrib.orchestration import ServiceController
from . import settings
class LetsEncryptController(ServiceController):
model = 'websites.Website'
verbose_name = "Let's encrypt!"
actions = ('encrypt',)
def prepare(self):
super().prepare()
self.cleanup = []
context = {
'letsencrypt_auto': settings.LETSENCRYPT_AUTO_PATH,
}
self.append(textwrap.dedent("""
%(letsencrypt_auto)s --non-interactive --no-self-upgrade \\
--keep --expand --agree-tos certonly --webroot \\""") % context
)
def encrypt(self, website):
context = self.get_context(website)
self.append(" --webroot-path %(webroot)s \\" % context)
self.append(" --email %(email)s \\" % context)
self.append(" -d %(domains)s \\" % context)
2016-06-17 10:00:04 +00:00
self.cleanup.append("rm -rf -- %(webroot)s/.well-known" % context)
2016-03-11 12:19:34 +00:00
def commit(self):
self.append(" || exit_code=$?")
for cleanup in self.cleanup:
self.append(cleanup)
context = {
'letsencrypt_live': os.path.normpath(settings.LETSENCRYPT_LIVE_PATH),
}
self.append(textwrap.dedent("""
# Report back the lineages in order to infere each certificate path
echo '<live-lineages>'
find %(letsencrypt_live)s/* -maxdepth 0
echo '</live-lineages>'""") % context
)
super().commit()
def get_context(self, website):
content = website.content_set.get(path='/')
2016-03-11 12:19:34 +00:00
return {
'letsencrypt_auto': settings.LETSENCRYPT_AUTO_PATH,
'webroot': content.webapp.get_path(),
2016-06-17 10:00:04 +00:00
'email': settings.LETSENCRYPT_EMAIL or website.account.email,
2016-03-11 12:19:34 +00:00
'domains': ' \\\n -d '.join(website.encrypt_domains),
}