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.

278 lines
11 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. #Python 2 to Python 3 conversion made by "Usama Tahir"
  2. # import getpass
  3. # import os
  4. from libcloud.compute.providers import get_driver
  5. from libcloud.compute.types import Provider
  6. # reqs:
  7. # services: nova, glance, neutron
  8. # resources: 2 instances, 2 floating ips (1 keypair, 2 security groups)
  9. # Please use 1-29 for X in the following variable to specify your group number. (will be used for the username,
  10. # project etc., as coordinated in the lab sessions)
  11. group_number = 30
  12. # web service endpoint of the private cloud infrastructure
  13. auth_url = 'https://private-cloud.informatik.hs-fulda.de:5000'
  14. # your username in OpenStack
  15. auth_username = 'CloudComp' + str(group_number)
  16. # your project in OpenStack
  17. project_name = 'CloudComp' + str(group_number)
  18. # A network in the project the started instance will be attached to
  19. project_network = 'CloudComp' + str(group_number) + '-net'
  20. # The image to look for and use for the started instance
  21. ubuntu_image_name = "Ubuntu 18.04 - Bionic Beaver - 64-bit - Cloud Based Image"
  22. # TODO: Ubuntu >18.04 would require major updates to faafo example again/better option: complete rewrite of example?
  23. # The public key to be used for SSH connection, please make sure, that you have the corresponding private key
  24. #
  25. # id_rsa.pub should look like this (standard sshd pubkey format):
  26. # ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAw+J...F3w2mleybgT1w== user@HOSTNAME
  27. keypair_name = 'srieger-pub'
  28. pub_key_file = '~/.ssh/id_rsa.pub'
  29. flavor_name = 'm1.small'
  30. # default region
  31. region_name = 'RegionOne'
  32. # domain to use, "default" for local accounts, "hsfulda" for RZ LDAP, e.g., using fdaiXXXX as auth_username
  33. # domain_name = "default"
  34. def main():
  35. ###########################################################################
  36. #
  37. # get credentials
  38. #
  39. ###########################################################################
  40. # if "OS_PASSWORD" in os.environ:
  41. # auth_password = os.environ["OS_PASSWORD"]
  42. # else:
  43. # auth_password = getpass.getpass("Enter your OpenStack password:")
  44. auth_password = "demo"
  45. ###########################################################################
  46. #
  47. # create connection
  48. #
  49. ###########################################################################
  50. provider = get_driver(Provider.OPENSTACK)
  51. conn = provider(auth_username,
  52. auth_password,
  53. ex_force_auth_url=auth_url,
  54. ex_force_auth_version='3.x_password',
  55. ex_tenant_name=project_name,
  56. ex_force_service_region=region_name)
  57. # ex_domain_name=domain_name)
  58. ###########################################################################
  59. #
  60. # get image, flavor, network for instance creation
  61. #
  62. ###########################################################################
  63. images = conn.list_images()
  64. image = ''
  65. for img in images:
  66. if img.name == ubuntu_image_name:
  67. image = img
  68. flavors = conn.list_sizes()
  69. flavor = ''
  70. for flav in flavors:
  71. if flav.name == flavor_name:
  72. flavor = conn.ex_get_size(flav.id)
  73. networks = conn.ex_list_networks()
  74. network = ''
  75. for net in networks:
  76. if net.name == project_network:
  77. network = net
  78. ###########################################################################
  79. #
  80. # create keypair dependency
  81. #
  82. ###########################################################################
  83. print('Checking for existing SSH key pair...')
  84. keypair_exists = False
  85. for keypair in conn.list_key_pairs():
  86. if keypair.name == keypair_name:
  87. keypair_exists = True
  88. if keypair_exists:
  89. print(('Keypair ' + keypair_name + ' already exists. Skipping import.'))
  90. else:
  91. print('adding keypair...')
  92. conn.import_key_pair_from_file(keypair_name, pub_key_file)
  93. for keypair in conn.list_key_pairs():
  94. print(keypair)
  95. ###########################################################################
  96. #
  97. # create security group dependency
  98. #
  99. ###########################################################################
  100. print('Checking for existing worker security group...')
  101. security_group_name = 'worker'
  102. security_group_exists = False
  103. worker_security_group = ''
  104. for security_group in conn.ex_list_security_groups():
  105. if security_group.name == security_group_name:
  106. worker_security_group = security_group
  107. security_group_exists = True
  108. if security_group_exists:
  109. print(('Worker Security Group ' + worker_security_group.name + ' already exists. Skipping creation.'))
  110. else:
  111. worker_security_group = conn.ex_create_security_group('worker', 'for services that run on a worker node')
  112. conn.ex_create_security_group_rule(worker_security_group, 'TCP', 22, 22)
  113. print('Checking for existing controller security group...')
  114. security_group_name = 'control'
  115. security_group_exists = False
  116. controller_security_group = ''
  117. for security_group in conn.ex_list_security_groups():
  118. if security_group.name == security_group_name:
  119. controller_security_group = security_group
  120. security_group_exists = True
  121. if security_group_exists:
  122. print(('Controller Security Group ' + controller_security_group.name + ' already exists. Skipping creation.'))
  123. else:
  124. controller_security_group = conn.ex_create_security_group('control', 'for services that run on a control node')
  125. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 22, 22)
  126. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 80, 80)
  127. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 5672, 5672,
  128. source_security_group=worker_security_group)
  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. networks=[network],
  153. ex_keyname=keypair_name,
  154. ex_userdata=userdata,
  155. ex_security_groups=[controller_security_group])
  156. conn.wait_until_running(nodes=[instance_controller_1], timeout=120, ssh_interface='private_ips')
  157. ###########################################################################
  158. #
  159. # assign app-controller floating ip
  160. #
  161. ###########################################################################
  162. print('Checking for unused Floating IP...')
  163. unused_floating_ip = None
  164. for floating_ip in conn.ex_list_floating_ips():
  165. if not floating_ip.node_id:
  166. unused_floating_ip = floating_ip
  167. break
  168. if not unused_floating_ip:
  169. pool = conn.ex_list_floating_ip_pools()[0]
  170. print(('Allocating new Floating IP from pool: {}'.format(pool)))
  171. unused_floating_ip = pool.create_floating_ip()
  172. conn.ex_attach_floating_ip_to_node(instance_controller_1, unused_floating_ip)
  173. print(('Controller Application will be deployed to http://%s' % unused_floating_ip.ip_address))
  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. networks=[network],
  199. ex_keyname=keypair_name,
  200. ex_userdata=userdata,
  201. ex_security_groups=[worker_security_group])
  202. conn.wait_until_running(nodes=[instance_worker_1], timeout=120, ssh_interface='private_ips')
  203. ###########################################################################
  204. #
  205. # assign app-worker floating ip
  206. #
  207. ###########################################################################
  208. print('Checking for unused Floating IP...')
  209. unused_floating_ip = None
  210. for floating_ip in conn.ex_list_floating_ips():
  211. if not floating_ip.node_id:
  212. unused_floating_ip = floating_ip
  213. break
  214. if not unused_floating_ip:
  215. pool = conn.ex_list_floating_ip_pools()[0]
  216. print(('Allocating new Floating IP from pool: {}'.format(pool)))
  217. unused_floating_ip = pool.create_floating_ip()
  218. conn.ex_attach_floating_ip_to_node(instance_worker_1, unused_floating_ip)
  219. print(('The worker will be available for SSH at %s' % unused_floating_ip.ip_address))
  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()