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.

249 lines
9.4 KiB

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