From 301c6c8b20ffd7ee49b09504e286f067623da320 Mon Sep 17 00:00:00 2001 From: Pierre-Louis Bonicoli Date: Fri, 14 Sep 2012 16:10:41 +0200 Subject: [PATCH] When a database is initialized, admin password could be read from a file. issue2780 review519002 --- bin/trytond | 4 ++++ trytond/server.py | 35 ++++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/bin/trytond b/bin/trytond index 3569cbac53..ea20af1ab9 100755 --- a/bin/trytond +++ b/bin/trytond @@ -41,6 +41,10 @@ def parse_commandline(): parser.add_option("--disable-cron", dest="cron", action="store_false", help="disable cron") + parser.epilog = 'The first time a database is initialized with "-i" admin'\ + ' password is read from file defined by TRYTONPASSFILE '\ + 'environment variable or interactively ask user' + (opt, _) = parser.parse_args() if opt.config: diff --git a/trytond/server.py b/trytond/server.py index e802d97729..e853ff98c0 100644 --- a/trytond/server.py +++ b/trytond/server.py @@ -142,17 +142,30 @@ class TrytonServer(object): for db_name in CONFIG['db_name']: if init[db_name]: - while True: - password = getpass('Admin Password for %s: ' % db_name) - password2 = getpass('Admin Password Confirmation: ') - if password != password2: - sys.stderr.write('Admin Password Confirmation ' \ - 'doesn\'t match Admin Password!\n') - continue - if not password: - sys.stderr.write('Admin Password is required!\n') - continue - break + # try to read password from environment variable + # TRYTONPASSFILE, empty TRYTONPASSFILE ignored + passpath = os.getenv('TRYTONPASSFILE') + password = '' + if passpath: + try: + with open(passpath) as passfile: + password = passfile.readline()[:-1] + except Exception, err: + sys.stderr.write('Can not read password ' \ + 'from "%s": "%s"\n' % (passpath, err)) + + if not password: + while True: + password = getpass('Admin Password for %s: ' % db_name) + password2 = getpass('Admin Password Confirmation: ') + if password != password2: + sys.stderr.write('Admin Password Confirmation ' \ + 'doesn\'t match Admin Password!\n') + continue + if not password: + sys.stderr.write('Admin Password is required!\n') + continue + break database = Database(db_name).connect() cursor = database.cursor()