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.

230 lines
8.8 KiB

  1. # import getpass
  2. # import os
  3. from libcloud.compute.providers import get_driver
  4. from libcloud.compute.types import Provider
  5. # Please use 1-29 for X in the following variable to specify your group number. (will be used for the username,
  6. # project etc., as coordinated in the lab sessions)
  7. group_number = 30
  8. # web service endpoint of the private cloud infrastructure
  9. auth_url = 'https://private-cloud.informatik.hs-fulda.de:5000'
  10. # your username in OpenStack
  11. auth_username = 'CloudComp' + str(group_number)
  12. # your project in OpenStack
  13. project_name = 'CloudComp' + str(group_number)
  14. # A network in the project the started instance will be attached to
  15. project_network = 'CloudComp' + str(group_number) + '-net'
  16. # The image to look for and use for the started instance
  17. ubuntu_image_name = "Ubuntu 18.04 - Bionic Beaver - 64-bit - Cloud Based Image"
  18. # TODO: Ubuntu >18.04 would require major updates to faafo example again/better option: complete rewrite of example?
  19. # The public key to be used for SSH connection, please make sure, that you have the corresponding private key
  20. #
  21. # id_rsa.pub should look like this (standard sshd pubkey format):
  22. # ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAw+J...F3w2mleybgT1w== user@HOSTNAME
  23. keypair_name = 'CloudComp30-keypair'
  24. pub_key_file = '~/.ssh/id_rsa.pub'
  25. flavor_name = 'm1.small'
  26. # default region
  27. region_name = 'RegionOne'
  28. # domain to use, "default" for local accounts, "hsfulda" for RZ LDAP, e.g., using fdaiXXXX as auth_username
  29. # domain_name = "default"
  30. def main():
  31. ###########################################################################
  32. #
  33. # get credentials
  34. #
  35. ###########################################################################
  36. # if "OS_PASSWORD" in os.environ:
  37. # auth_password = os.environ["OS_PASSWORD"]
  38. # else:
  39. # auth_password = getpass.getpass("Enter your OpenStack password:")
  40. auth_password = "demo"
  41. ###########################################################################
  42. #
  43. # create connection
  44. #
  45. ###########################################################################
  46. provider = get_driver(Provider.OPENSTACK)
  47. conn = provider(auth_username,
  48. auth_password,
  49. ex_force_auth_url=auth_url,
  50. ex_force_auth_version='3.x_password',
  51. ex_tenant_name=project_name,
  52. ex_force_service_region=region_name)
  53. # ex_domain_name=domain_name)
  54. ###########################################################################
  55. #
  56. # get image, flavor, network for instance creation
  57. #
  58. ###########################################################################
  59. images = conn.list_images()
  60. image = ''
  61. for img in images:
  62. if img.name == ubuntu_image_name:
  63. image = img
  64. flavors = conn.list_sizes()
  65. flavor = ''
  66. for flav in flavors:
  67. if flav.name == flavor_name:
  68. flavor = conn.ex_get_size(flav.id)
  69. networks = conn.ex_list_networks()
  70. network = ''
  71. for net in networks:
  72. if net.name == project_network:
  73. network = net
  74. ###########################################################################
  75. #
  76. # create keypair dependency
  77. #
  78. ###########################################################################
  79. print('Checking for existing SSH key pair...')
  80. keypair_exists = False
  81. for keypair in conn.list_key_pairs():
  82. if keypair.name == keypair_name:
  83. keypair_exists = True
  84. if keypair_exists:
  85. print('Keypair ' + keypair_name + ' already exists. Skipping import.')
  86. else:
  87. print('adding keypair...')
  88. conn.import_key_pair_from_file(keypair_name, pub_key_file)
  89. for keypair in conn.list_key_pairs():
  90. print(keypair)
  91. ###########################################################################
  92. #
  93. # create security group dependency
  94. #
  95. ###########################################################################
  96. print('Checking for existing security group...')
  97. security_group_name = 'all-in-one'
  98. security_group_exists = False
  99. all_in_one_security_group = ''
  100. for security_group in conn.ex_list_security_groups():
  101. if security_group.name == security_group_name:
  102. all_in_one_security_group = security_group
  103. security_group_exists = True
  104. if security_group_exists:
  105. print('Security Group ' + all_in_one_security_group.name + ' already exists. Skipping creation.')
  106. else:
  107. all_in_one_security_group = conn.ex_create_security_group(security_group_name,
  108. 'network access for all-in-one application.')
  109. conn.ex_create_security_group_rule(all_in_one_security_group, 'TCP', 80, 80)
  110. conn.ex_create_security_group_rule(all_in_one_security_group, 'TCP', 22, 22)
  111. for security_group in conn.ex_list_security_groups():
  112. print(security_group)
  113. ###########################################################################
  114. #
  115. # create all-in-one instance
  116. #
  117. ###########################################################################
  118. userdata = '''#!/usr/bin/env bash
  119. curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh | bash -s -- \
  120. -i faafo -i messaging -r api -r worker -r demo
  121. '''
  122. print('Checking for existing instance...')
  123. instance_name = 'all-in-one'
  124. instance_exists = False
  125. testing_instance = ''
  126. for instance in conn.list_nodes():
  127. if instance.name == instance_name:
  128. testing_instance = instance
  129. instance_exists = True
  130. if instance_exists:
  131. print('Instance ' + testing_instance.name + ' already exists. Skipping creation.')
  132. exit()
  133. else:
  134. print('Starting new all-in-one instance and wait until it is running...')
  135. testing_instance = conn.create_node(name=instance_name,
  136. image=image,
  137. size=flavor,
  138. networks=[network],
  139. ex_keyname=keypair_name,
  140. ex_userdata=userdata,
  141. ex_security_groups=[all_in_one_security_group])
  142. conn.wait_until_running(nodes=[testing_instance], timeout=120, ssh_interface='private_ips')
  143. ###########################################################################
  144. #
  145. # assign all-in-one instance floating ip
  146. #
  147. ###########################################################################
  148. private_ip = None
  149. if len(testing_instance.private_ips):
  150. private_ip = testing_instance.private_ips[0]
  151. print('Private IP found: {}'.format(private_ip))
  152. public_ip = None
  153. if len(testing_instance.public_ips):
  154. public_ip = testing_instance.public_ips[0]
  155. print('Public IP found: {}'.format(public_ip))
  156. print('Checking for unused Floating IP...')
  157. unused_floating_ip = None
  158. for floating_ip in conn.ex_list_floating_ips():
  159. if not floating_ip.node_id:
  160. unused_floating_ip = floating_ip
  161. break
  162. if not unused_floating_ip and len(conn.ex_list_floating_ip_pools()):
  163. pool = conn.ex_list_floating_ip_pools()[0]
  164. print('Allocating new Floating IP from pool: {}'.format(pool))
  165. unused_floating_ip = pool.create_floating_ip()
  166. if public_ip:
  167. print('Instance ' + testing_instance.name + ' already has a public ip. Skipping attachment.')
  168. elif unused_floating_ip:
  169. conn.ex_attach_floating_ip_to_node(testing_instance, unused_floating_ip)
  170. actual_ip_address = None
  171. if public_ip:
  172. actual_ip_address = public_ip
  173. elif unused_floating_ip:
  174. actual_ip_address = unused_floating_ip.ip_address
  175. elif private_ip:
  176. actual_ip_address = private_ip
  177. print('\n')
  178. print('The Fractals app will be deployed to http://{}\n'.format(actual_ip_address))
  179. print('You can use ssh to login to the instance using your private key. Default user name for official Ubuntu\n'
  180. 'Cloud Images is: ubuntu, so you can use, e.g.: "ssh -i ~/.ssh/id_rsa ubuntu@<floating-ip>" if your private\n'
  181. 'key is in the default location.\n\n'
  182. 'After login, you can list or "ssh ubuntu@<floating-ip>" available fractals using "faafo list". To request\n'
  183. 'the generation of new fractals, you can use "faafo create".\n\n'
  184. 'You can also see other options to use the faafo example cloud service using "faafo -h".\n\n'
  185. 'If you cannot start faafo command and/or do not see the webpage, you can check the Instance Console Log of\n'
  186. 'the instance, e.g., in OpenStack web interface.')
  187. if __name__ == '__main__':
  188. main()