Index: run.py |
=================================================================== |
--- a/run.py |
+++ b/run.py |
@@ -3,6 +3,7 @@ |
import sys |
import os |
+import os.path |
Felix Dahlke
2015/03/03 16:43:32
Not necessary, we're already importing os. Looks l
mathias
2015/03/04 12:32:37
Agreed, the explicit os.path import will be remove
|
import re |
import subprocess |
import getopt |
@@ -41,78 +42,42 @@ |
return user, hosts, ignore_errors, args |
-def readMonitoringConfig(): |
- # Use Puppet's parser to convert monitoringserver.pp into YAML |
- manifest = os.path.join(os.path.dirname(__file__), 'manifests', 'monitoringserver.pp') |
- parseScript = ''' |
- require 'puppet' |
- require 'puppet/parser' |
- parser = Puppet::Parser::Parser.new(Puppet[:environment]) |
- Puppet.settings[:ignoreimport] = true |
- parser.file = ARGV[0] |
- print ZAML.dump(parser.parse) |
- ''' |
- data, dummy = subprocess.Popen(['ruby', '', manifest], |
- stdin=subprocess.PIPE, |
- stdout=subprocess.PIPE).communicate(parseScript) |
- |
- # See http://stackoverflow.com/q/8357650/785541 on parsing Puppet's YAML |
- yaml.add_multi_constructor(u"!ruby/object:", lambda loader, suffix, node: loader.construct_yaml_map(node)) |
- yaml.add_constructor(u"!ruby/sym", lambda loader, node: loader.construct_yaml_str(node)) |
- return yaml.load(data) |
def getValidHosts(): |
- def processNode(node, hosts=None, groups=None): |
- if hosts == None: |
- hosts = set() |
- if groups == None: |
- groups = {} |
- |
- if 'context' in node and 'code' in node['context']: |
- node = node['context']['code'] |
- |
- if node.get('type', None) == 'nagios_hostgroup': |
- data = node['instances']['children'][0] |
- title = data['title']['value'] |
- members = filter(lambda c: c['param'] == 'members', data['parameters']['children'])[0]['value']['value'] |
- members = re.split(r'\s*,\s*', members) |
- groups[title] = members |
- elif node.get('type', None) == 'nagios_host': |
- data = node['instances']['children'][0] |
- title = data['title']['value'] |
- hosts.add(title) |
- |
- for child in node['children']: |
- processNode(child, hosts, groups) |
- return hosts, groups |
- |
- monitoringConfig = readMonitoringConfig() |
- if not monitoringConfig: |
- print >>sys.stderr, "Failed to parse monitoring configuration" |
- return [[], []] |
- # Extract hosts and groups from monitoring config |
- return processNode(monitoringConfig) |
+ dirname = os.path.dirname(sys.argv[0]) |
+ path_name = os.path.join(dirname, "hiera", "private", "hosts.yaml") |
+ with open(path_name, 'rb') as handle: |
+ config = yaml.load(handle) |
+ servers = config.get('servers', {}) |
+ return servers |
def resolveHostList(hosts): |
- validHosts, validGroups = getValidHosts() |
- if not validHosts: |
- print >>sys.stderr, "Warning: No valid hosts found, not validating" |
- return hosts |
+ host_names = set(str(item) for item in hosts) |
Wladimir Palant
2015/03/03 20:00:19
Given that hosts is a list of string, this seems e
mathias
2015/03/04 12:32:37
"Given that hosts is a list of string" <- this is
Felix Dahlke
2015/03/04 14:27:53
Nit: The parentheses are now not necessary anymore
Wladimir Palant
2015/03/04 15:14:11
This isn't a library function you wrote here, it i
mathias
2015/03/04 18:13:51
Done.
mathias
2015/03/04 18:13:51
Done.
|
result = set() |
- for param in hosts: |
- if param in validGroups: |
- for host in validGroups[param]: |
- if host == '*': |
- result = result | validHosts |
- else: |
- result.add(host) |
- elif param in validHosts: |
- result.add(param) |
- elif '%s.adblockplus.org' % param in validHosts: |
- result.add('%s.adblockplus.org' % param) |
- else: |
- print >>sys.stderr, 'Warning: failed to recognize host or group %s' %param |
+ |
+ try: |
+ valid_hosts = getValidHosts() |
Felix Dahlke
2015/03/03 16:43:32
Host validation doesn't make _that_ much sense any
Wladimir Palant
2015/03/03 20:00:19
Indeed, that's something we discussed previously -
mathias
2015/03/04 12:32:37
This version does two jobs at the same time; valid
Felix Dahlke
2015/03/04 14:27:53
For the record: I actually prefer Wladimir's sugge
Wladimir Palant
2015/03/04 15:14:11
Actually, changing the current validation code to
mathias
2015/03/04 18:13:51
@Felix
As I wrote before, this is actually intende
Felix Dahlke
2015/03/09 23:14:51
Didn't realise this, but yeah, the catch clause st
|
+ except Warning as error: |
+ print >>sys.stderr, 'Warning: failed to determine valid hosts:', error |
+ result.update(host_names) |
+ else: |
+ for name in host_names: |
+ chunk = tuple( |
+ item[1].get('dns', item[0]) for item in valid_hosts.items() |
Wladimir Palant
2015/03/03 20:00:19
for (key, value) in valid_hosts.iteritems() please
mathias
2015/03/04 12:32:37
Done.
To answer your question: There is no need
Wladimir Palant
2015/03/04 15:14:11
The difference between a list and a tuple isn't th
mathias
2015/03/04 18:13:51
This has already been changed in the current patch
|
+ |
+ if name == item[0] |
+ or name == '*' |
+ or name == item[1].get('dns', None) |
+ or name in item[1]['ip'] |
mathias
2015/02/26 18:41:23
While resolving IPs would be a nice feature, this
mathias
2015/03/04 12:32:37
Done.
|
+ or name in item[1].get('groups', ()) |
+ ) |
+ |
+ if len(chunk) == 0: |
+ print >>sys.stderr, 'Warning: failed to recognize host or group', name |
+ else: |
+ result.update(chunk) |
+ |
return result |
def runCommand(user, host, command, ignore_errors=False): |