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.

281 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. # 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 = X
  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. ubuntu_image_name = "Ubuntu 20.04 - Focal Fossa - 64-bit - Cloud Based Image"
  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 = 'CloudComp30-keypair'
  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. #hsfd_faafo_cloud_init_script = 'https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh'
  142. # testing / faafo dev branch:
  143. hsfd_faafo_cloud_init_script = 'https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/branch/dev_faafo/faafo/contrib/install.sh'
  144. userdata = '''#!/usr/bin/env bash
  145. curl -L -s ''' + hsfd_faafo_cloud_init_script + ''' | bash -s -- \
  146. -i messaging -i faafo -r api
  147. rabbitmqctl add_user faafo guest
  148. rabbitmqctl set_user_tags faafo administrator
  149. rabbitmqctl set_permissions -p / faafo ".*" ".*" ".*"
  150. '''
  151. print('Starting new app-controller instance and wait until it is running...')
  152. instance_controller_1 = conn.create_node(name='app-controller',
  153. image=image,
  154. size=flavor,
  155. networks=[network],
  156. ex_keyname=keypair_name,
  157. ex_userdata=userdata,
  158. ex_security_groups=[controller_security_group])
  159. conn.wait_until_running(nodes=[instance_controller_1], timeout=120, ssh_interface='private_ips')
  160. ###########################################################################
  161. #
  162. # assign app-controller floating ip
  163. #
  164. ###########################################################################
  165. print('Checking for unused Floating IP...')
  166. unused_floating_ip = None
  167. for floating_ip in conn.ex_list_floating_ips():
  168. if not floating_ip.node_id:
  169. unused_floating_ip = floating_ip
  170. break
  171. if not unused_floating_ip:
  172. pool = conn.ex_list_floating_ip_pools()[0]
  173. print(('Allocating new Floating IP from pool: {}'.format(pool)))
  174. unused_floating_ip = pool.create_floating_ip()
  175. conn.ex_attach_floating_ip_to_node(instance_controller_1, unused_floating_ip)
  176. print(('Controller Application will be deployed to http://%s' % unused_floating_ip.ip_address))
  177. ###########################################################################
  178. #
  179. # getting id and ip address of app-controller instance
  180. #
  181. ###########################################################################
  182. # instance should not have a public ip? floating ips are assigned later
  183. instance_controller_1 = conn.ex_get_node_details(instance_controller_1.id)
  184. if instance_controller_1.public_ips:
  185. ip_controller = instance_controller_1.public_ips[0]
  186. else:
  187. ip_controller = instance_controller_1.private_ips[0]
  188. ###########################################################################
  189. #
  190. # create app-worker-1
  191. #
  192. ###########################################################################
  193. userdata = '''#!/usr/bin/env bash
  194. curl -L -s ''' + hsfd_faafo_cloud_init_script + ''' | bash -s -- \
  195. -i faafo -r worker -e 'http://%(ip_controller)s' -m 'amqp://faafo:guest@%(ip_controller)s:5672/'
  196. ''' % {'ip_controller': ip_controller}
  197. print('Starting new app-worker-1 instance and wait until it is running...')
  198. instance_worker_1 = conn.create_node(name='app-worker-1',
  199. image=image,
  200. size=flavor,
  201. networks=[network],
  202. ex_keyname=keypair_name,
  203. ex_userdata=userdata,
  204. ex_security_groups=[worker_security_group])
  205. conn.wait_until_running(nodes=[instance_worker_1], timeout=120, ssh_interface='private_ips')
  206. ###########################################################################
  207. #
  208. # assign app-worker floating ip
  209. #
  210. ###########################################################################
  211. print('Checking for unused Floating IP...')
  212. unused_floating_ip = None
  213. for floating_ip in conn.ex_list_floating_ips():
  214. if not floating_ip.node_id:
  215. unused_floating_ip = floating_ip
  216. break
  217. if not unused_floating_ip:
  218. pool = conn.ex_list_floating_ip_pools()[0]
  219. print(('Allocating new Floating IP from pool: {}'.format(pool)))
  220. unused_floating_ip = pool.create_floating_ip()
  221. conn.ex_attach_floating_ip_to_node(instance_worker_1, unused_floating_ip)
  222. print(('The worker will be available for SSH at %s' % unused_floating_ip.ip_address))
  223. print('You can use ssh to login to the controller using your private key. After login, you can list available '
  224. 'fractals using "faafo list". To request the generation of new fractals, you can use "faafo create". '
  225. 'You can also see other options to use the faafo example cloud service using "faafo -h".')
  226. if __name__ == '__main__':
  227. main()