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.

317 lines
14 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. import configparser
  2. from os.path import expanduser
  3. from libcloud.compute.base import NodeImage
  4. from libcloud.compute.providers import get_driver
  5. from libcloud.compute.types import Provider
  6. home = expanduser("~")
  7. # requirements:
  8. # services: EC2
  9. # resources: 2 instances (1 keypair, 2 security groups)
  10. # optionally also elastic ip (comparable to floating ip) offering a persistent public
  11. # IP, but elastic ips are expensive - make sure to delete them after you used them
  12. # The image to look for and use for the started instance
  13. ubuntu_image_name = 'ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-20210128'
  14. # TODO: 18.04, currently still needed for faafo, need to port faafo demo app to 20.04 or higher and python3...
  15. # The public key to be used for SSH connection, please make sure, that you have the corresponding private key
  16. # use existing vockey in AWS Lab env from vocareum, enables login directly
  17. # from the lab's terminal:
  18. #
  19. # ssh -i ~/.ssh/labuser.pem ubuntu@<public-ip>
  20. keypair_name = "vockey"
  21. # keypair_name = 'srieger-pub'
  22. pub_key_file = home + '/.ssh/id_rsa.pub'
  23. # id_rsa.pub should look like this (standard sshd pubkey format):
  24. # ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAw+J...F3w2mleybgT1w== user@HOSTNAME
  25. # flavor_name = 't2.nano'
  26. # t2.nano only has 512 MB RAM, pip install will cause out of memory (OOM), install-aws.sh cloud-init script uses swap
  27. # to circumvent this issue, but t2.micro is also cheap and has 1 GB RAM which is sufficient for faafo example
  28. flavor_name = 't2.micro'
  29. # default region
  30. # region_name = 'eu-central-1'
  31. # region_name = 'ap-south-1'
  32. # AWS Academy Labs only allow us-east-1 see our AWS Academy Lab Guide, https://awsacademy.instructure.com/login/
  33. region_name = 'us-east-1'
  34. # starting instances in AWS Academy takes significantly longer compared to paid AWS accounts, allow ~ >2 minutes timeout
  35. timeout = 600
  36. def main():
  37. ###########################################################################
  38. #
  39. # get credentials
  40. #
  41. ###########################################################################
  42. # see AWS Academy Lab for Account Details
  43. # read credentials from file
  44. config = configparser.ConfigParser()
  45. config.read_file(open(home + '/.aws/credentials'))
  46. aws_access_key_id = config['default']['aws_access_key_id']
  47. aws_secret_access_key = config['default']['aws_secret_access_key']
  48. aws_session_token = config['default']['aws_session_token']
  49. # hard coded AWS credentials using vars
  50. # aws_access_key_id = "ASIAX..."
  51. # aws_secret_access_key = "WLxxXK+..."
  52. # aws_session_token = "FwoGZXIvYXdzEMb//////////wEaDE5rX.......0SleZ+L75I9iEri9LA4hovWul8HvexhCBK8.......................Ae/T+VkUbcQRtJEDwg+gYCABuk0JlSj5Wk7YA65r3BSNJXZFpkhbek6VBjvE/cEt5fKZEhENcdFxjAcAJLd6bOWi/oGXU5e3PX3mcXgm0oJpz6h3wqD1LvSDtw5GDwn0BHiF1Mu.......................cm/VukK5F"
  53. ###########################################################################
  54. #
  55. # create connection
  56. #
  57. ###########################################################################
  58. provider = get_driver(Provider.EC2)
  59. conn = provider(aws_access_key_id,
  60. aws_secret_access_key,
  61. token=aws_session_token,
  62. region=region_name)
  63. ###########################################################################
  64. #
  65. # get image, flavor, network for instance creation
  66. #
  67. ###########################################################################
  68. print("Search for AMI...")
  69. image = conn.list_images(ex_filters={"name": ubuntu_image_name})[0]
  70. print("Using image: %s" % image)
  71. # print("Fetching images (AMI) list from AWS region. This will take a lot of seconds (AWS has a very long list of "
  72. # "supported operating systems and versions)... please be patient...")
  73. # image = ''
  74. # for img in images:
  75. # # if img.name == ubuntu_image_name:
  76. # if img.extra['owner_alias'] == 'amazon':
  77. # print(img)
  78. # if img.id == ubuntu_image_name:
  79. # image = img
  80. # fetch/select the image referenced with ubuntu_image_name above
  81. # image = [i for i in images if i.name == ubuntu_image_name][0]
  82. # print(image)
  83. # select image directly to save time, as retrieving the image list takes several minutes now,
  84. # need to change ami id here if updated or for other regions, id is working for course in
  85. # summer term 2022, in region: us-east-1 and pointing to ubuntu 18.04 used in the instance wizard,
  86. # to update AMI id use the create instance wizard and copy amd64 image id for ubuntu 18.04 in the
  87. # desired region
  88. # image = NodeImage(id="ami-0e472ba40eb589f49",
  89. # name=ubuntu_image_name,
  90. # driver="hvm")
  91. flavors = conn.list_sizes()
  92. flavor = [s for s in flavors if s.id == flavor_name][0]
  93. print(flavor)
  94. # networks = conn.ex_list_networks()
  95. # network = ''
  96. # for net in networks:
  97. # if net.name == project_network:
  98. # network = net
  99. ###########################################################################
  100. #
  101. # create keypair dependency
  102. #
  103. ###########################################################################
  104. print('Checking for existing SSH key pair...')
  105. keypair_exists = False
  106. for keypair in conn.list_key_pairs():
  107. if keypair.name == keypair_name:
  108. keypair_exists = True
  109. if keypair_exists:
  110. print('Keypair ' + keypair_name + ' already exists. Skipping import.')
  111. else:
  112. print('adding keypair...')
  113. conn.import_key_pair_from_file(keypair_name, pub_key_file)
  114. for keypair in conn.list_key_pairs():
  115. print(keypair)
  116. ###########################################################################
  117. #
  118. # create security group dependency
  119. #
  120. ###########################################################################
  121. print('Checking for existing worker security group...')
  122. worker_security_group_exists = False
  123. worker_security_group_name = 'worker'
  124. for security_group in conn.ex_get_security_groups():
  125. if security_group.name == worker_security_group_name:
  126. worker_security_group_id = security_group.id
  127. worker_security_group_exists = True
  128. if worker_security_group_exists:
  129. print('Worker Security Group ' + worker_security_group_name + ' already exists. Skipping creation.')
  130. else:
  131. worker_security_group_result = conn.ex_create_security_group('worker', 'for services that run on a worker node')
  132. worker_security_group_id = worker_security_group_result['group_id']
  133. conn.ex_authorize_security_group_ingress(worker_security_group_id, 22, 22, cidr_ips=['0.0.0.0/0'],
  134. protocol='tcp')
  135. print('Checking for existing controller security group...')
  136. controller_security_group_exists = False
  137. controller_security_group_name = 'control'
  138. controller_security_group_id = ''
  139. for security_group in conn.ex_get_security_groups():
  140. if security_group.name == controller_security_group_name:
  141. controller_security_group_id = security_group.id
  142. controller_security_group_exists = True
  143. if controller_security_group_exists:
  144. print('Controller Security Group ' + controller_security_group_name + ' already exists. Skipping creation.')
  145. else:
  146. controller_security_group_result = conn.ex_create_security_group('control',
  147. 'for services that run on a control node')
  148. controller_security_group_id = controller_security_group_result['group_id']
  149. conn.ex_authorize_security_group_ingress(controller_security_group_id, 22, 22, cidr_ips=['0.0.0.0/0'],
  150. protocol='tcp')
  151. conn.ex_authorize_security_group_ingress(controller_security_group_id, 80, 80, cidr_ips=['0.0.0.0/0'],
  152. protocol='tcp')
  153. conn.ex_authorize_security_group_ingress(controller_security_group_id, 5672, 5672,
  154. group_pairs=[{'group_id': worker_security_group_id}], protocol='tcp')
  155. # for security_group in conn.ex_list_security_groups():
  156. # print(security_group)
  157. ###########################################################################
  158. #
  159. # create app-controller
  160. #
  161. ###########################################################################
  162. # https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh
  163. # is currently broken, hence the "rabbitctl" lines were added in the example
  164. # below, see also https://bugs.launchpad.net/faafo/+bug/1679710
  165. #
  166. # Thanks to Stefan Friedmann for finding this fix ;)
  167. userdata = '''#!/usr/bin/env bash
  168. curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install-aws.sh | bash -s -- \
  169. -i messaging -i faafo -r api
  170. rabbitmqctl add_user faafo guest
  171. rabbitmqctl set_user_tags faafo administrator
  172. rabbitmqctl set_permissions -p / faafo ".*" ".*" ".*"
  173. '''
  174. print('Starting new app-controller instance and wait until it is running (can take several minutes in free AWS'
  175. ' academy accounts compared to seconds when using a regular paid AWS account), timeout %i seconds...'
  176. % timeout)
  177. instance_controller_1 = conn.create_node(name='app-controller',
  178. image=image,
  179. size=flavor,
  180. ex_keyname=keypair_name,
  181. ex_userdata=userdata,
  182. ex_security_groups=[controller_security_group_name])
  183. wait_until_running_result = conn.wait_until_running(nodes=[instance_controller_1], timeout=timeout, ssh_interface='public_ips')
  184. instance_controller_1, node_addresses = wait_until_running_result[0]
  185. ###########################################################################
  186. #
  187. # assign app-controller elastic ip
  188. #
  189. ###########################################################################
  190. # AWS offers elastic ips, that have the same function as floating IPs in OpenStack. However, elastic IPs cost money,
  191. # and instances typically already have public IP in AWS, what a luxury ;) so I commented out elastic IP creation to
  192. # save your AWS academy budget
  193. # print('Checking for unused Elastic IP...')
  194. # unused_elastic_ip = None
  195. # for elastic_ip in conn.ex_describe_all_addresses():
  196. # if not elastic_ip.instance_id:
  197. # unused_elastic_ip = elastic_ip
  198. # break
  199. #
  200. # if not unused_elastic_ip:
  201. # print('Allocating new Elastic IP')
  202. # unused_elastic_ip = conn.ex_allocate_address()
  203. # conn.ex_associate_address_with_node(instance_controller_1, unused_elastic_ip)
  204. # print('Controller Application will be deployed to http://%s' % unused_elastic_ip.ip)
  205. ###########################################################################
  206. #
  207. # getting id and ip address of app-controller instance
  208. #
  209. ###########################################################################
  210. # instance_controller_1 = conn.list_nodes(ex_node_ids=instance_controller_1.id)
  211. public_ip_controller = instance_controller_1.public_ips[0]
  212. print('Controller Application %s will be reachable after cloud-init has run at http://%s, you can also connect'
  213. % (instance_controller_1.id, public_ip_controller) +
  214. ' via ssh ubuntu@%s' % public_ip_controller)
  215. # get private IP of instance to use in worker/service instances to connect to the controller
  216. private_ip_controller = instance_controller_1.private_ips[0]
  217. ###########################################################################
  218. #
  219. # create app-worker-1
  220. #
  221. ###########################################################################
  222. userdata = '''#!/usr/bin/env bash
  223. curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install-aws.sh | bash -s -- \
  224. -i faafo -r worker -e 'http://%(ip_controller)s' -m 'amqp://faafo:guest@%(ip_controller)s:5672/'
  225. ''' % {'ip_controller': private_ip_controller}
  226. print('Starting new app-worker-1 instance and wait until it is running...')
  227. instance_worker_1 = conn.create_node(name='app-worker-1',
  228. image=image,
  229. size=flavor,
  230. ex_keyname=keypair_name,
  231. ex_userdata=userdata,
  232. ex_security_groups=[worker_security_group_name])
  233. wait_until_running_result = conn.wait_until_running(nodes=[instance_worker_1], timeout=timeout, ssh_interface='public_ips')
  234. instance_worker_1, node_addresses = wait_until_running_result[0]
  235. print('Worker instance %s has private ip: %s ' % (instance_worker_1.id, instance_worker_1.private_ips[0]))
  236. ###########################################################################
  237. #
  238. # assign app-worker elastic ip
  239. #
  240. ###########################################################################
  241. # AWS offers elastic ips, that have the same function as floating IPs in OpenStack. However, elastic IPs cost money,
  242. # and instances typically already have public IP in AWS, what a luxury ;) so I commented out elastic IP creation to
  243. # save your AWS academy budget
  244. # print('Checking for unused Elastic IP...')
  245. # unused_elastic_ip = None
  246. # for elastic_ip in conn.ex_describe_all_addresses():
  247. # if not elastic_ip.instance_id:
  248. # unused_elastic_ip = elastic_ip
  249. # break
  250. #
  251. # if not unused_elastic_ip:
  252. # print('Allocating new Elastic IP')
  253. # unused_elastic_ip = conn.ex_allocate_address()
  254. # conn.ex_associate_address_with_node(instance_worker_1, unused_elastic_ip)
  255. # print('The worker will be available for SSH at %s' % unused_elastic_ip.ip)
  256. #
  257. print('You can use ssh to login to the controller using your private key. After login, you can list available '
  258. 'fractals using "faafo list". To request the generation of new fractals, you can use "faafo create". '
  259. 'You can also see other options to use the faafo example cloud service using "faafo -h".')
  260. if __name__ == '__main__':
  261. main()