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