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.

341 lines
12 KiB

  1. import time
  2. import boto3
  3. from botocore.exceptions import ClientError
  4. ################################################################################################
  5. #
  6. # Configuration Parameters
  7. #
  8. ################################################################################################
  9. region = 'eu-central-1'
  10. availabilityZone = 'eu-central-1b'
  11. imageId = 'ami-0cc293023f983ed53'
  12. instanceType = 't3.nano'
  13. keyName = 'srieger-pub'
  14. vpc_id = 'vpc-eedd4187'
  15. subnet1 = 'subnet-41422b28'
  16. subnet2 = 'subnet-5c5f6d16'
  17. subnet3 = 'subnet-6f2ea214'
  18. # if you only have one VPC, vpc_id can be retrieved using:
  19. #
  20. # response = ec2Client.describe_vpcs()
  21. # vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
  22. ################################################################################################
  23. #
  24. # boto3 code
  25. #
  26. ################################################################################################
  27. client = boto3.setup_default_session(region_name=region)
  28. ec2Client = boto3.client("ec2")
  29. ec2Resource = boto3.resource('ec2')
  30. elbv2Client = boto3.client('elbv2')
  31. asClient = boto3.client('autoscaling')
  32. print("Deleting auto scaling group...")
  33. print("------------------------------------")
  34. try:
  35. response = asClient.delete_auto_scaling_group(AutoScalingGroupName='tug-of-war-asg-autoscalinggroup', ForceDelete=True)
  36. except ClientError as e:
  37. print(e)
  38. print("Deleting launch configuration...")
  39. print("------------------------------------")
  40. try:
  41. response = asClient.delete_launch_configuration(LaunchConfigurationName='tug-of-war-asg-launchconfig')
  42. except ClientError as e:
  43. print(e)
  44. print("Deleting old instances...")
  45. print("------------------------------------")
  46. response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war-asg']}])
  47. print(response)
  48. reservations = response['Reservations']
  49. for reservation in reservations:
  50. for instance in reservation['Instances']:
  51. if instance['State']['Name'] == "running":
  52. response = ec2Client.terminate_instances(InstanceIds=[instance['InstanceId']])
  53. print(response)
  54. instanceToTerminate = ec2Resource.Instance(instance['InstanceId'])
  55. instanceToTerminate.wait_until_terminated()
  56. print("Deleting load balancer and deps...")
  57. print("------------------------------------")
  58. try:
  59. response = elbv2Client.describe_load_balancers(Names=['tug-of-war-asg-loadbalancer'])
  60. loadbalancer_arn = response.get('LoadBalancers', [{}])[0].get('LoadBalancerArn', '')
  61. response = elbv2Client.delete_load_balancer(LoadBalancerArn=loadbalancer_arn)
  62. waiter = elbv2Client.get_waiter('load_balancers_deleted')
  63. waiter.wait(LoadBalancerArns=[loadbalancer_arn])
  64. except ClientError as e:
  65. print(e)
  66. try:
  67. response = elbv2Client.describe_target_groups(Names=['tug-of-war-asg-targetgroup'])
  68. while len(response.get('TargetGroups', [{}])) > 0:
  69. targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '')
  70. try:
  71. response = elbv2Client.delete_target_group(TargetGroupArn=targetgroup_arn)
  72. except ClientError as e:
  73. print(e)
  74. response = elbv2Client.describe_target_groups(Names=['tug-of-war-asg-targetgroup'])
  75. time.sleep(5)
  76. except ClientError as e:
  77. print(e)
  78. print("Delete old security group...")
  79. print("------------------------------------")
  80. try:
  81. response = ec2Client.describe_security_groups(Filters=[{'Name': 'group-name', 'Values': ['tug-of-war-asg']}])
  82. while len(response.get('SecurityGroups', [{}])) > 0:
  83. security_group_id = response.get('SecurityGroups', [{}])[0].get('GroupId', '')
  84. try:
  85. response = ec2Client.delete_security_group(GroupName='tug-of-war-asg')
  86. except ClientError as e:
  87. print(e)
  88. response = ec2Client.describe_security_groups(Filters=[{'Name': 'group-name', 'Values': ['tug-of-war-asg']}])
  89. time.sleep(5)
  90. except ClientError as e:
  91. print(e)
  92. print("Create security group...")
  93. print("------------------------------------")
  94. try:
  95. response = ec2Client.create_security_group(GroupName='tug-of-war-asg',
  96. Description='tug-of-war-asg',
  97. VpcId=vpc_id)
  98. security_group_id = response['GroupId']
  99. print('Security Group Created %s in vpc %s.' % (security_group_id, vpc_id))
  100. data = ec2Client.authorize_security_group_ingress(
  101. GroupId=security_group_id,
  102. IpPermissions=[
  103. {'IpProtocol': 'tcp',
  104. 'FromPort': 3306,
  105. 'ToPort': 3306,
  106. 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]},
  107. {'IpProtocol': 'tcp',
  108. 'FromPort': 22,
  109. 'ToPort': 22,
  110. 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]},
  111. {'IpProtocol': 'tcp',
  112. 'FromPort': 80,
  113. 'ToPort': 80,
  114. 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]},
  115. {'IpProtocol': 'tcp',
  116. 'FromPort': 443,
  117. 'ToPort': 443,
  118. 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]}
  119. ])
  120. print('Ingress Successfully Set %s' % data)
  121. except ClientError as e:
  122. print(e)
  123. print("Running new DB instance...")
  124. print("------------------------------------")
  125. userDataDB = ('#!/bin/bash\n'
  126. '#!/bin/bash\n'
  127. '# extra repo for RedHat rpms\n'
  128. 'yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm\n'
  129. '# essential tools\n'
  130. 'yum install -y joe htop git\n'
  131. '# mysql\n'
  132. 'yum install -y mariadb mariadb-server\n'
  133. '\n'
  134. 'service mariadb start\n'
  135. '\n'
  136. 'echo "create database cloud_tug_of_war" | mysql -u root\n'
  137. '\n'
  138. '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'
  139. '\n'
  140. 'echo "CREATE USER \'cloud_tug_of_war\'@\'%\' IDENTIFIED BY \'cloudpass\';" | mysql -u root\n'
  141. 'echo "GRANT ALL PRIVILEGES ON cloud_tug_of_war.* TO \'cloud_tug_of_war\'@\'%\';" | mysql -u root\n'
  142. 'echo "FLUSH PRIVILEGES" | mysql -u root\n'
  143. )
  144. # convert user-data from script with: cat install-mysql | sed "s/^/'/; s/$/\\\n'/"
  145. response = ec2Client.run_instances(
  146. ImageId=imageId,
  147. InstanceType=instanceType,
  148. Placement={'AvailabilityZone': availabilityZone, },
  149. KeyName=keyName,
  150. MinCount=1,
  151. MaxCount=1,
  152. UserData=userDataDB,
  153. SecurityGroupIds=[
  154. security_group_id,
  155. ],
  156. TagSpecifications=[
  157. {
  158. 'ResourceType': 'instance',
  159. 'Tags': [
  160. {'Key': 'Name', 'Value': 'tug-of-war-asg-db1'},
  161. {'Key': 'tug-of-war-asg', 'Value': 'db'}
  162. ],
  163. }
  164. ],
  165. )
  166. instanceIdDB = response['Instances'][0]['InstanceId']
  167. privateIpDB = response['Instances'][0]['PrivateIpAddress']
  168. # privateIpDB = response['Instances'][0]['NetworkInterfaces'][0]['NetworkInterfaceId']
  169. instance = ec2Resource.Instance(instanceIdDB)
  170. instance.wait_until_running()
  171. print(instanceIdDB)
  172. userDataWebServer = ('#!/bin/bash\n'
  173. '# extra repo for RedHat rpms\n'
  174. 'yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm\n'
  175. '# essential tools\n'
  176. 'yum install -y joe htop git\n'
  177. '# mysql\n'
  178. 'yum install -y httpd php php-mysql\n'
  179. '\n'
  180. 'service httpd start\n'
  181. 'service httpd start\n'
  182. '\n'
  183. # 'wget http://mmnet.informatik.hs-fulda.de/cloudcomp/tug-of-war-in-the-clouds.tar.gz\n'
  184. # 'cp tug-of-war-in-the-clouds.tar.gz /var/www/html/\n'
  185. # 'tar zxvf tug-of-war-in-the-clouds.tar.gz\n'
  186. 'cd /var/www/html\n'
  187. 'wget https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/example-projects/tug-of-war-in-the-clouds/web-content/index.php\n'
  188. 'wget https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/example-projects/tug-of-war-in-the-clouds/web-content/cloud.php\n'
  189. 'wget https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/example-projects/tug-of-war-in-the-clouds/web-content/config.php\n'
  190. '\n'
  191. '# change hostname of db connection\n'
  192. 'sed -i s/localhost/' + privateIpDB + '/g /var/www/html/config.php\n'
  193. )
  194. print("Creating launch configuration...")
  195. print("------------------------------------")
  196. response = asClient.create_launch_configuration(
  197. #IamInstanceProfile='my-iam-role',
  198. ImageId=imageId,
  199. InstanceType=instanceType,
  200. LaunchConfigurationName='tug-of-war-asg-launchconfig',
  201. UserData=userDataWebServer,
  202. KeyName=keyName,
  203. SecurityGroups=[
  204. security_group_id,
  205. ],
  206. )
  207. elbv2Client = boto3.client('elbv2')
  208. print("Creating load balancer...")
  209. print("------------------------------------")
  210. response = elbv2Client.create_load_balancer(
  211. Name='tug-of-war-asg-loadbalancer',
  212. Subnets=[
  213. subnet1,
  214. subnet2,
  215. subnet3,
  216. ],
  217. SecurityGroups=[
  218. security_group_id
  219. ]
  220. )
  221. loadbalancer_arn = response.get('LoadBalancers', [{}])[0].get('LoadBalancerArn', '')
  222. loadbalancer_dns = response.get('LoadBalancers', [{}])[0].get('DNSName', '')
  223. print("Creating target group...")
  224. print("------------------------------------")
  225. response = elbv2Client.create_target_group(
  226. Name='tug-of-war-asg-targetgroup',
  227. Port=80,
  228. Protocol='HTTP',
  229. VpcId=vpc_id,
  230. )
  231. targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '')
  232. print("Creating listener...")
  233. print("------------------------------------")
  234. response = elbv2Client.create_listener(
  235. DefaultActions=[
  236. {
  237. 'TargetGroupArn': targetgroup_arn,
  238. 'Type': 'forward',
  239. },
  240. ],
  241. LoadBalancerArn=loadbalancer_arn,
  242. Port=80,
  243. Protocol='HTTP',
  244. )
  245. response = elbv2Client.modify_target_group_attributes(
  246. TargetGroupArn=targetgroup_arn,
  247. Attributes=[
  248. {
  249. 'Key': 'stickiness.enabled',
  250. 'Value': 'true'
  251. },
  252. ]
  253. )
  254. print("Creating auto scaling group...")
  255. print("------------------------------------")
  256. response = asClient.create_auto_scaling_group(
  257. AutoScalingGroupName='tug-of-war-asg-autoscalinggroup',
  258. LaunchConfigurationName='tug-of-war-asg-launchconfig',
  259. MaxSize=3,
  260. MinSize=1,
  261. HealthCheckGracePeriod=120,
  262. HealthCheckType='ELB',
  263. TargetGroupARNs=[
  264. targetgroup_arn,
  265. ],
  266. VPCZoneIdentifier=subnet1 + ', ' + ', ' + subnet2 + ', ' + subnet3,
  267. Tags=[
  268. {'Key': 'Name', 'Value': 'tug-of-war-asg-webserver', 'PropagateAtLaunch': True},
  269. {'Key': 'tug-of-war', 'Value': 'webserver', 'PropagateAtLaunch': True}
  270. ],
  271. )
  272. print(loadbalancer_arn)
  273. print(targetgroup_arn)
  274. print('app/tug-of-war-asg-loadbalancer/'+str(loadbalancer_arn).split('/')[3]+'/targetgroup/tug-of-war-asg-targetgroup/'+str(targetgroup_arn).split('/')[2])
  275. response = asClient.put_scaling_policy(
  276. AutoScalingGroupName='tug-of-war-asg-autoscalinggroup',
  277. PolicyName='tug-of-war-asg-scalingpolicy',
  278. PolicyType='TargetTrackingScaling',
  279. EstimatedInstanceWarmup=30,
  280. TargetTrackingConfiguration={
  281. 'PredefinedMetricSpecification': {
  282. 'PredefinedMetricType': 'ALBRequestCountPerTarget',
  283. 'ResourceLabel': 'app/tug-of-war-asg-loadbalancer/'+str(loadbalancer_arn).split('/')[3]+'/targetgroup/tug-of-war-asg-targetgroup/'+str(targetgroup_arn).split('/')[2]
  284. },
  285. 'TargetValue': 5.0,
  286. }
  287. )
  288. print('Load Balancer should be reachable at: ' + loadbalancer_dns)