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.

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