~ionutbalutoiu/charms/trusty/neutron-api/next

« back to all changes in this revision

Viewing changes to hooks/neutron_api_utils.py

  • Committer: Liam Young
  • Date: 2014-06-05 10:59:00 UTC
  • Revision ID: liam.young@canonical.com-20140605105900-j96iaknh0eq4cyus
Principle for Neutron API

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from collections import OrderedDict
 
2
from copy import deepcopy
 
3
import ConfigParser
 
4
import os
 
5
from base64 import b64encode
 
6
from charmhelpers.contrib.openstack import context, templating
 
7
from charmhelpers.contrib.openstack.neutron import (
 
8
    network_manager, neutron_plugin_attribute)
 
9
 
 
10
from charmhelpers.contrib.openstack.utils import (
 
11
    os_release,
 
12
)
 
13
 
 
14
from charmhelpers.core.hookenv import (
 
15
    config,
 
16
)
 
17
 
 
18
import neutron_api_context
 
19
 
 
20
TEMPLATES = 'templates/'
 
21
 
 
22
CLUSTER_RES = 'res_nova_vip'
 
23
 
 
24
# removed from original: charm-helper-sh
 
25
BASE_PACKAGES = [
 
26
    'python-keystoneclient',
 
27
    'python-mysqldb',
 
28
    'python-psycopg2',
 
29
    'uuid',
 
30
]
 
31
 
 
32
BASE_SERVICES = [
 
33
    'neutron-server'
 
34
]
 
35
API_PORTS = {
 
36
    'neutron-server': 9696,
 
37
}
 
38
 
 
39
NEUTRON_CONF_DIR = "/etc/neutron"
 
40
 
 
41
NEUTRON_CONF = '%s/neutron.conf' % NEUTRON_CONF_DIR
 
42
HAPROXY_CONF = '/etc/haproxy/haproxy.cfg'
 
43
APACHE_CONF = '/etc/apache2/sites-available/openstack_https_frontend'
 
44
APACHE_24_CONF = '/etc/apache2/sites-available/openstack_https_frontend.conf'
 
45
NEUTRON_DEFAULT = '/etc/default/neutron-server'
 
46
CA_CERT_PATH = '/usr/local/share/ca-certificates/keystone_juju_ca_cert.crt'
 
47
 
 
48
BASE_RESOURCE_MAP = OrderedDict([
 
49
    (NEUTRON_CONF, {
 
50
        'services': ['neutron-server'],
 
51
        'contexts': [context.AMQPContext(ssl_dir=NEUTRON_CONF_DIR),
 
52
                     context.SharedDBContext(
 
53
                         user=config('neutron-database-user'),
 
54
                         database=config('neutron-database'),
 
55
                         relation_prefix='neutron',
 
56
                         ssl_dir=NEUTRON_CONF_DIR),
 
57
                     neutron_api_context.NeutronPostgresqlDBContext(),
 
58
                     neutron_api_context.IdentityServiceContext(),
 
59
                     neutron_api_context.NeutronCCContext(),
 
60
                     context.SyslogContext()],
 
61
    }),
 
62
    (NEUTRON_DEFAULT, {
 
63
        'services': ['neutron-server'],
 
64
        'contexts': [neutron_api_context.NeutronCCContext()],
 
65
    }),
 
66
])
 
67
def api_port(service):
 
68
    return API_PORTS[service]
 
69
 
 
70
def determine_endpoints(url):
 
71
    '''Generates a dictionary containing all relevant endpoints to be
 
72
    passed to keystone as relation settings.'''
 
73
    region = config('region')
 
74
 
 
75
    neutron_url = '%s:%s' % (url, api_port('neutron-server'))
 
76
 
 
77
    endpoints = ({
 
78
        'quantum_service': 'quantum',
 
79
        'quantum_region': region,
 
80
        'quantum_public_url': neutron_url,
 
81
        'quantum_admin_url': neutron_url,
 
82
        'quantum_internal_url': neutron_url,
 
83
    })
 
84
 
 
85
 
 
86
    return endpoints
 
87
 
 
88
 
 
89
def determine_packages():
 
90
    # currently all packages match service names
 
91
    packages = [] + BASE_PACKAGES
 
92
    for v in resource_map().values():
 
93
        packages.extend(v['services'])
 
94
        pkgs = neutron_plugin_attribute(config('neutron-plugin'), 'server_packages',
 
95
                                        network_manager())
 
96
        packages.extend(pkgs)
 
97
    return list(set(packages))
 
98
 
 
99
def determine_ports():
 
100
    '''Assemble a list of API ports for services we are managing'''
 
101
    ports = []
 
102
    for services in restart_map().values():
 
103
        for service in services:
 
104
            try:
 
105
                ports.append(API_PORTS[service])
 
106
            except KeyError:
 
107
                pass
 
108
    return list(set(ports))
 
109
 
 
110
 
 
111
def resource_map():
 
112
    '''
 
113
    Dynamically generate a map of resources that will be managed for a single
 
114
    hook execution.
 
115
    '''
 
116
    resource_map = deepcopy(BASE_RESOURCE_MAP)
 
117
 
 
118
    net_manager = network_manager()
 
119
 
 
120
    # add neutron plugin requirements. nova-c-c only needs the neutron-server
 
121
    # associated with configs, not the plugin agent.
 
122
    plugin = config('neutron-plugin')
 
123
    conf = neutron_plugin_attribute(plugin, 'config', net_manager)
 
124
    ctxts = (neutron_plugin_attribute(plugin, 'contexts', net_manager)
 
125
             or [])
 
126
    services = neutron_plugin_attribute(plugin, 'server_services',
 
127
                                        net_manager)
 
128
    resource_map[conf] = {}
 
129
    resource_map[conf]['services'] = services
 
130
    resource_map[conf]['contexts'] = ctxts
 
131
    resource_map[conf]['contexts'].append(
 
132
        neutron_api_context.NeutronCCContext())
 
133
 
 
134
    # update for postgres
 
135
    resource_map[conf]['contexts'].append(
 
136
        neutron_api_context.NeutronPostgresqlDBContext())
 
137
 
 
138
    return resource_map
 
139
 
 
140
 
 
141
def register_configs(release=None):
 
142
    release = release or os_release('nova-common')
 
143
    configs = templating.OSConfigRenderer(templates_dir=TEMPLATES,
 
144
                                          openstack_release=release)
 
145
    for cfg, rscs in resource_map().iteritems():
 
146
        configs.register(cfg, rscs['contexts'])
 
147
    return configs
 
148
 
 
149
 
 
150
def restart_map():
 
151
    return OrderedDict([(cfg, v['services'])
 
152
                        for cfg, v in resource_map().iteritems()
 
153
                        if v['services']])
 
154
 
 
155
def auth_token_config(setting):                                                                                                                                                                               
 
156
    """
 
157
    Returns currently configured value for setting in api-paste.ini's
 
158
    authtoken section, or None.
 
159
    """
 
160
    config = ConfigParser.RawConfigParser()
 
161
    config.read('/etc/neutron/api-paste.ini')
 
162
    try:
 
163
        value = config.get('filter:authtoken', setting)
 
164
    except:
 
165
        return None
 
166
    if value.startswith('%'):
 
167
        return None
 
168
    return value
 
169
 
 
170
def keystone_ca_cert_b64():                                                                                                                                                                                   
 
171
    '''Returns the local Keystone-provided CA cert if it exists, or None.'''
 
172
    if not os.path.isfile(CA_CERT_PATH):
 
173
        return None
 
174
    with open(CA_CERT_PATH) as _in:
 
175
        return b64encode(_in.read())