~jaywink/charms/trusty/jujubackup/trunk

« back to all changes in this revision

Viewing changes to lib/charmhelpers/contrib/network/ovs/__init__.py

  • Committer: vincenzo di somma
  • Date: 2014-02-12 10:50:10 UTC
  • Revision ID: vincenzo.di.somma@canonical.com-20140212105010-tn1it3seyzk4ld2g
Works with u1 and s3, few minor details still need to be fixed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 '''
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')