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):
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"])
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",
37
subprocess.check_call(["ip", "link", "set", port, "down"])
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)])
47
CERT_PATH = '/etc/openvswitch/ovsclient-cert.pem'
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"
63
full_cert = full_cert[begin_index:(end_index + len(end_marker))]
66
log('Certificate not found', level=WARNING)
71
''' Full restart and reload of openvswitch '''
72
if os.path.exists('/etc/init/openvswitch-force-reload-kmod.conf'):
73
service('start', 'openvswitch-force-reload-kmod')
75
service('force-reload-kmod', 'openvswitch-switch')