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

« back to all changes in this revision

Viewing changes to hooks/ceilometer_contexts.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.core.hookenv import (
 
4
    relation_ids,
 
5
    relation_get,
 
6
    related_units,
 
7
    config
 
8
)
 
9
 
 
10
from charmhelpers.contrib.openstack.context import (
 
11
    OSContextGenerator,
 
12
    context_complete
 
13
)
 
14
 
 
15
CEILOMETER_DB = 'ceilometer'
 
16
 
 
17
 
 
18
class LoggingConfigContext(OSContextGenerator):
 
19
    def __call__(self):
 
20
        return {'debug': config('debug'), 'verbose': config('verbose')}
 
21
 
 
22
 
 
23
class MongoDBContext(OSContextGenerator):
 
24
    interfaces = ['mongodb']
 
25
 
 
26
    def __call__(self):
 
27
        for relid in relation_ids('shared-db'):
 
28
            for unit in related_units(relid):
 
29
                conf = {
 
30
                    "db_host": relation_get('hostname', unit, relid),
 
31
                    "db_port": relation_get('port', unit, relid),
 
32
                    "db_name": CEILOMETER_DB
 
33
                }
 
34
                if context_complete(conf):
 
35
                    return conf
 
36
        return {}
 
37
 
 
38
 
 
39
SHARED_SECRET = "/etc/ceilometer/secret.txt"
 
40
 
 
41
 
 
42
def get_shared_secret():
 
43
    secret = None
 
44
    if not os.path.exists(SHARED_SECRET):
 
45
        secret = str(uuid.uuid4())
 
46
        with open(SHARED_SECRET, 'w') as secret_file:
 
47
            secret_file.write(secret)
 
48
    else:
 
49
        with open(SHARED_SECRET, 'r') as secret_file:
 
50
            secret = secret_file.read().strip()
 
51
    return secret
 
52
 
 
53
CEILOMETER_PORT = 8777
 
54
 
 
55
 
 
56
class CeilometerContext(OSContextGenerator):
 
57
    def __call__(self):
 
58
        ctxt = {
 
59
            'port': CEILOMETER_PORT,
 
60
            'metering_secret': get_shared_secret()
 
61
        }
 
62
        return ctxt