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.

128 lines
4.6 KiB

  1. import boto3
  2. from botocore.exceptions import ClientError
  3. ################################################################################################
  4. #
  5. # Configuration Parameters
  6. #
  7. ################################################################################################
  8. region = 'eu-central-1'
  9. availabilityZone = 'eu-central-1b'
  10. vpc_id = 'vpc-eedd4187'
  11. imageId = 'ami-0cc293023f983ed53'
  12. instanceType = 't3.nano'
  13. keyName = 'srieger-pub'
  14. # if you only have one VPC, vpc_id can be retrieved using:
  15. #
  16. # response = ec2Client.describe_vpcs()
  17. # vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
  18. ################################################################################################
  19. #
  20. # boto3 code
  21. #
  22. ################################################################################################
  23. client = boto3.setup_default_session(region_name=region)
  24. ec2Client = boto3.client("ec2")
  25. ec2Resource = boto3.resource('ec2')
  26. elbv2Client = boto3.client('elbv2')
  27. response = ec2Client.describe_security_groups(Filters=[{'Name': 'group-name', 'Values': ['tug-of-war']}])
  28. security_group_id = response.get('SecurityGroups', [{}])[0].get('GroupId', '')
  29. print("Getting DB IP...")
  30. print("------------------------------------")
  31. response = ec2Client.describe_instances(Filters=[{'Name': 'tag:tug-of-war', 'Values': ['db']}])
  32. print(response)
  33. reservations = response['Reservations']
  34. for reservation in reservations:
  35. for instance in reservation['Instances']:
  36. if instance['State']['Name'] == "running" or instance['State']['Name'] == "pending":
  37. instanceDB = ec2Resource.Instance(instance['InstanceId'])
  38. privateIpDB = instanceDB.private_ip_address
  39. userDataWebServer = ('#!/bin/bash\n'
  40. '# extra repo for RedHat rpms\n'
  41. 'yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm\n'
  42. '# essential tools\n'
  43. 'yum install -y joe htop git\n'
  44. '# mysql\n'
  45. 'yum install -y httpd php php-mysql\n'
  46. '\n'
  47. 'service httpd start\n'
  48. 'service httpd start\n'
  49. '\n'
  50. # 'wget http://mmnet.informatik.hs-fulda.de/cloudcomp/tug-of-war-in-the-clouds.tar.gz\n'
  51. # 'cp tug-of-war-in-the-clouds.tar.gz /var/www/html/\n'
  52. # 'tar zxvf tug-of-war-in-the-clouds.tar.gz\n'
  53. 'cd /var/www/html\n'
  54. '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'
  55. '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'
  56. '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'
  57. '\n'
  58. '# change hostname of db connection\n'
  59. 'sed -i s/localhost/' + privateIpDB + '/g /var/www/html/config.php\n'
  60. )
  61. for i in range(3, 4):
  62. print("Running new Web Server instance...")
  63. print("------------------------------------")
  64. response = ec2Client.run_instances(
  65. ImageId=imageId,
  66. InstanceType=instanceType,
  67. Placement={'AvailabilityZone': availabilityZone, },
  68. KeyName=keyName,
  69. MinCount=1,
  70. MaxCount=1,
  71. UserData=userDataWebServer,
  72. SecurityGroupIds=[
  73. security_group_id,
  74. ],
  75. TagSpecifications=[
  76. {
  77. 'ResourceType': 'instance',
  78. 'Tags': [
  79. {'Key': 'Name', 'Value': 'tug-of-war-webserver' + str(i)},
  80. {'Key': 'tug-of-war', 'Value': 'webserver'}
  81. ],
  82. }
  83. ],
  84. )
  85. instanceIdWeb = response['Instances'][0]['InstanceId']
  86. instance = ec2Resource.Instance(instanceIdWeb)
  87. instance.wait_until_running()
  88. instance.load()
  89. print("tug-of-war-in-the-clouds can be accessed at: " + instance.public_ip_address)
  90. try:
  91. response = elbv2Client.describe_target_groups(Names=['tug-of-war-targetgroup'])
  92. targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '')
  93. except ClientError as e:
  94. print(e)
  95. print("Registering instance...")
  96. print("------------------------------------")
  97. response = elbv2Client.register_targets(
  98. TargetGroupArn=targetgroup_arn,
  99. Targets=[
  100. {
  101. 'Id': instanceIdWeb,
  102. },
  103. ],
  104. )