Add context generator for handling config-flags charm config setting.

This commit is contained in:
Adam Gandelman 2013-08-01 12:53:09 -07:00
parent f239ef80f0
commit 3088213f27
8 changed files with 74 additions and 11 deletions

View File

@ -71,7 +71,6 @@ options:
type: string
description: Network interface on which to build bridge
config-flags:
default: None
type: string
description: Comma separated list of key=value config flags to be set in nova.conf.
nagios_context:

View File

@ -207,7 +207,7 @@ class HAProxyContext(OSContextGenerator):
class ImageServiceContext(OSContextGenerator):
interfaces = ['image-servce']
interfaces = ['image-service']
def __call__(self):
'''

View File

@ -9,6 +9,7 @@ from charmhelpers.core.hookenv import (
relation_ids,
unit_private_ip,
ERROR,
WARNING,
)
from charmhelpers.contrib.openstack.utils import get_os_codename_package
@ -38,8 +39,7 @@ class NovaComputeLibvirtContext(context.OSContextGenerator):
def __call__(self):
# enable tcp listening if configured for live migration.
migration = config('enable-live-migration')
if migration and migration.lower() == 'true':
if config('enable-live-migration'):
opts = '-d -l'
else:
opts = '-d'
@ -75,7 +75,9 @@ class CloudComputeContext(context.OSContextGenerator):
def _ensure_packages(self, packages):
'''Install but do not upgrade required packages'''
apt_install(filter_installed_packages(packages))
required = filter_installed_packages(packages)
if required:
apt_install(required)
def flat_dhcp_context(self):
ec2_host = relation_get('ec2_host')
@ -131,6 +133,7 @@ class CloudComputeContext(context.OSContextGenerator):
raise
return vol_ctxt
def __call__(self):
rids = relation_ids('cloud-compute')
if not rids:
@ -153,14 +156,40 @@ class CloudComputeContext(context.OSContextGenerator):
vol_service = self.volume_context()
if vol_service:
ctxt.update({'volume_service_config': vol_service})
return ctxt
class OSConfigFlagContext(context.OSContextGenerator):
'''
Responsible adding user-defined config-flags in charm config to a
to a template context.
'''
# this can be moved to charm-helpers?
def __call__(self):
config_flags = config('config-flags')
if not config_flags:
return {}
config_flags = config_flags.split(',')
flags = {}
for flag in config_flags:
if '=' not in flag:
log('Impoperly formatted config-flag, expected k=v '
' got %s' % flag, level=WARNING)
continue
k, v = flag.split('=')
flags[k.strip()] = v
ctxt = {'user_config_flags': flags}
return ctxt
class QuantumPluginContext(context.OSContextGenerator):
interfaces = []
def _ensure_packages(self, packages):
'''Install but do not upgrade required plugin packages'''
apt_install(filter_installed_packages(packages))
required = filter_installed_packages(packages)
if required:
apt_install(required)
def ovs_context(self):
q_driver = 'quantum.plugins.openvswitch.ovs_quantum_plugin.'\

View File

@ -17,6 +17,7 @@ from charmhelpers.core.hookenv import (
from charmhelpers.core.host import (
apt_install,
apt_update,
filter_installed_packages,
restart_on_change,
)
@ -142,7 +143,7 @@ def compute_changed():
def ceph_joined():
if not os.path.isdir('/etc/ceph'):
os.mkdir('/etc/ceph')
apt_install('ceph-common')
apt_install(filter_installed_packages('ceph-common'))
@hooks.hook('ceph-relation-changed')

View File

@ -21,6 +21,7 @@ from nova_compute_context import (
CloudComputeContext,
NovaComputeLibvirtContext,
NovaComputeCephContext,
OSConfigFlagContext,
QuantumPluginContext,
)
@ -49,6 +50,7 @@ BASE_RESOURCE_MAP = {
context.ImageServiceContext(),
CloudComputeContext(),
NovaComputeCephContext(),
OSConfigFlagContext(),
QuantumPluginContext()]
},
}
@ -225,7 +227,7 @@ def quantum_attribute(plugin, attr):
def public_ssh_key(user='root'):
home = pwd.getpwnam(user).pw_dir
try:
with open(os.path.join(home, '.ssh', 'id_rsa')) as key:
with open(os.path.join(home, '.ssh', 'id_rsa.pub')) as key:
return key.read().strip()
except:
return None

View File

@ -1 +1 @@
99
105

View File

@ -1,4 +1,8 @@
# juju managed
###############################################################################
# [ WARNING ]
# cinder configuration file maintained by Juju
# local changes may be overwritten.
###############################################################################
[DEFAULT]
dhcpbridge_flagfile=/etc/nova/nova.conf
dhcpbridge=/usr/bin/nova-dhcpbridge
@ -48,5 +52,9 @@ nova_firewall_driver = nova.virt.firewall.NoopFirewallDriver
{% for key, value in volume_service_config.iteritems() -%}
{{ key }} = {{ value }}
{% endfor -%}
{% endif -%}
{% if user_config_flags -%}
{% for key, value in user_config_flags.iteritems() -%}
{{ key }} = {{ value }}
{% endfor -%}
{% endif -%}

View File

@ -172,3 +172,27 @@ class NovaComputeContextTests(CharmTestCase):
libvirt = context.NovaComputeLibvirtContext()
self.assertEquals({'libvirtd_opts': '-d -l'}, libvirt())
def test_config_flag_context_none_set_in_config(self):
flags = context.OSConfigFlagContext()
self.assertEquals({}, flags())
def test_conflig_flag_context(self):
self.test_config.set('config-flags', 'one=two,three=four,five=six')
flags = context.OSConfigFlagContext()
ex = {
'user_config_flags': {
'one': 'two', 'three': 'four', 'five': 'six'
}
}
self.assertEquals(ex, flags())
def test_conflig_flag_context_filters_bad_input(self):
self.test_config.set('config-flags', 'one=two,threefour,five=six')
flags = context.OSConfigFlagContext()
ex = {
'user_config_flags': {
'one': 'two', 'five': 'six'
}
}
self.assertEquals(ex, flags())