setup_aws.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env python
  2. #
  3. # Prepares Amazon network to run either vagrant-development or
  4. # vagrant-production. Configures subnets, virtual private clouds,
  5. # security groups, etc
  6. #
  7. # Currently is a one-way operation, you have to delete these
  8. # manually. Visit https://console.aws.amazon.com/vpc and remove the
  9. # VPC tagged TFB_Network and that should undo all changes made my
  10. # this script (for production mode). For development mode, the only
  11. # things created are a security group and a subnet, so find those in
  12. # your standard EC2 console https://console.aws.amazon.com/ec2 and
  13. # delete them manually
  14. import subprocess
  15. import json
  16. import logging
  17. import sys
  18. log = logging.getLogger('aws')
  19. nwtags = "Key=Project,Value=FrameworkBenchmarks Key=TFB_Role,Value=network"
  20. def setup_vpc():
  21. '''Sets up a Virtual Private Cloud to allow hosts to communicate'''
  22. # Setup VPC
  23. log.info("Creating a new Virtual Private Cloud...")
  24. log.info(" See details at http://console.aws.amazon.com/vpc")
  25. vpc = run_aws("create-vpc --cidr-block 172.16.0.0/16 --instance-tenancy default")
  26. vpcid = vpc["Vpc"]["VpcId"]
  27. run_aws("modify-vpc-attribute --vpc-id %s --enable-dns-support" % vpcid)
  28. run_aws("modify-vpc-attribute --vpc-id %s --no-enable-dns-hostnames" % vpcid)
  29. run_aws("create-tags --resources %s --tags %s Key=Name,Value=TFB_Network" % (vpcid, nwtags))
  30. log.debug(run_aws("describe-vpcs --vpc-id %s" % vpcid, load=False))
  31. # Setup internet gateway
  32. log.info("Creating InternetGateway for the VPC...")
  33. igw = run_aws("create-internet-gateway")
  34. igwid = igw["InternetGateway"]["InternetGatewayId"]
  35. run_aws("create-tags --resources %s --tags %s Key=Name,Value=TFB_Gateway" % (igwid, nwtags))
  36. run_aws("attach-internet-gateway --internet-gateway-id %s --vpc-id %s" % (igwid, vpcid))
  37. log.debug(run_aws("describe-internet-gateways --internet-gateway-ids %s" % igwid, load=False))
  38. # Setup public subnet
  39. # NOTE: We considered using a public and private subnet, but
  40. # this requires us to launch an extra EC2 instance for the duration of the
  41. # benchmark to handle the NAT between the public subnet and the private subnet,
  42. # so the cost is quite high. Also, Internet traffic is only generated during
  43. # framework setup stages (e.g. during software installation), not during the
  44. # running of the benchmark.
  45. # We chose to use a single public subnet and filter inbound traffic to prevent
  46. # interference during the test
  47. log.info("Creating subnet inside the VPC...")
  48. pubsub = run_aws("create-subnet --vpc-id %s --cidr-block 172.16.0.0/24" % vpcid)
  49. pubid = pubsub["Subnet"]["SubnetId"]
  50. log.debug("Found subnet id: %s", pubid)
  51. #run_aws("modify-subnet-attribute --subnet-id %s --map-public-ip-on-launch" % pubid)
  52. run_aws("create-tags --resources %s --tags %s Key=Name,Value=TFB_Public" % (pubid, nwtags))
  53. log.debug(run_aws("describe-subnets --subnet-ids %s" % pubid, load=False))
  54. # Setup routing
  55. log.info("Creating routing table for VPC...")
  56. route = run_aws("describe-route-tables --filters Name=vpc-id,Values=%s" % vpcid)
  57. routeid = route["RouteTables"][0]["RouteTableId"]
  58. run_aws("create-tags --resources %s --tags %s Key=Name,Value=TFB_Routing" % (routeid, nwtags))
  59. log.info(" Creating route to internet...")
  60. run_aws("create-route --route-table-id %s --destination-cidr-block 0.0.0.0/0 --gateway-id %s" % (routeid, igwid))
  61. log.info(" Associating routing table and subnet...")
  62. run_aws("associate-route-table --route-table-id %s --subnet-id %s" % (routeid, pubid))
  63. # Setup default security group for instances launched in the VPC
  64. log.info("Creating default security group for VPC")
  65. group = run_aws("create-security-group --group-name TFB_Security --vpc-id %s --description 'FrameworkBenchmarks security group'" % vpcid)
  66. groupid = group["GroupId"]
  67. run_aws("create-tags --resources %s --tags %s Key=Name,Value=TFB_Security" % (groupid, nwtags))
  68. run_aws("authorize-security-group-ingress --group-id %s --protocol tcp --port 22 --cidr 0.0.0.0/0" % groupid)
  69. # run_aws("authorize-security-group-egress --group-id %s --protocol -1 --cidr 0.0.0.0/0 --port all" % groupid)
  70. run_aws("authorize-security-group-ingress --group-id %s --source-group %s --protocol -1 --port -1" % (groupid, groupid))
  71. log.info("Complete."
  72. log.info(" Here are the environment variables you should use:")
  73. print "export TFB_AWS_SUBNET=%s" % pubid
  74. print "export TFB_AWS_SEC_GROUP=%s" % groupid
  75. return vpcid
  76. def unset_vpc(vpcid):
  77. '''Doesn't work at the moment, we need to delete all the other items first'''
  78. run_aws("delete-vpc --vpc-id %s" % vpcid)
  79. def run_aws(command, prefix=True, load=True):
  80. '''Runs an AWS command and returns the JSON
  81. prefix: Should we prefix "aws ec2 " to your command
  82. load: Should we auto-load the response JSON into a python object?
  83. '''
  84. if prefix:
  85. command = "aws ec2 %s" % command
  86. log.debug("Request : %s", command)
  87. result = subprocess.check_output(command, shell=True)
  88. log.debug("Response: %s", result)
  89. if load:
  90. return json.loads(result)
  91. else:
  92. return result
  93. if __name__ == "__main__":
  94. args = sys.argv[1:]
  95. logging.basicConfig(level=logging.INFO)
  96. usage = '''Usage: setup_aws.py
  97. Prepares Amazon network to run either vagrant-development
  98. or vagrant-production. Configures subnets, virtual private
  99. clouds, security groups, etc.
  100. Outputs TFB_AWS_SEC_GROUP and TFB_AWS_SUBNET
  101. Currently is a one-way operation, you have to delete these
  102. manually. This script expects standard AWS environment
  103. variables to exist e.g. AWS_ACCESS_KEY_ID,
  104. AWS_SECRET_ACCESS_KEY, AWS_DEFAULT_REGION
  105. '''
  106. if len(args) != 0:
  107. print usage
  108. sys.exit(1)
  109. setup_vpc()