~james-page/charms/quantal/quantum/trunk

« back to all changes in this revision

Viewing changes to hooks/quantum_utils.py

  • Committer: James Page
  • Date: 2012-11-07 12:11:38 UTC
  • Revision ID: james.page@canonical.com-20121107121138-8j3va4u11qp9tetx
Refactoring to include external network setup as part of charm

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
 
1
import utils
2
2
import subprocess
3
3
 
 
4
try:
 
5
    from quantumclient.v2_0 import client
 
6
except ImportError:
 
7
    utils.install('python-quantumclient')
 
8
    from quantumclient.v2_0 import client
 
9
 
 
10
 
4
11
OVS_PLUGIN = \
5
12
    "quantum.plugins.openvswitch.ovs_quantum_plugin.OVSQuantumPluginV2"
6
13
NVP_PLUGIN = \
19
26
    "nvp": NVP_PLUGIN_CONF
20
27
    }
21
28
 
 
29
PLUGIN_PKGS = {
 
30
    "ovs": [
 
31
        "quantum-plugin-openvswitch",
 
32
        "quantum-plugin-openvswitch-agent",
 
33
        "quantum-l3-agent",
 
34
        "quantum-dhcp-agent"
 
35
        ],
 
36
    "nvp": [  # No agent required
 
37
        "quantum-plugin-nicira"
 
38
        ]
 
39
    }
 
40
 
 
41
PLUGIN_AGENT = {
 
42
    "ovs": [
 
43
        "quantum-plugin-openvswitch-agent",
 
44
        ]
 
45
    }
 
46
 
 
47
GATEWAY_AGENTS = {
 
48
    "ovs": [
 
49
        "quantum-server",
 
50
        "quantum-l3-agent",
 
51
        "quantum-dhcp-agent"
 
52
        ]
 
53
    }
 
54
 
22
55
DB_USER = "quantum"
23
56
QUANTUM_DB = "quantum"
 
57
KEYSTONE_SERVICE = "quantum"
24
58
 
25
59
QUANTUM_CONF = "/etc/quantum/quantum.conf"
26
60
L3_AGENT_CONF = "/etc/quantum/l3_agent.ini"
30
64
RABBIT_USER = "nova"
31
65
RABBIT_VHOST = "nova"
32
66
 
 
67
EXT_BRIDGE = 'br-ex'
 
68
INT_BRIDGE = 'br-int'
 
69
 
33
70
 
34
71
def add_bridge(name):
35
72
    status = subprocess.check_output(["ovs-vsctl", "show"])
36
73
    if "Bridge {}".format(name) not in status:
 
74
        utils.juju_log('INFO',
 
75
                       'Creating bridge {}'.format(name))
37
76
        subprocess.check_call(["ovs-vsctl", "add-br", name])
38
77
 
39
78
 
40
79
def del_bridge(name):
41
80
    status = subprocess.check_output(["ovs-vsctl", "show"])
42
81
    if "Bridge {}".format(name) in status:
 
82
        utils.juju_log('INFO',
 
83
                       'Deleting bridge {}'.format(name))
43
84
        subprocess.check_call(["ovs-vsctl", "del-br", name])
44
85
 
45
86
 
47
88
    status = subprocess.check_output(["ovs-vsctl", "show"])
48
89
    if ("Bridge {}".format(name) in status and
49
90
        "Interface \"{}\"".format(port) not in status):
 
91
        utils.juju_log('INFO',
 
92
                       'Adding port {} to bridge {}'
 
93
                        .format(port, name))
50
94
        subprocess.check_call(["ovs-vsctl", "add-port", name, port])
 
95
        subprocess.check_call(["ip", "link", "set", port, "up"])
51
96
 
52
97
 
53
98
def del_bridge_port(name, port):
54
99
    status = subprocess.check_output(["ovs-vsctl", "show"])
55
100
    if ("Bridge {}".format(name) in status and
56
101
        "Interface \"{}\"".format(port) in status):
 
102
        utils.juju_log('INFO',
 
103
                       'Deleting port {} from bridge {}'
 
104
                        .format(port, name))
57
105
        subprocess.check_call(["ovs-vsctl", "del-port", name, port])
 
106
        subprocess.check_call(["ip", "link", "set", port, "down"])
 
107
 
 
108
 
 
109
def configure_ext_net(username,
 
110
                      password,
 
111
                      tenant,
 
112
                      url,
 
113
                      ext_net_name,
 
114
                      gateway_ip,
 
115
                      default_gateway,
 
116
                      cidr,
 
117
                      start_floating_ip,
 
118
                      end_floating_ip):
 
119
 
 
120
    ext_net_len = cidr.split('/')[1]
 
121
    quantum = client.Client(username=username,
 
122
                            password=password,
 
123
                            tenant_name=tenant,
 
124
                            auth_url=url)
 
125
 
 
126
    networks = quantum.list_networks(name=ext_net_name)
 
127
    if len(networks['networks']) == 0:
 
128
        utils.juju_log('INFO',
 
129
                       'Configuring external bridge')
 
130
        network_msg = {
 
131
            'network': {
 
132
                'name': ext_net_name,
 
133
                'router:external': True
 
134
            }
 
135
        }
 
136
        utils.juju_log('INFO',
 
137
                       'Creating new external network definition: {}'
 
138
                            .format(ext_net_name))
 
139
        network = quantum.create_network(network_msg)
 
140
        utils.juju_log('INFO',
 
141
                       'New external network created: {}'
 
142
                            .format(network['network']['id']))
 
143
 
 
144
        subnet_msg = {
 
145
            'subnet': {
 
146
                'name': '{}_subnet'.format(ext_net_name),
 
147
                'network_id': network['network']['id'],
 
148
                'enable_dhcp': False,
 
149
                'gateway_ip': default_gateway,
 
150
                'cidr': cidr,
 
151
                'ip_version': 4,
 
152
                'allocation_pools': [
 
153
                        {
 
154
                        'start': start_floating_ip,
 
155
                        'end': end_floating_ip
 
156
                        }
 
157
                 ]
 
158
            }
 
159
        }
 
160
        utils.juju_log('INFO',
 
161
                       'Creating new subnet for {}'
 
162
                            .format(ext_net_name))
 
163
        subnet = quantum.create_subnet(subnet_msg)
 
164
        utils.juju_log('INFO',
 
165
                       'New subnet created: {}'
 
166
                            .format(subnet['subnet']['id']))
 
167
 
 
168
    utils.juju_log('INFO',
 
169
                   'Configuring external bridge connectivity')
 
170
    subprocess.check_call(['ip', 'addr', 'flush',
 
171
                           'dev', EXT_BRIDGE])
 
172
    subprocess.check_call(['ip', 'addr', 'add',
 
173
                           '{}/{}'.format(gateway_ip, ext_net_len),
 
174
                           'dev', EXT_BRIDGE])
 
175
    subprocess.check_call(['ip', 'addr', 'set',
 
176
                           EXT_BRIDGE, 'up'])