Created
April 14, 2016 14:23
-
-
Save ymcdull/94fadab1c848d4874c660c1427470ce9 to your computer and use it in GitHub Desktop.
launch/destroy AWS RDS instances
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys, os | |
import boto | |
def print_usage(): | |
print """ | |
mysqlrds launch <id> | |
mysqlrds status <id> | |
mysqlrds destroy <id> | |
""" | |
class RDS(object): | |
def __init__(self,config): | |
self.config = config | |
self.api = boto.connect_rds() | |
def do_destroy(self): | |
for instance in self.api.get_all_dbinstances(): | |
if instance.id == self.config.get('id'): | |
self.api.delete_dbinstance(instance.id,skip_final_snapshot=True) | |
print "destroyed" | |
return | |
print "no instances are destroyed" | |
def do_check(self): | |
#todo: check if it is initializing... | |
for instance in self.api.get_all_dbinstances(): | |
if instance.id == self.config.get('id'): | |
info = { | |
"ipaddress": instance._address, | |
} | |
print self.tmpl % info | |
return | |
print "didnot much" | |
class OracleRDS(RDS): | |
def __init__(self, config): | |
RDS.__init__(self, config) | |
self.tmpl = """try\nsqlplus64 'username@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=%(ipaddress)s) (PORT=1521))(CONNECT_DATA=(SID=MYDB)))'""" | |
def do_launch(self): | |
# create_dbinstance() does not support params['LicenseModel'], so I hacked... | |
def update_get_object(myself, target): | |
def revised_target(action, params, cls): | |
params['LicenseModel'] = 'license-included' | |
return target(action, params, cls) | |
return revised_target | |
self.api.get_object = update_get_object(self.api, self.api.get_object) | |
ret = self.api.create_dbinstance( | |
self.config.get('id'), | |
self.config.get('size', 10), #allocated_storage, in GB [10-1024] | |
self.config.get('instance_class','db.m1.small'), #instance_class | |
self.config.get('master_username','username'), | |
self.config.get('master_password','password'), | |
port=1521, | |
engine='oracle-se1', | |
db_name='MYDB', | |
param_group=None, | |
security_groups=None, | |
availability_zone=None, | |
preferred_maintenance_window=None, | |
backup_retention_period=None, | |
preferred_backup_window=None, | |
multi_az=False, | |
engine_version=None, | |
auto_minor_version_upgrade=True | |
) | |
return ret | |
class MySQLRDS(RDS): | |
def __init__(self, config): | |
RDS.__init__(self, config) | |
self.tmpl = "try\nmysql -h %(ipaddress)s -u username -p mysql" | |
def do_launch(self): | |
ret = self.api.create_dbinstance( | |
self.config.get('id'), | |
self.config.get('size', 5), #allocated_storage, in GB [5-1024] | |
self.config.get('instance_class','db.m1.small'), #instance_class | |
self.config.get('master_username','username'), | |
self.config.get('master_password','password'), | |
port=3306, | |
engine='mysql', | |
db_name=None, | |
param_group=None, | |
security_groups=None, | |
availability_zone=None, | |
preferred_maintenance_window=None, | |
backup_retention_period=None, | |
preferred_backup_window=None, | |
multi_az=False, | |
engine_version=None, | |
auto_minor_version_upgrade=True | |
) | |
return ret | |
def main(mode): | |
import optparse | |
parser = optparse.OptionParser() | |
opts, args = parser.parse_args() | |
if len(args) != 2: | |
print_usage() | |
sys.exit() | |
cmd, dbid = args[0], args[1] | |
if mode == 'oracle': | |
rds = OracleRDS({"id":dbid}) | |
elif mode == 'mysql': | |
rds = MySQLRDS({"id":dbid}) | |
else: | |
print "error" | |
sys.exit(1) | |
if args[0] == 'launch': rds.do_launch() | |
elif args[0] == 'status': rds.do_check() | |
elif args[0] == 'destroy': rds.do_destroy() | |
else: print_usage() | |
if __name__ == '__main__': | |
fname = os.path.basename(__file__) | |
if fname == 'mysqlrds': | |
mode = "mysql" | |
elif fname == 'oraclerds': | |
mode = "oracle" | |
else: | |
print "Check scriptname. It should be mysqlrds or oraclerds" | |
sys.exit(1) | |
main(mode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment