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
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 20.04 - Focal Fossa - 64-bit - Cloud Based Image"
  21. ubuntu_image_name = "Ubuntu 22.04 - Jammy Jellyfish - 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. #hsfd_faafo_cloud_init_script = 'https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh'
  137. # testing / faafo dev branch:
  138. 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'
  139. userdata = '''#!/usr/bin/env bash
  140. curl -L -s ''' + hsfd_faafo_cloud_init_script + ''' | 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 ''' + hsfd_faafo_cloud_init_script + ''' | 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()