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.

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