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.

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