~brad-marshall/charms/trusty/heat/add-nrpe-checks

« back to all changes in this revision

Viewing changes to hooks/heat_context.py

  • Committer: yolanda.robla at canonical
  • Date: 2013-12-04 17:04:42 UTC
  • Revision ID: yolanda.robla@canonical.com-20131204170442-j1p6t1azf0zu390o
using pwgen for keystone authkey
added more unit tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import subprocess
2
1
import os
3
2
 
 
3
from charmhelpers.contrib.openstack import context
4
4
from charmhelpers.core.hookenv import config
5
 
from charmhelpers.contrib.openstack import context
 
5
from charmhelpers.core.host import pwgen
 
6
 
6
7
 
7
8
HEAT_PATH = '/var/lib/heat/'
8
9
 
9
10
 
10
 
class IdentityServiceContext(context.IdentityServiceContext):
 
11
def generate_ec2_tokens(host, port):
 
12
    ec2_tokens = 'http://%s:%s/v2.0/ec2tokens' % (host, port)
 
13
    return ec2_tokens
 
14
 
 
15
 
 
16
class HeatIdentityServiceContext(context.IdentityServiceContext):
11
17
    def __call__(self):
12
 
        ctxt = super(IdentityServiceContext, self).__call__()
 
18
        ctxt = super(HeatIdentityServiceContext, self).__call__()
13
19
        if not ctxt:
14
20
            return
15
21
 
16
22
        # the ec2 api needs to know the location of the keystone ec2
17
23
        # tokens endpoint, set in nova.conf
18
 
        ec2_tokens = 'http://%s:%s/v2.0/ec2tokens' % (ctxt['service_host'],
19
 
                                                      ctxt['service_port'])
 
24
        ec2_tokens = generate_ec2_tokens(ctxt['service_host'],
 
25
                                         ctxt['service_port'])
20
26
        ctxt['keystone_ec2_url'] = ec2_tokens
21
27
        return ctxt
22
28
 
23
29
 
 
30
def get_encryption_key():
 
31
    encryption_path = os.path.join(HEAT_PATH, 'encryption-key')
 
32
    if os.path.isfile(encryption_path):
 
33
        with open(encryption_path, 'r') as enc:
 
34
            encryption = enc.read()
 
35
    else:
 
36
        # create encryption key and store it
 
37
        if not os.path.isdir(HEAT_PATH):
 
38
            os.makedirs(HEAT_PATH)
 
39
        encryption = config("encryption-key")
 
40
        if not encryption:
 
41
            # generate random key
 
42
            encryption = pwgen(16)
 
43
        with open(encryption_path, 'w') as enc:
 
44
            enc.write(encryption)
 
45
 
 
46
 
24
47
class EncryptionContext(context.OSContextGenerator):
 
48
 
25
49
    def __call__(self):
26
50
        ctxt = {}
27
51
 
28
52
        # check if we have stored encryption key
29
 
        encryption_path = os.path.join(HEAT_PATH, 'encryption-key')
30
 
        if os.path.isfile(encryption_path):
31
 
            with open(encryption_path, 'r') as enc:
32
 
                encryption = enc.read()
33
 
        else:
34
 
            # create encryption key and store it
35
 
            if not os.path.isdir(HEAT_PATH):
36
 
                os.makedirs(HEAT_PATH)
37
 
            encryption = config("encryption-key")
38
 
            if not encryption:
39
 
                # generate random key
40
 
                cmd = 'hexdump -n 16 -v -e \'/1 "%02x"\' /dev/random'
41
 
                encryption = subprocess.check_output(cmd, shell=True).strip()
42
 
            with open(encryption_path, 'w') as enc:
43
 
                enc.write(encryption)
44
 
 
 
53
        encryption = get_encryption_key()
45
54
        ctxt['encryption_key'] = encryption
46
55
        return ctxt