You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.5 KiB

  1. import time
  2. import boto3
  3. from botocore.exceptions import ClientError
  4. region = 'eu-central-1'
  5. availabilityZone = 'eu-central-1b'
  6. imageId = 'ami-0cc293023f983ed53'
  7. instanceType = 't3.nano'
  8. keyName = 'srieger-pub'
  9. subnet1 = 'subnet-41422b28'
  10. subnet2 = 'subnet-5c5f6d16'
  11. subnet3 = 'subnet-6f2ea214'
  12. userDataDB = ('#!/bin/bash\n'
  13. '#!/bin/bash\n'
  14. '# extra repo for RedHat rpms\n'
  15. 'yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm\n'
  16. '# essential tools\n'
  17. 'yum install -y joe htop git\n'
  18. '# mysql\n'
  19. 'yum install -y mariadb mariadb-server\n'
  20. '\n'
  21. 'service mariadb start\n'
  22. '\n'
  23. 'echo "create database cloud_tug_of_war" | mysql -u root\n'
  24. '\n'
  25. 'echo "create table clouds ( cloud_id INT AUTO_INCREMENT, name VARCHAR(255) NOT NULL, value INT, max_value INT, PRIMARY KEY (cloud_id))" | mysql -u root cloud_tug_of_war\n'
  26. '\n'
  27. 'echo "CREATE USER \'cloud_tug_of_war\'@\'%\' IDENTIFIED BY \'cloudpass\';" | mysql -u root\n'
  28. 'echo "GRANT ALL PRIVILEGES ON cloud_tug_of_war.* TO \'cloud_tug_of_war\'@\'%\';" | mysql -u root\n'
  29. 'echo "FLUSH PRIVILEGES" | mysql -u root\n'
  30. )
  31. # convert with: cat install-mysql | sed "s/^/'/; s/$/\\\n'/"
  32. client = boto3.setup_default_session(region_name=region)
  33. ec2Client = boto3.client("ec2")
  34. ec2Resource = boto3.resource('ec2')
  35. response = ec2Client.describe_vpcs()
  36. vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
  37. elbv2Client = boto3.client('elbv2')
  38. asClient = boto3.client('autoscaling')
  39. print("Deleting auto scaling group...")
  40. print("------------------------------------")
  41. try:
  42. response = asClient.delete_auto_scaling_group(AutoScalingGroupName='tug-of-war-asg-autoscalinggroup', ForceDelete=True)
  43. except ClientError as e:
  44. print(e)
  45. print("Deleting launch configuration...")
  46. print("------------------------------------")
  47. try:
  48. response = asClient.delete_launch_configuration(LaunchConfigurationName='tug-of-war-asg-launchconfig')
  49. except ClientError as e:
  50. print(e)
  51. print("Deleting old instances...")
  52. print("------------------------------------")
  53. response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war-asg']}])
  54. print(response)
  55. reservations = response['Reservations']
  56. for reservation in reservations:
  57. for instance in reservation['Instances']:
  58. if instance['State']['Name'] == "running":
  59. response = ec2Client.terminate_instances(InstanceIds=[instance['InstanceId']])
  60. print(response)
  61. instanceToTerminate = ec2Resource.Instance(instance['InstanceId'])
  62. instanceToTerminate.wait_until_terminated()
  63. print("Deleting load balancer and deps...")
  64. print("------------------------------------")
  65. try:
  66. response = elbv2Client.describe_load_balancers(Names=['tug-of-war-asg-loadbalancer'])
  67. loadbalancer_arn = response.get('LoadBalancers', [{}])[0].get('LoadBalancerArn', '')
  68. response = elbv2Client.delete_load_balancer(LoadBalancerArn=loadbalancer_arn)
  69. waiter = elbv2Client.get_waiter('load_balancers_deleted')
  70. waiter.wait(LoadBalancerArns=[loadbalancer_arn])
  71. except ClientError as e:
  72. print(e)
  73. try:
  74. response = elbv2Client.describe_target_groups(Names=['tug-of-war-asg-targetgroup'])
  75. while len(response.get('TargetGroups', [{}])) > 0:
  76. targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '')
  77. try:
  78. response = elbv2Client.delete_target_group(TargetGroupArn=targetgroup_arn)
  79. except ClientError as e:
  80. print(e)
  81. response = elbv2Client.describe_target_groups(Names=['tug-of-war-asg-targetgroup'])
  82. time.sleep(5)
  83. except ClientError as e:
  84. print(e)
  85. print("Delete old security group...")
  86. print("------------------------------------")
  87. try:
  88. response = ec2Client.describe_security_groups(Filters=[{'Name': 'group-name', 'Values': ['tug-of-war-asg']}])
  89. while len(response.get('SecurityGroups', [{}])) > 0:
  90. security_group_id = response.get('SecurityGroups', [{}])[0].get('GroupId', '')
  91. try:
  92. response = ec2Client.delete_security_group(GroupName='tug-of-war-asg')
  93. except ClientError as e:
  94. print(e)
  95. response = ec2Client.describe_security_groups(Filters=[{'Name': 'group-name', 'Values': ['tug-of-war-asg']}])
  96. time.sleep(5)
  97. except ClientError as e:
  98. print(e)