~charmers/charms/precise/nova-compute/trunk

46.1.73 by James Page
Resync charmhelpers, use ovs helper to create bridges
1
''' Helpers for interacting with OpenvSwitch '''
2
import subprocess
3
import os
4
from charmhelpers.core.hookenv import (
5
    log, WARNING
6
)
7
from charmhelpers.core.host import (
8
    service
9
)
10
11
12
def add_bridge(name):
13
    ''' Add the named bridge to openvswitch '''
14
    log('Creating bridge {}'.format(name))
15
    subprocess.check_call(["ovs-vsctl", "--", "--may-exist", "add-br", name])
16
17
18
def del_bridge(name):
19
    ''' Delete the named bridge from openvswitch '''
20
    log('Deleting bridge {}'.format(name))
21
    subprocess.check_call(["ovs-vsctl", "--", "--if-exists", "del-br", name])
22
23
24
def add_bridge_port(name, port):
25
    ''' Add a port to the named openvswitch bridge '''
26
    log('Adding port {} to bridge {}'.format(port, name))
27
    subprocess.check_call(["ovs-vsctl", "--", "--may-exist", "add-port",
28
                           name, port])
29
    subprocess.check_call(["ip", "link", "set", port, "up"])
30
31
32
def del_bridge_port(name, port):
33
    ''' Delete a port from the named openvswitch bridge '''
34
    log('Deleting port {} from bridge {}'.format(port, name))
35
    subprocess.check_call(["ovs-vsctl", "--", "--if-exists", "del-port",
36
                           name, port])
37
    subprocess.check_call(["ip", "link", "set", port, "down"])
38
39
40
def set_manager(manager):
41
    ''' Set the controller for the local openvswitch '''
42
    log('Setting manager for local ovs to {}'.format(manager))
43
    subprocess.check_call(['ovs-vsctl', 'set-manager',
44
                           'ssl:{}'.format(manager)])
45
46
47
CERT_PATH = '/etc/openvswitch/ovsclient-cert.pem'
48
49
50
def get_certificate():
51
    ''' Read openvswitch certificate from disk '''
52
    if os.path.exists(CERT_PATH):
53
        log('Reading ovs certificate from {}'.format(CERT_PATH))
54
        with open(CERT_PATH, 'r') as cert:
55
            full_cert = cert.read()
56
            begin_marker = "-----BEGIN CERTIFICATE-----"
57
            end_marker = "-----END CERTIFICATE-----"
58
            begin_index = full_cert.find(begin_marker)
59
            end_index = full_cert.rfind(end_marker)
60
            if end_index == -1 or begin_index == -1:
61
                raise RuntimeError("Certificate does not contain valid begin"
62
                                   " and end markers.")
63
            full_cert = full_cert[begin_index:(end_index + len(end_marker))]
64
            return full_cert
65
    else:
66
        log('Certificate not found', level=WARNING)
67
        return None
68
69
70
def full_restart():
71
    ''' Full restart and reload of openvswitch '''
47.2.5 by James Page
Resync charm helpers
72
    if os.path.exists('/etc/init/openvswitch-force-reload-kmod.conf'):
73
        service('start', 'openvswitch-force-reload-kmod')
74
    else:
75
        service('force-reload-kmod', 'openvswitch-switch')