Skip to content

Instantly share code, notes, and snippets.

@imbolc
Created February 26, 2018 06:16
Show Gist options
  • Save imbolc/d4b627a3966921ac8396c485187bd0b2 to your computer and use it in GitHub Desktop.
Save imbolc/d4b627a3966921ac8396c485187bd0b2 to your computer and use it in GitHub Desktop.
Django command to dealing with certbot (let's encrypt, letsencrypt.org) and nginx
import os
from django.core.management.base import BaseCommand
from django.conf import settings
COMMAND = f'''
./var/env/bin/certbot {{}}
--config-dir=./var/certbot/cfg
--work-dir=./var/certbot/work
--logs-dir=./var/log/certbot
--renew-hook "sudo /etc/init.d/nginx reload"
'''
COMMAND = ' '.join(COMMAND.split())
class Command(BaseCommand):
help = 'Obtain and renew certificates with certbot'
def add_arguments(self, parser):
parser.add_argument('command', help='obtain or revew')
def handle(self, *args, **options):
command = options['command']
assert command in ['obtain', 'renew']
globals()[command]()
def obtain():
host = settings.HOST
dir = './static/root'
os.makedirs(dir, exist_ok=True)
command = COMMAND.format(
f'certonly --webroot -w {dir} -d {host} -d {host}')
print(command)
os.system(command)
def renew():
command = COMMAND.format(f'renew')
print(command)
os.system(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment