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.

209 lines
7.2 KiB

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