~nuage-canonical/charms/trusty/nuage-vsc/trunk

« back to all changes in this revision

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

  • Committer: Aniket Bhat
  • Date: 2014-10-13 20:04:14 UTC
  • Revision ID: abhat@nuagenetworks.net-20141013200414-6q0dwxvduu3xqg7e
Added sync target to Makefile and submitting charmhelpers sync

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, 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",
 
28
                           name, port])
 
29
    subprocess.check_call(["ip", "link", "set", port, "up"])
 
30
    if promisc:
 
31
        subprocess.check_call(["ip", "link", "set", port, "promisc", "on"])
 
32
    else:
 
33
        subprocess.check_call(["ip", "link", "set", port, "promisc", "off"])
 
34
 
 
35
 
 
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",
 
40
                           name, port])
 
41
    subprocess.check_call(["ip", "link", "set", port, "down"])
 
42
    subprocess.check_call(["ip", "link", "set", port, "promisc", "off"])
 
43
 
 
44
 
 
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)])
 
50
 
 
51
 
 
52
CERT_PATH = '/etc/openvswitch/ovsclient-cert.pem'
 
53
 
 
54
 
 
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"
 
67
                                   " and end markers.")
 
68
            full_cert = full_cert[begin_index:(end_index + len(end_marker))]
 
69
            return full_cert
 
70
    else:
 
71
        log('Certificate not found', level=WARNING)
 
72
        return None
 
73
 
 
74
 
 
75
def full_restart():
 
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')
 
79
    else:
 
80
        service('force-reload-kmod', 'openvswitch-switch')