def http(): conf = template('remote/gunicorn.conf', workers=config.workers) with sudo(): put(conf, '/srv/addok/gunicorn.conf') nginx_conf = template('remote/nginx.conf', domain=config.domain) with sudo(): put(nginx_conf, '/etc/nginx/sites-enabled/addok') # On LXC containers, somaxconn cannot be changed. This must be done on the # host machine. run(f'sudo sysctl -w net.core.somaxconn={config.connections} || exit 0') restart()
def install_matomo(): token = os.environ.get('MATOMO_TOKEN') if not token: sys.exist('You need to provide $MATOMO_TOKEN env var') wget('https://raw.githubusercontent.com/matomo-org/matomo-log-analytics' '/master/import_logs.py', '/srv/tilery/src/matomo.py') cron = template('remote/run-matomo', matomo_token=token) put(cron, '/etc/cron.daily/run-matomo') run('chmod +x /etc/cron.daily/run-matomo')
def deploy(): """Send config files.""" with sudo(user='******'): mkdir('/srv/tilery/pianoforte/data') put(config.source_dir / 'mapping.yml', '/srv/tilery/mapping.yml') imposm_conf = template('remote/imposm.conf', **config) put(imposm_conf, '/srv/tilery/imposm.conf') put('remote/renderd.conf', '/etc/renderd.conf') put(config.source_dir / 'dist/', '/srv/tilery/pianoforte/') put(config.source_dir / 'fonts/', '/srv/tilery/pianoforte/fonts') put(config.source_dir / 'icon/', '/srv/tilery/pianoforte/icon')
def letsencrypt(): """Configure letsencrypt.""" with sudo(): run('add-apt-repository --yes ppa:certbot/certbot') run('apt update') run('apt install -y certbot') mkdir('/var/www/letsencrypt/.well-known/acme-challenge') domains = ','.join(list(config.piano_domains) + list(config.forte_domains)) certbot_conf = template('remote/certbot.ini', domains=domains) put(certbot_conf, '/var/www/certbot.ini') run('certbot certonly -c /var/www/certbot.ini --non-interactive ' '--agree-tos')
def db(): """Create the database and the needed extensions.""" dest = '/ssd/postgresql' if not exists(dest): src = '/var/lib/postgresql' mv(src, dest) run(f'ln --symbolic --force {dest} {src}') chown('postgres:postgres', src) with sudo(user='******'): conf = template('remote/postgresql.conf', **config) put(conf, f'/etc/postgresql/{config.psql_version}/main/postgresql.conf') run('createuser tilery || exit 0') run('createdb tilery -O tilery || exit 0') run('psql tilery -c "CREATE EXTENSION IF NOT EXISTS postgis"')
def http(): """Configure Nginx and letsencrypt.""" # When we'll have a domain. put('remote/piano.conf', '/etc/nginx/snippets/piano.conf') put('remote/forte.conf', '/etc/nginx/snippets/forte.conf') put('remote/letsencrypt.conf', '/etc/nginx/snippets/letsencrypt.conf') put('remote/ssl.conf', '/etc/nginx/snippets/ssl.conf') domain = config.piano_domains[0] pempath = f'/etc/letsencrypt/live/{domain}/fullchain.pem' if exists(pempath): print(f'{pempath} found, using https configuration') conf = template('remote/nginx-https.conf', piano_domains=' '.join(config.piano_domains), forte_domains=' '.join(config.forte_domains), domain=domain) else: print(f'{pempath} not found, using http configuration') # Before letsencrypt. conf = template('remote/nginx-http.conf', piano_domains=' '.join(config.piano_domains), forte_domains=' '.join(config.forte_domains), domain=domain) put(conf, '/etc/nginx/sites-enabled/pianoforte.conf') restart(services='nginx')
def test_with_filepath_as_string(): assert template('tests/template.txt', what='text').read() == \ 'This is a text file.\n'
def test_with_non_identifier_bracket(): assert template(StringIO('A ${what}ever.'), what='text').read() == \ 'A ${what}ever.'
def test_in_a_middle_of_a_string(): assert template(StringIO('A $${what}ever.'), what='text').read() == \ 'A textever.'
def test_with_extra_context(): assert template(StringIO('A $$what.'), what='text', extra='ok').read() == \ 'A text.'
def test_with_stringio(): assert template(StringIO('This is a $$what.'), what='text').read() == \ 'This is a text.'
def test_with_filepath_as_path(): assert template(Path('tests/template.txt'), what='text').read() == \ 'This is a text file.\n'
def test_with_file_not_found(): with pytest.raises(SystemExit): template('tests/notfound.txt', what='text')
def service(): """Deploy/update the addok systemd service.""" conf = template('remote/addok.service', **config) put(conf, '/etc/systemd/system/addok.service') systemctl('enable addok.service')