postfix_maps command silent mode.

fix #536
This commit is contained in:
Antoine Nguyen
2014-07-04 10:56:39 +02:00
parent 081b6e90af
commit f83df1c707
2 changed files with 105 additions and 13 deletions
+44 -13
View File
@@ -10,19 +10,29 @@ from . import Command
class MapFilesGenerator(object):
def __init__(self):
def __init__(self, dbinfo=None):
"""Constructor.
"""
self.template = Template("""user = {{ dbuser }}
password = {{ dbpass }}
dbname = {{ dbname }}
hosts = {{ dbhost }}
query = {{ query|safe }}
""")
self.tpl_context = {
'dbhost': raw_input('Database host: '),
'dbname': raw_input('Database name: '),
'dbuser': raw_input('Username: '),
'dbpass': getpass.getpass('Password: ')
}
if dbinfo is None:
self.tpl_context = {
'dbhost': raw_input('Database host: '),
'dbname': raw_input('Database name: '),
'dbuser': raw_input('Username: '),
'dbpass': getpass.getpass('Password: ')
}
else:
self.tpl_context = {
'dbhost': dbinfo['HOST'],
'dbname': dbinfo['NAME'],
'dbuser': dbinfo['USER'],
'dbpass': dbinfo['PASSWORD']
}
if not self.tpl_context['dbhost']:
self.tpl_context['dbhost'] = '127.0.0.1'
@@ -77,13 +87,18 @@ query = {{ query|safe }}
class SQLiteMapFilesGenerator(MapFilesGenerator):
def __init__(self):
def __init__(self, dbinfo=None):
self.template = Template("""dbpath = {{ dbpath }}
query = {{ query|safe }}
""")
self.tpl_context = {
'dbpath': raw_input('Database path: ')
}
if dbinfo is None:
self.tpl_context = {
'dbpath': raw_input('Database path: ')
}
else:
self.tpl_context = {
'dbpath': dbinfo['NAME']
}
class MapFile(object):
@@ -194,14 +209,30 @@ class PostfixMapsCommand(Command):
default='mysql',
help='Used database type'
)
self._parser.add_argument(
'--dburl', type=str, nargs=1, default=None,
help="url of Modoboa's database"
)
self._parser.add_argument(
'--categories', type=str, nargs='*', default=['std'],
help='Map file categories to generate (choices: std, autoreply)'
)
def handle(self, parsed_args):
"""Command entry point.
"""
dbinfo = None
if parsed_args.dburl:
import dj_database_url
dbinfo = dj_database_url.config(default=parsed_args.dburl[0])
if 'sqlite' in dbinfo['ENGINE']:
parsed_args.dbtype = 'sqlite'
elif 'psycopg2' in dbinfo['ENGINE']:
parsed_args.dbtype = 'postgres'
else:
parsed_args.dbtype = 'mysql'
if parsed_args.dbtype == 'sqlite':
g = SQLiteMapFilesGenerator()
g = SQLiteMapFilesGenerator(dbinfo)
else:
g = MapFilesGenerator()
g = MapFilesGenerator(dbinfo)
g.render(parsed_args)
+61
View File
@@ -1,4 +1,10 @@
import os
import tempfile
from django.core.urlresolvers import reverse
from django.test import TestCase
from modoboa.lib.sysutils import exec_cmd
from modoboa.lib.tests import ModoTestCase
from . import factories
@@ -40,3 +46,58 @@ class ProfileTestCase(ModoTestCase):
self.assertTrue(
self.clt.login(username="user@test.com", password="tutu")
)
class ModoboaAdminCommandTestCase(TestCase):
MAP_FILES = {
"std": [
"sql-domains.cf", "sql-domain-aliases.cf", "sql-aliases.cf",
"sql-domain-aliases-mailboxes.cf", "sql-catchall-aliases.cf",
"sql-maintain.cf"
],
"autoreply": ["sql-autoreplies-transport.cf", "sql-autoreplies.cf"],
"relaydomains": [
"sql-relaydomains.cf", "sql-relaydomains-transport.cf",
"sql-relaydomain-aliases-transport.cf",
"sql-relay-recipient-verification.cf"
]
}
def setUp(self):
self.workdir = tempfile.mkdtemp()
def tearDown(self):
exec_cmd("rm -rf {}".format(self.workdir))
def _test_maps_generation(self, engine, categories=None):
if categories is None:
categories = ["std", "autoreply", "relaydomains"]
dburl = "{}://user:password@localhost/testdb".format(engine)
code, output = exec_cmd(
"modoboa-admin.py postfix_maps --categories {} --dburl {} {}".format(
" ".join(categories), dburl, self.workdir
)
)
self.assertEqual(code, 0)
for category in categories:
for mapfile in self.MAP_FILES[category]:
path = "{}/{}".format(self.workdir, mapfile)
self.assertTrue(os.path.exists(path))
with open(path) as fpo:
content = fpo.read()
if engine != "sqlite":
self.assertIn("user = user", content)
self.assertIn("password = password", content)
self.assertIn("dbname = testdb", content)
self.assertIn("hosts = localhost", content)
else:
self.assertIn("dbpath = testdb", content)
def test_postgres_maps(self):
self._test_maps_generation("postgres")
def test_mysql_maps(self):
self._test_maps_generation("mysql")
def test_sqlite_maps(self):
self._test_maps_generation("sqlite")