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.

285 lines
12 KiB

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