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.

275 lines
12 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
  1. import getpass
  2. # import os
  3. from libcloud.compute.providers import get_driver
  4. from libcloud.compute.types import Provider
  5. # reqs:
  6. # services: EC2 (nova, glance, neutron)
  7. # resources: 2 instances, 2 elastic ips (1 keypair, 2 security groups)
  8. # The image to look for and use for the started instance
  9. ubuntu_image_name = 'ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-20200408'
  10. # ubuntu_image_id = "ami-0e342d72b12109f91" # local ami id for resent ubuntu 18.04 20200408 in eu-central-1
  11. # The public key to be used for SSH connection, please make sure, that you have the corresponding private key
  12. #
  13. # id_rsa.pub should look like this (standard sshd pubkey format):
  14. # ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAw+J...F3w2mleybgT1w== user@HOSTNAME
  15. keypair_name = 'srieger-pub'
  16. pub_key_file = '~/.ssh/id_rsa.pub'
  17. flavor_name = 't2.nano'
  18. # default region
  19. # region_name = 'eu-central-1'
  20. # region_name = 'ap-south-1'
  21. # AWS Educate only allows us-east-1 see our AWS classroom at https://www.awseducate.com
  22. # e.g., https://www.awseducate.com/student/s/launch-classroom?classroomId=a1v3m000005mNm6AAE
  23. region_name = 'us-east-1'
  24. def main():
  25. ###########################################################################
  26. #
  27. # get credentials
  28. #
  29. ###########################################################################
  30. # see AWS Educate classroom, Account Details
  31. # access_id = getpass.win_getpass("Enter your access_id:")
  32. # secret_key = getpass.win_getpass("Enter your secret_key:")
  33. # session_token = getpass.win_getpass("Enter your session_token:")
  34. access_id = "ASIAU..."
  35. secret_key = "7lafW..."
  36. session_token = "IQoJb3JpZ...EMb//..."
  37. ###########################################################################
  38. #
  39. # create connection
  40. #
  41. ###########################################################################
  42. provider = get_driver(Provider.EC2)
  43. conn = provider(access_id,
  44. secret_key,
  45. token=session_token,
  46. region=region_name)
  47. ###########################################################################
  48. #
  49. # get image, flavor, network for instance creation
  50. #
  51. ###########################################################################
  52. print("Fetching images (AMI) list from AWS region. This will take a lot of seconds (AWS has a very long list of "
  53. "supported operating systems and versions)... please be patient...")
  54. images = conn.list_images()
  55. # image = ''
  56. # for img in images:
  57. # # if img.name == ubuntu_image_name:
  58. # if img.extra['owner_alias'] == 'amazon':
  59. # print(img)
  60. # if img.id == ubuntu_image_name:
  61. # image = img
  62. # fetch/select the image referenced with ubuntu_image_name above
  63. image = [i for i in images if i.name == ubuntu_image_name][0]
  64. print(image)
  65. flavors = conn.list_sizes()
  66. flavor = [s for s in flavors if s.id == flavor_name][0]
  67. print(flavor)
  68. # networks = conn.ex_list_networks()
  69. # network = ''
  70. # for net in networks:
  71. # if net.name == project_network:
  72. # network = net
  73. ###########################################################################
  74. #
  75. # create keypair dependency
  76. #
  77. ###########################################################################
  78. print('Checking for existing SSH key pair...')
  79. keypair_exists = False
  80. for keypair in conn.list_key_pairs():
  81. if keypair.name == keypair_name:
  82. keypair_exists = True
  83. if keypair_exists:
  84. print('Keypair ' + keypair_name + ' already exists. Skipping import.')
  85. else:
  86. print('adding keypair...')
  87. conn.import_key_pair_from_file(keypair_name, pub_key_file)
  88. for keypair in conn.list_key_pairs():
  89. print(keypair)
  90. ###########################################################################
  91. #
  92. # create security group dependency
  93. #
  94. ###########################################################################
  95. print('Checking for existing worker security group...')
  96. worker_security_group_exists = False
  97. worker_security_group_name = 'worker'
  98. for security_group in conn.ex_get_security_groups():
  99. if security_group.name == worker_security_group_name:
  100. worker_security_group_id = security_group.id
  101. worker_security_group_exists = True
  102. if worker_security_group_exists:
  103. print('Worker Security Group ' + worker_security_group_name + ' already exists. Skipping creation.')
  104. else:
  105. worker_security_group_result = conn.ex_create_security_group('worker', 'for services that run on a worker node')
  106. worker_security_group_id = worker_security_group_result['group_id']
  107. conn.ex_authorize_security_group_ingress(worker_security_group_id, 22, 22, cidr_ips=['0.0.0.0/0'],
  108. protocol='tcp')
  109. print('Checking for existing controller security group...')
  110. controller_security_group_exists = False
  111. controller_security_group_name = 'control'
  112. controller_security_group_id = ''
  113. for security_group in conn.ex_get_security_groups():
  114. if security_group.name == controller_security_group_name:
  115. controller_security_group_id = security_group.id
  116. controller_security_group_exists = True
  117. if controller_security_group_exists:
  118. print('Controller Security Group ' + controller_security_group_name + ' already exists. Skipping creation.')
  119. else:
  120. controller_security_group_result = conn.ex_create_security_group('control',
  121. 'for services that run on a control node')
  122. controller_security_group_id = controller_security_group_result['group_id']
  123. conn.ex_authorize_security_group_ingress(controller_security_group_id, 22, 22, cidr_ips=['0.0.0.0/0'],
  124. protocol='tcp')
  125. conn.ex_authorize_security_group_ingress(controller_security_group_id, 80, 80, cidr_ips=['0.0.0.0/0'],
  126. protocol='tcp')
  127. conn.ex_authorize_security_group_ingress(controller_security_group_id, 5672, 5672,
  128. group_pairs=[{'group_id': worker_security_group_id}], protocol='tcp')
  129. # for security_group in conn.ex_list_security_groups():
  130. # print(security_group)
  131. ###########################################################################
  132. #
  133. # create app-controller
  134. #
  135. ###########################################################################
  136. # https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh
  137. # is currently broken, hence the "rabbitctl" lines were added in the example
  138. # below, see also https://bugs.launchpad.net/faafo/+bug/1679710
  139. #
  140. # Thanks to Stefan Friedmann for finding this fix ;)
  141. userdata = '''#!/usr/bin/env bash
  142. curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh | bash -s -- \
  143. -i messaging -i faafo -r api
  144. rabbitmqctl add_user faafo guest
  145. rabbitmqctl set_user_tags faafo administrator
  146. rabbitmqctl set_permissions -p / faafo ".*" ".*" ".*"
  147. '''
  148. print('Starting new app-controller instance and wait until it is running...')
  149. instance_controller_1 = conn.create_node(name='app-controller',
  150. image=image,
  151. size=flavor,
  152. ex_keyname=keypair_name,
  153. ex_userdata=userdata,
  154. ex_security_groups=[controller_security_group_name])
  155. conn.wait_until_running(nodes=[instance_controller_1], timeout=120, ssh_interface='public_ips')
  156. ###########################################################################
  157. #
  158. # assign app-controller elastic ip
  159. #
  160. ###########################################################################
  161. # AWS offers elastic ips, that have the same function as floating IPs in OpenStack. However elastic IPs cost money,
  162. # and instances typically already have public IP in AWS, what a luxury ;)
  163. print('Checking for unused Elastic IP...')
  164. unused_elastic_ip = None
  165. for elastic_ip in conn.ex_describe_all_addresses():
  166. if not elastic_ip.instance_id:
  167. unused_elastic_ip = elastic_ip
  168. break
  169. if not unused_elastic_ip:
  170. print('Allocating new Elastic IP')
  171. unused_elastic_ip = conn.ex_allocate_address()
  172. conn.ex_associate_address_with_node(instance_controller_1, unused_elastic_ip)
  173. print('Controller Application will be deployed to http://%s' % unused_elastic_ip.ip)
  174. ###########################################################################
  175. #
  176. # getting id and ip address of app-controller instance
  177. #
  178. ###########################################################################
  179. # instance should not have a public ip? floating ips are assigned later
  180. # instance_controller_1 = conn.ex_get_node_details(instance_controller_1.id)
  181. # if instance_controller_1.public_ips:
  182. # ip_controller = instance_controller_1.public_ips[0]
  183. # else:
  184. ip_controller = instance_controller_1.private_ips[0]
  185. ###########################################################################
  186. #
  187. # create app-worker-1
  188. #
  189. ###########################################################################
  190. userdata = '''#!/usr/bin/env bash
  191. curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh | bash -s -- \
  192. -i faafo -r worker -e 'http://%(ip_controller)s' -m 'amqp://faafo:guest@%(ip_controller)s:5672/'
  193. ''' % {'ip_controller': ip_controller}
  194. print('Starting new app-worker-1 instance and wait until it is running...')
  195. instance_worker_1 = conn.create_node(name='app-worker-1',
  196. image=image,
  197. size=flavor,
  198. ex_keyname=keypair_name,
  199. ex_userdata=userdata,
  200. ex_security_groups=[worker_security_group_name])
  201. conn.wait_until_running(nodes=[instance_worker_1], timeout=120, ssh_interface='public_ips')
  202. ###########################################################################
  203. #
  204. # assign app-worker elastic ip
  205. #
  206. ###########################################################################
  207. # AWS offers elastic ips, that have the same function as floating IPs in OpenStack. However elastic IPs cost money,
  208. # and instances typically already have public IP in AWS, what a luxury ;)
  209. print('Checking for unused Elastic IP...')
  210. unused_elastic_ip = None
  211. for elastic_ip in conn.ex_describe_all_addresses():
  212. if not elastic_ip.instance_id:
  213. unused_elastic_ip = elastic_ip
  214. break
  215. if not unused_elastic_ip:
  216. print('Allocating new Elastic IP')
  217. unused_elastic_ip = conn.ex_allocate_address()
  218. conn.ex_associate_address_with_node(instance_worker_1, unused_elastic_ip)
  219. print('The worker will be available for SSH at %s' % unused_elastic_ip.ip)
  220. print('You can use ssh to login to the controller using your private key. After login, you can list available '
  221. 'fractals using "faafo list". To request the generation of new fractals, you can use "faafo create". '
  222. 'You can also see other options to use the faafo example cloud service using "faafo -h".')
  223. if __name__ == '__main__':
  224. main()