Created
December 17, 2013 07:48
-
-
Save shantanuo/8001474 to your computer and use it in GitHub Desktop.
start a cluster if there is no cluster already running. Use start or stop after the file name. For e.g. # python redshift.py stop
This file contains hidden or 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/python | |
| import boto | |
| conn = boto.connect_redshift(aws_access_key_id='key', aws_secret_access_key='secret') | |
| import datetime | |
| mymonth = datetime.datetime.now().strftime("%b").lower() | |
| myday = datetime.datetime.now().strftime("%d") | |
| myvar = mymonth+myday+'-viva-mar5-deliveryreport-new' | |
| response = conn.describe_cluster_snapshots() | |
| snapshots = response['DescribeClusterSnapshotsResponse']['DescribeClusterSnapshotsResult']['Snapshots'] | |
| snapshots.sort(key=lambda d: d['SnapshotCreateTime']) | |
| mysnapidentifier = snapshots[-1]['SnapshotIdentifier'] | |
| mydict=conn.describe_clusters() | |
| def start_cluster(): | |
| try: | |
| myidentifier=mydict['DescribeClustersResponse']['DescribeClustersResult']['Clusters'][0]['ClusterIdentifier'] | |
| except IndexError: | |
| conn.restore_from_cluster_snapshot('viva-mar5-deliveryreport-new', mysnapidentifier, availability_zone='us-east-1a') | |
| print "start a new cluster if no cluster is active" | |
| def stop_cluster(): | |
| try: | |
| myidentifier=mydict['DescribeClustersResponse']['DescribeClustersResult']['Clusters'][0]['ClusterIdentifier'] | |
| conn.delete_cluster(myidentifier, skip_final_cluster_snapshot=False, final_cluster_snapshot_identifier=myvar) | |
| print "stop cluster and take backup" | |
| except: | |
| print "No cluster is active right now" | |
| print "viv.us-east-1.redshift.amazonaws.com:5439:mydb:root:passwd" | |
| import argparse | |
| parser = argparse.ArgumentParser(description="Manage the cluster") | |
| parser.add_argument("action", choices=["stop", "start"], help="Choose start or stop") | |
| args = parser.parse_args() | |
| print args | |
| if args.action == "start": | |
| start_cluster() | |
| if args.action == "stop": | |
| stop_cluster() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment