martin f. krafft | f37f068 | 2013-06-14 16:36:20 +0200 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # |
| 4 | # reclass — recursive external node classifier |
| 5 | # |
| 6 | # Copyright © 2007–13 martin f. krafft <madduck@madduck.net> |
| 7 | # Released under the terms of the Artistic Licence 2.0 |
| 8 | # |
| 9 | __prog__ = 'reclass' |
| 10 | __description__ = 'classify nodes based on an external data source' |
| 11 | __version__ = '1.0' |
| 12 | __author__ = 'martin f. krafft <madduck@madduck.net>' |
| 13 | __copyright__ = 'Copyright © 2007–13 ' + __author__ |
| 14 | __licence__ = 'Artistic Licence 2.0' |
| 15 | |
| 16 | import sys, os, posix, time |
| 17 | import config |
| 18 | from output import OutputLoader |
| 19 | from storage import StorageBackendLoader |
martin f. krafft | 3434b6b | 2013-06-14 20:40:52 +0200 | [diff] [blame] | 20 | import errors |
martin f. krafft | f37f068 | 2013-06-14 16:36:20 +0200 | [diff] [blame] | 21 | |
| 22 | def get_options(config_file=None): |
| 23 | return config.get_options(__name__, __version__, __description__, config_file) |
| 24 | |
martin f. krafft | 9a9b0ac | 2013-06-21 21:24:18 +0200 | [diff] [blame] | 25 | def get_data(storage_type, nodes_uri, classes_uri, applications_postfix, node): |
martin f. krafft | f37f068 | 2013-06-14 16:36:20 +0200 | [diff] [blame] | 26 | storage_class = StorageBackendLoader(storage_type).load() |
| 27 | storage = storage_class(os.path.abspath(os.path.expanduser(nodes_uri)), |
martin f. krafft | 9a9b0ac | 2013-06-21 21:24:18 +0200 | [diff] [blame] | 28 | os.path.abspath(os.path.expanduser(classes_uri)), |
| 29 | applications_postfix |
| 30 | ) |
martin f. krafft | f37f068 | 2013-06-14 16:36:20 +0200 | [diff] [blame] | 31 | if node is False: |
| 32 | ret = storage.inventory() |
| 33 | else: |
| 34 | ret = storage.nodeinfo(node) |
| 35 | ret['RECLASS']['timestamp'] = time.strftime('%c') |
| 36 | |
| 37 | return ret |
| 38 | |
| 39 | def output(data, fmt, pretty_print=False): |
| 40 | output_class = OutputLoader(fmt).load() |
| 41 | outputter = output_class() |
| 42 | return outputter.dump(data, pretty_print=pretty_print) |
| 43 | |
martin f. krafft | 3434b6b | 2013-06-14 20:40:52 +0200 | [diff] [blame] | 44 | def _error(msg, rc): |
| 45 | print >>sys.stderr, msg |
| 46 | sys.exit(rc) |
| 47 | |
martin f. krafft | f37f068 | 2013-06-14 16:36:20 +0200 | [diff] [blame] | 48 | if __name__ == '__main__': |
| 49 | __name__ = __prog__ |
| 50 | config_file = None |
| 51 | for d in (os.getcwd(), os.path.dirname(sys.argv[0])): |
| 52 | f = os.path.join(d, __name__ + '-config.yml') |
| 53 | if os.access(f, os.R_OK): |
| 54 | config_file = f |
| 55 | break |
martin f. krafft | 3434b6b | 2013-06-14 20:40:52 +0200 | [diff] [blame] | 56 | try: |
| 57 | options = get_options(config_file) |
| 58 | data = get_data(options.storage_type, options.nodes_uri, |
martin f. krafft | 9a9b0ac | 2013-06-21 21:24:18 +0200 | [diff] [blame] | 59 | options.classes_uri, options.applications_postfix, |
| 60 | options.node) |
martin f. krafft | 3434b6b | 2013-06-14 20:40:52 +0200 | [diff] [blame] | 61 | print output(data, options.output, options.pretty_print) |
| 62 | sys.exit(posix.EX_OK) |
| 63 | |
| 64 | except errors.ReclassException, e: |
| 65 | _error(e.message, e.rc) |