~junaidali/charms/trusty/plumgrid-director/pg-restart

« back to all changes in this revision

Viewing changes to hooks/charmhelpers-pl/contrib/network/ovs/__init__.py

  • Committer: bbaqar at plumgrid
  • Date: 2015-07-29 18:07:31 UTC
  • Revision ID: bbaqar@plumgrid.com-20150729180731-ioynar8x3u5pxytc
Addressed reviews by Charmers

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2014-2015 Canonical Limited.
2
 
#
3
 
# This file is part of charm-helpers.
4
 
#
5
 
# charm-helpers is free software: you can redistribute it and/or modify
6
 
# it under the terms of the GNU Lesser General Public License version 3 as
7
 
# published by the Free Software Foundation.
8
 
#
9
 
# charm-helpers is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU Lesser General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU Lesser General Public License
15
 
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
16
 
 
17
 
''' Helpers for interacting with OpenvSwitch '''
18
 
import subprocess
19
 
import os
20
 
from charmhelpers.core.hookenv import (
21
 
    log, WARNING
22
 
)
23
 
from charmhelpers.core.host import (
24
 
    service
25
 
)
26
 
 
27
 
 
28
 
def add_bridge(name):
29
 
    ''' Add the named bridge to openvswitch '''
30
 
    log('Creating bridge {}'.format(name))
31
 
    subprocess.check_call(["ovs-vsctl", "--", "--may-exist", "add-br", name])
32
 
 
33
 
 
34
 
def del_bridge(name):
35
 
    ''' Delete the named bridge from openvswitch '''
36
 
    log('Deleting bridge {}'.format(name))
37
 
    subprocess.check_call(["ovs-vsctl", "--", "--if-exists", "del-br", name])
38
 
 
39
 
 
40
 
def add_bridge_port(name, port, promisc=False):
41
 
    ''' Add a port to the named openvswitch bridge '''
42
 
    log('Adding port {} to bridge {}'.format(port, name))
43
 
    subprocess.check_call(["ovs-vsctl", "--", "--may-exist", "add-port",
44
 
                           name, port])
45
 
    subprocess.check_call(["ip", "link", "set", port, "up"])
46
 
    if promisc:
47
 
        subprocess.check_call(["ip", "link", "set", port, "promisc", "on"])
48
 
    else:
49
 
        subprocess.check_call(["ip", "link", "set", port, "promisc", "off"])
50
 
 
51
 
 
52
 
def del_bridge_port(name, port):
53
 
    ''' Delete a port from the named openvswitch bridge '''
54
 
    log('Deleting port {} from bridge {}'.format(port, name))
55
 
    subprocess.check_call(["ovs-vsctl", "--", "--if-exists", "del-port",
56
 
                           name, port])
57
 
    subprocess.check_call(["ip", "link", "set", port, "down"])
58
 
    subprocess.check_call(["ip", "link", "set", port, "promisc", "off"])
59
 
 
60
 
 
61
 
def set_manager(manager):
62
 
    ''' Set the controller for the local openvswitch '''
63
 
    log('Setting manager for local ovs to {}'.format(manager))
64
 
    subprocess.check_call(['ovs-vsctl', 'set-manager',
65
 
                           'ssl:{}'.format(manager)])
66
 
 
67
 
 
68
 
CERT_PATH = '/etc/openvswitch/ovsclient-cert.pem'
69
 
 
70
 
 
71
 
def get_certificate():
72
 
    ''' Read openvswitch certificate from disk '''
73
 
    if os.path.exists(CERT_PATH):
74
 
        log('Reading ovs certificate from {}'.format(CERT_PATH))
75
 
        with open(CERT_PATH, 'r') as cert:
76
 
            full_cert = cert.read()
77
 
            begin_marker = "-----BEGIN CERTIFICATE-----"
78
 
            end_marker = "-----END CERTIFICATE-----"
79
 
            begin_index = full_cert.find(begin_marker)
80
 
            end_index = full_cert.rfind(end_marker)
81
 
            if end_index == -1 or begin_index == -1:
82
 
                raise RuntimeError("Certificate does not contain valid begin"
83
 
                                   " and end markers.")
84
 
            full_cert = full_cert[begin_index:(end_index + len(end_marker))]
85
 
            return full_cert
86
 
    else:
87
 
        log('Certificate not found', level=WARNING)
88
 
        return None
89
 
 
90
 
 
91
 
def full_restart():
92
 
    ''' Full restart and reload of openvswitch '''
93
 
    if os.path.exists('/etc/init/openvswitch-force-reload-kmod.conf'):
94
 
        service('start', 'openvswitch-force-reload-kmod')
95
 
    else:
96
 
        service('force-reload-kmod', 'openvswitch-switch')