Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. import os
  2. import sys
  3. import time
  4. import boto3
  5. import botocore
  6. import pprint
  7. from invoke import run, task
  8.  
  9. DB_CONFIG = {
  10. # SecurityGroup='sg-name" with ingress on port 5432 and source from itself
  11. # .ebextensions/02_ec2.config should have:
  12. # - namespace: aws:autoscaling:launchconfiguration
  13. # option_name: SecurityGroups
  14. # value: 'sg-name'
  15. 'SECURITY_GROUP': 'sg-xxxxx',
  16. 'DB_ID': 'rds-db1',
  17. 'DB_NAME': 'mydb',
  18. 'DB_VERSION': '9.5',
  19. 'DB_TYPE': 'postgres',
  20. 'DB_USER': 'ebroot',
  21. 'DB_PASSWORD': 'super.secure', # TODO: Hide this
  22. 'DB_INSTANCE_TYPE': 'db.t2.micro', # TODO: Change to db.t2.medium or db.m4.large
  23. 'DB_STORAGE': 5, # TODO: Should be 20 or more
  24. 'DB_IOPS': 1000 # Not used
  25. }
  26.  
  27. CACHE_CONFIG = {
  28. # SecurityGroup='sg-name' with ingress on port 6379 and source from itself
  29. # .ebextensions/02_ec2.config should have:
  30. # - namespace: aws:autoscaling:launchconfiguration
  31. # option_name: SecurityGroups
  32. # value: 'sg-name'
  33. 'SECURITY_GROUP': 'sg-xxxxxxxx',
  34. 'REDIS_ID': 'redis-cache1',
  35. 'REDIS_NUM_NODES': 1,
  36. 'REDIS_INSTANCE_TYPE': 'cache.t2.micro',
  37. 'REDIS_VERSION': '2.8.24',
  38. 'REDIS_PORT': 6379,
  39. 'NOTIFICATION': 'arn:aws:sns:us-east-1:xxxxxxxx:MyNotifications'
  40.  
  41. }
  42.  
  43. AWS_SERVER_TAGS = [{
  44. 'Key': 'Project',
  45. 'Value': 'Strato'
  46. }, {
  47. 'Key': 'Stage',
  48. 'Value': 'Production'
  49. }]
  50.  
  51.  
  52. @task
  53. def report_rds_instance(ctx, all=False):
  54. print('**** report_rds_instance ****')
  55. rds = boto3.client('rds')
  56.  
  57. running = True
  58. while running:
  59. response = rds.describe_db_instances()
  60.  
  61. db_instances = response['DBInstances']
  62. if not all and len(db_instances) != 1:
  63. raise Exception('Whoa cowboy! More than one DB (or None) instance returned; this should never happen')
  64.  
  65. for db_instance in db_instances:
  66. #pprint.pprint(db_instance)
  67.  
  68. interesting_data = [
  69. 'DBInstanceIdentifier', 'DBInstanceClass', 'Engine', 'EngineVersion',
  70. 'DBName', 'MasterUsername',
  71. 'AllocatedStorage', 'MultiAZ', 'StorageEncrypted', 'StorageType'
  72. ]
  73. for key in interesting_data:
  74. print('{0:<20} = {1}'.format(key, db_instance[key]))
  75. print('')
  76.  
  77. status = db_instance['DBInstanceStatus']
  78.  
  79. print('Last DB status: {0}'.format(status))
  80.  
  81. time.sleep(10)
  82. if status == 'available':
  83. endpoint = db_instance['Endpoint']
  84. host = endpoint['Address']
  85. # port = endpoint['Port']
  86.  
  87. print('DB instance ready with host: {}'.format(host))
  88. running = False
  89. print('----------------')
  90.  
  91. @task
  92. def report_redis_cluster(ctx, all=False):
  93. print('**** report_redis_cluster ****')
  94. redis = boto3.client('elasticache')
  95.  
  96. response = redis.describe_cache_clusters()
  97. #pprint.pprint(response)
  98.  
  99. clusters = response['CacheClusters']
  100. for cluster in clusters:
  101. interesting_data = [
  102. 'CacheClusterId', 'CacheClusterStatus', 'CacheNodeType', 'NumCacheNodes',
  103. 'Engine', 'EngineVersion',
  104. ]
  105. for key in interesting_data:
  106. print('{0:<20} = {1}'.format(key, cluster[key]))
  107. print('')
  108.  
  109. groups = redis.describe_replication_groups()
  110. if len(groups['ReplicationGroups']):
  111. pprint.pprint(groups['ReplicationGroups'])
  112.  
  113. print('----------------')
  114.  
  115.  
  116. @task
  117. def create_rds_instance(ctx):
  118. rds = boto3.client('rds')
  119. try:
  120. rds.create_db_instance(
  121. DBInstanceIdentifier=DB_CONFIG['DB_ID'],
  122. AllocatedStorage=DB_CONFIG['DB_STORAGE'],
  123. DBName=DB_CONFIG['DB_NAME'],
  124. Engine=DB_CONFIG['DB_TYPE'],
  125. EngineVersion=DB_CONFIG['DB_VERSION'],
  126. AutoMinorVersionUpgrade=True,
  127. # General purpose SSD
  128. StorageType='gp2', # TODO: Is this ok?
  129. StorageEncrypted=False, # TODO: Change to True for Production
  130. MultiAZ=False, # TODO: Change to True for Production
  131. MasterUsername=DB_CONFIG['DB_USER'],
  132. MasterUserPassword=DB_CONFIG['DB_PASSWORD'],
  133. VpcSecurityGroupIds=[DB_CONFIG['SECURITY_GROUP'],],
  134. DBInstanceClass=DB_CONFIG['DB_INSTANCE_TYPE'],
  135. Tags=AWS_SERVER_TAGS
  136. )
  137. print('Starting RDS instance with ID: {0}'.format(DB_CONFIG['DB_ID']))
  138. except botocore.exceptions.ClientError as e:
  139. print(str(e))
  140. if e and 'DBInstanceAlreadyExists' in e.message:
  141. print('DB instance {0} exists already, continuing to poll ...'.format(DB_CONFIG['DB_ID']))
  142. else:
  143. raise
  144. sys.exit()
  145.  
  146. report_rds_instance(ctx)
  147.  
  148. @task
  149. def create_redis_cluster(ctx):
  150. redis = boto3.client('elasticache')
  151. try:
  152. response = redis.create_cache_cluster(
  153. CacheClusterId=CACHE_CONFIG['REDIS_ID'],
  154. AZMode='single-az', # TODO: Change for production: 'cross-az',
  155. NumCacheNodes=CACHE_CONFIG['REDIS_NUM_NODES'],
  156. CacheNodeType=CACHE_CONFIG['REDIS_INSTANCE_TYPE'],
  157. Engine='redis',
  158. EngineVersion=CACHE_CONFIG['REDIS_VERSION'],
  159. SecurityGroupIds=[CACHE_CONFIG['SECURITY_GROUP'],],
  160. Tags=AWS_SERVER_TAGS,
  161. Port=CACHE_CONFIG['REDIS_PORT'],
  162. NotificationTopicArn=CACHE_CONFIG['NOTIFICATION'],
  163. AutoMinorVersionUpgrade=True,
  164. )
  165. print('Starting Redus instance with ID: {0}'.format(CACHE_CONFIG['REDIS_ID']))
  166. except botocore.exceptions.ClientError as e:
  167. print(str(e))
  168. sys.exit()
  169.  
  170. report_redis_cluster(ctx)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement