blob: 2dc267e03d661e8e379f956f6f83fa6f27d0cb85 [file] [log] [blame]
Andy Bavierddb7cfc2017-08-15 06:28:17 -07001#!/usr/bin/env python
Matteo Scandolo3896c472017-08-01 13:31:42 -07002
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 Baviera17d84b2016-11-16 09:39:26 -080017import sys
18import json
19import shlex
20import subprocess
21
22# Assume nothing has changed
23result = {
24 "changed" : False,
25 "everyone" : "OK"
26}
27
28# read the argument string from the arguments file
29args_file = sys.argv[1]
30args_data = file(args_file).read()
31
32# Variables for the task options
33host_list = []
34command_on_fail = None
35
36# parse the task options
37arguments = shlex.split(args_data)
38
39for 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
61for 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.
78if 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
90print json.dumps(result)
91
Zack Williamsc5b59052017-09-29 07:58:45 -070092if 'failed' in result:
Andy Baviera17d84b2016-11-16 09:39:26 -080093 sys.exit(1)
Zack Williamsc5b59052017-09-29 07:58:45 -070094