martin f. krafft | 3094327 | 2013-07-04 08:32:00 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
martin f. krafft | 42c475d | 2013-06-26 18:39:06 +0200 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
| 3 | # |
| 4 | # This file is part of reclass (http://github.com/madduck/reclass) |
| 5 | # |
| 6 | # Copyright © 2007–13 martin f. krafft <madduck@madduck.net> |
| 7 | # Released under the terms of the Artistic Licence 2.0 |
| 8 | # |
martin f. krafft | 42c475d | 2013-06-26 18:39:06 +0200 | [diff] [blame] | 9 | |
martin f. krafft | 3094327 | 2013-07-04 08:32:00 +0200 | [diff] [blame] | 10 | import os, sys, posix |
martin f. krafft | 42c475d | 2013-06-26 18:39:06 +0200 | [diff] [blame] | 11 | |
martin f. krafft | 3094327 | 2013-07-04 08:32:00 +0200 | [diff] [blame] | 12 | from reclass import get_nodeinfo, get_inventory, output |
| 13 | from reclass.errors import ReclassException |
| 14 | from reclass.config import find_and_read_configfile, get_options |
| 15 | from reclass.constants import MODE_NODEINFO |
| 16 | from reclass.defaults import * |
| 17 | from reclass.version import * |
martin f. krafft | 42c475d | 2013-06-26 18:39:06 +0200 | [diff] [blame] | 18 | |
martin f. krafft | 3094327 | 2013-07-04 08:32:00 +0200 | [diff] [blame] | 19 | def ext_pillar(minion_id, pillar, |
| 20 | storage_type=OPT_STORAGE_TYPE, |
| 21 | inventory_base_uri=OPT_INVENTORY_BASE_URI, |
| 22 | nodes_uri=OPT_NODES_URI, |
| 23 | classes_uri=OPT_CLASSES_URI): |
martin f. krafft | 42c475d | 2013-06-26 18:39:06 +0200 | [diff] [blame] | 24 | |
martin f. krafft | 3094327 | 2013-07-04 08:32:00 +0200 | [diff] [blame] | 25 | data = get_nodeinfo(storage_type, inventory_base_uri, nodes_uri, |
| 26 | classes_uri, minion_id) |
martin f. krafft | 42c475d | 2013-06-26 18:39:06 +0200 | [diff] [blame] | 27 | params = data.get('parameters', {}) |
| 28 | params['__reclass__'] = {} |
| 29 | params['__reclass__']['applications'] = data['applications'] |
| 30 | params['__reclass__']['classes'] = data['classes'] |
martin f. krafft | 42c475d | 2013-06-26 18:39:06 +0200 | [diff] [blame] | 31 | return params |
| 32 | |
| 33 | |
martin f. krafft | 3094327 | 2013-07-04 08:32:00 +0200 | [diff] [blame] | 34 | def top(storage_type=OPT_STORAGE_TYPE, |
| 35 | inventory_base_uri=OPT_INVENTORY_BASE_URI, nodes_uri=OPT_NODES_URI, |
| 36 | classes_uri=OPT_CLASSES_URI): |
martin f. krafft | 42c475d | 2013-06-26 18:39:06 +0200 | [diff] [blame] | 37 | |
martin f. krafft | 3094327 | 2013-07-04 08:32:00 +0200 | [diff] [blame] | 38 | data = get_inventory(storage_type, inventory_base_uri, nodes_uri, |
| 39 | classes_uri) |
martin f. krafft | 42c475d | 2013-06-26 18:39:06 +0200 | [diff] [blame] | 40 | env = 'base' |
| 41 | top = {env: {}} |
| 42 | # TODO: node environments |
| 43 | for node_id, node_data in data['nodes'].iteritems(): |
| 44 | #env = data.environment |
| 45 | #if env not in top: |
| 46 | # top[env] = {} |
| 47 | top[env][node_id] = node_data['applications'] |
| 48 | |
| 49 | return top |
martin f. krafft | 3094327 | 2013-07-04 08:32:00 +0200 | [diff] [blame] | 50 | |
| 51 | |
| 52 | def cli(): |
| 53 | try: |
| 54 | defaults = {'pretty_print' : True, |
| 55 | 'output' : 'yaml' |
| 56 | } |
| 57 | defaults.update(find_and_read_configfile()) |
| 58 | options = get_options(RECLASS_NAME, VERSION, DESCRIPTION, |
| 59 | inventory_shortopt='-t', |
| 60 | inventory_longopt='--top', |
| 61 | inventory_help='output the state tops (inventory)', |
| 62 | nodeinfo_shortopt='-p', |
| 63 | nodeinfo_longopt='--pillar', |
| 64 | nodeinfo_dest='nodename', |
| 65 | nodeinfo_help='output pillar data for a specific node', |
| 66 | defaults=defaults) |
| 67 | |
| 68 | if options.mode == MODE_NODEINFO: |
| 69 | data = ext_pillar(options.nodename, {}, |
| 70 | storage_type=options.storage_type, |
| 71 | inventory_base_uri=options.inventory_base_uri, |
| 72 | nodes_uri=options.nodes_uri, |
| 73 | classes_uri=options.classes_uri) |
| 74 | else: |
| 75 | data = top(storage_type=options.storage_type, |
| 76 | inventory_base_uri=options.inventory_base_uri, |
| 77 | nodes_uri=options.nodes_uri, |
| 78 | classes_uri=options.classes_uri) |
| 79 | |
| 80 | print output(data, options.output, options.pretty_print) |
| 81 | |
| 82 | except ReclassException, e: |
| 83 | e.exit_with_message(sys.stderr) |
| 84 | |
| 85 | sys.exit(posix.EX_OK) |
| 86 | |
| 87 | if __name__ == '__main__': |
| 88 | cli() |