Newer
Older
# -*- coding: utf-8 -*-
import utils
def setup(interactive=True):
# Get hostname
utils.log('Getting system hostname.')
code, hostname = utils.exec_cmd('hostname')
if code == 0:
utils.log('Hostname is %s.' % hostname)
else:
raise Exception('Failed to get hostname.')
# Install and configure postfix
dir_path = utils.get_dir(__file__)
server = utils.get_conf('EMAIL_SMTP_SERVER', '')
'DEBIAN_FRONTEND=noninteractive apt-get install -y postfix bsd-mailx',
dict(line='write', template='%s/main.cf' % dir_path, target='/etc/postfix/main.cf', params=(
('{{ hostname }}', hostname),
]
# Configure mail aliases
if not server:
# with relayless cases emails are not always delivered to google mailing lists unless mailname is ubicast.eu and DNS spf records are set
mailname = 'ubicast.eu'
else:
mailname = utils.get_conf('MS_SERVER_NAME')
if not mailname or mailname == 'mediaserver':
mailname = hostname
cmds.extend([
'echo "%s" > /etc/mailname' % mailname,
'rgrep "root:" /etc/aliases || echo "root: sysadmin@ubicast.eu" >> /etc/aliases',
])
# Configure mail sender
sender = utils.get_conf('EMAIL_SENDER', '').strip(' \t@')
if sender.count('@') != 1:
utils.log('Invalid sender address: "%s" (one "@" must be in the sender address).')
sender = None
if not sender and hostname:
if utils.get_conf('MS_SERVER_NAME', '') not in ('', 'mediaserver'):
sender = '%s@%s' % (hostname, utils.get_conf('MS_SERVER_NAME'))
elif utils.get_conf('CM_SERVER_NAME', '') not in ('', 'mirismanager'):
sender = '%s@%s' % (hostname, utils.get_conf('CM_SERVER_NAME'))
elif utils.get_conf('MONITOR_SERVER_NAME', '') not in ('', 'monitor'):
sender = '%s@%s' % (hostname, utils.get_conf('MONITOR_SERVER_NAME'))
if sender:
cmds.extend([
'rm -f /etc/postfix/generic',
'echo "root@localhost %s" >> /etc/postfix/generic' % sender,
'echo "root@%s %s" >> /etc/postfix/generic' % (hostname, sender),
'echo "@%s %s" >> /etc/postfix/generic' % (hostname, sender),
'postmap hash:/etc/postfix/generic',
'sed -i "s/#smtp_generic_maps/smtp_generic_maps/" /etc/postfix/main.cf',
])
cmds.append('service postfix restart')
# Setup authentication if any
user = utils.get_conf('EMAIL_SMTP_USER')
pwd = utils.get_conf('EMAIL_SMTP_PWD')
if user and pwd:
utils.log('Enabling authentication for SMTP relay.')
with open('/etc/postfix/sasl-passwords', 'w') as fo:
fo.write('%s %s:%s\n' % (server, user, pwd))
auth_conf = '''
# SMTP relay authentication
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl-passwords
smtp_sasl_security_options = noanonymous
'''
with open('/etc/postfix/main.cf', 'a') as fo:
fo.write(auth_conf)
cmds = [
'postmap hash:/etc/postfix/sasl-passwords',
'service postfix restart',
]
utils.run_commands(cmds)