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.

184 lines
6.0 KiB

  1. import boto3
  2. from botocore.exceptions import ClientError
  3. region = 'eu-central-1'
  4. availabilityZone = 'eu-central-1b'
  5. imageId = 'ami-0cc293023f983ed53'
  6. instanceType = 't3.nano'
  7. keyName = 'srieger-pub'
  8. userDataDB = ('#!/bin/bash\n'
  9. '#!/bin/bash\n'
  10. '# extra repo for RedHat rpms\n'
  11. 'yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm\n'
  12. '# essential tools\n'
  13. 'yum install -y joe htop git\n'
  14. '# mysql\n'
  15. 'yum install -y mariadb mariadb-server\n'
  16. '\n'
  17. 'service mariadb start\n'
  18. '\n'
  19. 'echo "create database cloud_tug_of_war" | mysql -u root\n'
  20. '\n'
  21. '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'
  22. '\n'
  23. 'echo "CREATE USER \'cloud_tug_of_war\'@\'%\' IDENTIFIED BY \'cloud\';" | mysql -u root\n'
  24. 'echo "GRANT ALL PRIVILEGES ON cloud_tug_of_war.* TO \'cloud_tug_of_war\'@\'%\';" | mysql -u root\n'
  25. 'echo "FLUSH PRIVILEGES" | mysql -u root\n'
  26. )
  27. # convert with: cat install-mysql | sed "s/^/'/; s/$/\\\n'/"
  28. client = boto3.setup_default_session(region_name=region)
  29. ec2Client = boto3.client("ec2")
  30. ec2Resource = boto3.resource('ec2')
  31. response = ec2Client.describe_vpcs()
  32. vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
  33. subnet_id = ec2Client.describe_subnets(
  34. Filters=[
  35. {
  36. 'Name': 'availability-zone', 'Values': [availabilityZone]
  37. }
  38. ])['Subnets'][0]['SubnetId']
  39. print("Deleting old instance...")
  40. print("------------------------------------")
  41. response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war']}])
  42. print(response)
  43. reservations = response['Reservations']
  44. for reservation in reservations:
  45. for instance in reservation['Instances']:
  46. if instance['State']['Name'] == "running" or instance['State']['Name'] == "pending":
  47. response = ec2Client.terminate_instances(InstanceIds=[instance['InstanceId']])
  48. print(response)
  49. instanceToTerminate = ec2Resource.Instance(instance['InstanceId'])
  50. instanceToTerminate.wait_until_terminated()
  51. print("Delete old security group...")
  52. print("------------------------------------")
  53. try:
  54. response = ec2Client.delete_security_group(GroupName='tug-of-war')
  55. except ClientError as e:
  56. print(e)
  57. print("Create security group...")
  58. print("------------------------------------")
  59. try:
  60. response = ec2Client.create_security_group(GroupName='tug-of-war',
  61. Description='tug-of-war',
  62. VpcId=vpc_id)
  63. security_group_id = response['GroupId']
  64. print('Security Group Created %s in vpc %s.' % (security_group_id, vpc_id))
  65. data = ec2Client.authorize_security_group_ingress(
  66. GroupId=security_group_id,
  67. IpPermissions=[
  68. {'IpProtocol': 'tcp',
  69. 'FromPort': 3306,
  70. 'ToPort': 3306,
  71. 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]},
  72. {'IpProtocol': 'tcp',
  73. 'FromPort': 22,
  74. 'ToPort': 22,
  75. 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]},
  76. {'IpProtocol': 'tcp',
  77. 'FromPort': 80,
  78. 'ToPort': 80,
  79. 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]},
  80. {'IpProtocol': 'tcp',
  81. 'FromPort': 443,
  82. 'ToPort': 443,
  83. 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]}
  84. ])
  85. print('Ingress Successfully Set %s' % data)
  86. except ClientError as e:
  87. print(e)
  88. print("Running new DB instance...")
  89. print("------------------------------------")
  90. response = ec2Client.run_instances(
  91. ImageId=imageId,
  92. InstanceType=instanceType,
  93. Placement={'AvailabilityZone': availabilityZone, },
  94. KeyName=keyName,
  95. MinCount=1,
  96. MaxCount=1,
  97. UserData=userDataDB,
  98. SecurityGroupIds=[
  99. security_group_id,
  100. ],
  101. TagSpecifications=[
  102. {
  103. 'ResourceType': 'instance',
  104. 'Tags': [
  105. {'Key': 'Name', 'Value': 'tug-of-war-db1'},
  106. {'Key': 'tug-of-war', 'Value': 'installed'}
  107. ],
  108. }
  109. ],
  110. )
  111. instanceIdDB = response['Instances'][0]['InstanceId']
  112. privateIpDB = response['Instances'][0]['PrivateIpAddress']
  113. #privateIpDB = response['Instances'][0]['NetworkInterfaces'][0]['NetworkInterfaceId']
  114. instance = ec2Resource.Instance(instanceIdDB)
  115. instance.wait_until_running()
  116. print(instanceIdDB)
  117. userDataWebServer = ('#!/bin/bash\n'
  118. '# extra repo for RedHat rpms\n'
  119. 'yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm\n'
  120. '# essential tools\n'
  121. 'yum install -y joe htop git\n'
  122. '# mysql\n'
  123. 'yum install -y httpd php php-mysql\n'
  124. '\n'
  125. 'service httpd start\n'
  126. '\n'
  127. 'wget http://mmnet.informatik.hs-fulda.de/cloudcomp/tug-of-war-in-the-clouds.tar.gz\n'
  128. 'cp tug-of-war-in-the-clouds.tar.gz /var/www/html/\n'
  129. 'cd /var/www/html\n'
  130. 'tar zxvf tug-of-war-in-the-clouds.tar.gz\n'
  131. '\n'
  132. '# change hostname of db connection\n'
  133. 'sed -i s/localhost/' + privateIpDB + '/g /var/www/html/config.php\n'
  134. )
  135. print("Running new Web Server instance...")
  136. print("------------------------------------")
  137. response = ec2Client.run_instances(
  138. ImageId=imageId,
  139. InstanceType=instanceType,
  140. Placement={'AvailabilityZone': availabilityZone, },
  141. KeyName=keyName,
  142. MinCount=1,
  143. MaxCount=1,
  144. UserData=userDataWebServer,
  145. SecurityGroupIds=[
  146. security_group_id,
  147. ],
  148. TagSpecifications=[
  149. {
  150. 'ResourceType': 'instance',
  151. 'Tags': [
  152. {'Key': 'Name', 'Value': 'tug-of-war-webserver1'},
  153. {'Key': 'tug-of-war', 'Value': 'installed'}
  154. ],
  155. }
  156. ],
  157. )
  158. instanceIdDB = response['Instances'][0]['InstanceId']
  159. privateIpDB = response['Instances'][0]['PrivateIpAddress']
  160. #privateIpDB = response['Instances'][0]['NetworkInterfaces'][0]['NetworkInterfaceId']
  161. instance = ec2Resource.Instance(instanceIdDB)
  162. instance.wait_until_running()