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.

207 lines
7.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 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-25 for X in username, project etc., as coordinated in the lab sessions
  6. # web service endpoint of the private cloud infrastructure
  7. auth_url = 'https://private-cloud2.informatik.hs-fulda.de:5000'
  8. # your username in OpenStack
  9. auth_username = 'CloudCompX'
  10. # your project in OpenStack
  11. project_name = 'CloudCompGrpX'
  12. # default region
  13. region_name = 'RegionOne'
  14. # domain to use, "default" for local accounts, "hsfulda" for LDAP of DVZ, e.g., using fdaiXXXX as auth_username
  15. domain_name = "default"
  16. ubuntu_image_name = "Ubuntu 14.04 - Trusty Tahr - 64-bit - Cloud Based Image"
  17. flavor_name = 'm1.small'
  18. network_name = "CloudCompGrpX-net"
  19. keypair_name = 'srieger-pub'
  20. pub_key_file = '~/.ssh/id_rsa.pub'
  21. def main():
  22. ###########################################################################
  23. #
  24. # get credentials
  25. #
  26. ###########################################################################
  27. # if "OS_PASSWORD" in os.environ:
  28. # auth_password = os.environ["OS_PASSWORD"]
  29. # else:
  30. # auth_password = getpass.getpass("Enter your OpenStack password:")
  31. auth_password = "demo"
  32. ###########################################################################
  33. #
  34. # create connection
  35. #
  36. ###########################################################################
  37. provider = get_driver(Provider.OPENSTACK)
  38. conn = provider(auth_username,
  39. auth_password,
  40. ex_force_auth_url=auth_url,
  41. ex_force_auth_version='3.x_password',
  42. ex_tenant_name=project_name,
  43. ex_force_service_region=region_name,
  44. ex_domain_name=domain_name)
  45. ###########################################################################
  46. #
  47. # get image, flavor, network for instance creation
  48. #
  49. ###########################################################################
  50. images = conn.list_images()
  51. image = ''
  52. for img in images:
  53. if img.name == ubuntu_image_name:
  54. image = img
  55. flavors = conn.list_sizes()
  56. flavor = ''
  57. for flav in flavors:
  58. if flav.name == flavor_name:
  59. flavor = conn.ex_get_size(flav.id)
  60. networks = conn.ex_list_networks()
  61. network = ''
  62. for net in networks:
  63. if net.name == network_name:
  64. network = net
  65. ###########################################################################
  66. #
  67. # create keypair dependency
  68. #
  69. ###########################################################################
  70. print('Checking for existing SSH key pair...')
  71. keypair_exists = False
  72. for keypair in conn.list_key_pairs():
  73. if keypair.name == keypair_name:
  74. keypair_exists = True
  75. if keypair_exists:
  76. print('Keypair ' + keypair_name + ' already exists. Skipping import.')
  77. else:
  78. print('adding keypair...')
  79. conn.import_key_pair_from_file(keypair_name, pub_key_file)
  80. for keypair in conn.list_key_pairs():
  81. print(keypair)
  82. ###########################################################################
  83. #
  84. # create security group dependency
  85. #
  86. ###########################################################################
  87. print('Checking for existing security group...')
  88. security_group_name = 'all-in-one'
  89. security_group_exists = False
  90. all_in_one_security_group = ''
  91. for security_group in conn.ex_list_security_groups():
  92. if security_group.name == security_group_name:
  93. all_in_one_security_group = security_group
  94. security_group_exists = True
  95. if security_group_exists:
  96. print('Security Group ' + all_in_one_security_group.name + ' already exists. Skipping creation.')
  97. else:
  98. all_in_one_security_group = conn.ex_create_security_group(security_group_name,
  99. 'network access for all-in-one application.')
  100. conn.ex_create_security_group_rule(all_in_one_security_group, 'TCP', 80, 80)
  101. conn.ex_create_security_group_rule(all_in_one_security_group, 'TCP', 22, 22)
  102. for security_group in conn.ex_list_security_groups():
  103. print(security_group)
  104. ###########################################################################
  105. #
  106. # create all-in-one instance
  107. #
  108. ###########################################################################
  109. userdata = '''#!/usr/bin/env bash
  110. curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \
  111. -i faafo -i messaging -r api -r worker -r demo
  112. '''
  113. print('Checking for existing instance...')
  114. instance_name = 'all-in-one'
  115. instance_exists = False
  116. testing_instance = ''
  117. for instance in conn.list_nodes():
  118. if instance.name == instance_name:
  119. testing_instance = instance
  120. instance_exists = True
  121. if instance_exists:
  122. print('Instance ' + testing_instance.name + ' already exists. Skipping creation.')
  123. else:
  124. print('Starting new all-in-one instance and wait until it is running...')
  125. testing_instance = conn.create_node(name=instance_name,
  126. image=image,
  127. size=flavor,
  128. networks=[network],
  129. ex_keyname=keypair_name,
  130. ex_userdata=userdata,
  131. ex_security_groups=[all_in_one_security_group])
  132. conn.wait_until_running(nodes=[testing_instance], timeout=120, ssh_interface='private_ips')
  133. ###########################################################################
  134. #
  135. # assign all-in-one instance floating ip
  136. #
  137. ###########################################################################
  138. private_ip = None
  139. if len(testing_instance.private_ips):
  140. private_ip = testing_instance.private_ips[0]
  141. print('Private IP found: {}'.format(private_ip))
  142. public_ip = None
  143. if len(testing_instance.public_ips):
  144. public_ip = testing_instance.public_ips[0]
  145. print('Public IP found: {}'.format(public_ip))
  146. print('Checking for unused Floating IP...')
  147. unused_floating_ip = None
  148. for floating_ip in conn.ex_list_floating_ips():
  149. if not floating_ip.node_id:
  150. unused_floating_ip = floating_ip
  151. break
  152. if not unused_floating_ip and len(conn.ex_list_floating_ip_pools()):
  153. pool = conn.ex_list_floating_ip_pools()[0]
  154. print('Allocating new Floating IP from pool: {}'.format(pool))
  155. unused_floating_ip = pool.create_floating_ip()
  156. if public_ip:
  157. print('Instance ' + testing_instance.name + ' already has a public ip. Skipping attachment.')
  158. elif unused_floating_ip:
  159. conn.ex_attach_floating_ip_to_node(testing_instance, unused_floating_ip)
  160. actual_ip_address = None
  161. if public_ip:
  162. actual_ip_address = public_ip
  163. elif unused_floating_ip:
  164. actual_ip_address = unused_floating_ip.ip_address
  165. elif private_ip:
  166. actual_ip_address = private_ip
  167. print('The Fractals app will be deployed to http://{}'.format(actual_ip_address))
  168. if __name__ == '__main__':
  169. main()