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.

270 lines
11 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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. images = conn.list_images()
  53. # image = ''
  54. # for img in images:
  55. # # if img.name == ubuntu_image_name:
  56. # if img.extra['owner_alias'] == 'amazon':
  57. # print(img)
  58. # if img.id == ubuntu_image_name:
  59. # image = img
  60. image = [i for i in images if i.name == ubuntu_image_name][0]
  61. print(image)
  62. flavors = conn.list_sizes()
  63. flavor = [s for s in flavors if s.id == flavor_name][0]
  64. print(flavor)
  65. # networks = conn.ex_list_networks()
  66. # network = ''
  67. # for net in networks:
  68. # if net.name == project_network:
  69. # network = net
  70. ###########################################################################
  71. #
  72. # create keypair dependency
  73. #
  74. ###########################################################################
  75. print('Checking for existing SSH key pair...')
  76. keypair_exists = False
  77. for keypair in conn.list_key_pairs():
  78. if keypair.name == keypair_name:
  79. keypair_exists = True
  80. if keypair_exists:
  81. print('Keypair ' + keypair_name + ' already exists. Skipping import.')
  82. else:
  83. print('adding keypair...')
  84. conn.import_key_pair_from_file(keypair_name, pub_key_file)
  85. for keypair in conn.list_key_pairs():
  86. print(keypair)
  87. ###########################################################################
  88. #
  89. # create security group dependency
  90. #
  91. ###########################################################################
  92. print('Checking for existing worker security group...')
  93. worker_security_group_exists = False
  94. worker_security_group_name = 'worker'
  95. for security_group in conn.ex_get_security_groups():
  96. if security_group.name == worker_security_group_name:
  97. worker_security_group_id = security_group.id
  98. worker_security_group_exists = True
  99. if worker_security_group_exists:
  100. print('Worker Security Group ' + worker_security_group_name + ' already exists. Skipping creation.')
  101. else:
  102. worker_security_group_result = conn.ex_create_security_group('worker', 'for services that run on a worker node')
  103. worker_security_group_id = worker_security_group_result['group_id']
  104. conn.ex_authorize_security_group_ingress(worker_security_group_id, 22, 22, cidr_ips=['0.0.0.0/0'],
  105. protocol='tcp')
  106. print('Checking for existing controller security group...')
  107. controller_security_group_exists = False
  108. controller_security_group_name = 'control'
  109. controller_security_group_id = ''
  110. for security_group in conn.ex_get_security_groups():
  111. if security_group.name == controller_security_group_name:
  112. controller_security_group_id = security_group.id
  113. controller_security_group_exists = True
  114. if controller_security_group_exists:
  115. print('Controller Security Group ' + controller_security_group_name + ' already exists. Skipping creation.')
  116. else:
  117. controller_security_group_result = conn.ex_create_security_group('control',
  118. 'for services that run on a control node')
  119. controller_security_group_id = controller_security_group_result['group_id']
  120. conn.ex_authorize_security_group_ingress(controller_security_group_id, 22, 22, cidr_ips=['0.0.0.0/0'],
  121. protocol='tcp')
  122. conn.ex_authorize_security_group_ingress(controller_security_group_id, 80, 80, cidr_ips=['0.0.0.0/0'],
  123. protocol='tcp')
  124. conn.ex_authorize_security_group_ingress(controller_security_group_id, 5672, 5672,
  125. group_pairs=[{'group_id': worker_security_group_id}], protocol='tcp')
  126. # for security_group in conn.ex_list_security_groups():
  127. # print(security_group)
  128. ###########################################################################
  129. #
  130. # create app-controller
  131. #
  132. ###########################################################################
  133. # https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh
  134. # is currently broken, hence the "rabbitctl" lines were added in the example
  135. # below, see also https://bugs.launchpad.net/faafo/+bug/1679710
  136. #
  137. # Thanks to Stefan Friedmann for finding this fix ;)
  138. userdata = '''#!/usr/bin/env bash
  139. curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh | bash -s -- \
  140. -i messaging -i faafo -r api
  141. rabbitmqctl add_user faafo guest
  142. rabbitmqctl set_user_tags faafo administrator
  143. rabbitmqctl set_permissions -p / faafo ".*" ".*" ".*"
  144. '''
  145. print('Starting new app-controller instance and wait until it is running...')
  146. instance_controller_1 = conn.create_node(name='app-controller',
  147. image=image,
  148. size=flavor,
  149. ex_keyname=keypair_name,
  150. ex_userdata=userdata,
  151. ex_security_groups=[controller_security_group_name])
  152. conn.wait_until_running(nodes=[instance_controller_1], timeout=120, ssh_interface='public_ips')
  153. ###########################################################################
  154. #
  155. # assign app-controller elastic ip
  156. #
  157. ###########################################################################
  158. # AWS offers elastic ips, that have the same function as floating IPs in OpenStack. However elastic IPs cost money,
  159. # and instances typically already have public IP in AWS, what a luxury ;)
  160. print('Checking for unused Elastic IP...')
  161. unused_elastic_ip = None
  162. for elastic_ip in conn.ex_describe_all_addresses():
  163. if not elastic_ip.instance_id:
  164. unused_elastic_ip = elastic_ip
  165. break
  166. if not unused_elastic_ip:
  167. print('Allocating new Elastic IP')
  168. unused_elastic_ip = conn.ex_allocate_address()
  169. conn.ex_associate_address_with_node(instance_controller_1, unused_elastic_ip)
  170. print('Controller Application will be deployed to http://%s' % unused_elastic_ip.ip)
  171. ###########################################################################
  172. #
  173. # getting id and ip address of app-controller instance
  174. #
  175. ###########################################################################
  176. # instance should not have a public ip? floating ips are assigned later
  177. # instance_controller_1 = conn.ex_get_node_details(instance_controller_1.id)
  178. # if instance_controller_1.public_ips:
  179. # ip_controller = instance_controller_1.public_ips[0]
  180. # else:
  181. ip_controller = instance_controller_1.private_ips[0]
  182. ###########################################################################
  183. #
  184. # create app-worker-1
  185. #
  186. ###########################################################################
  187. userdata = '''#!/usr/bin/env bash
  188. curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh | bash -s -- \
  189. -i faafo -r worker -e 'http://%(ip_controller)s' -m 'amqp://faafo:guest@%(ip_controller)s:5672/'
  190. ''' % {'ip_controller': ip_controller}
  191. print('Starting new app-worker-1 instance and wait until it is running...')
  192. instance_worker_1 = conn.create_node(name='app-worker-1',
  193. image=image,
  194. size=flavor,
  195. ex_keyname=keypair_name,
  196. ex_userdata=userdata,
  197. ex_security_groups=[worker_security_group_name])
  198. conn.wait_until_running(nodes=[instance_worker_1], timeout=120, ssh_interface='public_ips')
  199. ###########################################################################
  200. #
  201. # assign app-worker elastic ip
  202. #
  203. ###########################################################################
  204. # AWS offers elastic ips, that have the same function as floating IPs in OpenStack. However elastic IPs cost money,
  205. # and instances typically already have public IP in AWS, what a luxury ;)
  206. print('Checking for unused Elastic IP...')
  207. unused_elastic_ip = None
  208. for elastic_ip in conn.ex_describe_all_addresses():
  209. if not elastic_ip.instance_id:
  210. unused_elastic_ip = elastic_ip
  211. break
  212. if not unused_elastic_ip:
  213. print('Allocating new Elastic IP')
  214. unused_elastic_ip = conn.ex_allocate_address()
  215. conn.ex_associate_address_with_node(instance_worker_1, unused_elastic_ip)
  216. print('The worker will be available for SSH at %s' % unused_elastic_ip.ip)
  217. print('You can use ssh to login to the controller using your private key. After login, you can list available '
  218. 'fractals using "faafo list". To request the generation of new fractals, you can use "faafo create". '
  219. 'You can also see other options to use the faafo example cloud service using "faafo -h".')
  220. if __name__ == '__main__':
  221. main()