summaryrefslogtreecommitdiffstats
path: root/fabfile.py
blob: e246ae49bb8776016ff95541e2a5c04ff8b0450f (plain)
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
from fabric.api import env, local, put, run, sudo, task

env.hosts = ['theos.kyriasis.com', 'lucifer.kyriasis.com']


def checksum_match(local_file, remote_file):
    from fabric.contrib.files import exists

    if not exists(remote_file):
        return False

    md5 = '/usr/bin/md5sum {}'
    local_sum = local(md5.format(local_file), capture=True).split()[0]
    remote_sum = run(md5.format(remote_file)).split()[0]

    return local_sum == remote_sum


@task
def deploy():
    upload_conf()
    upload_table('users', 'common/users')
    upload_table('senders', 'common/senders')


@task
def upload_conf():
    from fabric.colors import green

    local_file = '{}/smtpd.conf'.format(env.host)

    if not checksum_match(local_file, '/etc/smtpd/smtpd.conf'):
        print(green('==> Config changed, uploading new file...'))
        put(local_path=local_file, remote_path='/etc/smtpd/smtpd.conf',
            use_sudo=True)
        sudo('systemctl restart smtpd.service')


@task
def upload_table(table_name, local_path):
    from fabric.colors import green

    remote_path = '/etc/smtpd/' + table_name

    if not checksum_match(local_path, remote_path):
        print(green('==> Table {} changed, uploading new one...'
              .format(table_name)))
        put(local_path=local_path, remote_path=remote_path, use_sudo=True)
        sudo('smtpctl update table {}'.format(table_name))