|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# This file is part of ALA ansible scripts |
| 5 | +# |
| 6 | +# TODO Licence |
| 7 | + |
| 8 | +DOCUMENTATION = ''' |
| 9 | +--- |
| 10 | +module: tomcat_connector |
| 11 | +short_description: Add or remove a Tomcat connector |
| 12 | +description: |
| 13 | + - Add or remove a Tomcat connector. |
| 14 | +version_added: "1.8.2" |
| 15 | +options: |
| 16 | + name: |
| 17 | + description: |
| 18 | + - the name of the connector |
| 19 | + required: true |
| 20 | + choices: [ "HTTP/1.1", "AJP/1.3" ] |
| 21 | + service: |
| 22 | + description: |
| 23 | + - the name of the service to put the connector in |
| 24 | + required: false |
| 25 | + default: Catalina |
| 26 | + bind_addr: |
| 27 | + description: |
| 28 | + - The ip address to bind the server socket to |
| 29 | + required: false |
| 30 | + default: 0.0.0.0 |
| 31 | + port: |
| 32 | + description: |
| 33 | + - the port to use for connector |
| 34 | + - will use default port for protocol (8080 for HTTP or 8009 for AJP) if omitted |
| 35 | + required: false |
| 36 | + connection_timeout: |
| 37 | + description: |
| 38 | + - the amount of time in ms to wait for the client before timing out |
| 39 | + required: false |
| 40 | + default: 20000 |
| 41 | + uri_encoding: |
| 42 | + description: |
| 43 | + - the charset for uri encoding |
| 44 | + required: false |
| 45 | + default: UTF-8 |
| 46 | + redirect_port: |
| 47 | + description: |
| 48 | + - the port to automatically redirect to for requests that require servlet security-constraints |
| 49 | + required: false |
| 50 | + default: 443 |
| 51 | + state: |
| 52 | + description: |
| 53 | + - The connector state |
| 54 | + required: false |
| 55 | + default: present |
| 56 | + choices: [ "present", "absent" ] |
| 57 | + tomcat_conf_dir: |
| 58 | + description: |
| 59 | + - The tomcat conf directory |
| 60 | + required: false |
| 61 | + default: /etc/tomcat7 on debian, /etc/tomcat6 on enterprise linux |
| 62 | +notes: |
| 63 | + - This module uses I(python-augeas), a Python Augeas binding library. You must ensure that python-agueas is installed |
| 64 | + on the host before using this module. |
| 65 | + - This module uses I(python-libxml2), a Python binding for the libxml2 library. You must ensure that python-libxml2 |
| 66 | + is installed on the host before using this module. |
| 67 | +requirements: [ python-augeas, python-libxml2 ] |
| 68 | +author: Simon Bear |
| 69 | +''' |
| 70 | + |
| 71 | +EXAMPLES = ''' |
| 72 | +''' |
| 73 | + |
| 74 | +try: |
| 75 | + import augeas |
| 76 | +except ImportError: |
| 77 | + augeas_found = False |
| 78 | +else: |
| 79 | + augeas_found = True |
| 80 | + |
| 81 | +import logging |
| 82 | +logging.basicConfig(level=logging.DEBUG) |
| 83 | +log = logging.getLogger('tomcat_connector') |
| 84 | + |
| 85 | +class NotSupportedError(Exception): |
| 86 | + pass |
| 87 | + |
| 88 | + |
| 89 | +# =========================================== |
| 90 | +# Tomcat Service module specific support methods. |
| 91 | +# |
| 92 | + |
| 93 | +def load_augeas_for_file(root="/etc/tomcat7", lens="Xml.lns", filename="server.xml", check_mode=False): |
| 94 | + aug = augeas.Augeas(root, None, augeas.Augeas.NO_LOAD | augeas.Augeas.NO_MODL_AUTOLOAD | ( |
| 95 | + augeas.Augeas.SAVE_NOOP if check_mode else augeas.Augeas.SAVE_BACKUP)) |
| 96 | + aug.add_transform(lens, filename) |
| 97 | + aug.load() |
| 98 | + return aug |
| 99 | + |
| 100 | + |
| 101 | +def tomcat_dir_for_distrib(distribution): |
| 102 | + if distribution.lower() in ["redhat", "centos"]: |
| 103 | + return '/etc/tomcat6' |
| 104 | + else: |
| 105 | + return '/etc/tomcat7' |
| 106 | + |
| 107 | + |
| 108 | +def xml_lint(filename): |
| 109 | + try: |
| 110 | + import libxml2 |
| 111 | + libxml2.thrDefIndentTreeOutput(1) |
| 112 | + libxml2.keepBlanksDefault(0) |
| 113 | + x = libxml2.parseFile(filename) |
| 114 | + x.saveFormatFile(filename, 1) |
| 115 | + except ImportError: |
| 116 | + pass |
| 117 | + |
| 118 | + |
| 119 | +def b2xs(string): |
| 120 | + return str(string).lower() |
| 121 | + |
| 122 | + |
| 123 | +def default_port(name): |
| 124 | + return 8080 if name == "HTTP/1.1" else 8009 |
| 125 | + |
| 126 | + |
| 127 | +def add_connector(aug, protocol, service, bind_addr, port, connection_timeout=20000, uri_encoding="UTF-8", redirect_port=443): |
| 128 | + |
| 129 | + aug.defvar("service", "/files/server.xml/Server/Service[#attribute/name=\"%(service)s\"]" % locals()) |
| 130 | + |
| 131 | + log.debug("add_connector") |
| 132 | + log.debug(locals()) |
| 133 | + |
| 134 | + aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/port" % locals(), str(port)) |
| 135 | + |
| 136 | + aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/protocol" % locals(), protocol) |
| 137 | + |
| 138 | + aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/address" % locals(), bind_addr) |
| 139 | + |
| 140 | + aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/URIEncoding" % locals(), uri_encoding) |
| 141 | + |
| 142 | + aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/redirectPort" % locals(), str(redirect_port)) |
| 143 | + |
| 144 | + aug.set("$service/Connector[#attribute/port=\"%(port)s\"]/#attribute/connectionTimeout" % locals(), |
| 145 | + str(connection_timeout)) |
| 146 | + |
| 147 | + |
| 148 | +def remove_connector(aug, protocol, port): |
| 149 | + aug.remove("$service/Connector[#attribute/port=\"%(port)s\"]" % locals()) |
| 150 | + |
| 151 | + |
| 152 | +# =========================================== |
| 153 | +# Module execution. |
| 154 | +# |
| 155 | + |
| 156 | + |
| 157 | +def main(): |
| 158 | + module = AnsibleModule( |
| 159 | + argument_spec=dict( |
| 160 | + name=dict(required=True, choices=["HTTP/1.1", "AJP/1.3"]), |
| 161 | + service=dict(default="Catalina"), |
| 162 | + bind_addr=dict(default="0.0.0.0"), |
| 163 | + port=dict(type="int"), |
| 164 | + connection_timeout=dict(default=20000, type="int"), |
| 165 | + uri_encoding=dict(default="UTF-8"), |
| 166 | + redirect_port=dict(default=443, type="int"), |
| 167 | + state=dict(default="present", choices=["absent", "present"]), |
| 168 | + tomcat_conf_dir=dict(default=None) |
| 169 | + ), |
| 170 | + supports_check_mode=True |
| 171 | + ) |
| 172 | + |
| 173 | + if not augeas_found: |
| 174 | + module.fail_json(msg="the python augeas module is required") |
| 175 | + return |
| 176 | + |
| 177 | + name = module.params["name"] |
| 178 | + service = module.params["service"] |
| 179 | + bind_addr = module.params["bind_addr"] |
| 180 | + port = module.params["port"] |
| 181 | + connection_timeout = module.params["connection_timeout"] |
| 182 | + uri_encoding = module.params["uri_encoding"] |
| 183 | + redirect_port = module.params["redirect_port"] |
| 184 | + state = module.params["state"] |
| 185 | + |
| 186 | + tomcat_conf_dir = module.params["tomcat_conf_dir"] |
| 187 | + |
| 188 | + if not port: |
| 189 | + port = default_port(name) |
| 190 | + |
| 191 | + if not tomcat_conf_dir: |
| 192 | + distribution = get_distribution() |
| 193 | + tomcat_conf_dir = tomcat_dir_for_distrib(distribution) |
| 194 | + |
| 195 | + try: |
| 196 | + aug = load_augeas_for_file(root=tomcat_conf_dir, check_mode=module.check_mode) |
| 197 | + except Exception, e: |
| 198 | + module.fail_json(msg="unable to connect to Augeas: %s" % e) |
| 199 | + return |
| 200 | + |
| 201 | + try: |
| 202 | + if state == "absent": |
| 203 | + remove_connector(aug, name, port) |
| 204 | + else: |
| 205 | + add_connector(aug, name, service, bind_addr, port, connection_timeout, uri_encoding, redirect_port) |
| 206 | + |
| 207 | + try: |
| 208 | + aug.save() |
| 209 | + except Exception, e: |
| 210 | + log.exception("Couldn't save server.xml") |
| 211 | + saved = aug.get("/augeas/events/saved") |
| 212 | + errors = aug.match("//errors/*") |
| 213 | + aug.close() |
| 214 | + if saved and not module.check_mode: |
| 215 | + xml_lint(tomcat_conf_dir + "/server.xml") |
| 216 | + module.exit_json(changed=True if saved else False, name=name, saved=saved, errors=errors) |
| 217 | + |
| 218 | + except Exception, e: |
| 219 | + log.exception("exception while performing augeas operations") |
| 220 | + module.fail_json(msg="exception while performing augeas operations: %s" % e) |
| 221 | + |
| 222 | +# import module snippets |
| 223 | +from ansible.module_utils.basic import * |
| 224 | + |
| 225 | +main() |
0 commit comments