1
''' Helpers for interacting with OpenvSwitch '''
4
from charmhelpers.core.hookenv import (
7
from charmhelpers.core.host import (
13
''' Add the named bridge to openvswitch '''
14
log('Creating bridge {}'.format(name))
15
subprocess.check_call(["ovs-vsctl", "--", "--may-exist", "add-br", 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])
24
def add_bridge_port(name, port, promisc=False):
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",
29
subprocess.check_call(["ip", "link", "set", port, "up"])
31
subprocess.check_call(["ip", "link", "set", port, "promisc", "on"])
33
subprocess.check_call(["ip", "link", "set", port, "promisc", "off"])
36
def del_bridge_port(name, port):
37
''' Delete a port from the named openvswitch bridge '''
38
log('Deleting port {} from bridge {}'.format(port, name))
39
subprocess.check_call(["ovs-vsctl", "--", "--if-exists", "del-port",
41
subprocess.check_call(["ip", "link", "set", port, "down"])
42
subprocess.check_call(["ip", "link", "set", port, "promisc", "off"])
45
def set_manager(manager):
46
''' Set the controller for the local openvswitch '''
47
log('Setting manager for local ovs to {}'.format(manager))
48
subprocess.check_call(['ovs-vsctl', 'set-manager',
49
'ssl:{}'.format(manager)])
52
CERT_PATH = '/etc/openvswitch/ovsclient-cert.pem'
55
def get_certificate():
56
''' Read openvswitch certificate from disk '''
57
if os.path.exists(CERT_PATH):
58
log('Reading ovs certificate from {}'.format(CERT_PATH))
59
with open(CERT_PATH, 'r') as cert:
60
full_cert = cert.read()
61
begin_marker = "-----BEGIN CERTIFICATE-----"
62
end_marker = "-----END CERTIFICATE-----"
63
begin_index = full_cert.find(begin_marker)
64
end_index = full_cert.rfind(end_marker)
65
if end_index == -1 or begin_index == -1:
66
raise RuntimeError("Certificate does not contain valid begin"
68
full_cert = full_cert[begin_index:(end_index + len(end_marker))]
71
log('Certificate not found', level=WARNING)
76
''' Full restart and reload of openvswitch '''
77
if os.path.exists('/etc/init/openvswitch-force-reload-kmod.conf'):
78
service('start', 'openvswitch-force-reload-kmod')
80
service('force-reload-kmod', 'openvswitch-switch')