This commit is contained in:
Antoine Nguyen
2016-03-04 15:46:38 +01:00
parent 7eb937aeff
commit 0eeae4b8a9
2 changed files with 12 additions and 9 deletions

View File

@@ -81,6 +81,9 @@ class PostgreSQL(Database):
"""Setup .pgpass file.""" """Setup .pgpass file."""
if self._pgpass_done: if self._pgpass_done:
return return
if self.dbname not in ["localhost", "127.0.0.1"]:
self._pgpass_done = True
return
pw = pwd.getpwnam(self.dbuser) pw = pwd.getpwnam(self.dbuser)
target = os.path.join(pw[5], ".pgpass") target = os.path.join(pw[5], ".pgpass")
with open(target, "w") as fp: with open(target, "w") as fp:
@@ -96,7 +99,7 @@ class PostgreSQL(Database):
self._setup_pgpass(dbname, dbuser, dbpassword) self._setup_pgpass(dbname, dbuser, dbpassword)
utils.exec_cmd( utils.exec_cmd(
"psql -h {} -d {} -U {} -w < {}".format( "psql -h {} -d {} -U {} -w < {}".format(
"127.0.0.1", dbname, dbuser, path), self.dbhost, dbname, dbuser, path),
sudo_user=self.dbuser) sudo_user=self.dbuser)
@@ -120,7 +123,7 @@ class MySQL(Database):
if dbuser is None and dbpassword is None: if dbuser is None and dbpassword is None:
dbuser = self.dbuser dbuser = self.dbuser
dbpassword = self.dbpassword dbpassword = self.dbpassword
cmd = "mysql -u {} -p{}".format(dbuser, dbpassword) cmd = "mysql -h {} -u {} -p{}".format(self.dbhost, dbuser, dbpassword)
if dbname: if dbname:
cmd += " -D {}".format(dbname) cmd += " -D {}".format(dbname)
utils.exec_cmd(cmd + """ -e "{}" """.format(query)) utils.exec_cmd(cmd + """ -e "{}" """.format(query))
@@ -128,7 +131,7 @@ class MySQL(Database):
def create_user(self, name, password): def create_user(self, name, password):
"""Create a user.""" """Create a user."""
self._exec_query( self._exec_query(
"CREATE USER '{}'@'localhost' IDENTIFIED BY '{}'".format( "CREATE USER '{}'@'%' IDENTIFIED BY '{}'".format(
name, password)) name, password))
def create_database(self, name, owner): def create_database(self, name, owner):
@@ -140,15 +143,14 @@ class MySQL(Database):
def grant_access(self, dbname, user): def grant_access(self, dbname, user):
"""Grant access to dbname.""" """Grant access to dbname."""
self._exec_query( self._exec_query(
"GRANT ALL PRIVILEGES ON {}.* to '{}'@'localhost'" "GRANT ALL PRIVILEGES ON {}.* to '{}'@'%'"
.format(dbname, user)) .format(dbname, user))
def load_sql_file(self, dbname, dbuser, dbpassword, path): def load_sql_file(self, dbname, dbuser, dbpassword, path):
"""Load SQL file.""" """Load SQL file."""
dbhost = self.config.get("database", "host")
utils.exec_cmd( utils.exec_cmd(
"mysql -h {} -u {} -p{} {} < {}".format( "mysql -h {} -u {} -p{} {} < {}".format(
dbhost, dbuser, dbpassword, dbname, path) self.dbhost, dbuser, dbpassword, dbname, path)
) )

View File

@@ -67,16 +67,17 @@ class Modoboa(base.Installer):
"--timezone", self.config.get("modoboa", "timezone"), "--timezone", self.config.get("modoboa", "timezone"),
"--domain", self.config.get("general", "hostname"), "--domain", self.config.get("general", "hostname"),
"--extensions", " ".join(self.extensions), "--extensions", " ".join(self.extensions),
"--dburl", "default:{0}://{1}:{2}@localhost/{1}".format( "--dburl", "default:{0}://{1}:{2}@{3}/{1}".format(
self.config.get("database", "engine"), self.dbname, self.config.get("database", "engine"), self.dbname,
self.dbpasswd) self.dbpasswd, self.dbhost)
] ]
if self.config.getboolean("amavis", "enabled"): if self.config.getboolean("amavis", "enabled"):
args += [ args += [
"amavis:{}://{}:{}@localhost/{}".format( "amavis:{}://{}:{}@{}/{}".format(
self.config.get("database", "engine"), self.config.get("database", "engine"),
self.config.get("amavis", "dbuser"), self.config.get("amavis", "dbuser"),
self.config.get("amavis", "dbpassword"), self.config.get("amavis", "dbpassword"),
self.dbhost,
self.config.get("amavis", "dbname") self.config.get("amavis", "dbname")
) )
] ]