~1chb1n/charms/trusty/quantum-gateway/next-amulet-tk2

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/network/ufw.py

  • Committer: Edward Hope-Morley
  • Date: 2015-02-24 11:09:04 UTC
  • Revision ID: edward.hope-morley@canonical.com-20150224110904-fgjh7ikqwy6d9kf8
[trivial] charmhelpers sync

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
  >>> ufw.enable()
38
38
  >>> ufw.service('4949', 'close')  # munin
39
39
"""
40
 
 
41
 
__author__ = "Felipe Reyes <felipe.reyes@canonical.com>"
42
 
 
43
40
import re
44
41
import os
45
42
import subprocess
46
43
from charmhelpers.core import hookenv
47
44
 
 
45
__author__ = "Felipe Reyes <felipe.reyes@canonical.com>"
 
46
 
48
47
 
49
48
class UFWError(Exception):
50
49
    pass
51
50
 
52
51
 
 
52
class UFWIPv6Error(UFWError):
 
53
    pass
 
54
 
 
55
 
53
56
def is_enabled():
54
57
    """
55
58
    Check if `ufw` is enabled
66
69
    return len(m) >= 1
67
70
 
68
71
 
69
 
def is_ipv6_ok():
 
72
def is_ipv6_ok(soft_fail=False):
70
73
    """
71
74
    Check if IPv6 support is present and ip6tables functional
72
75
 
 
76
    :param soft_fail: If set to True and IPv6 support is broken, then reports
 
77
                      that the host doesn't have IPv6 support, otherwise a
 
78
                      UFWIPv6Error exception is raised.
73
79
    :returns: True if IPv6 is working, False otherwise
74
80
    """
75
81
 
89
95
                hookenv.log("Couldn't load ip6_tables module: %s" % ex.output,
90
96
                            level="WARN")
91
97
                # we are in a world where ip6tables isn't working
92
 
                # so we inform that the machine doesn't have IPv6
93
 
                return False
 
98
                if soft_fail:
 
99
                    # so we inform that the machine doesn't have IPv6
 
100
                    return False
 
101
                else:
 
102
                    raise UFWIPv6Error("IPv6 firewall support broken")
94
103
        else:
95
104
            # the module is present :)
96
105
            return True
113
122
        raise UFWError("Couldn't disable IPv6 support in ufw")
114
123
 
115
124
 
116
 
def enable():
 
125
def enable(soft_fail=False):
117
126
    """
118
127
    Enable ufw
119
128
 
 
129
    :param soft_fail: If set to True silently disables IPv6 support in ufw,
 
130
                      otherwise a UFWIPv6Error exception is raised when IP6
 
131
                      support is broken.
120
132
    :returns: True if ufw is successfully enabled
121
133
    """
122
134
    if is_enabled():
123
135
        return True
124
136
 
125
 
    if not is_ipv6_ok():
 
137
    if not is_ipv6_ok(soft_fail):
126
138
        disable_ipv6()
127
139
 
128
140
    output = subprocess.check_output(['ufw', 'enable'],