Andy Bavier | ddb7cfc | 2017-08-15 06:28:17 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
Matteo Scandolo | 3896c47 | 2017-08-01 13:31:42 -0700 | [diff] [blame] | 2 | |
| 3 | # Copyright 2017-present Open Networking Foundation |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Andy Bavier | a17d84b | 2016-11-16 09:39:26 -0800 | [diff] [blame] | 17 | import sys |
| 18 | import json |
| 19 | import shlex |
| 20 | import subprocess |
| 21 | |
| 22 | # Assume nothing has changed |
| 23 | result = { |
| 24 | "changed" : False, |
| 25 | "everyone" : "OK" |
| 26 | } |
| 27 | |
| 28 | # read the argument string from the arguments file |
| 29 | args_file = sys.argv[1] |
| 30 | args_data = file(args_file).read() |
| 31 | |
| 32 | # Variables for the task options |
| 33 | host_list = [] |
| 34 | command_on_fail = None |
| 35 | |
| 36 | # parse the task options |
| 37 | arguments = shlex.split(args_data) |
| 38 | |
| 39 | for arg in arguments: |
| 40 | # ignore any arguments without an equals in it |
| 41 | if "=" in arg: |
| 42 | (key, value) = arg.split("=") |
| 43 | |
| 44 | if key == "hosts": |
| 45 | # The list of hosts comes as a string that looks sort of like a python list, |
| 46 | # so some replace magic so we can parse it in to a real list |
| 47 | try: |
| 48 | value = value.replace("u'", "").replace("'", "") |
| 49 | value = json.loads(value) |
| 50 | host_list = value |
| 51 | except Exception as e: |
| 52 | result["everyone"] = "Not OK" |
| 53 | result["failed"] = True |
| 54 | result["msg"] = "Unable to parse 'hosts' argument to module : '%s'" % (e) |
| 55 | print json.dumps(result) |
| 56 | sys.stdout.flush() |
| 57 | sys.exit(1) |
| 58 | if key == "command_on_fail": |
| 59 | command_on_fail = value |
| 60 | |
| 61 | for host in host_list: |
| 62 | # Attempt to resolve hostname, if a host can't be resolved then fail the task |
| 63 | try: |
| 64 | if subprocess.check_output(["dig", "+short", "+search", host]) == '': |
| 65 | result["everyone"] = "Not OK" |
| 66 | result["failed"] = True |
| 67 | result["msg"] = "Unable to resolve host '%s'" % (host) |
| 68 | except Exception as e: |
| 69 | result["everyone"] = "Not OK" |
| 70 | result["failed"] = True |
| 71 | result["msg"] = "Error encountered while resolving '%s' : '%s'" % (host, e) |
| 72 | print json.dumps(result) |
| 73 | sys.stdout.flush() |
| 74 | sys.exit(1) |
| 75 | |
| 76 | # If not all hosts were resolved and a failure command was specified then call that |
| 77 | # command and capture the results. |
| 78 | if command_on_fail != None: |
| 79 | result["command_on_fail"] = {} |
| 80 | result["command_on_fail"]["command"] = command_on_fail |
| 81 | try: |
| 82 | cmd_out = subprocess.check_output(shlex.split(command_on_fail), stderr=subprocess.STDOUT) |
| 83 | result["command_on_fail"]["retcode"] = 0 |
| 84 | result["command_on_fail"]["out"] = cmd_out |
| 85 | except subprocess.CalledProcessError as e: |
| 86 | result["command_on_fail"]["retcode"] = e.returncode |
| 87 | result["command_on_fail"]["out"] = e.output |
| 88 | |
| 89 | # Output the results |
| 90 | print json.dumps(result) |
| 91 | |
Zack Williams | 2020afb | 2017-09-29 07:58:45 -0700 | [diff] [blame] | 92 | if 'failed' in result: |
Andy Bavier | a17d84b | 2016-11-16 09:39:26 -0800 | [diff] [blame] | 93 | sys.exit(1) |
Zack Williams | 2020afb | 2017-09-29 07:58:45 -0700 | [diff] [blame] | 94 | |