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.

127 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. '\n'
  49. # 'wget http://mmnet.informatik.hs-fulda.de/cloudcomp/tug-of-war-in-the-clouds.tar.gz\n'
  50. # 'cp tug-of-war-in-the-clouds.tar.gz /var/www/html/\n'
  51. # 'tar zxvf tug-of-war-in-the-clouds.tar.gz\n'
  52. 'cd /var/www/html\n'
  53. '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'
  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/cloud.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/config.php\n'
  56. '\n'
  57. '# change hostname of db connection\n'
  58. 'sed -i s/localhost/' + privateIpDB + '/g /var/www/html/config.php\n'
  59. )
  60. for i in range(3, 4):
  61. print("Running new Web Server instance...")
  62. print("------------------------------------")
  63. response = ec2Client.run_instances(
  64. ImageId=imageId,
  65. InstanceType=instanceType,
  66. Placement={'AvailabilityZone': availabilityZone, },
  67. KeyName=keyName,
  68. MinCount=1,
  69. MaxCount=1,
  70. UserData=userDataWebServer,
  71. SecurityGroupIds=[
  72. security_group_id,
  73. ],
  74. TagSpecifications=[
  75. {
  76. 'ResourceType': 'instance',
  77. 'Tags': [
  78. {'Key': 'Name', 'Value': 'tug-of-war-webserver' + str(i)},
  79. {'Key': 'tug-of-war', 'Value': 'webserver'}
  80. ],
  81. }
  82. ],
  83. )
  84. instanceIdWeb = response['Instances'][0]['InstanceId']
  85. instance = ec2Resource.Instance(instanceIdWeb)
  86. instance.wait_until_running()
  87. instance.load()
  88. print("tug-of-war-in-the-clouds can be accessed at: " + instance.public_ip_address)
  89. try:
  90. response = elbv2Client.describe_target_groups(Names=['tug-of-war-targetgroup'])
  91. targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '')
  92. except ClientError as e:
  93. print(e)
  94. print("Registering instance...")
  95. print("------------------------------------")
  96. response = elbv2Client.register_targets(
  97. TargetGroupArn=targetgroup_arn,
  98. Targets=[
  99. {
  100. 'Id': instanceIdWeb,
  101. },
  102. ],
  103. )