-
Notifications
You must be signed in to change notification settings - Fork 7
/
rds_param_group_util.py
executable file
·403 lines (321 loc) · 16.4 KB
/
rds_param_group_util.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
#!/usr/bin/env python3
# Copyright (c) 2018, Justin D Holcomb ([email protected])
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
################################################################################
# Script Info #
################################################################################
# Title: rds_param_group_util.py
# Author: Justin Holcomb
# Created: December 18, 2018
# Version: 0.0.3
# Source/Inspiration: https://gist.github.com/phill-tornroth/f0ef50f9402c7c94cbafd8c94bbec9c9
# Requires: Python 3.6+ (fstrings)
# boto3 module
# Assumes: AWS credentials are environment vars or supplied
# outside of this script.
# Description: Compares/diffs two parameter groups.
# Copies a RDS parameter group.
# Merges one parameter group into another.
################################################################################
# Import Modules #
################################################################################
from os import environ
from sys import argv
import argparse
import boto3
################################################################################
# Variables #
################################################################################
default_source_region = environ['AWS_DEFAULT_REGION']
argparse_description = """
Cross AWS region capable for any action.
Compares two parameter groups and displays the differences, similar to diff.
OR
Copies a parameter group to a new destination.
OR
Merges parameters differences from the source parameter group to the destination
parameter group. Any parameter that does not exist in the destination is copied.
Any parameter that is different between the two is copied from the source OVER
the destination.
"""
################################################################################
# Functions #
################################################################################
def append_if_value_present(list_to_append, eval_append):
"""
Checks to see if the "ParameterValue" key is present in the 'eval_append' dict.
"""
if "ParameterValue" in eval_append:
list_to_append.append(eval_append)
def change_list_to_dict(list_to_convert, key):
"""
Changes a list into a dictionary using the 'key' parameter as the dictionary keyself.
Assumes the key is in each dictionary.
Assumes the key is unique.
"""
dict_to_export = {}
for each_obj_in_list in list_to_convert:
# Check if key already exists in dictionary.
if key in dict_to_export.keys():
raise Exception(f"This key name is not unique.")
dict_to_export[each_obj_in_list[key]] = each_obj_in_list
return dict_to_export
def chunks(sequence, chunk_size):
"""
Yields the sequence as a sequence of sequences size in chunk_size (or fewer,
in the case of the last chunk). Guarantees delivery of everything (as
opposed to strategies that leave elements off of the end when:
len(sequence) % chunk_size != 0
"""
start = 0
while start < len(sequence):
end = start + chunk_size
yield sequence[start : start + chunk_size]
start = end
def copy_rds_parameters(source_rds_client_obj, source_param_group, dest_rds_client_obj, dest_param_group):
"""
Copies a parameter group.
"""
# Construct variables
source_summary = source_rds_client_obj.describe_db_parameter_groups(DBParameterGroupName=source_param_group)
source_family = source_summary["DBParameterGroups"][0]["DBParameterGroupFamily"]
source_description = source_summary["DBParameterGroups"][0]["Description"]
source_parameters = return_all_modifiable_parameters_with_value_from_parameter_group(source_rds_client_obj, source_param_group)
groups_in_dest_region = return_parameter_groups(dest_rds_client_obj)
# Error if 'dest_param_group' already exists.
if dest_param_group in groups_in_dest_region:
raise ValueError(f"This group ({dest_param_group}) already exists in region.")
print(f"Created {dest_param_group}:")
print(f" Number of 'source_parameters' to set: {len(source_parameters)}")
# Create an origin trail of the parameter group.
aws_tag = [
{
'Key': 'CopiedFrom',
'Value': source_summary["DBParameterGroups"][0]['DBParameterGroupArn']
},
{
'Key': 'CopiedUsingCmd',
'Value': " ".join(argv)
},
{
'Key': 'Repo',
'Value': 'github.com/EpiJunkie/aws_rds_parameter_groups_utility'
}
]
# Create destination parameter group.
dest_rds_client_obj.create_db_parameter_group(
DBParameterGroupName=dest_param_group,
DBParameterGroupFamily=source_family,
Description=source_description,
Tags=aws_tag
)
# Apply changes to parameter group from the base DBParameterGroupFamily values.
post_parameters_to_group(dest_rds_client_obj, dest_param_group, source_parameters)
print("Complete.")
def compare_rds_parameters(source_rds_client_obj, source_param_group, dest_rds_client_obj, dest_param_group, return_list=False):
"""
Compares two parameters group for differences.
"""
# Construct variables
diff_list = []
source_summary = source_rds_client_obj.describe_db_parameter_groups(DBParameterGroupName=source_param_group)
source_family = source_summary["DBParameterGroups"][0]["DBParameterGroupFamily"]
source_parameters = return_all_parameters_from_parameter_group(source_rds_client_obj, source_param_group)
dest_summary = dest_rds_client_obj.describe_db_parameter_groups(DBParameterGroupName=dest_param_group)
dest_family = dest_summary["DBParameterGroups"][0]["DBParameterGroupFamily"]
dest_parameters = return_all_parameters_from_parameter_group(dest_rds_client_obj, dest_param_group)
print(f"Comparing {source_param_group} and {dest_param_group}")
print(f" Number of 'source_parameters' to compare: {len(source_parameters)}")
print(f" Number of 'dest_parameters' to compare: {len(dest_parameters)}")
# Notify that the 'DBParameterGroupFamily' don't match as it will be indicative of many differences in the compare.
if source_family != dest_family:
print(f" DBParameterGroupFamily mismatch.")
print(f" Source DBParameterGroupFamily: {source_family}")
print(f" Destination DBParameterGroupFamily: {dest_family}")
else:
print(f" DBParameterGroupFamily: {source_family}")
print("")
print("")
print("diff like comparison:")
print(f"< Source parameter group name: {source_param_group}")
print(f"> Destination parameter group name: {dest_param_group}")
# Convert list to dictionaries to lookup by key ('ParameterName').
source_parameters = change_list_to_dict(source_parameters, "ParameterName")
dest_parameters = change_list_to_dict(dest_parameters, "ParameterName")
# Compare source to dest, delete mutuals keys from 'dest_parameters' as compared is processed.
for param_name, param_value in source_parameters.items():
if param_name not in dest_parameters.keys():
print(f"{param_name}:")
print(f"< {source_parameters[param_name]}")
append_if_value_present(diff_list, source_parameters[param_name])
elif source_parameters[param_name] != dest_parameters[param_name]:
print(f"{param_name}:")
print(f"< {source_parameters[param_name]}")
print(f"> {dest_parameters[param_name]}")
append_if_value_present(diff_list, source_parameters[param_name])
# Delete from dict to prevent showing on the second compare.
del dest_parameters[param_name]
# Show remaining dest parameters that do not exist in the source parameters.
for param_name, param_value in dest_parameters.items():
if param_name not in source_parameters.keys():
print(f"{param_name}:")
print(f"> {dest_parameters[param_name]}")
print("")
print("")
# Future use for merge function.
if return_list == False:
print("Complete.")
else:
return diff_list
def merge_rds_parameters(source_rds_client_obj, source_param_group, dest_rds_client_obj, dest_param_group):
"""
Merges parameters differences from the source parameter group to the destination parameter group.
Any parameter that does not exist in the destination is copied.
Any parameter that is different between the two is copied from the source over the destination.
"""
parameters_diff = compare_rds_parameters(source_rds_client_obj, source_param_group, dest_rds_client_obj, dest_param_group, return_list=True)
print(f"Merging differences from '{source_param_group}' into '{dest_param_group}'.")
post_parameters_to_group(dest_rds_client_obj, dest_param_group, parameters_diff)
def post_parameters_to_group(rds_client_obj, param_group, parameters):
"""
Modifies the parameters in a parameter group and post in 20x chunks.
"""
# Return if there are no parameters
if len(parameters) == 0:
return
# Posts changes to AWS using 20 parameters chunks due to API limit.
for parameter_chunk in chunks(parameters, 20):
# Modified Parameter Group.
rds_client_obj.modify_db_parameter_group(DBParameterGroupName=param_group, Parameters=parameter_chunk)
# Print parameters
for p in parameter_chunk:
print(f" {p['ParameterName']} = {p['ParameterValue']}")
def return_parameter_groups(rds_client_obj):
"""
Returns all the parameter groups in a region.
"""
groups_to_return = []
pagination_token = None
# Iterate through all parameters in parameter group and break out when appropriate.
while(True):
# Returns when last iteration "Marker" was missing, otherwise retrieves parameters.
if pagination_token == False:
break
elif pagination_token == None:
group_chunk = rds_client_obj.describe_db_parameter_groups()
else:
group_chunk = rds_client_obj.describe_db_parameter_groups(Marker=pagination_token)
# Iterate through each parameter group name.
for g in group_chunk["DBParameterGroups"]:
groups_to_return.append(g["DBParameterGroupName"])
# Boto3 returns a field "Marker" when the returned parameter groups have been paginated. If missing, time to return function.
if "Marker" in group_chunk:
pagination_token = group_chunk['Marker']
else:
pagination_token = False
return groups_to_return
def return_all_modifiable_parameters_with_value_from_parameter_group(rds_client_obj, param_group):
"""
Returns all the modifiable parameters that have a value to set.
"""
all_parameters = return_all_parameters_from_parameter_group(rds_client_obj, param_group)
parameters_to_return = []
# Iterate through each parameter.
for p in all_parameters:
# Only modifiable parameters ('IsModifiable' == True) with a settable value ('ParameterValue').
if p["IsModifiable"]:
append_if_value_present(parameters_to_return, p)
return parameters_to_return
def return_all_parameters_from_parameter_group(rds_client_obj, param_group):
"""
Returns all the parameters for a parameter group.
"""
parameters_to_return = []
pagination_token = None
# Iterate through all parameters in parameter group and break out when appropriate.
while(True):
# Returns when last iteration "Marker" was missing, otherwise retrieves parameters.
if pagination_token == False:
break
elif pagination_token == None:
param_chunk = rds_client_obj.describe_db_parameters(DBParameterGroupName=param_group)
else:
param_chunk = rds_client_obj.describe_db_parameters(DBParameterGroupName=param_group, Marker=pagination_token)
# Iterate over the returned parameters.
for p in param_chunk['Parameters']:
parameters_to_return.append(p)
# Boto3 returns a field "Marker" when the returned parameters have been paginated. If missing, time to return function.
if "Marker" in param_chunk:
pagination_token = param_chunk['Marker']
else:
pagination_token = False
return parameters_to_return
################################################################################
# Main #
################################################################################
if __name__ == "__main__":
''' When manually ran from command line. '''
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=argparse_description)
parser.add_argument('-a', '--action',
help='Action to take.',
choices=['compare', 'copy', 'diff', 'merge'],
required=True,
type=str)
parser.add_argument('-p', '--parameter-group',
help='Source parameter group.',
dest='source_param_group',
required=True,
type=str)
parser.add_argument('-s', '--source-region',
help=f"Source region of parameter group. Default: {default_source_region}",
dest='source_region',
default=default_source_region,
type=str)
parser.add_argument('-d', '--dest-parameter-group',
help='Destination parameter group.',
dest='dest_param_group',
required=True,
type=str)
parser.add_argument('-w', '--dest-region',
help='Destination region of parameter group.',
dest='dest_region',
type=str)
args = parser.parse_args()
# Assume the dest AWS region is the same as the source if not given.
if args.dest_region is None:
args.dest_region = args.source_region
# Create boto objects
source_client = boto3.client("rds", region_name=args.source_region)
dest_client = boto3.client("rds", region_name=args.dest_region)
# Execute.
if args.action == "compare" or args.action == "diff":
compare_rds_parameters(source_client, args.source_param_group, dest_client, args.dest_param_group)
elif args.action == "copy":
copy_rds_parameters(source_client, args.source_param_group, dest_client, args.dest_param_group)
elif args.action == "merge":
merge_rds_parameters(source_client, args.source_param_group, dest_client, args.dest_param_group)