-
Notifications
You must be signed in to change notification settings - Fork 0
/
spacewalk_channel_freeze.py
588 lines (539 loc) · 22.3 KB
/
spacewalk_channel_freeze.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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
spacewalk_channel_freeze.py - a tool for freezing patches and errata for systems managed
with Spacewalk and Uyuni
2019 By Christian Stankowic
<info at cstan dot io>
https://github.com/stdevel/spacewalk_channel_freeze
"""
import os
import stat
import getpass
from fnmatch import fnmatch
import logging
import sys
import argparse
import datetime
try:
# Python 3
# noinspection PyCompatibility
from xmlrpc.server import SimpleXMLRPCServer as Server
# noinspection PyCompatibility
from xmlrpc.client import Fault
except ImportError:
# Python 2
# noinspection PyCompatibility
import xmlrpclib
# define global variables
__version__ = "0.1.0"
LOGGER = logging.getLogger('spacewalk_channel_freeze')
SYSTEMS = []
CHANNELS = {}
SUPPORTED_API_LEVEL = 12.0
class APILevelNotSupportedException(Exception):
"""
Class for unsupported API versions
"""
pass
def check_if_api_is_supported(client):
""""
Check whether API is supported
:param client: Spacewalk client
:type client: XMLRPC object
"""
api_level = client.api.getVersion()
if float(api_level) < SUPPORTED_API_LEVEL:
raise APILevelNotSupportedException(
"Your API version ({0}) does not support the required calls. "
"You'll need API version 12 or higher!".format(api_level)
)
else:
LOGGER.info("Supported API version (%s) found.", api_level)
def get_credentials(cred_type, input_file=None):
"""
Retrieve credentials from authfile, environment variable or input
:param cred_type: authentication type (Spacewalk, etc.)
:type cred_type: str
:param input_file: authentication file path
:type input_file: str
:return: authentication credentials
"""
# raw_input() was replaced by input() in Python 3
try:
input = raw_input
except NameError:
pass
if input_file:
LOGGER.debug("Using authfile")
try:
# check filemode and read file
filemode = oct(stat.S_IMODE(os.lstat(input_file).st_mode))
if filemode == "0400":
LOGGER.debug("File permission matches 0400")
with open(input_file, "r") as auth_file:
s_username = auth_file.readline().replace("\n", "")
s_password = auth_file.readline().replace("\n", "")
return s_username, s_password
else:
LOGGER.warning("File permissions (%s) not matching 0400!", filemode)
except OSError:
LOGGER.warning("File non-existent or permissions not 0400!")
LOGGER.debug("Prompting for login credentials as we have a faulty file")
s_username = input(cred_type + " Username: ")
s_password = getpass.getpass(cred_type + " Password: ")
return s_username, s_password
elif cred_type.upper() + "_LOGIN" in os.environ and \
cred_type.upper() + "_PASSWORD" in os.environ:
# shell variables
LOGGER.debug("Checking shell variables")
return os.environ[cred_type.upper() + "_LOGIN"], os.environ[cred_type.upper() + "_PASSWORD"]
else:
# prompt user
LOGGER.debug("Prompting for login credentials")
s_username = input(cred_type + " Username: ")
s_password = getpass.getpass(cred_type + " Password: ")
return s_username, s_password
def is_blacklisted(name, target_list):
"""
Check whether system is blacklisted
:param name: system name
:type name: str
:param target_list: element list
:type target_list: list
:return: bool
"""
for entry in target_list:
LOGGER.debug("Checking whether %s is blacklisted by *%s*", name, entry)
if fnmatch(name.lower(), "*{seek}*".format(seek=entry.lower())):
return True
return False
def get_channels(client, key):
"""
Gets all the host
:param client: Spacewalk client
:type client: XMLRPC client
:param key: Spacewalk client authentication
:type key: Spacewalk client key
:return: list
"""
sat_groups = []
global CHANNELS
global SYSTEMS
for item in client.systemgroup.listAllGroups(key):
sat_groups.append(item["name"])
LOGGER.debug("This Satellite server's groups: '%s'", sat_groups)
temp_hosts = []
for host in options.targetSystems:
if client.system.getId(key, host):
temp_hosts.append(host)
else:
LOGGER.error("System '%s' appears not to be a valid host", host)
for group in options.targetGroups:
if group in sat_groups:
group_hosts = client.systemgroup.listSystems(key, group)
for host in group_hosts:
temp_hosts.append(host["profile_name"])
LOGGER.debug("Adding system '%s'", host["profile_name"])
else:
LOGGER.error("Group '%s' appears not to be a valid group", group)
# removing blacklisted or hosts without base channel
for host in temp_hosts:
host_id = client.system.getId(key, host)
if is_blacklisted(host, options.exclude):
LOGGER.debug("System '%s' is blacklisted", host)
elif not client.system.getSubscribedBaseChannel(key, host_id[0]["id"]):
LOGGER.error("System '%s' has no base channel", host)
else:
LOGGER.debug("Adding valid system '%s'", host)
SYSTEMS.append(host)
# list hosts or die in a fire
if not SYSTEMS:
LOGGER.info("Nothing to do, giving up!")
sys.exit(1)
LOGGER.debug("Validated hosts:")
for host in SYSTEMS:
LOGGER.debug(host)
# get _all_ the software channels
for host in SYSTEMS:
# adding base-channel
LOGGER.debug("Check base-channel for system '%s'", host)
host_id = client.system.getId(key, host)
try:
LOGGER.debug("This system's profile ID: %s", host_id)
base_channel = client.system.getSubscribedBaseChannel(key, host_id[0]["id"])
clean_base = base_channel["label"]
if "." in clean_base:
clean_base = clean_base[clean_base.find(".") + 1:]
if clean_base not in CHANNELS:
# channel non-present
LOGGER.debug("Adding channel '%s'", clean_base)
CHANNELS[clean_base] = []
# adding child channels
child_channels = client.system.listSubscribedChildChannels(key, host_id[0]["id"])
for channel in child_channels:
clean_child = channel["label"]
if "." in clean_child:
clean_child = clean_child[clean_child.find(".") + 1:]
if clean_child not in CHANNELS[clean_base]:
LOGGER.debug("Adding child-channel '%s'", clean_child)
CHANNELS[clean_base].append(clean_child)
# also list non-subscribed channels if wanted
if options.allSubchannels:
child_channels = client.system.listSubscribableChildChannels(
key, host_id[0]["id"]
)
for channel in child_channels:
if clean_child not in CHANNELS[clean_base]:
LOGGER.debug("Adding non-subscribed child-channel '%s'", clean_child)
CHANNELS[clean_base].append(clean_child)
except IndexError:
LOGGER.error(
"Unable to scan system '%s', check hostname, profile name "
"and whether a base channel was set!",
host
)
# print channel information
LOGGER.debug("Software channel tree: %s", str(CHANNELS))
def clone_channels(client, key, date, label, unfreeze=False):
"""
Clone channels
:param client: Spacewalk client
:type client: XMLRPC client
:param key: Spacewalk client authentication
:type key: Spacewalk client key
:param date: date prefix
:type date: str
:param label: label prefix
:type label: str
:param unfreeze: flag whether channels should be cloned/removed
:type unfreeze: bool
:return: bool
"""
if unfreeze:
# remove clones
for channel in CHANNELS:
# remove child-channels
for child in CHANNELS[channel]:
if options.dryrun:
LOGGER.info(
"I'd like to remove cloned child-channel '%s'",
label + "-" + date + "." + child
)
else:
try:
LOGGER.info(
"Deleting child-channel '%s'",
label + "-" + date + "." + child
)
client.channel.software.delete(
key, label + "-" + date + "." + child
)
except xmlrpclib.Fault as err:
LOGGER.error(
"Unable to remove child-channel '%s': '%s'",
label + "-" + date + "." + child, err.faultString
)
except xmlrpclib.ProtocolError as err:
LOGGER.error(
"Unable to remove child-channel '%s': '%s'",
label + "-" + date + "." + child, err.errmsg
)
# remove base-channel
if options.dryrun:
LOGGER.info(
"I'd like to remove cloned base-channel '%s'",
label + "-" + date + "." + channel
)
else:
try:
LOGGER.info(
"Deleting base-channel '%s'",
label + "-" + date + "." + channel
)
client.channel.software.delete(
key, label + "-" + date + "." + channel
)
except xmlrpclib.Fault as err:
LOGGER.error(
"Unable to remove base-channel '%s': '%s'",
label + "-" + date + "." + channel, err.faultString
)
except xmlrpclib.ProtocolError as err:
LOGGER.error(
"Unable to remove base-channel '%s': '%s'",
label + "-" + date + "." + channel, err.errmsg
)
return True
# clone channels
for channel in CHANNELS:
# clone base-channels
my_args = {"name": "Cloned " + channel + " from " + date + " (" + label + ")",
"label": label + "-" + date + "." + channel,
"summary": "Software channel cloned by satprep"}
if options.dryrun:
LOGGER.info(
"I'd like to clone base-channel '%s' as '%s'",
channel, label + "-" + date + "." + channel
)
else:
LOGGER.info(
"Cloning base-channel '%s' as '%s'",
channel, label + "-" + date + "." + channel
)
try:
result = client.channel.software.clone(key, channel, my_args, False)
if result != 0:
LOGGER.debug("Cloned base-channel")
except xmlrpclib.Fault as err:
LOGGER.error("Unable to clone base-channel: %s", err.faultString)
except xmlrpclib.ProtocolError as err:
LOGGER.error("Unable to clone base-channel: %s", err.errmsg)
# clone child-channels
for child in CHANNELS[channel]:
my_args = {
"name": "Cloned " + child + " from " + date,
"label": label + "-" + date + "." + child,
"summary": "Software channel cloned by satprep",
"parent_label": label + "-" + date + "." + channel
}
if options.dryrun:
LOGGER.info(
"I'd like to clone child-channel '%s' as '%s'",
child, label + "-" + date + "." + child
)
else:
LOGGER.info(
"Cloning child-channel '%s' as '%s'",
child, label + "-" + date + "." + child
)
try:
result = client.channel.software.clone(key, child, my_args, False)
if result != 0:
LOGGER.debug("Cloned child-channel")
except xmlrpclib.Fault as err:
LOGGER.error("Unable to clone base-channel: %s", err.faultString)
except xmlrpclib.ProtocolError as err:
LOGGER.error("Unable to clone base-channel: %s", err.errmsg)
def remap_systems(client, key, unfreeze=False):
"""
Remap systems
:param client: Spacewalk client
:type client: XMLRPC client
:param key: Spacewalk client authentication
:type key: Spacewalk client key
:param unfreeze: flag whether original channels should be used
:type unfreeze: bool
"""
if options.noRemap:
LOGGER.info("Not remapping system's channels")
else:
for system in SYSTEMS:
# remap base-channel
host_id = client.system.getId(key, system)
my_base = client.system.getSubscribedBaseChannel(key, host_id[0]["id"])
if unfreeze:
my_new_base = my_base["label"]
my_new_base = my_new_base[my_new_base.find(".") + 1:]
else:
my_new_base = options.targetLabel + "-" + options.targetDate + "." + my_base["label"]
if options.dryrun:
LOGGER.info(
"I'd like to remap %s's base-channel from %s to %s",
system, my_base["label"], my_new_base
)
else:
try:
LOGGER.debug(
"Remapping %s's base-channel from %s to %s",
system, my_base["label"], my_new_base
)
result = client.system.setBaseChannel(key, host_id[0]["id"], my_new_base)
if result == 1:
LOGGER.debug("Remapped system")
except xmlrpclib.Fault as err:
LOGGER.error(
"Unable to change base-channel for system '%s' - '%s - %s'",
system, err.faultCode, err.faultString
)
except xmlrpclib.ProtocolError as err:
LOGGER.error(
"Unable to change base-channel for system '%s' - '%s - %s'",
system, err.faultCode, err.faultString
)
# remap child-channels
child_channels = client.system.listSubscribedChildChannels(key, host_id[0]["id"])
tmp_channels = []
for channel in child_channels:
my_new_channel = channel["label"]
if unfreeze:
# switch back to non-cloned
my_new_channel = my_new_channel[my_new_channel.find(".") + 1:]
else:
# switch to cloned
my_new_channel = options.targetLabel + "-" + options.targetDate + "." + channel["label"]
tmp_channels.append(my_new_channel)
if options.dryrun:
LOGGER.info(
"I'd like to set the following child-channels for %s: %s",
system, str(tmp_channels)
)
else:
try:
LOGGER.debug(
"Setting child-channels for %s: %s",
system, str(tmp_channels)
)
client.system.setChildChannels(key, host_id[0]["id"], tmp_channels)
except xmlrpclib.Fault:
# ignore xmlrpclib.Fault as it works like a charm
pass
except xmlrpclib.ProtocolError as err:
LOGGER.error(
"Unable to set child-channels (%s) for system '%s' - '%s' - '%s'",
str(tmp_channels), system, err.faultCode, err.faultString
)
del tmp_channels
def main(this_options):
"""
Check/set some necessary information
:param this_options: program options
:type this_options: Namespace
"""
# check/set some necessary information
if not this_options.targetSystems and not this_options.targetGroups:
LOGGER.error("You need to specify at least one system or system group!")
exit(1)
if this_options.targetDate == "wingardiumleviosa":
# set current date
now = datetime.datetime.now()
this_options.targetDate = now.strftime("%Y-%m-%d")
LOGGER.debug("Flicked date to: %s", now.strftime("%Y-%m-%d"))
# split label, systems and groups
this_options.targetLabel = ''.join(this_options.targetLabel.split()).strip("-").lower()
if "sp" not in this_options.targetLabel:
this_options.targetLabel = "sp-" + this_options.targetLabel
if len(this_options.targetSystems) == 1:
this_options.targetSystems = str(this_options.targetSystems).strip("[]'").split(",")
if len(this_options.targetGroups) == 1:
this_options.targetGroups = str(this_options.targetGroups).strip("[]'").split(",")
if len(this_options.exclude) == 1:
this_options.exclude = str(this_options.exclude).strip("[]'").split(",")
LOGGER.debug("Options: %s", this_options)
# authenticate against Satellite and check whether supported API found
(username, password) = get_credentials("Satellite", this_options.authfile)
satellite_url = "http://{0}/rpc/api".format(this_options.server)
client = xmlrpclib.Server(satellite_url, verbose=this_options.debug)
key = client.auth.login(username, password)
check_if_api_is_supported(client)
# get channels
get_channels(client, key)
if this_options.unfreeze:
remap_systems(client, key, True)
clone_channels(client, key, this_options.targetDate, this_options.targetLabel, True)
else:
clone_channels(client, key, this_options.targetDate, this_options.targetLabel)
remap_systems(client, key)
def parse_options():
"""
Parses options
:return: list
"""
# define description, version and load parser
desc = '''spacewalk_channel_freeze is used to clone software channels managed with Uyuni,
Spacewalk, Red Hat Satellite 5.x and SUSE Manager to freeze system updates. It automatically
clones appropriate software channels for particular systems or system groups and also remaps
software channels to affected hosts. Login credentials are assigned using the following
shell variables:
SATELLITE_LOGIN username
SATELLITE_PASSWORD password
It is also possible to create an authfile (permissions 0400) for usage with this script. The
first line needs to contain the username, the second line should consist of the appropriate
password.
If you're not defining variables or an authfile you will be prompted to enter your login
information.'''
epilog = '''Checkout the GitHub page for updates:
https://github.com/stdevel/spacewalk_channel_freeze'''
parser = argparse.ArgumentParser(description=desc, epilog=epilog)
parser.add_argument('--version', action='version', version=__version__)
# define option groups
gen_opts = parser.add_argument_group("Generic Options")
srv_opts = parser.add_argument_group("Server Options")
sys_opts = parser.add_argument_group("System Options")
chn_opts = parser.add_argument_group("Channel Options")
# GENERIC OPTIONS
# -d / --debug
gen_opts.add_argument(
"-d", "--debug", dest="debug", default=False,
action="store_true", help="enable debugging outputs (default: no)"
)
# -n / --dry-run
gen_opts.add_argument(
"-n", "--dry-run", action="store_true", dest="dryrun", default=False,
help="only simulates what would be done (default: no)"
)
# -u / --unfreeze
gen_opts.add_argument(
"-u", "--unfreeze", action="store_true", dest="unfreeze", default=False,
help="removes clones and remaps systems (default: no)"
)
# SERVER OPTIONS
# -a / --authfile
srv_opts.add_argument(
"-a", "--authfile", dest="authfile", metavar="FILE", default="",
help="defines an auth file to use instead of shell variables"
)
# -s / --server
srv_opts.add_argument(
"-s", "--server", dest="server", metavar="SERVER", default="localhost",
help="defines the server to use (default: localhost)"
)
# SYSTEM OPTIONS
# -S / --system
sys_opts.add_argument(
"-S", "--system", action="append", dest="targetSystems", metavar="SYSTEM",
default=[], help="specifies a system to use for freezing patches"
)
# -g / --group
sys_opts.add_argument(
"-g", "--group", action="append", dest="targetGroups", metavar="GROUP",
default=[], help="specifies a system group to use for freezing patches"
)
# -e / --exclude
sys_opts.add_argument(
"-e", "--exclude", action="append", dest="exclude", metavar="SYSTEM",
default=[], help="defines hosts that should be excluded for freezing patches"
)
# -i / --no-remap
sys_opts.add_argument(
"-i", "--no-remap", action="store_true", dest="noRemap", default=False,
help="disables remapping affected systems to cloned channels (default: no)"
)
# CHANNEL OPTIONS
# -A / --all-subchannels
chn_opts.add_argument(
"-A", "--all-subchannels", action="store_true", dest="allSubchannels", default=False,
help="clones all sub-channels instead of only required ones (default: no)"
)
# -l / --label
chn_opts.add_argument(
"-l", "--label", action="store", dest="targetLabel", metavar="LABEL", default="sp",
help="defines a label for the cloned channel (e.g. application name)"
)
# -D / --date
chn_opts.add_argument(
"-D", "--date", action="store", dest="targetDate", metavar="DATE",
default="wingardiumleviosa",
help="defines the date patches should be freezed (default: current date)"
)
this_options = parser.parse_args()
return this_options
if __name__ == "__main__":
options = parse_options()
if options.debug:
logging.basicConfig(level=logging.DEBUG)
LOGGER.setLevel(logging.DEBUG)
else:
logging.basicConfig()
LOGGER.setLevel(logging.INFO)
main(options)