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.

253 lines
9.4 KiB

7 years ago
  1. import getpass
  2. import os
  3. import libcloud.security
  4. from libcloud.compute.providers import get_driver
  5. from libcloud.compute.types import Provider
  6. # reqs:
  7. # services: nova, glance, neutron
  8. # resources: 2 instances, 2 floating ips (1 keypair, 2 security groups)
  9. # HS-Fulda Private Cloud
  10. auth_url = 'https://192.168.72.40:5000'
  11. region_name = 'RegionOne'
  12. domain_name = "hsfulda"
  13. ubuntu_image_name = "Ubuntu 14.04 - Trusty Tahr - 64-bit - Cloud Based Image"
  14. flavor_name = 'm1.small'
  15. network_name = "ai-netlab-pro-net"
  16. keypair_name = 'srieger-pub'
  17. pub_key_file = '~/.ssh/id_rsa.pub'
  18. def main():
  19. ###########################################################################
  20. #
  21. # get credentials
  22. #
  23. ###########################################################################
  24. if "OS_PROJECT_NAME" in os.environ:
  25. project_name = os.environ["OS_PROJECT_NAME"]
  26. else:
  27. project_name = input("Enter your OpenStack project:")
  28. if "OS_USERNAME" in os.environ:
  29. auth_username = os.environ["OS_USERNAME"]
  30. else:
  31. auth_username = input("Enter your OpenStack username:")
  32. if "OS_PASSWORD" in os.environ:
  33. auth_password = os.environ["OS_PASSWORD"]
  34. else:
  35. auth_password = getpass.getpass("Enter your OpenStack password:")
  36. ###########################################################################
  37. #
  38. # create connection
  39. #
  40. ###########################################################################
  41. libcloud.security.VERIFY_SSL_CERT = False
  42. provider = get_driver(Provider.OPENSTACK)
  43. conn = provider(auth_username,
  44. auth_password,
  45. ex_force_auth_url=auth_url,
  46. ex_force_auth_version='3.x_password',
  47. ex_tenant_name=project_name,
  48. ex_force_service_region=region_name,
  49. ex_domain_name=domain_name)
  50. ###########################################################################
  51. #
  52. # get image, flavor, network for instance creation
  53. #
  54. ###########################################################################
  55. images = conn.list_images()
  56. image = ''
  57. for img in images:
  58. if img.name == ubuntu_image_name:
  59. image = img
  60. flavors = conn.list_sizes()
  61. flavor = ''
  62. for flav in flavors:
  63. if flav.name == flavor_name:
  64. flavor = conn.ex_get_size(flav.id)
  65. networks = conn.ex_list_networks()
  66. network = ''
  67. for net in networks:
  68. if net.name == network_name:
  69. network = net
  70. ###########################################################################
  71. #
  72. # create keypair dependency
  73. #
  74. ###########################################################################
  75. print('Checking for existing SSH key pair...')
  76. keypair_exists = False
  77. for keypair in conn.list_key_pairs():
  78. if keypair.name == keypair_name:
  79. keypair_exists = True
  80. if keypair_exists:
  81. print('Keypair ' + keypair_name + ' already exists. Skipping import.')
  82. else:
  83. print('adding keypair...')
  84. conn.import_key_pair_from_file(keypair_name, pub_key_file)
  85. for keypair in conn.list_key_pairs():
  86. print(keypair)
  87. ###########################################################################
  88. #
  89. # create security group dependency
  90. #
  91. ###########################################################################
  92. print('Checking for existing worker security group...')
  93. security_group_name = 'worker'
  94. security_group_exists = False
  95. worker_security_group = ''
  96. for security_group in conn.ex_list_security_groups():
  97. if security_group.name == security_group_name:
  98. worker_security_group = security_group
  99. security_group_exists = True
  100. if security_group_exists:
  101. print('Worker Security Group ' + worker_security_group.name + ' already exists. Skipping creation.')
  102. else:
  103. worker_security_group = conn.ex_create_security_group('worker', 'for services that run on a worker node')
  104. conn.ex_create_security_group_rule(worker_security_group, 'TCP', 22, 22)
  105. print('Checking for existing controller security group...')
  106. security_group_name = 'control'
  107. security_group_exists = False
  108. controller_security_group = ''
  109. for security_group in conn.ex_list_security_groups():
  110. if security_group.name == security_group_name:
  111. controller_security_group = security_group
  112. security_group_exists = True
  113. if security_group_exists:
  114. print('Controller Security Group ' + controller_security_group.name + ' already exists. Skipping creation.')
  115. else:
  116. controller_security_group = conn.ex_create_security_group('control', 'for services that run on a control node')
  117. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 22, 22)
  118. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 80, 80)
  119. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 5672, 5672,
  120. source_security_group=worker_security_group)
  121. for security_group in conn.ex_list_security_groups():
  122. print(security_group)
  123. ###########################################################################
  124. #
  125. # create app-controller
  126. #
  127. ###########################################################################
  128. userdata = '''#!/usr/bin/env bash
  129. curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \
  130. -i messaging -i faafo -r api
  131. '''
  132. print('Starting new app-controller instance and wait until it is running...')
  133. instance_controller_1 = conn.create_node(name='app-controller',
  134. image=image,
  135. size=flavor,
  136. networks=[network],
  137. ex_keyname=keypair_name,
  138. ex_userdata=userdata,
  139. ex_security_groups=[controller_security_group])
  140. conn.wait_until_running(nodes=[instance_controller_1], timeout=120, ssh_interface='private_ips')
  141. ###########################################################################
  142. #
  143. # assign app-controller floating ip
  144. #
  145. ###########################################################################
  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:
  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. conn.ex_attach_floating_ip_to_node(instance_controller_1, unused_floating_ip)
  157. print('Controller Application will be deployed to http://%s' % unused_floating_ip.ip_address)
  158. ###########################################################################
  159. #
  160. # getting id and ip address of app-controller instance
  161. #
  162. ###########################################################################
  163. # instance should not have a public ip? floating ips are assigned later
  164. instance_controller_1 = conn.ex_get_node_details(instance_controller_1.id)
  165. if instance_controller_1.public_ips:
  166. ip_controller = instance_controller_1.public_ips[0]
  167. else:
  168. ip_controller = instance_controller_1.private_ips[0]
  169. ###########################################################################
  170. #
  171. # create app-worker-1
  172. #
  173. ###########################################################################
  174. userdata = '''#!/usr/bin/env bash
  175. curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \
  176. -i faafo -r worker -e 'http://%(ip_controller)s' -m 'amqp://guest:guest@%(ip_controller)s:5672/'
  177. ''' % {'ip_controller': ip_controller}
  178. print('Starting new app-worker-1 instance and wait until it is running...')
  179. instance_worker_1 = conn.create_node(name='app-worker-1',
  180. image=image,
  181. size=flavor,
  182. ex_keyname=keypair_name,
  183. ex_userdata=userdata,
  184. ex_security_groups=[worker_security_group])
  185. conn.wait_until_running(nodes=[instance_worker_1], timeout=120, ssh_interface='private_ips')
  186. ###########################################################################
  187. #
  188. # assign app-worker floating ip
  189. #
  190. ###########################################################################
  191. print('Checking for unused Floating IP...')
  192. unused_floating_ip = None
  193. for floating_ip in conn.ex_list_floating_ips():
  194. if not floating_ip.node_id:
  195. unused_floating_ip = floating_ip
  196. break
  197. if not unused_floating_ip:
  198. pool = conn.ex_list_floating_ip_pools()[0]
  199. print('Allocating new Floating IP from pool: {}'.format(pool))
  200. unused_floating_ip = pool.create_floating_ip()
  201. conn.ex_attach_floating_ip_to_node(instance_worker_1, unused_floating_ip)
  202. print('The worker will be available for SSH at %s' % unused_floating_ip.ip_address)
  203. if __name__ == '__main__':
  204. main()