Skip to content
Snippets Groups Projects
Commit 3b5e05295e5e authored by Vincent Hatakeyama's avatar Vincent Hatakeyama
Browse files

docker dev start: option to create user (also tries to detect if connection will work)

parent 929d341db31f
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
""" """
import argparse import argparse
import logging import logging
import pwd
from subprocess import call from subprocess import call
import sys import sys
import os import os
...@@ -12,6 +13,7 @@ ...@@ -12,6 +13,7 @@
import ConfigParser import ConfigParser
from netifaces import interfaces, ifaddresses, AF_INET from netifaces import interfaces, ifaddresses, AF_INET
from psycopg2 import connect, OperationalError
# TODO auto create list of module # TODO auto create list of module
...@@ -97,7 +99,13 @@ ...@@ -97,7 +99,13 @@
help="Module to avoid including demo data [default: %(default)s]", help="Module to avoid including demo data [default: %(default)s]",
default=None, default=None,
) )
# XXX add auto create user and password? parser.add_argument(
'--create',
help="Create user/database (using your user)",
action='store_true',
)
# TODO detect that user is member of docker group
# TODO add a way to add options to docker # TODO add a way to add options to docker
# TODO add a way to add arg to odoo # TODO add a way to add arg to odoo
...@@ -176,8 +184,10 @@ ...@@ -176,8 +184,10 @@
# auto detect local conf # auto detect local conf
local_conf_path = 'conf/dev/odoo.conf' local_conf_path = 'conf/dev/odoo.conf'
user = db_user
password = db_password
if os.path.isfile(local_conf_path): if os.path.isfile(local_conf_path):
logging.info('Local configuration file found: %s' % local_conf_path) logging.info('Local configuration file found: %s' % local_conf_path)
options.append('--volume') options.append('--volume')
options.append('%s:/opt/odoo/etc/odoo.conf' % os.path.join( options.append('%s:/opt/odoo/etc/odoo.conf' % os.path.join(
project_path, local_conf_path)) project_path, local_conf_path))
...@@ -179,8 +189,35 @@ ...@@ -179,8 +189,35 @@
if os.path.isfile(local_conf_path): if os.path.isfile(local_conf_path):
logging.info('Local configuration file found: %s' % local_conf_path) logging.info('Local configuration file found: %s' % local_conf_path)
options.append('--volume') options.append('--volume')
options.append('%s:/opt/odoo/etc/odoo.conf' % os.path.join( options.append('%s:/opt/odoo/etc/odoo.conf' % os.path.join(
project_path, local_conf_path)) project_path, local_conf_path))
cp_local = ConfigParser.ConfigParser()
cp_local.read(local_conf_path)
if not user:
user = cp_local.get('options', 'db_user', 'pg')
if not password:
password = cp_local.get('options', 'db_password', 'THIS-IS-NOT-USED-DONOT-CHANGE')
# Check that connection can be done, try to create user if asked to
try:
test_connection = connect(user=user, password=password, database='postgres', host=local_ip)
except OperationalError as exception:
if nmspc.create:
logging.info('Cannot connect to database with user %s', user)
logging.info(exception)
logging.info('Creating user %s', user)
loginname = pwd.getpwuid(os.getuid())[0]
connection = connect(user=loginname, database='postgres')
with connection.cursor() as cursor:
# not injection safe but you are on your own machine
# with already full access to db
cursor.execute(
'CREATE ROLE %s LOGIN CREATEDB PASSWORD %%s' % user, (password, ))
connection.commit()
connection.close()
else:
logging.warn('Cannot connect to database with user %s', user)
logging.warn(exception)
# volume magic # volume magic
for addons_dir in addon_dirs: for addons_dir in addon_dirs:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment