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.

276 lines
11 KiB

  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: nova, glance, neutron
  7. # resources: 2 instances, 2 floating ips (1 keypair, 2 security groups)
  8. # Please use 1-29 for X in the following variable to specify your group number. (will be used for the username,
  9. # project etc., as coordinated in the lab sessions)
  10. group_number = 30
  11. # web service endpoint of the private cloud infrastructure
  12. auth_url = 'https://private-cloud.informatik.hs-fulda.de:5000'
  13. # your username in OpenStack
  14. auth_username = 'CloudComp' + str(group_number)
  15. # your project in OpenStack
  16. project_name = 'CloudComp' + str(group_number)
  17. # A network in the project the started instance will be attached to
  18. project_network = 'CloudComp' + str(group_number) + '-net'
  19. # The image to look for and use for the started instance
  20. ubuntu_image_name = "Ubuntu 18.04 - Bionic Beaver - 64-bit - Cloud Based Image"
  21. # TODO: Ubuntu >18.04 would require major updates to faafo example again/better option: complete rewrite of example?
  22. # The public key to be used for SSH connection, please make sure, that you have the corresponding private key
  23. #
  24. # id_rsa.pub should look like this (standard sshd pubkey format):
  25. # ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAw+J...F3w2mleybgT1w== user@HOSTNAME
  26. keypair_name = 'srieger-pub'
  27. pub_key_file = '~/.ssh/id_rsa.pub'
  28. flavor_name = 'm1.small'
  29. # default region
  30. region_name = 'RegionOne'
  31. # domain to use, "default" for local accounts, "hsfulda" for RZ LDAP, e.g., using fdaiXXXX as auth_username
  32. # domain_name = "default"
  33. def main():
  34. ###########################################################################
  35. #
  36. # get credentials
  37. #
  38. ###########################################################################
  39. # if "OS_PASSWORD" in os.environ:
  40. # auth_password = os.environ["OS_PASSWORD"]
  41. # else:
  42. # auth_password = getpass.getpass("Enter your OpenStack password:")
  43. auth_password = "demo"
  44. ###########################################################################
  45. #
  46. # create connection
  47. #
  48. ###########################################################################
  49. provider = get_driver(Provider.OPENSTACK)
  50. conn = provider(auth_username,
  51. auth_password,
  52. ex_force_auth_url=auth_url,
  53. ex_force_auth_version='3.x_password',
  54. ex_tenant_name=project_name,
  55. ex_force_service_region=region_name)
  56. # ex_domain_name=domain_name)
  57. ###########################################################################
  58. #
  59. # get image, flavor, network for instance creation
  60. #
  61. ###########################################################################
  62. images = conn.list_images()
  63. image = ''
  64. for img in images:
  65. if img.name == ubuntu_image_name:
  66. image = img
  67. flavors = conn.list_sizes()
  68. flavor = ''
  69. for flav in flavors:
  70. if flav.name == flavor_name:
  71. flavor = conn.ex_get_size(flav.id)
  72. networks = conn.ex_list_networks()
  73. network = ''
  74. for net in networks:
  75. if net.name == project_network:
  76. network = net
  77. ###########################################################################
  78. #
  79. # create keypair dependency
  80. #
  81. ###########################################################################
  82. print('Checking for existing SSH key pair...')
  83. keypair_exists = False
  84. for keypair in conn.list_key_pairs():
  85. if keypair.name == keypair_name:
  86. keypair_exists = True
  87. if keypair_exists:
  88. print('Keypair ' + keypair_name + ' already exists. Skipping import.')
  89. else:
  90. print('adding keypair...')
  91. conn.import_key_pair_from_file(keypair_name, pub_key_file)
  92. for keypair in conn.list_key_pairs():
  93. print(keypair)
  94. ###########################################################################
  95. #
  96. # create security group dependency
  97. #
  98. ###########################################################################
  99. print('Checking for existing worker security group...')
  100. security_group_name = 'worker'
  101. security_group_exists = False
  102. worker_security_group = ''
  103. for security_group in conn.ex_list_security_groups():
  104. if security_group.name == security_group_name:
  105. worker_security_group = security_group
  106. security_group_exists = True
  107. if security_group_exists:
  108. print('Worker Security Group ' + worker_security_group.name + ' already exists. Skipping creation.')
  109. else:
  110. worker_security_group = conn.ex_create_security_group('worker', 'for services that run on a worker node')
  111. conn.ex_create_security_group_rule(worker_security_group, 'TCP', 22, 22)
  112. print('Checking for existing controller security group...')
  113. security_group_name = 'control'
  114. security_group_exists = False
  115. controller_security_group = ''
  116. for security_group in conn.ex_list_security_groups():
  117. if security_group.name == security_group_name:
  118. controller_security_group = security_group
  119. security_group_exists = True
  120. if security_group_exists:
  121. print('Controller Security Group ' + controller_security_group.name + ' already exists. Skipping creation.')
  122. else:
  123. controller_security_group = conn.ex_create_security_group('control', 'for services that run on a control node')
  124. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 22, 22)
  125. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 80, 80)
  126. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 5672, 5672,
  127. source_security_group=worker_security_group)
  128. for security_group in conn.ex_list_security_groups():
  129. print(security_group)
  130. ###########################################################################
  131. #
  132. # create app-controller
  133. #
  134. ###########################################################################
  135. # https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh
  136. # is currently broken, hence the "rabbitctl" lines were added in the example
  137. # below, see also https://bugs.launchpad.net/faafo/+bug/1679710
  138. #
  139. # Thanks to Stefan Friedmann for finding this fix ;)
  140. userdata = '''#!/usr/bin/env bash
  141. curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh | bash -s -- \
  142. -i messaging -i faafo -r api
  143. rabbitmqctl add_user faafo guest
  144. rabbitmqctl set_user_tags faafo administrator
  145. rabbitmqctl set_permissions -p / faafo ".*" ".*" ".*"
  146. '''
  147. print('Starting new app-controller instance and wait until it is running...')
  148. instance_controller_1 = conn.create_node(name='app-controller',
  149. image=image,
  150. size=flavor,
  151. networks=[network],
  152. ex_keyname=keypair_name,
  153. ex_userdata=userdata,
  154. ex_security_groups=[controller_security_group])
  155. conn.wait_until_running(nodes=[instance_controller_1], timeout=120, ssh_interface='private_ips')
  156. ###########################################################################
  157. #
  158. # assign app-controller floating ip
  159. #
  160. ###########################################################################
  161. print('Checking for unused Floating IP...')
  162. unused_floating_ip = None
  163. for floating_ip in conn.ex_list_floating_ips():
  164. if not floating_ip.node_id:
  165. unused_floating_ip = floating_ip
  166. break
  167. if not unused_floating_ip:
  168. pool = conn.ex_list_floating_ip_pools()[0]
  169. print('Allocating new Floating IP from pool: {}'.format(pool))
  170. unused_floating_ip = pool.create_floating_ip()
  171. conn.ex_attach_floating_ip_to_node(instance_controller_1, unused_floating_ip)
  172. print('Controller Application will be deployed to http://%s' % unused_floating_ip.ip_address)
  173. ###########################################################################
  174. #
  175. # getting id and ip address of app-controller instance
  176. #
  177. ###########################################################################
  178. # instance should not have a public ip? floating ips are assigned later
  179. instance_controller_1 = conn.ex_get_node_details(instance_controller_1.id)
  180. if instance_controller_1.public_ips:
  181. ip_controller = instance_controller_1.public_ips[0]
  182. else:
  183. ip_controller = instance_controller_1.private_ips[0]
  184. ###########################################################################
  185. #
  186. # create app-worker-1
  187. #
  188. ###########################################################################
  189. userdata = '''#!/usr/bin/env bash
  190. curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh | bash -s -- \
  191. -i faafo -r worker -e 'http://%(ip_controller)s' -m 'amqp://faafo:guest@%(ip_controller)s:5672/'
  192. ''' % {'ip_controller': ip_controller}
  193. print('Starting new app-worker-1 instance and wait until it is running...')
  194. instance_worker_1 = conn.create_node(name='app-worker-1',
  195. image=image,
  196. size=flavor,
  197. networks=[network],
  198. ex_keyname=keypair_name,
  199. ex_userdata=userdata,
  200. ex_security_groups=[worker_security_group])
  201. conn.wait_until_running(nodes=[instance_worker_1], timeout=120, ssh_interface='private_ips')
  202. ###########################################################################
  203. #
  204. # assign app-worker floating ip
  205. #
  206. ###########################################################################
  207. print('Checking for unused Floating IP...')
  208. unused_floating_ip = None
  209. for floating_ip in conn.ex_list_floating_ips():
  210. if not floating_ip.node_id:
  211. unused_floating_ip = floating_ip
  212. break
  213. if not unused_floating_ip:
  214. pool = conn.ex_list_floating_ip_pools()[0]
  215. print('Allocating new Floating IP from pool: {}'.format(pool))
  216. unused_floating_ip = pool.create_floating_ip()
  217. conn.ex_attach_floating_ip_to_node(instance_worker_1, unused_floating_ip)
  218. print('The worker will be available for SSH at %s' % unused_floating_ip.ip_address)
  219. print('You can use ssh to login to the controller using your private key. After login, you can list available '
  220. 'fractals using "faafo list". To request the generation of new fractals, you can use "faafo create". '
  221. 'You can also see other options to use the faafo example cloud service using "faafo -h".')
  222. if __name__ == '__main__':
  223. main()