summaryrefslogtreecommitdiffstats
path: root/bin/ohi
blob: 9c2ce8432511e943fdbacc2165b31b513021ef66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python
'''
Ohi = Openshift Host Inventory

This script provides an easy way to look at your host inventory.

This depends on multi_inventory being setup correctly.
'''
# vim: expandtab:tabstop=4:shiftwidth=4

import argparse
import sys
import os
import ConfigParser

from openshift_ansible import awsutil
from openshift_ansible import utils
from openshift_ansible.awsutil import ArgumentError

CONFIG_MAIN_SECTION = 'main'
CONFIG_HOST_TYPE_ALIAS_SECTION = 'host_type_aliases'


class Ohi(object):
    '''
        Class for managing openshift host inventory
    '''
    def __init__(self):
        self.host_type_aliases = {}
        self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)))

        # Default the config path to /etc
        self.config_path = os.path.join(os.path.sep, 'etc',  \
                                        'openshift_ansible', \
                                        'openshift_ansible.conf')

        self.args = None
        self.parse_cli_args()
        self.parse_config_file()

        self.aws = awsutil.AwsUtil(self.host_type_aliases)

    def run(self):
        '''
            Call into awsutil and retrieve the desired hosts and environments
        '''

        if self.args.list_host_types:
            self.aws.print_host_types()
            return 0

        if self.args.v3:
            version = '3'
        elif self.args.all_versions:
            version = 'all'
        else:
            version = '2'

        hosts = self.aws.get_host_list(clusters=self.args.cluster,
                                       host_type=self.args.host_type,
                                       sub_host_type=self.args.sub_host_type,
                                       envs=self.args.env,
                                       version=version,
                                       cached=self.args.cache_only)

        if hosts is None:
            # We weren't able to determine what they wanted to do
            raise ArgumentError("Invalid combination of arguments")

        if self.args.ip:
            hosts = self.aws.convert_to_ip(hosts)

        for host in sorted(hosts, key=utils.normalize_dnsname):
            if self.args.user:
                print "%s@%s" % (self.args.user, host)
            else:
                print host

        return 0

    def parse_config_file(self):
        '''
            Parse the config file for ohi
        '''
        if os.path.isfile(self.config_path):
            config = ConfigParser.ConfigParser()
            config.read(self.config_path)

            self.host_type_aliases = {}
            if config.has_section(CONFIG_HOST_TYPE_ALIAS_SECTION):
                for alias in config.options(CONFIG_HOST_TYPE_ALIAS_SECTION):
                    value = config.get(CONFIG_HOST_TYPE_ALIAS_SECTION, alias).split(',')
                    self.host_type_aliases[alias] = value

    def parse_cli_args(self):
        """Setup the command line parser with the options we want
        """

        parser = argparse.ArgumentParser(description='OpenShift Host Inventory')

        parser.add_argument('--list-host-types', default=False, action='store_true', help='List all of the host types')
        parser.add_argument('--list', default=False, action='store_true', help='List all hosts')

        parser.add_argument('-c', '--cluster', action="append", help="Which clusterid to use")
        parser.add_argument('-e', '--env', action="append", help="Which environment to use")

        parser.add_argument('-t', '--host-type', action="store", help="Which host type to use")

        parser.add_argument('-s', '--sub-host-type', action="store", help="Which sub host type to use")

        parser.add_argument('-l', '--user', action='store', default=None, help='username')

        parser.add_argument('--cache-only', action='store_true', default=False,
                            help='Retrieve the host inventory by cache only. Default is false.')

        parser.add_argument('--v2', action='store_true', default=True,
                            help='Specify the openshift version. Default is 2')

        parser.add_argument('--v3', action='store_true', default=False,
                            help='Specify the openshift version.')

        parser.add_argument('--ip', action='store_true', default=False,
                            help='Return ip address only.')

        parser.add_argument('--all-versions', action='store_true', default=False,
                            help='Specify the openshift version. Return all versions')

        self.args = parser.parse_args()

def main():
    '''
    Ohi will do its work here
    '''
    if len(sys.argv) == 1:
        print "\nError: No options given. Use --help to see the available options\n"
        sys.exit(0)

    try:
        ohi = Ohi()
        exitcode = ohi.run()
        sys.exit(exitcode)
    except ArgumentError as err:
        print "\nError: %s\n" % err.message

if __name__ == '__main__':
    main()