summaryrefslogtreecommitdiff
path: root/net_infrastructure/openvswitch_bridge
blob: 4b05f4079f5719c13b01ff8d38f23a25ee2322c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/python
#coding: utf-8 -*-

# This module is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software.  If not, see <http://www.gnu.org/licenses/>.

DOCUMENTATION = '''
---
module: openvswitch_bridge
version_added: 1.4
short_description: Manage Open vSwitch bridges
requirements: [ ovs-vsctl ]
description:
    - Manage Open vSwitch bridges
options:
    bridge:
        required: true
        description:
            - Name of bridge to manage
    state:
        required: false
        default: "present"
        choices: [ present, absent ]
        description:
            - Whether the bridge should exist
    timeout:
        required: false
        default: 5
        description:
            - How long to wait for ovs-vswitchd to respond
'''

EXAMPLES = '''
# Create a bridge named br-int
- openvswitch_bridge: bridge=br-int state=present
'''


class OVSBridge(object):
    def __init__(self, module):
        self.module = module
        self.bridge = module.params['bridge']
        self.state = module.params['state']
        self.timeout = module.params['timeout']

    def _vsctl(self, command):
        '''Run ovs-vsctl command'''
        return self.module.run_command(['ovs-vsctl', '-t', str(self.timeout)] + command)

    def exists(self):
        '''Check if the bridge already exists'''
        rc, _, err = self._vsctl(['br-exists', self.bridge])
        if rc == 0:  # See ovs-vsctl(8) for status codes
            return True
        if rc == 2:
            return False
        raise Exception(err)

    def add(self):
        '''Create the bridge'''
        rc, _, err = self._vsctl(['add-br', self.bridge])
        if rc != 0:
            raise Exception(err)

    def delete(self):
        '''Delete the bridge'''
        rc, _, err = self._vsctl(['del-br', self.bridge])
        if rc != 0:
            raise Exception(err)

    def check(self):
        '''Run check mode'''
        try:
            if self.state == 'absent' and self.exists():
                changed = True
            elif self.state == 'present' and not self.exists():
                changed = True
            else:
                changed = False
        except Exception, e:
            self.module.fail_json(msg=str(e))
        self.module.exit_json(changed=changed)

    def run(self):
        '''Make the necessary changes'''
        changed = False
        try:
            if self.state == 'absent':
                if self.exists():
                    self.delete()
                    changed = True
            elif self.state == 'present':
                if not self.exists():
                    self.add()
                    changed = True
        except Exception, e:
            self.module.fail_json(msg=str(e))
        self.module.exit_json(changed=changed)


def main():
    module = AnsibleModule(
        argument_spec={
            'bridge': {'required': True},
            'state': {'default': 'present', 'choices': ['present', 'absent']},
            'timeout': {'default': 5, 'type': 'int'}
        },
        supports_check_mode=True,
    )

    br = OVSBridge(module)
    if module.check_mode:
        br.check()
    else:
        br.run()


# import module snippets
from ansible.module_utils.basic import *
main()