adding postgresql support

This commit is contained in:
yolanda.robla@canonical.com
2014-03-26 09:21:15 +01:00
parent 05eea0ba1d
commit 6ad4381a36
7 changed files with 90 additions and 13 deletions

View File

@@ -146,6 +146,36 @@ class SharedDBContext(OSContextGenerator):
'database': self.database, 'database': self.database,
'database_user': self.user, 'database_user': self.user,
'database_password': passwd, '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): if context_complete(ctxt):
return ctxt return ctxt
@@ -199,7 +229,6 @@ class AMQPContext(OSContextGenerator):
ctxt = {} ctxt = {}
for rid in relation_ids('amqp'): for rid in relation_ids('amqp'):
ha_vip_only = False
for unit in related_units(rid): for unit in related_units(rid):
if relation_get('clustered', rid=rid, unit=unit): if relation_get('clustered', rid=rid, unit=unit):
ctxt['clustered'] = True ctxt['clustered'] = True
@@ -214,18 +243,16 @@ class AMQPContext(OSContextGenerator):
unit=unit), unit=unit),
'rabbitmq_virtual_host': vhost, '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): if context_complete(ctxt):
# Sufficient information found = break out! # Sufficient information found = break out!
break break
# Used for active/active rabbitmq >= grizzly # Used for active/active rabbitmq >= grizzly
if ('clustered' not in ctxt or ha_vip_only) \ if ('clustered' not in ctxt or relation_get('ha-vip-only') == 'True') and \
and len(related_units(rid)) > 1: 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 = [] rabbitmq_hosts = []
for unit in related_units(rid): for unit in related_units(rid):
rabbitmq_hosts.append(relation_get('private-address', rabbitmq_hosts.append(relation_get('private-address',

View File

@@ -22,7 +22,9 @@ from charmhelpers.core.hookenv import (
config, config,
Hooks, Hooks,
log as juju_log, log as juju_log,
ERROR,
open_port, open_port,
is_relation_made,
relation_get, relation_get,
relation_set, relation_set,
relation_ids, relation_ids,
@@ -79,10 +81,29 @@ def install_hook():
@hooks.hook('shared-db-relation-joined') @hooks.hook('shared-db-relation-joined')
def db_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'), relation_set(database=config('database'), username=config('database-user'),
hostname=unit_get('private-address')) 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') @hooks.hook('shared-db-relation-changed')
@restart_on_change(restart_map()) @restart_on_change(restart_map())
def db_changed(): def db_changed():
@@ -108,6 +129,31 @@ def db_changed():
migrate_database() 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') @hooks.hook('image-service-relation-joined')
def image_service_joined(relation_id=None): def image_service_joined(relation_id=None):
if not eligible_leader(CLUSTER_RES): if not eligible_leader(CLUSTER_RES):

View File

@@ -46,7 +46,7 @@ CLUSTER_RES = "res_glance_vip"
PACKAGES = [ PACKAGES = [
"apache2", "glance", "python-mysqldb", "python-swift", "apache2", "glance", "python-mysqldb", "python-swift",
"python-keystone", "uuid", "haproxy", ] "python-psycopg2", "python-keystone", "uuid", "haproxy", ]
SERVICES = [ SERVICES = [
"glance-api", "glance-registry", ] "glance-api", "glance-registry", ]
@@ -75,12 +75,14 @@ def ceph_config_file():
CONFIG_FILES = OrderedDict([ CONFIG_FILES = OrderedDict([
(GLANCE_REGISTRY_CONF, { (GLANCE_REGISTRY_CONF, {
'hook_contexts': [context.SharedDBContext(), 'hook_contexts': [context.SharedDBContext(),
context.PostgresqlDBContext(),
context.IdentityServiceContext(), context.IdentityServiceContext(),
context.SyslogContext()], context.SyslogContext()],
'services': ['glance-registry'] 'services': ['glance-registry']
}), }),
(GLANCE_API_CONF, { (GLANCE_API_CONF, {
'hook_contexts': [context.SharedDBContext(), 'hook_contexts': [context.SharedDBContext(),
context.PostgresqlDBContext(),
context.AMQPContext(), context.AMQPContext(),
context.IdentityServiceContext(), context.IdentityServiceContext(),
glance_contexts.CephGlanceContext(), glance_contexts.CephGlanceContext(),

View File

@@ -14,6 +14,8 @@ provides:
requires: requires:
shared-db: shared-db:
interface: mysql-shared interface: mysql-shared
pgsql-db:
interface: pgsql
amqp: amqp:
interface: rabbitmq interface: rabbitmq
object-store: object-store:

View File

@@ -1 +1 @@
148 149

View File

@@ -20,7 +20,7 @@ bind_port = 9292
log_file = /var/log/glance/api.log log_file = /var/log/glance/api.log
backlog = 4096 backlog = 4096
{% if database_host %} {% 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 %} {% else %}
sql_connection = sqlite:////var/lib/glance/glance.sqlite sql_connection = sqlite:////var/lib/glance/glance.sqlite
{% endif %} {% endif %}

View File

@@ -7,7 +7,7 @@ bind_port = 9191
log_file = /var/log/glance/registry.log log_file = /var/log/glance/registry.log
backlog = 4096 backlog = 4096
{% if database_host %} {% 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 %} {% endif %}
sql_idle_timeout = 3600 sql_idle_timeout = 3600
api_limit_max = 1000 api_limit_max = 1000