-
Notifications
You must be signed in to change notification settings - Fork 0
/
rds_boto_code.py
executable file
·105 lines (98 loc) · 3.45 KB
/
rds_boto_code.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
#!/usr/bin/python
""" Utility script for manipulating AWS RDS """
import sys
import logging
import boto3
from switch import switch
import botocore
import configparser
def usage(option):
""" Usage function """
print "\nUSAGE: " + option + "\n"
for case in switch(option):
if case('default'):
print """
./rds_boto_code.py create-db-instance
./rds_boto_code.py delete-db-instance <dbname>
./rds_boto_code.py list-db-instances
"""
break
def create_db_instance():
""" Creates mysql Instance """
try:
if len(sys.argv) < 1:
usage('default')
else:
logging.info("Creating RDS Instance ")
try:
file = '/root/dbinstance.cfg'
config = configparser.ConfigParser()
config.read(file)
response = rds.create_db_instance(
DBInstanceIdentifier=config['DBPARAMS']['DBInstanceIdentifier'],
MasterUsername=config['DBPARAMS']['MasterUsername'],
MasterUserPassword=config['DBPARAMS']['MasterUserPassword'],
DBInstanceClass=config['DBPARAMS']['DBInstanceClass'],
Engine=config['DBPARAMS']['Engine'],
AllocatedStorage=int(config['DBPARAMS']['AllocatedStorage']))
print response
except botocore.exceptions.ClientError as e:
logging.error(e.response['Error']['Code'])
except IndexError:
usage('default')
def list_db_instances():
""" Get all DB Instances """
try:
dbs = rds.describe_db_instances()
for db in dbs['DBInstances']:
print ("%s@%s:%s %s") % (
db['MasterUsername'],
db['Endpoint']['Address'],
db['Endpoint']['Port'],
db['DBInstanceStatus'])
except botocore.exceptions.ClientError as e:
logging.error(e.response['Error']['Code'])
def delete_db_instance():
"""Detete a db instance"""
db = sys.argv[2]
try:
response = rds.delete_db_instance(
DBInstanceIdentifier=db,
SkipFinalSnapshot=True)
print response
except botocore.exceptions.ClientError as e:
logging.error(e.response['Error']['Code'])
if __name__ == '__main__':
rds = boto3.client('rds')
"""Sets up logging"""
logging.basicConfig(filename='aws_rds.log',
format='%(asctime)s - %(levelname)s: %(message)s',
level=logging.DEBUG)
try:
ACTION = sys.argv[1]
for case in switch(ACTION):
if case('create-db-instance'):
if len(sys.argv) > 1:
logging.info("Creating Instance")
create_db_instance()
else:
usage('default')
break
if case('list-db-instances'):
if len(sys.argv) == 2:
logging.info("querying instance")
list_db_instances()
else:
usage('default')
break
if case('delete-db-instance'):
if len(sys.argv) == 3:
delete_db_instance()
else:
usage('default')
break
if case():
print "No Database instance action"
break
except:
usage('default')