From 6ad4381a36b907d06f7229b4bf6879440bb4c580 Mon Sep 17 00:00:00 2001 From: "yolanda.robla@canonical.com" <> Date: Wed, 26 Mar 2014 09:21:15 +0100 Subject: [PATCH] adding postgresql support --- .../charmhelpers/contrib/openstack/context.py | 45 ++++++++++++++---- hooks/glance_relations.py | 46 +++++++++++++++++++ hooks/glance_utils.py | 4 +- metadata.yaml | 2 + revision | 2 +- templates/folsom/glance-api.conf | 2 +- templates/glance-registry.conf | 2 +- 7 files changed, 90 insertions(+), 13 deletions(-) diff --git a/hooks/charmhelpers/contrib/openstack/context.py b/hooks/charmhelpers/contrib/openstack/context.py index 6014ee1b..d547a4fb 100644 --- a/hooks/charmhelpers/contrib/openstack/context.py +++ b/hooks/charmhelpers/contrib/openstack/context.py @@ -146,6 +146,36 @@ class SharedDBContext(OSContextGenerator): 'database': self.database, 'database_user': self.user, 'database_password': passwd, + 'database_type': 'mysql', + } + if context_complete(ctxt): + return ctxt + return {} + + +class PostgresqlDBContext(OSContextGenerator): + interfaces = ['pgsql-db'] + + def __init__(self, database=None): + self.database = database + + def __call__(self): + self.database = self.database or config('database') + if self.database is None: + log('Could not generate postgresql_db context. ' + 'Missing required charm config options. ' + '(database name)') + raise OSContextError + ctxt = {} + + for rid in relation_ids(self.interfaces[0]): + for unit in related_units(rid): + ctxt = { + 'database_host': relation_get('host', rid=rid, unit=unit), + 'database': self.database, + 'database_user': relation_get('user', rid=rid, unit=unit), + 'database_password': relation_get('password', rid=rid, unit=unit), + 'database_type': 'postgresql', } if context_complete(ctxt): return ctxt @@ -199,7 +229,6 @@ class AMQPContext(OSContextGenerator): ctxt = {} for rid in relation_ids('amqp'): - ha_vip_only = False for unit in related_units(rid): if relation_get('clustered', rid=rid, unit=unit): ctxt['clustered'] = True @@ -214,18 +243,16 @@ class AMQPContext(OSContextGenerator): unit=unit), 'rabbitmq_virtual_host': vhost, }) - if relation_get('ha_queues', rid=rid, unit=unit) is not None: - ctxt['rabbitmq_ha_queues'] = True - - ha_vip_only = relation_get('ha-vip-only', - rid=rid, unit=unit) is not None - if context_complete(ctxt): # Sufficient information found = break out! break # Used for active/active rabbitmq >= grizzly - if ('clustered' not in ctxt or ha_vip_only) \ - and len(related_units(rid)) > 1: + if ('clustered' not in ctxt or relation_get('ha-vip-only') == 'True') and \ + len(related_units(rid)) > 1: + if relation_get('ha_queues'): + ctxt['rabbitmq_ha_queues'] = relation_get('ha_queues') + else: + ctxt['rabbitmq_ha_queues'] = False rabbitmq_hosts = [] for unit in related_units(rid): rabbitmq_hosts.append(relation_get('private-address', diff --git a/hooks/glance_relations.py b/hooks/glance_relations.py index 7a7f7658..dbdb267d 100755 --- a/hooks/glance_relations.py +++ b/hooks/glance_relations.py @@ -22,7 +22,9 @@ from charmhelpers.core.hookenv import ( config, Hooks, log as juju_log, + ERROR, open_port, + is_relation_made, relation_get, relation_set, relation_ids, @@ -79,10 +81,29 @@ def install_hook(): @hooks.hook('shared-db-relation-joined') def db_joined(): + if is_relation_made('pgsql-db'): + # error, postgresql is used + e = ('Attempting to associate a mysql database when there is already ' + 'associated a postgresql one') + juju_log(e, level=ERROR) + raise Exception(e) + relation_set(database=config('database'), username=config('database-user'), hostname=unit_get('private-address')) +@hooks.hook('pgsql-db-relation-joined') +def pgsql_db_joined(): + if is_relation_made('shared-db'): + # raise error + e = ('Attempting to associate a postgresql database when there is already ' + 'associated a mysql one') + juju_log(e, level=ERROR) + raise Exception(e) + + relation_set(database=config('database')) + + @hooks.hook('shared-db-relation-changed') @restart_on_change(restart_map()) def db_changed(): @@ -108,6 +129,31 @@ def db_changed(): migrate_database() +@hooks.hook('pgsql-db-relation-changed') +@restart_on_change(restart_map()) +def pgsql_db_changed(): + rel = get_os_codename_package("glance-common") + + if 'pgsql-db' not in CONFIGS.complete_contexts(): + juju_log('pgsql-db relation incomplete. Peer not ready?') + return + + CONFIGS.write(GLANCE_REGISTRY_CONF) + # since folsom, a db connection setting in glance-api.conf is required. + if rel != "essex": + CONFIGS.write(GLANCE_API_CONF) + + if eligible_leader(CLUSTER_RES): + if rel == "essex": + status = call(['glance-manage', 'db_version']) + if status != 0: + juju_log('Setting version_control to 0') + check_call(["glance-manage", "version_control", "0"]) + + juju_log('Cluster leader, performing db sync') + migrate_database() + + @hooks.hook('image-service-relation-joined') def image_service_joined(relation_id=None): if not eligible_leader(CLUSTER_RES): diff --git a/hooks/glance_utils.py b/hooks/glance_utils.py index 26b5b2d5..a9050672 100755 --- a/hooks/glance_utils.py +++ b/hooks/glance_utils.py @@ -46,7 +46,7 @@ CLUSTER_RES = "res_glance_vip" PACKAGES = [ "apache2", "glance", "python-mysqldb", "python-swift", - "python-keystone", "uuid", "haproxy", ] + "python-psycopg2", "python-keystone", "uuid", "haproxy", ] SERVICES = [ "glance-api", "glance-registry", ] @@ -75,12 +75,14 @@ def ceph_config_file(): CONFIG_FILES = OrderedDict([ (GLANCE_REGISTRY_CONF, { 'hook_contexts': [context.SharedDBContext(), + context.PostgresqlDBContext(), context.IdentityServiceContext(), context.SyslogContext()], 'services': ['glance-registry'] }), (GLANCE_API_CONF, { 'hook_contexts': [context.SharedDBContext(), + context.PostgresqlDBContext(), context.AMQPContext(), context.IdentityServiceContext(), glance_contexts.CephGlanceContext(), diff --git a/metadata.yaml b/metadata.yaml index 0d5ba63d..c9580fff 100644 --- a/metadata.yaml +++ b/metadata.yaml @@ -14,6 +14,8 @@ provides: requires: shared-db: interface: mysql-shared + pgsql-db: + interface: pgsql amqp: interface: rabbitmq object-store: diff --git a/revision b/revision index 0d667b5e..15c44e93 100644 --- a/revision +++ b/revision @@ -1 +1 @@ -148 +149 diff --git a/templates/folsom/glance-api.conf b/templates/folsom/glance-api.conf index 690e4863..b35457b9 100644 --- a/templates/folsom/glance-api.conf +++ b/templates/folsom/glance-api.conf @@ -20,7 +20,7 @@ bind_port = 9292 log_file = /var/log/glance/api.log backlog = 4096 {% if database_host %} -sql_connection = mysql://{{ database_user }}:{{ database_password }}@{{ database_host }}/{{ database }} +sql_connection = {{ database_type }}://{{ database_user }}:{{ database_password }}@{{ database_host }}/{{ database }} {% else %} sql_connection = sqlite:////var/lib/glance/glance.sqlite {% endif %} diff --git a/templates/glance-registry.conf b/templates/glance-registry.conf index b9b90904..1247963a 100644 --- a/templates/glance-registry.conf +++ b/templates/glance-registry.conf @@ -7,7 +7,7 @@ bind_port = 9191 log_file = /var/log/glance/registry.log backlog = 4096 {% if database_host %} -sql_connection = mysql://{{ database_user }}:{{ database_password }}@{{ database_host }}/{{ database }} +sql_connection = {{ database_type }}://{{ database_user }}:{{ database_password }}@{{ database_host }}/{{ database }} {% endif %} sql_idle_timeout = 3600 api_limit_max = 1000