summaryrefslogtreecommitdiffstats
path: root/utils/src/ooinstall/oo_config.py
blob: e37892c9bdd404ca3564e514433225a3379e931e (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# pylint: disable=bad-continuation,missing-docstring,no-self-use,invalid-name,too-many-instance-attributes,too-few-public-methods

import os
import sys
import yaml
from pkg_resources import resource_filename

CONFIG_PERSIST_SETTINGS = [
    'ansible_ssh_user',
    'ansible_callback_facts_yaml',
    'ansible_config',
    'ansible_inventory_path',
    'ansible_log_path',
    'deployment',
    'version',
    'variant',
    'variant_version',
    ]

DEPLOYMENT_PERSIST_SETTINGS = [
    'master_routingconfig_subdomain',
    'proxy_http',
    'proxy_https',
    'proxy_exclude_hosts',
]

DEFAULT_REQUIRED_FACTS = ['ip', 'public_ip', 'hostname', 'public_hostname']
PRECONFIGURED_REQUIRED_FACTS = ['hostname', 'public_hostname']

class OOConfigFileError(Exception):
    """The provided config file path can't be read/written
    """
    pass


class OOConfigInvalidHostError(Exception):
    """ Host in config is missing both ip and hostname. """
    pass


class Host(object):
    """ A system we will or have installed OpenShift on. """
    def __init__(self, **kwargs):
        self.ip = kwargs.get('ip', None)
        self.hostname = kwargs.get('hostname', None)
        self.public_ip = kwargs.get('public_ip', None)
        self.public_hostname = kwargs.get('public_hostname', None)
        self.connect_to = kwargs.get('connect_to', None)

        self.preconfigured = kwargs.get('preconfigured', None)
        self.schedulable = kwargs.get('schedulable', None)
        self.new_host = kwargs.get('new_host', None)
        self.containerized = kwargs.get('containerized', False)
        self.node_labels = kwargs.get('node_labels', '')

        # allowable roles: master, node, etcd, storage, master_lb, new
        self.roles = kwargs.get('roles', [])

        self.other_variables = kwargs.get('other_variables', {})

        if self.connect_to is None:
            raise OOConfigInvalidHostError(
                "You must specify either an ip or hostname as 'connect_to'")

    def __str__(self):
        return self.connect_to

    def __repr__(self):
        return self.connect_to

    def to_dict(self):
        """ Used when exporting to yaml. """
        d = {}

        for prop in ['ip', 'hostname', 'public_ip', 'public_hostname', 'connect_to',
                     'preconfigured', 'containerized', 'schedulable', 'roles', 'node_labels',
                     'other_variables']:
            # If the property is defined (not None or False), export it:
            if getattr(self, prop):
                d[prop] = getattr(self, prop)
        return d

    def is_master(self):
        return 'master' in self.roles

    def is_node(self):
        return 'node' in self.roles

    def is_master_lb(self):
        return 'master_lb' in self.roles

    def is_storage(self):
        return 'storage' in self.roles


    def is_etcd_member(self, all_hosts):
        """ Will this host be a member of a standalone etcd cluster. """
        if not self.is_master():
            return False
        masters = [host for host in all_hosts if host.is_master()]
        if len(masters) > 1:
            return True
        return False

    def is_dedicated_node(self):
        """ Will this host be a dedicated node. (not a master) """
        return self.is_node() and not self.is_master()

    def is_schedulable_node(self, all_hosts):
        """ Will this host be a node marked as schedulable. """
        if not self.is_node():
            return False
        if not self.is_master():
            return True

        masters = [host for host in all_hosts if host.is_master()]
        nodes = [host for host in all_hosts if host.is_node()]
        if len(masters) == len(nodes):
            return True
        return False


class Role(object):
    """ A role that will be applied to a host. """
    def __init__(self, name, variables):
        self.name = name
        self.variables = variables

    def __str__(self):
        return self.name

    def __repr__(self):
        return self.name

    def to_dict(self):
        """ Used when exporting to yaml. """
        d = {}
        for prop in ['name', 'variables']:
            # If the property is defined (not None or False), export it:
            if getattr(self, prop):
                d[prop] = getattr(self, prop)
        return d


class Deployment(object):
    def __init__(self, **kwargs):
        self.hosts = kwargs.get('hosts', [])
        self.roles = kwargs.get('roles', {})
        self.variables = kwargs.get('variables', {})


class OOConfig(object):
    default_dir = os.path.normpath(
        os.environ.get('XDG_CONFIG_HOME',
                       os.environ['HOME'] + '/.config/') + '/openshift/')
    default_file = '/installer.cfg.yml'

    def __init__(self, config_path):
        if config_path:
            self.config_path = os.path.normpath(config_path)
        else:
            self.config_path = os.path.normpath(self.default_dir +
                                                self.default_file)
        self.deployment = Deployment(hosts=[], roles={}, variables={})
        self.settings = {}
        self._read_config()
        self._set_defaults()


    def _read_config(self):
        try:
            if os.path.exists(self.config_path):
                with open(self.config_path, 'r') as cfgfile:
                    loaded_config = yaml.safe_load(cfgfile.read())

                # Use the presence of a Description as an indicator this is
                # a legacy config file:
                if 'Description' in self.settings:
                    self._upgrade_legacy_config()

                try:
                    host_list = loaded_config['deployment']['hosts']
                    role_list = loaded_config['deployment']['roles']
                except KeyError as e:
                    print "Error loading config, no such key: {}".format(e)
                    sys.exit(0)

                for setting in CONFIG_PERSIST_SETTINGS:
                    try:
                        self.settings[setting] = str(loaded_config[setting])
                    except KeyError:
                        continue

                for setting in DEPLOYMENT_PERSIST_SETTINGS:
                    try:
                        self.deployment.variables[setting] = \
                            str(loaded_config['deployment'][setting])
                    except KeyError:
                        continue

                # Parse the hosts into DTO objects:
                for host in host_list:
                    self.deployment.hosts.append(Host(**host))

                # Parse the roles into Objects
                for name, variables in role_list.iteritems():
                    self.deployment.roles.update({name: Role(name, variables)})


        except IOError, ferr:
            raise OOConfigFileError('Cannot open config file "{}": {}'.format(ferr.filename,
                                                                              ferr.strerror))
        except yaml.scanner.ScannerError:
            raise OOConfigFileError(
                'Config file "{}" is not a valid YAML document'.format(self.config_path))

    def _upgrade_legacy_config(self):
        new_hosts = []
        remove_settings = ['validated_facts', 'Description', 'Name',
            'Subscription', 'Vendor', 'Version', 'masters', 'nodes']

        if 'validated_facts' in self.settings:
            for key, value in self.settings['validated_facts'].iteritems():
                value['connect_to'] = key
                if 'masters' in self.settings and key in self.settings['masters']:
                    value['master'] = True
                if 'nodes' in self.settings and key in self.settings['nodes']:
                    value['node'] = True
                new_hosts.append(value)
        self.settings['hosts'] = new_hosts

        for s in remove_settings:
            if s in self.settings:
                del self.settings[s]

        # A legacy config implies openshift-enterprise 3.0:
        self.settings['variant'] = 'openshift-enterprise'
        self.settings['variant_version'] = '3.0'

    def _upgrade_v1_config(self):
        #TODO write code to upgrade old config
        return

    def _set_defaults(self):

        if 'ansible_inventory_directory' not in self.settings:
            self.settings['ansible_inventory_directory'] = self._default_ansible_inv_dir()
        if not os.path.exists(self.settings['ansible_inventory_directory']):
            os.makedirs(self.settings['ansible_inventory_directory'])
        if 'ansible_plugins_directory' not in self.settings:
            self.settings['ansible_plugins_directory'] = \
                resource_filename(__name__, 'ansible_plugins')
        if 'version' not in self.settings:
            self.settings['version'] = 'v2'

        if 'ansible_callback_facts_yaml' not in self.settings:
            self.settings['ansible_callback_facts_yaml'] = '%s/callback_facts.yaml' % \
                self.settings['ansible_inventory_directory']

        if 'ansible_ssh_user' not in self.settings:
            self.settings['ansible_ssh_user'] = ''

        self.settings['ansible_inventory_path'] = \
            '{}/hosts'.format(os.path.dirname(self.config_path))

        # clean up any empty sets
        for setting in self.settings.keys():
            if not self.settings[setting]:
                self.settings.pop(setting)

    def _default_ansible_inv_dir(self):
        return os.path.normpath(
            os.path.dirname(self.config_path) + "/.ansible")

    def calc_missing_facts(self):
        """
        Determine which host facts are not defined in the config.

        Returns a hash of host to a list of the missing facts.
        """
        result = {}

        for host in self.deployment.hosts:
            missing_facts = []
            if host.preconfigured:
                required_facts = PRECONFIGURED_REQUIRED_FACTS
            else:
                required_facts = DEFAULT_REQUIRED_FACTS

            for required_fact in required_facts:
                if not getattr(host, required_fact):
                    missing_facts.append(required_fact)
            if len(missing_facts) > 0:
                result[host.connect_to] = missing_facts
        return result

    def save_to_disk(self):
        out_file = open(self.config_path, 'w')
        out_file.write(self.yaml())
        out_file.close()

    def persist_settings(self):
        p_settings = {}

        for setting in CONFIG_PERSIST_SETTINGS:
            if setting in self.settings and self.settings[setting]:
                p_settings[setting] = self.settings[setting]


        p_settings['deployment'] = {}
        p_settings['deployment']['hosts'] = []
        p_settings['deployment']['roles'] = {}

        for setting in DEPLOYMENT_PERSIST_SETTINGS:
            if setting in self.deployment.variables:
                p_settings['deployment'][setting] = self.deployment.variables[setting]

        for host in self.deployment.hosts:
            p_settings['deployment']['hosts'].append(host.to_dict())

        for name, role in self.deployment.roles.iteritems():
            p_settings['deployment']['roles'][name] = role.variables

        try:
            p_settings['variant'] = self.settings['variant']
            p_settings['variant_version'] = self.settings['variant_version']

            if self.settings['ansible_inventory_directory'] != self._default_ansible_inv_dir():
                p_settings['ansible_inventory_directory'] = self.settings['ansible_inventory_directory']
        except KeyError as e:
            print "Error persisting settings: {}".format(e)
            sys.exit(0)


        return p_settings

    def yaml(self):
        return yaml.safe_dump(self.persist_settings(), default_flow_style=False)

    def __str__(self):
        return self.yaml()

    def get_host(self, name):
        for host in self.deployment.hosts:
            if host.connect_to == name:
                return host
        return None