255 {'short': 'v', 'type' : 'yn', 'metavar': '<verbose>', |
255 {'short': 'v', 'type' : 'yn', 'metavar': '<verbose>', |
256 'default': 'n', |
256 'default': 'n', |
257 'help': 'verbose mode: will ask all possible configuration questions', |
257 'help': 'verbose mode: will ask all possible configuration questions', |
258 } |
258 } |
259 ), |
259 ), |
|
260 ('automatic', |
|
261 {'short': 'a', 'type' : 'yn', 'metavar': '<auto>', |
|
262 'default': 'n', |
|
263 'help': 'automatic mode: never ask and use default answer to every question', |
|
264 } |
|
265 ), |
260 ) |
266 ) |
261 def run(self, args): |
267 def run(self, args): |
262 """run the command with its specific arguments""" |
268 """run the command with its specific arguments""" |
263 from logilab.common.adbh import get_adv_func_helper |
269 from logilab.common.adbh import get_adv_func_helper |
264 from indexer import get_indexer |
270 from indexer import get_indexer |
265 verbose = self.get('verbose') |
271 verbose = self.get('verbose') |
|
272 automatic = self.get('automatic') |
266 appid = pop_arg(args, msg='No instance specified !') |
273 appid = pop_arg(args, msg='No instance specified !') |
267 config = ServerConfiguration.config_for(appid) |
274 config = ServerConfiguration.config_for(appid) |
268 create_db = self.config.create_db |
275 create_db = self.config.create_db |
269 source = config.sources()['system'] |
276 source = config.sources()['system'] |
270 driver = source['db-driver'] |
277 driver = source['db-driver'] |
275 dbcnx = _db_sys_cnx(source, 'CREATE DATABASE and / or USER', verbose=verbose) |
282 dbcnx = _db_sys_cnx(source, 'CREATE DATABASE and / or USER', verbose=verbose) |
276 cursor = dbcnx.cursor() |
283 cursor = dbcnx.cursor() |
277 try: |
284 try: |
278 if helper.users_support: |
285 if helper.users_support: |
279 user = source['db-user'] |
286 user = source['db-user'] |
280 if not helper.user_exists(cursor, user) and \ |
287 if not helper.user_exists(cursor, user) and (automatic or \ |
281 ASK.confirm('Create db user %s ?' % user, default_is_yes=False): |
288 ASK.confirm('Create db user %s ?' % user, default_is_yes=False)): |
282 helper.create_user(source['db-user'], source['db-password']) |
289 helper.create_user(source['db-user'], source['db-password']) |
283 print '-> user %s created.' % user |
290 print '-> user %s created.' % user |
284 dbname = source['db-name'] |
291 dbname = source['db-name'] |
285 if dbname in helper.list_databases(cursor): |
292 if dbname in helper.list_databases(cursor): |
286 if ASK.confirm('Database %s already exists -- do you want to drop it ?' % dbname): |
293 if automatic or ASK.confirm('Database %s already exists -- do you want to drop it ?' % dbname): |
287 cursor.execute('DROP DATABASE %s' % dbname) |
294 cursor.execute('DROP DATABASE %s' % dbname) |
288 else: |
295 else: |
289 return |
296 return |
290 if dbcnx.logged_user != source['db-user']: |
297 if dbcnx.logged_user != source['db-user']: |
291 helper.create_database(cursor, dbname, source['db-user'], |
298 helper.create_database(cursor, dbname, source['db-user'], |
309 helper.create_language(cursor, extlang) |
316 helper.create_language(cursor, extlang) |
310 cursor.close() |
317 cursor.close() |
311 cnx.commit() |
318 cnx.commit() |
312 print '-> database for instance %s created and necessary extensions installed.' % appid |
319 print '-> database for instance %s created and necessary extensions installed.' % appid |
313 print |
320 print |
314 if ASK.confirm('Run db-init to initialize the system database ?'): |
321 if automatic or ASK.confirm('Run db-init to initialize the system database ?'): |
315 cmd_run('db-init', config.appid) |
322 cmd_run('db-init', config.appid) |
316 else: |
323 else: |
317 print ('-> nevermind, you can do it later with ' |
324 print ('-> nevermind, you can do it later with ' |
318 '"cubicweb-ctl db-init %s".' % self.config.appid) |
325 '"cubicweb-ctl db-init %s".' % self.config.appid) |
319 |
326 |