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.

222 lines
8.2 KiB

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