/
toolopts.py
342 lines (308 loc) · 13.2 KB
/
toolopts.py
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
# -*- coding: utf-8 -*-
#
# Copyright(C) 2016 Red Hat, Inc.
#
# 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 <https://www.gnu.org/licenses/>.
import logging
import optparse
import sys
from convert2rhel import __version__, utils
loggerinst = logging.getLogger(__name__)
class ToolOpts(object):
def __init__(self):
self.debug = False
self.username = None
self.password_file = None
self.password = None
self.no_rhsm = False
self.enablerepo = []
self.disablerepo = []
self.pool = None
self.serverurl = None
self.autoaccept = None
self.auto_attach = None
self.restart = None
self.activation_key = None
self.org = None
self.arch = None
self.no_rpm_va = False
self.disable_colors = False
self.keep_rhsm = False
# set True when credentials (username & password) are given through CLI
self.credentials_thru_cli = False
class CLI(object):
def __init__(self):
self._parser = self._get_argparser()
self._register_options()
self._process_cli_options()
@staticmethod
def _get_argparser():
usage = (
"\n"
" convert2rhel [-h]\n"
" convert2rhel [--version]\n"
" convert2rhel [-u username] [-p password | -f pswd_file] [--pool pool_id | -a] [--disablerepo repoid]"
" [--enablerepo repoid] [--serverurl url] [--keep-rhsm] [--no-rpm-va] [--debug] [--restart]"
" [--disable-colors] [-y]\n"
" convert2rhel [--no-rhsm] [--disablerepo repoid]"
" [--enablerepo repoid] [--no-rpm-va] [--debug] [--restart] [--disable-colors] [-y]\n"
" convert2rhel [-k key] [-o organization] [--pool pool_id | -a] [--disablerepo repoid] [--enablerepo"
" repoid] [--serverurl url] [--keep-rhsm] [--no-rpm-va] [--debug] [--restart] [--disable-colors] [-y]"
"\n\n"
"WARNING: The tool needs to be run under the root user"
)
return optparse.OptionParser(
conflict_handler="resolve",
usage=usage,
add_help_option=False,
version=__version__,
)
def _register_options(self):
"""Prescribe what command line options the tool accepts."""
self._parser.add_option(
"-h",
"--help",
action="help",
help="Show help message and exit.",
)
self._parser.add_option(
"--version",
action="version",
help="Show convert2rhel version and exit.",
)
self._parser.add_option(
"--debug",
action="store_true",
help="Print traceback in case of an abnormal exit and messages that could help find an issue.",
)
self._parser.add_option(
"--disable-colors",
action="store_true",
help="Disable color output",
)
# Importing here instead of on top of the file to avoid cyclic dependency
from convert2rhel.systeminfo import POST_RPM_VA_LOG_FILENAME, PRE_RPM_VA_LOG_FILENAME
self._parser.add_option(
"--no-rpm-va",
action="store_true",
help="Skip gathering changed rpm files using"
" 'rpm -Va'. By default it's performed before and after the conversion with the output"
" stored in log files %s and %s. At the end of the conversion, these logs are compared"
" to show you what rpm files have been affected by the conversion."
% (PRE_RPM_VA_LOG_FILENAME, POST_RPM_VA_LOG_FILENAME),
)
self._parser.add_option(
"--enablerepo",
metavar="repoidglob",
action="append",
help="Enable specific"
" repositories by ID or glob. For more repositories to enable, use this option"
" multiple times. If you don't use the --no-rhsm option, you can use this option"
" to override the default RHEL repoids that convert2rhel enables through"
" subscription-manager.",
)
self._parser.add_option(
"--disablerepo",
metavar="repoidglob",
action="append",
help="Disable specific"
" repositories by ID or glob. For more repositories to disable, use this option"
" multiple times. This option defaults to all repositories ('*').",
)
group = optparse.OptionGroup(
self._parser,
"Subscription Manager Options",
"The following options are specific to using subscription-manager.",
)
group.add_option(
"-u",
"--username",
help="Username for the"
" subscription-manager. If neither --username nor"
" --activation-key option is used, the user"
" is asked to enter the username.",
)
group.add_option(
"-p",
"--password",
help="Password for the"
" subscription-manager. If --password,"
" --password-from-file or --activationkey are not"
" used, the user is asked to enter the password.",
)
group.add_option(
"-f",
"--password-from-file",
help="File containing"
" password for the subscription-manager in the plain"
" text form. It's an alternative to the --password"
" option.",
)
group.add_option(
"-k",
"--activationkey",
help="Activation key used"
" for the system registration by the"
" subscription-manager. It requires to have the --org"
" option specified.",
)
group.add_option(
"-o",
"--org",
help="Organization with which the"
" system will be registered by the"
" subscription-manager. A list of available"
" organizations is possible to obtain by running"
" 'subscription-manager orgs'. From the listed pairs"
" Name:Key, use the Key here.",
)
group.add_option(
"-a",
"--auto-attach",
help="Automatically attach compatible subscriptions to the system.",
action="store_true",
)
group.add_option(
"--pool",
help="Subscription pool ID. If not used,"
" the user is asked to choose from the available"
" subscriptions. A list of the available"
" subscriptions is possible to obtain by running"
" 'subscription-manager list --available'.",
)
group.add_option(
"-v",
"--variant",
help="This option is not supported anymore and has no effect. When"
" converting a system to RHEL 6 or 7 using subscription-manager,"
" the system is now always converted to the Server variant. In case"
" of using custom repositories, the system is converted to the variant"
" provided by these repositories.",
)
group.add_option(
"--serverurl",
help="Hostname of the subscription service with which to register the system through subscription-manager."
" The default is the Customer Portal Subscription Management service. It is not to be used to specify a"
" Satellite server. For that, read the product documentation at https://access.redhat.com/.",
)
group.add_option(
"--keep-rhsm",
action="store_true",
help="Keep the already installed Red Hat Subscription Management-related packages. By default,"
" during the conversion, these packages are removed, downloaded from verified sources and re-installed."
" This option is suitable for environments with no connection to the Internet, or for systems managed by"
" Red Hat Satellite. Warning: The system is being re-registered during the conversion and when the"
" re-registration fails, there's no automated rollback to the original registration.",
)
self._parser.add_option_group(group)
group = optparse.OptionGroup(
self._parser,
"Alternative Installation Options",
"The following options are required if you do not intend on using subscription-manager",
)
group.add_option(
"--disable-submgr",
action="store_true",
help="Replaced by --no-rhsm. Both options have the same effect.",
)
group.add_option(
"--no-rhsm",
action="store_true",
help="Do not use the subscription-manager, use custom repositories instead. See --enablerepo/--disablerepo"
" options. Without this option, the subscription-manager is used to access RHEL repositories by default."
" Using this option requires to have the --enablerepo specified.",
)
self._parser.add_option_group(group)
group = optparse.OptionGroup(
self._parser,
"Automation Options",
"The following options are used to automate the installation",
)
group.add_option(
"-r",
"--restart",
help="Restart the system when it is successfully converted to RHEL to boot the new RHEL kernel.",
action="store_true",
)
group.add_option(
"-y",
help="Answer yes to all yes/no questions the tool asks.",
action="store_true",
)
self._parser.add_option_group(group)
def _process_cli_options(self):
"""Process command line options used with the tool."""
warn_on_unsupported_options()
parsed_opts, _ = self._parser.parse_args()
global tool_opts # pylint: disable=C0103
if parsed_opts.debug:
tool_opts.debug = True
if parsed_opts.disable_colors:
tool_opts.disable_colors = True
if parsed_opts.no_rpm_va:
tool_opts.no_rpm_va = True
if parsed_opts.username:
tool_opts.username = parsed_opts.username
if parsed_opts.password:
tool_opts.password = parsed_opts.password
if parsed_opts.password_from_file:
tool_opts.password_file = parsed_opts.password_from_file
tool_opts.password = utils.get_file_content(parsed_opts.password_from_file)
if parsed_opts.enablerepo:
tool_opts.enablerepo = parsed_opts.enablerepo
if parsed_opts.disablerepo:
tool_opts.disablerepo = parsed_opts.disablerepo
if parsed_opts.no_rhsm or parsed_opts.disable_submgr:
tool_opts.no_rhsm = True
if not tool_opts.enablerepo:
loggerinst.critical("The --enablerepo option is required when --disable-submgr or --no-rhsm is used.")
if not tool_opts.disablerepo:
# Default to disable every repo except:
# - the ones passed through --enablerepo
# - the ones enabled through subscription-manager based on convert2rhel config files
tool_opts.disablerepo = ["*"]
if parsed_opts.pool:
tool_opts.pool = parsed_opts.pool
if parsed_opts.serverurl:
if tool_opts.no_rhsm:
loggerinst.warning(
"Ignoring the --serverurl option. It has no effect when --disable-submgr or --no-rhsm is used."
)
else:
tool_opts.serverurl = parsed_opts.serverurl
if parsed_opts.keep_rhsm:
if tool_opts.no_rhsm:
loggerinst.warning(
"Ignoring the --keep-rhsm option. It has no effect when --disable-submgr or --no-rhsm is used."
)
else:
tool_opts.keep_rhsm = parsed_opts.keep_rhsm
tool_opts.autoaccept = parsed_opts.y
tool_opts.auto_attach = parsed_opts.auto_attach
tool_opts.restart = parsed_opts.restart
if parsed_opts.activationkey:
tool_opts.activation_key = parsed_opts.activationkey
if parsed_opts.org:
tool_opts.org = parsed_opts.org
if tool_opts.username and tool_opts.password:
tool_opts.credentials_thru_cli = True
def warn_on_unsupported_options():
if any(x in sys.argv[1:] for x in ["--variant", "-v"]):
loggerinst.warning(
"The -v|--variant option is not supported anymore and has no effect.\n"
"See help (convert2rhel -h) for more information."
)
utils.ask_to_continue()
# Code to be executed upon module import
tool_opts = ToolOpts() # pylint: disable=C0103