summaryrefslogtreecommitdiffstats
path: root/ipatests/ipa-test-task
blob: 02a04b6ab3fabbefec0ea6b33e2d37b955e529f3 (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#! /usr/bin/python2

# Authors:
#   Petr Viktorin <pviktori@redhat.com>
#
# Copyright (C) 2013  Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import sys
import os
import argparse

from ipapython.ipa_log_manager import log_mgr, standard_logging_setup
from ipatests.test_integration import config
from ipatests.test_integration import tasks
from ipatests.test_integration.host import Host
from ipatests.beakerlib_plugin import BeakerLibProcess


log = log_mgr.get_logger(__name__)


class TaskRunner(object):
    def __init__(self):
        self._prepared_hosts = set()

    def get_parser(self):
        parser = argparse.ArgumentParser(
            description="Perform an operation for integration testing. "
                        "All operations are performed on configured hosts, see "
                        "http://www.freeipa.org/page/V3/Integration_testing "
                        "for configuration details")

        parser.add_argument('--with-beakerlib', action='store_true',
                            dest='with_beakerlib',
                            help="""Issue BeakerLib commands for logging
                                    and log collection""")

        subparsers = parser.add_subparsers(
            metavar='SUBCOMMAND',
            help='The action to perform (* indicates an idempotent operation)')

        subparser = subparsers.add_parser(
            'install-topo',
            help='Install IPA in a given topology')
        subparser.add_argument('topo',
                            metavar='TOPO',
                            help='Desired topology '
                                '(see `ipa-test-task list-topos` for details)',
                            choices=tasks.topologies)
        subparser.add_argument('--skip-master', action='store_true',
                            help='Skip installing master')
        subparser.add_argument('--skip-clients', action='store_true',
                            help='Skip installing clients')
        subparser.add_argument('--master', type=str,
                            help='Master to use (Default: from config)')
        subparser.add_argument('--replicas', type=str, nargs='*',
                            help='Replicas to install (Default: from config)')
        subparser.add_argument('--clients', type=str, nargs='*',
                            help='Clients to install (Default: from config)')
        subparser.set_defaults(func=self.install_topo)

        subparser = subparsers.add_parser(
            'list-topos',
            help='List the available topologies')
        subparser.set_defaults(func=self.list_topos)

        subparser = subparsers.add_parser(
            'install-master',
            help='Install IPA on the master')
        subparser.add_argument('--host', type=str,
                            help='Host to use (Default: from config)')
        subparser.set_defaults(func=self.install_master)

        subparser = subparsers.add_parser(
            'install-replica',
            help='Install an IPA replica')
        subparser.add_argument('replica', type=str,
                            help='Replica to install')
        subparser.add_argument('--master', type=str,
                            help="""Master to replicate from
                                    (Default: from config)""")
        subparser.set_defaults(func=self.install_replica)

        subparser = subparsers.add_parser(
            'install-client',
            help='Install an IPA client')
        subparser.add_argument('client', type=str,
                            help='Client to install')
        subparser.add_argument('--master', type=str,
                            help="""Master to replicate from
                                    (Default: from config)""")
        subparser.set_defaults(func=self.install_client)

        subparser = subparsers.add_parser(
            'connect-replica',
            help='Connect two IPA masters')
        subparser.add_argument('host1', type=str,
                            help='First replica to connect')
        subparser.add_argument('host2', type=str,
                            help='Second replica to connect')
        subparser.set_defaults(func=self.connect_replica)

        subparser = subparsers.add_parser(
            'disconnect-replica',
            help='Disconnect two IPA masters')
        subparser.add_argument('host1', type=str,
                            help='First replica to disconnect')
        subparser.add_argument('host2', type=str,
                            help='Second replica to disconnect')
        subparser.set_defaults(func=self.disconnect_replica)

        subparser = subparsers.add_parser(
            'uninstall-server',
            help='Uninstall IPA server *')
        subparser.add_argument('host', type=str, nargs='*',
                            help="""Host to use
                                    (Default: master and all replicas
                                    from config)""")
        subparser.set_defaults(func=self.uninstall_master)

        subparser = subparsers.add_parser(
            'uninstall-client',
            help='Uninstall IPA client *')
        subparser.add_argument('host', type=str, nargs='*',
                            help="""Host to use
                                    (Default: all clients from config)""")
        subparser.set_defaults(func=self.uninstall_client)

        subparser = subparsers.add_parser(
            'uninstall-all',
            help='Uninstall all hosts, according to config *')
        subparser.set_defaults(func=self.uninstall_all, host=None)

        subparser = subparsers.add_parser(
            'cleanup',
            help='Clean up a host *')
        subparser.add_argument('host', type=str, nargs='*',
                            help="""Host to clean up
                                    (Default: all hosts from config)""")
        subparser.set_defaults(func=self.cleanup)

        subparser = subparsers.add_parser(
            'install-adtrust',
            help='Runs ipa-adtrust-install on the host')
        subparser.add_argument('host', type=str,
                            help='Host to run ipa-adtrust-install on')
        subparser.set_defaults(func=self.install_adtrust)

        subparser = subparsers.add_parser(
            'configure-dns-for-trust',
            help='Sets DNS on the given host for trust with the given AD')
        subparser.add_argument('host', type=str,
                            help='Host to change DNS configuration on')
        subparser.add_argument('ad', type=str,
                            help='AD that trust will be established with')
        subparser.set_defaults(func=self.configure_dns_for_trust)

        subparser = subparsers.add_parser(
            'establish-trust-with-ad',
            help='Establishes trust between IPA host and AD')
        subparser.add_argument('host', type=str,
                            help='IPA Host to establish AD trust on')
        subparser.add_argument('ad', type=str,
                            help='AD to establish trust with')
        subparser.set_defaults(func=self.establish_trust_with_ad)

        subparser = subparsers.add_parser(
            'remove-trust-with-ad',
            help='Removes trust between IPA host and AD')
        subparser.add_argument('host', type=str,
                            help='IPA Host to remove AD trust on')
        subparser.add_argument('ad', type=str,
                            help='AD to remove trust with')
        subparser.set_defaults(func=self.remove_trust_with_ad)

        subparser = subparsers.add_parser(
            'configure-auth-to-local-rule',
            help='Configures auth_to_local rule on IPA host with respect to AD')
        subparser.add_argument('host', type=str,
                            help='IPA Host to configure auth_to_local rule on')
        subparser.add_argument('ad', type=str,
                            help='AD to configure the rule with')
        subparser.set_defaults(func=self.configure_auth_to_local_rule)

        subparser = subparsers.add_parser(
            'clear-sssd-cache',
            help='Clears SSSD cache on the IPA host.')
        subparser.add_argument('host', type=str,
                            help='IPA Host to clear SSSD cache on')
        subparser.set_defaults(func=self.clear_sssd_cache)

        subparser = subparsers.add_parser(
            'setup-sssd-debugging',
            help='Turns on SSSD debugging levels.')
        subparser.add_argument('host', type=str,
                            help='IPA Host to turn SSSD debugging on')
        subparser.set_defaults(func=self.setup_sssd_debugging)

        subparser = subparsers.add_parser(
            'sync-time',
            help='Synchronize time on host with respect to server')
        subparser.add_argument('host', type=str,
                            help='IPA Host to set the time on')
        subparser.add_argument('server', type=str,
                            help='Server that serves as a time source')
        subparser.set_defaults(func=self.sync_time)

        subparser = subparsers.add_parser(
            'add-a-records-in-master-domain',
            help='Adds A records to the IPA master for all the hosts in the '
                 'master domain.')
        subparser.add_argument('master', type=str,
                            help='IPA master to add records on')
        subparser.set_defaults(
            func=self.add_a_records_for_hosts_in_master_domain)

        subparser = subparsers.add_parser(
            'add-a-record',
            help='Adds A record for the host to the IPA master')
        subparser.add_argument('master', type=str,
                            help='IPA master to add record on')
        subparser.add_argument('host', type=str,
                            help='Host whose record should be added')
        subparser.set_defaults(func=self.add_a_record)

        return parser

    def main(self, argv):

        args = self.get_parser().parse_args(argv)
        self.config = config.Config.from_env(os.environ)

        logs_to_collect = {}

        def collect_log(host, filename):
            logs_to_collect.setdefault(host, []).append(filename)

        self.collect_log = collect_log

        if args.with_beakerlib:
            beakerlib_process = BeakerLibProcess()
            args.verbose = True

        standard_logging_setup(
            console_format='%(name)s: %(levelname)s: %(message)s',
            debug=True)

        if not self.config.domains:
            raise SystemExit('No configuration available')

        args.domain = self.config.domains[0]

        if self.config.ad_domains:
            args.ad_domain = self.config.ad_domains[0]
        else:
            args.ad_domain = None

        import logging; logging.basicConfig()

        try:
            return args.func(args)
        except Exception, e:
            if args.with_beakerlib:
                beakerlib_process.log_exception()
                beakerlib_process.run_beakerlib_command(
                    ['rlFail', 'Unhandled exception'])
            raise
        finally:
            if args.with_beakerlib:
                beakerlib_process.end()
                beakerlib_process.collect_logs(logs_to_collect)
            for host in self._prepared_hosts:
                host.remove_log_collector(self.collect_log)

    def get_host(self, host_name, default=None):
        if host_name is None:
            host = default
        else:
            host = self.config.host_by_name(host_name)
        return self.prepare_host(host)

    def get_hosts(self, host_names, default=()):
        if host_names is None:
            host_names = ()
        hosts = [self.get_host(host_name) for host_name in host_names]
        if hosts:
            return hosts
        else:
            return [self.prepare_host(h) for h in default]

    def prepare_host(self, host):
        # Prepare only UNIX hosts
        if host not in self._prepared_hosts and isinstance(host, Host):
            host.add_log_collector(self.collect_log)
            tasks.prepare_host(host)
            self._prepared_hosts.add(host)
        return host

    def require_ad_domain(self, args):
        if not args.ad_domain:
            SystemExit("At least one AD domain is required for this task")

    def install_master(self, args):
        master = self.get_host(args.host, default=args.domain.master)
        log.info('Installing master %s', master.hostname)
        tasks.install_master(master)

    def install_replica(self, args):
        replica = self.get_host(args.replica)
        master = self.get_host(args.master, default=args.domain.master)
        log.info('Installing replica %s from %s',
                replica.hostname, master.hostname)
        tasks.install_replica(master, replica)

    def install_client(self, args):
        client = self.get_host(args.client)
        master = self.get_host(args.master, default=args.domain.master)
        log.info('Installing client %s on %s', client.hostname, master.hostname)
        tasks.install_client(master, client)

    def uninstall_master(self, args):
        default_hosts = [args.domain.master] + args.domain.replicas
        hosts = self.get_hosts(args.host, default=default_hosts)
        log.info('Uninstalling masters: %s', [h.hostname for h in hosts])
        for master in hosts:
            log.info('Uninstalling %s', master.hostname)
            tasks.uninstall_master(master)

    def uninstall_client(self, args):
        default_hosts = args.domain.clients
        hosts = self.get_hosts(args.host, default=default_hosts)
        log.info('Uninstalling clients: %s', [h.hostname for h in hosts])
        for client in hosts:
            log.info('Uninstalling %s', client.hostname)
            tasks.uninstall_client(client)

    def uninstall_all(self, args):
        self.uninstall_master(args)
        self.uninstall_client(args)

    def cleanup(self, args):
        default_hosts = args.domain.hosts
        hosts = self.get_hosts(args.host, default=default_hosts)
        log.info('Cleaning up hosts: %s', [h.hostname for h in hosts])
        for host in hosts:
            log.info('Cleaning up %s', host.hostname)
            tasks.unapply_fixes(host)

    def connect_replica(self, args):
        host1 = self.get_host(args.host1)
        host2 = self.get_host(args.host2)
        tasks.connect_replica(host1, host2)

    def disconnect_replica(self, args):
        host1 = self.get_host(args.host1)
        host2 = self.get_host(args.host2)
        tasks.disconnect_replica(host1, host2)

    def list_topos(self, args):
        for name, topo in tasks.topologies.items():
            print '%s: %s' % (name, topo.__doc__)

    def install_topo(self, args):
        master = self.get_host(args.master, default=args.domain.master)
        replicas = self.get_hosts(args.replicas, default=args.domain.replicas)
        clients = self.get_hosts(args.clients, default=args.domain.clients)
        if args.skip_clients:
            clients = []
        tasks.install_topo(args.topo, master, replicas, clients,
                           skip_master=args.skip_master)

    def install_adtrust(self, args):
        master = self.get_host(args.host, default=args.domain.master)
        log.info('Configuring AD trust support on %s', master.hostname)
        tasks.install_adtrust(master)

    def configure_dns_for_trust(self, args):
        self.require_ad_domain(args)
        host = self.get_host(args.host, default=args.domain.master)
        ad = self.get_host(args.ad, default=args.ad_domain.ads[0])
        tasks.configure_dns_for_trust(host, ad)

    def establish_trust_with_ad(self, args):
        self.require_ad_domain(args)
        host = self.get_host(args.host, default=args.domain.master)
        ad = self.get_host(args.ad, default=args.ad_domain.ads[0])
        tasks.establish_trust_with_ad(host, ad)

    def remove_trust_with_ad(self, args):
        self.require_ad_domain(args)
        host = self.get_host(args.host, default=args.domain.master)
        ad = self.get_host(args.ad, default=args.ad_domain.ads[0])
        tasks.remove_trust_with_ad(host, ad)

    def configure_auth_to_local_rule(self, args):
        self.require_ad_domain(args)
        host = self.get_host(args.host, default=args.domain.master)
        ad = self.get_host(args.ad, default=args.ad_domain.ads[0])
        tasks.configure_auth_to_local_rule(host, ad)

    def clear_sssd_cache(self, args):
        host = self.get_host(args.host, default=args.domain.master)
        tasks.clear_sssd_cache(host)

    def setup_sssd_debugging(self, args):
        host = self.get_host(args.host, default=args.domain.master)
        tasks.setup_sssd_debugging(host)

    def sync_time(self, args):
        host = self.get_host(args.host, default=args.domain.master)
        server = self.get_host(args.server)
        tasks.sync_time(host, server)

    def add_a_records_for_hosts_in_master_domain(self, args):
        master = self.get_host(args.master, default=args.domain.master)
        tasks.add_a_records_for_hosts_in_master_domain(master)

    def add_a_record(self, args):
        master = self.get_host(args.master, default=args.domain.master)
        host = self.get_host(args.host)
        tasks.add_a_record(master, host)

if __name__ == '__main__':
    exit(TaskRunner().main(sys.argv[1:]))