2018-12-03 01:11:38 -06:00
|
|
|
# -*- coding: utf-8; -*-
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Rattail -- Retail Software Framework
|
2022-11-01 18:41:27 -05:00
|
|
|
# Copyright © 2010-2022 Lance Edgar
|
2018-12-03 01:11:38 -06:00
|
|
|
#
|
|
|
|
# This file is part of Rattail.
|
|
|
|
#
|
|
|
|
# Rattail is free software: you can redistribute it and/or modify it under the
|
|
|
|
# terms of the GNU General Public License as published by the Free Software
|
|
|
|
# Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
|
|
|
#
|
|
|
|
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# Rattail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
"""
|
|
|
|
Fabric library for Postfix
|
|
|
|
"""
|
|
|
|
|
|
|
|
from rattail_fabric2 import apt
|
2022-11-21 20:09:51 -06:00
|
|
|
from rattail.util import shlex_join
|
2018-12-03 01:11:38 -06:00
|
|
|
|
|
|
|
|
|
|
|
def install(c):
|
|
|
|
"""
|
|
|
|
Install the Postfix mail service
|
|
|
|
"""
|
|
|
|
apt.install(c, 'postfix')
|
|
|
|
apt.purge(c, 'exim4', 'exim4-base', 'exim4-config', 'exim4-daemon-light')
|
|
|
|
|
|
|
|
|
|
|
|
def alias(c, name, alias_to, path='/etc/aliases'):
|
|
|
|
"""
|
|
|
|
Set a mail alias for Postfix
|
|
|
|
"""
|
|
|
|
# does alias entry already exist?
|
|
|
|
if c.run("grep '^{}:' /etc/aliases".format(name), warn=True).failed:
|
|
|
|
# append new entry
|
2022-11-01 18:41:27 -05:00
|
|
|
entry = '{}: {}'.format(name, alias_to)
|
2022-11-21 20:09:51 -06:00
|
|
|
echo = shlex_join(['echo', entry])
|
2022-11-01 18:41:27 -05:00
|
|
|
cmd = '{} >> /etc/aliases'.format(echo)
|
2022-11-21 20:09:51 -06:00
|
|
|
cmd = shlex_join(['bash', '-c', cmd])
|
2022-11-01 18:41:27 -05:00
|
|
|
c.sudo(cmd)
|
2018-12-03 01:11:38 -06:00
|
|
|
else:
|
|
|
|
# update existing entry
|
2022-11-01 18:41:27 -05:00
|
|
|
alias_to = alias_to.replace('|', '\\|')
|
|
|
|
sub = "s|^{}: .*|{}: {}|".format(name, name, alias_to)
|
2022-11-21 20:09:51 -06:00
|
|
|
cmd = shlex_join(['sed', '-i.bak', '-E', sub, '/etc/aliases'])
|
2022-11-01 18:41:27 -05:00
|
|
|
c.sudo(cmd)
|
2018-12-03 01:11:38 -06:00
|
|
|
|
|
|
|
c.sudo('newaliases')
|
|
|
|
|
|
|
|
|
|
|
|
def restart(c):
|
|
|
|
"""
|
|
|
|
Restart the Postfix mail service
|
|
|
|
"""
|
|
|
|
c.sudo('systemctl restart postfix.service')
|
|
|
|
|
|
|
|
|
|
|
|
def set_config(c, setting, value):
|
|
|
|
"""
|
|
|
|
Configure the given setting with the given value.
|
|
|
|
"""
|
|
|
|
c.sudo("postconf -e '{}={}'".format(setting, value))
|
|
|
|
|
|
|
|
|
2019-09-09 14:17:49 -05:00
|
|
|
def set_myhostname(c, hostname):
|
|
|
|
"""
|
|
|
|
Configure the 'myhostname' setting with the given string.
|
|
|
|
"""
|
|
|
|
set_config(c, 'myhostname', hostname)
|
|
|
|
|
|
|
|
|
2019-09-12 22:05:59 -05:00
|
|
|
def set_myorigin(c, origin):
|
|
|
|
"""
|
|
|
|
Configure the 'myorigin' setting with the given string.
|
|
|
|
"""
|
|
|
|
set_config(c, 'myorigin', origin)
|
|
|
|
|
|
|
|
|
2019-09-09 14:17:49 -05:00
|
|
|
def set_mydestination(c, *destinations):
|
|
|
|
"""
|
|
|
|
Configure the 'mydestinations' setting with the given strings.
|
|
|
|
"""
|
|
|
|
set_config(c, 'mydestination', ', '.join(destinations))
|
|
|
|
|
|
|
|
|
2019-09-12 22:05:59 -05:00
|
|
|
def set_mynetworks(c, *networks):
|
|
|
|
"""
|
|
|
|
Configure the 'mynetworks' setting with the given strings.
|
|
|
|
"""
|
|
|
|
set_config(c, 'mynetworks', ' '.join(networks))
|
|
|
|
|
|
|
|
|
2018-12-03 01:11:38 -06:00
|
|
|
def set_relayhost(c, relayhost):
|
|
|
|
"""
|
|
|
|
Configure the 'relayhost' setting with the given string
|
|
|
|
"""
|
|
|
|
set_config(c, 'relayhost', relayhost)
|