~moon127/charms/trusty/ceilometer/add-execd-preinstall

« back to all changes in this revision

Viewing changes to hooks/ceilometer_utils.py

  • Committer: James Page
  • Date: 2013-10-18 13:38:01 UTC
  • Revision ID: james.page@canonical.com-20131018133801-hfj9nj0hr55knly6
Switch to using openstack context templating

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import os
2
 
import uuid
3
 
from charmhelpers.fetch import apt_install as install
 
1
from charmhelpers.contrib.openstack import (
 
2
    templating,
 
3
    context,
 
4
)
 
5
from ceilometer_contexts import (
 
6
    LoggingConfigContext,
 
7
    MongoDBContext,
 
8
    CeilometerContext
 
9
)
 
10
from charmhelpers.contrib.openstack.utils import (
 
11
    get_os_codename_package
 
12
)
4
13
 
5
 
RABBIT_USER = "ceilometer"
6
 
RABBIT_VHOST = "openstack"
7
14
CEILOMETER_CONF = "/etc/ceilometer/ceilometer.conf"
8
15
 
9
 
SHARED_SECRET = "/etc/ceilometer/secret.txt"
10
16
CEILOMETER_SERVICES = [
11
 
    'ceilometer-agent-central', 'ceilometer-collector',
 
17
    'ceilometer-agent-central',
 
18
    'ceilometer-collector',
12
19
    'ceilometer-api'
13
20
]
 
21
 
14
22
CEILOMETER_DB = "ceilometer"
15
23
CEILOMETER_SERVICE = "ceilometer"
16
 
CEILOMETER_COMPUTE_SERVICES = ['ceilometer-agent-compute']
 
24
 
17
25
CEILOMETER_PACKAGES = [
18
 
    'python-ceilometer', 'ceilometer-common',
19
 
    'ceilometer-agent-central', 'ceilometer-collector', 'ceilometer-api'
20
 
]
21
 
CEILOMETER_AGENT_PACKAGES = [
22
 
    'python-ceilometer', 'ceilometer-common',
23
 
    'ceilometer-agent-compute'
24
 
]
25
 
CEILOMETER_PORT = 8777
 
26
    'ceilometer-agent-central',
 
27
    'ceilometer-collector',
 
28
    'ceilometer-api'
 
29
]
 
30
 
26
31
CEILOMETER_ROLE = "ResellerAdmin"
27
32
 
28
 
NOVA_CONF = "/etc/nova/nova.conf"
29
 
NOVA_SETTINGS = [
30
 
    ('DEFAULT', 'instance_usage_audit', 'True'),
31
 
    ('DEFAULT', 'instance_usage_audit_period', 'hour'),
32
 
    ('DEFAULT', 'notification_driver', 'ceilometer.compute.nova_notifier')
33
 
]
34
 
 
35
 
 
36
 
def get_shared_secret():
37
 
    secret = None
38
 
    if not os.path.exists(SHARED_SECRET):
39
 
        secret = str(uuid.uuid4())
40
 
        with open(SHARED_SECRET, 'w') as secret_file:
41
 
            secret_file.write(secret)
42
 
    else:
43
 
        with open(SHARED_SECRET, 'r') as secret_file:
44
 
            secret = secret_file.read().strip()
45
 
    return secret
46
 
 
47
 
 
48
 
TEMPLATES_DIR = 'templates'
49
 
 
50
 
try:
51
 
    import jinja2
52
 
except ImportError:
53
 
    install(['python-jinja2'])
54
 
    import jinja2
55
 
 
56
 
 
57
 
def render_template(template_name, context, template_dir=TEMPLATES_DIR):
58
 
    templates = \
59
 
        jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir))
60
 
    template = templates.get_template(template_name)
61
 
    return template.render(context)
 
33
#NOVA_CONF = "/etc/nova/nova.conf"
 
34
#NOVA_SETTINGS = [
 
35
#    ('DEFAULT', 'instance_usage_audit', 'True'),
 
36
#    ('DEFAULT', 'instance_usage_audit_period', 'hour'),
 
37
#    ('DEFAULT', 'notification_driver', 'ceilometer.compute.nova_notifier')
 
38
#]
 
39
 
 
40
CONFIG_FILES = {
 
41
    CEILOMETER_CONF: {
 
42
        'hook_contexts': [context.IdentityServiceContext(),
 
43
                          context.AMQPContext(),
 
44
                          LoggingConfigContext(),
 
45
                          MongoDBContext(),
 
46
                          CeilometerContext()],
 
47
        'services': CEILOMETER_SERVICES
 
48
    }
 
49
}
 
50
 
 
51
TEMPLATES = 'templates'
 
52
 
 
53
 
 
54
def register_configs():
 
55
    """
 
56
    Register config files with their respective contexts.
 
57
    Regstration of some configs may not be required depending on
 
58
    existing of certain relations.
 
59
    """
 
60
    # if called without anything installed (eg during install hook)
 
61
    # just default to earliest supported release. configs dont get touched
 
62
    # till post-install, anyway.
 
63
    release = get_os_codename_package('ceilometer-common', fatal=False) \
 
64
                or 'grizzly'
 
65
    configs = templating.OSConfigRenderer(templates_dir=TEMPLATES,
 
66
                                          openstack_release=release)
 
67
 
 
68
    for conf in CONFIG_FILES:
 
69
        configs.register(conf, CONFIG_FILES[conf]['hook_contexts'])
 
70
 
 
71
    return configs
 
72
 
 
73
 
 
74
def restart_map():
 
75
    '''
 
76
    Determine the correct resource map to be passed to
 
77
    charmhelpers.core.restart_on_change() based on the services configured.
 
78
 
 
79
    :returns: dict: A dictionary mapping config file to lists of services
 
80
                    that should be restarted when file changes.
 
81
    '''
 
82
    _map = {}
 
83
    for f, ctxt in CONFIG_FILES.iteritems():
 
84
        svcs = []
 
85
        for svc in ctxt['services']:
 
86
            svcs.append(svc)
 
87
        if svcs:
 
88
            _map[f] = svcs
 
89
    return _map
 
90
 
 
91
 
 
92
def get_ceilometer_context():
 
93
    ''' Retrieve a map of all current relation data for agent configuration '''
 
94
    ctxt = {}
 
95
    for context in CONFIG_FILES[CEILOMETER_CONF]['hook_contexts']:
 
96
        ctxt.update(context())
 
97
    return ctxt