hobo: configure sql support if required (#5039)

This commit is contained in:
Frédéric Péters 2014-10-31 14:06:44 +01:00
parent 7f77ed94b4
commit 83199773dc
1 changed files with 70 additions and 1 deletions

View File

@ -18,6 +18,7 @@ import ConfigParser
import fnmatch
import json
import os
import subprocess
import sys
import tempfile
import urllib2
@ -32,6 +33,7 @@ class CmdCheckHobos(Command):
def execute(self, base_options, sub_options, args):
import publisher
self.base_options = base_options
publisher.WcsPublisher.configure(self.config, sub_options.extra)
pub = publisher.WcsPublisher.create_publisher()
@ -64,6 +66,7 @@ class CmdCheckHobos(Command):
self.configure_site_options(service, pub)
self.update_configuration(service, pub)
self.configure_authentication_methods(service, pub)
self.configure_sql(service, pub)
def update_configuration(self, service, pub):
if not pub.cfg.get('misc'):
@ -121,7 +124,7 @@ class CmdCheckHobos(Command):
metadata_pathname, None, metadata_url, None)
pub.cfg['idp'][key_provider_id]['admin-attributes'] = \
{'role': 'admin::%s' % str(service.get('slug'))}
pub.write_cfg()
pub.write_cfg()
def get_instance_path(self, service):
parsed_url = urllib2.urlparse.urlsplit(service.get('base_url'))
@ -150,4 +153,70 @@ class CmdCheckHobos(Command):
with open(site_options_filepath, 'wb') as site_options:
config.write(site_options)
def configure_sql(self, service, pub):
if not pub.cfg.get('postgresql'):
return
if not pub.has_site_option('postgresql'):
return
import psycopg2
import psycopg2.errorcodes
# determine database name using the instance path; if the template
# database name contained an underscore character, use the first part
# as a prefix to the database name
database_name = pub.cfg['postgresql'].get('database', 'wcs')
domain_table_name = self.get_instance_path(service).replace(
'-', '_').replace('.', '_').replace('+', '_')
if not domain_table_name in database_name:
database_name = '%s_%s' % (database_name.split('_')[0], domain_table_name)
postgresql_cfg = {}
for k, v in pub.cfg['postgresql'].items():
if v:
postgresql_cfg[k] = v
try:
pgconn = psycopg2.connect(**postgresql_cfg)
except psycopg2.Error:
print >> sys.stderr, 'failed to connect to postgresql (%s)' % \
psycopg2.errorcodes.lookup(e.pgcode)
return
pgconn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cur = pgconn.cursor()
new_database = True
try:
cur.execute('''CREATE DATABASE %s''' % database_name)
except psycopg2.Error as e:
if e.pgcode == psycopg2.errorcodes.DUPLICATE_DATABASE:
new_database = False
else:
print >> sys.stderr, 'failed to create database (%s)' % \
psycopg2.errorcodes.lookup(e.pgcode)
return
else:
cur.close()
pub.cfg['postgresql']['database'] = database_name
pub.write_cfg()
if not new_database:
return
cmd = [sys.argv[0]]
if self.base_options.configfile:
cmd.extend(['-f', self.base_options.configfile])
cmd.append('convert-to-sql')
for param in ('database', 'user', 'password', 'host', 'port'):
if postgresql_cfg.get(param):
if param == 'database':
cmd.append('--dbname')
else:
cmd.append('--' + param)
cmd.append(str(postgresql_cfg.get(param)))
cmd.append(str(self.get_instance_path(service)))
subprocess.call(cmd)
CmdCheckHobos.register()