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.

261 lines
11 KiB

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