Debian Codegraph @

ipatests/ipa-test-config

raw
1 #!/usr/bin/python3
2
3 # Authors:
4 # Petr Viktorin <[email protected]>
5 #
6 # Copyright (C) 2013 Red Hat
7 # see file 'COPYING' for use and warranty information
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22 from __future__ import print_function
23
24 import sys
25 import os
26 import argparse
27 import json
28
29 from ipalib.constants import FQDN
30 from ipatests.pytest_ipa.integration import config, env_config
31
32
33 def main(argv):
34 parser = argparse.ArgumentParser(
35 description='Prints out IPA test configuration for use in shell scripts.'
36 'IPA integration tests are configured via environment variables')
37
38 parser.add_argument('host', nargs='?',
39 help='Print config for the given hostname')
40
41 parser.add_argument('--global', action='store_true', dest='global_',
42 help='Print global config (not specific to a host '
43 'or domain)')
44
45 parser.add_argument('--domain',
46 help='IPA domain name, or number (the X in _envX)')
47
48 parser.add_argument('--master',
49 help='Print config for the master',
50 action='store_true')
51
52 parser.add_argument('--replica', type=int,
53 help='Print config for the replica with this number')
54
55 parser.add_argument('--client', type=int,
56 help='Print config for the client with this number')
57
58 parser.add_argument('--role', type=str,
59 help='Print config for machine with this role')
60
61 parser.add_argument('--no-simple', dest='simple', action='store_false',
62 help='Do not print Simple Vars '
63 '(normally included backwards-compatibility)')
64
65 parser.add_argument('--yaml', action='store_const', dest='output_format',
66 const='yaml',
67 help='Output configuration in YAML format')
68
69 parser.add_argument('--json', action='store_const', dest='output_format',
70 const='json',
71 help='Output configuration in JSON format')
72
73 args = parser.parse_args(argv)
74
75 hostsargs = [bool(args.host), bool(args.master), bool(args.replica),
76 bool(args.client)]
77 if hostsargs.count(True) > 1:
78 parser.error('Must specify at most one of host selection options')
79 if any(hostsargs) or args.domain:
80 if args.global_:
81 parser.error('--global may not be combined with host selection options')
82 else:
83 args.host = FQDN
84
85 kwargs = {}
86 if not args.simple:
87 kwargs['simple'] = False
88
89 conf = config.Config.from_env(os.environ)
90
91 if args.output_format == 'json':
92 return json.dumps(conf.to_dict(), indent=2)
93 elif args.output_format == 'yaml':
94 import yaml
95 return yaml.safe_dump(conf.to_dict(), default_flow_style=False)
96 else:
97 env = get_object(conf, args).to_env(**kwargs)
98 return env_config.env_to_script(env)
99
100
101 def get_object(conf, args):
102 if args.global_:
103 return conf
104 elif args.host:
105 try:
106 return conf.host_by_name(args.host)
107 except LookupError:
108 sys.exit('Host %s not found in config. Try --global' % args.host)
109 else:
110 if args.domain:
111 try:
112 num = int(args.domain) - 1
113 except ValueError:
114 domains = [d for d in conf.domains if d.name == args.domain]
115 if not domains:
116 sys.exit('Domain %s not found' % args.domain)
117 domain = domains[0]
118 else:
119 try:
120 domain = conf.domains[num]
121 except LookupError:
122 sys.exit('Domain %s not found.' % args.domain)
123 else:
124 try:
125 domain = conf.domains[0]
126 except IndexError:
127 sys.exit('No domains are configured.')
128 if args.master:
129 return domain.master
130
131 elif args.replica:
132 num = int(args.replica) - 1
133 try:
134 return domain.replicas[args.replica]
135 except LookupError:
136 sys.exit(
137 'Domain %s not found in domain %s'
138 % (args.replica, domain.name)
139 )
140
141 elif args.client:
142 num = int(args.client) - 1
143 try:
144 return domain.replicas[args.client]
145 except LookupError:
146 sys.exit(
147 'Client %s not found in domain %s'
148 % (args.client, domain.name)
149 )
150
151 elif args.role:
152 try:
153 return domain.get_host_by_role(args.role)
154 except LookupError:
155 sys.exit(
156 'No host with role %s not found in domain %s'
157 % (args.role, domain.name)
158 )
159
160 else:
161 return domain
162
163
164 if __name__ == '__main__':
165 print(main(sys.argv[1:]), end=' ')