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.

250 lines
9.4 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. # 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 = "CloudCompGrpX-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. auth_password = "demo"
  35. ###########################################################################
  36. #
  37. # create connection
  38. #
  39. ###########################################################################
  40. provider = get_driver(Provider.OPENSTACK)
  41. conn = provider(auth_username,
  42. auth_password,
  43. ex_force_auth_url=auth_url,
  44. ex_force_auth_version='3.x_password',
  45. ex_tenant_name=project_name,
  46. ex_force_service_region=region_name,
  47. ex_domain_name=domain_name)
  48. ###########################################################################
  49. #
  50. # get image, flavor, network for instance creation
  51. #
  52. ###########################################################################
  53. images = conn.list_images()
  54. image = ''
  55. for img in images:
  56. if img.name == ubuntu_image_name:
  57. image = img
  58. flavors = conn.list_sizes()
  59. flavor = ''
  60. for flav in flavors:
  61. if flav.name == flavor_name:
  62. flavor = conn.ex_get_size(flav.id)
  63. networks = conn.ex_list_networks()
  64. network = ''
  65. for net in networks:
  66. if net.name == network_name:
  67. network = net
  68. ###########################################################################
  69. #
  70. # create keypair dependency
  71. #
  72. ###########################################################################
  73. print('Checking for existing SSH key pair...')
  74. keypair_exists = False
  75. for keypair in conn.list_key_pairs():
  76. if keypair.name == keypair_name:
  77. keypair_exists = True
  78. if keypair_exists:
  79. print('Keypair ' + keypair_name + ' already exists. Skipping import.')
  80. else:
  81. print('adding keypair...')
  82. conn.import_key_pair_from_file(keypair_name, pub_key_file)
  83. for keypair in conn.list_key_pairs():
  84. print(keypair)
  85. ###########################################################################
  86. #
  87. # create security group dependency
  88. #
  89. ###########################################################################
  90. print('Checking for existing worker security group...')
  91. security_group_name = 'worker'
  92. security_group_exists = False
  93. worker_security_group = ''
  94. for security_group in conn.ex_list_security_groups():
  95. if security_group.name == security_group_name:
  96. worker_security_group = security_group
  97. security_group_exists = True
  98. if security_group_exists:
  99. print('Worker Security Group ' + worker_security_group.name + ' already exists. Skipping creation.')
  100. else:
  101. worker_security_group = conn.ex_create_security_group('worker', 'for services that run on a worker node')
  102. conn.ex_create_security_group_rule(worker_security_group, 'TCP', 22, 22)
  103. print('Checking for existing controller security group...')
  104. security_group_name = 'control'
  105. security_group_exists = False
  106. controller_security_group = ''
  107. for security_group in conn.ex_list_security_groups():
  108. if security_group.name == security_group_name:
  109. controller_security_group = security_group
  110. security_group_exists = True
  111. if security_group_exists:
  112. print('Controller Security Group ' + controller_security_group.name + ' already exists. Skipping creation.')
  113. else:
  114. controller_security_group = conn.ex_create_security_group('control', 'for services that run on a control node')
  115. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 22, 22)
  116. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 80, 80)
  117. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 5672, 5672,
  118. source_security_group=worker_security_group)
  119. for security_group in conn.ex_list_security_groups():
  120. print(security_group)
  121. ###########################################################################
  122. #
  123. # create app-controller
  124. #
  125. ###########################################################################
  126. userdata = '''#!/usr/bin/env bash
  127. curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \
  128. -i messaging -i faafo -r api
  129. '''
  130. print('Starting new app-controller instance and wait until it is running...')
  131. instance_controller_1 = conn.create_node(name='app-controller',
  132. image=image,
  133. size=flavor,
  134. networks=[network],
  135. ex_keyname=keypair_name,
  136. ex_userdata=userdata,
  137. ex_security_groups=[controller_security_group])
  138. conn.wait_until_running(nodes=[instance_controller_1], timeout=120, ssh_interface='private_ips')
  139. ###########################################################################
  140. #
  141. # assign app-controller floating ip
  142. #
  143. ###########################################################################
  144. print('Checking for unused Floating IP...')
  145. unused_floating_ip = None
  146. for floating_ip in conn.ex_list_floating_ips():
  147. if not floating_ip.node_id:
  148. unused_floating_ip = floating_ip
  149. break
  150. if not unused_floating_ip:
  151. pool = conn.ex_list_floating_ip_pools()[0]
  152. print('Allocating new Floating IP from pool: {}'.format(pool))
  153. unused_floating_ip = pool.create_floating_ip()
  154. conn.ex_attach_floating_ip_to_node(instance_controller_1, unused_floating_ip)
  155. print('Controller Application will be deployed to http://%s' % unused_floating_ip.ip_address)
  156. ###########################################################################
  157. #
  158. # getting id and ip address of app-controller instance
  159. #
  160. ###########################################################################
  161. # instance should not have a public ip? floating ips are assigned later
  162. instance_controller_1 = conn.ex_get_node_details(instance_controller_1.id)
  163. if instance_controller_1.public_ips:
  164. ip_controller = instance_controller_1.public_ips[0]
  165. else:
  166. ip_controller = instance_controller_1.private_ips[0]
  167. ###########################################################################
  168. #
  169. # create app-worker-1
  170. #
  171. ###########################################################################
  172. userdata = '''#!/usr/bin/env bash
  173. curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \
  174. -i faafo -r worker -e 'http://%(ip_controller)s' -m 'amqp://guest:guest@%(ip_controller)s:5672/'
  175. ''' % {'ip_controller': ip_controller}
  176. print('Starting new app-worker-1 instance and wait until it is running...')
  177. instance_worker_1 = conn.create_node(name='app-worker-1',
  178. image=image,
  179. size=flavor,
  180. ex_keyname=keypair_name,
  181. ex_userdata=userdata,
  182. ex_security_groups=[worker_security_group])
  183. conn.wait_until_running(nodes=[instance_worker_1], timeout=120, ssh_interface='private_ips')
  184. ###########################################################################
  185. #
  186. # assign app-worker floating ip
  187. #
  188. ###########################################################################
  189. print('Checking for unused Floating IP...')
  190. unused_floating_ip = None
  191. for floating_ip in conn.ex_list_floating_ips():
  192. if not floating_ip.node_id:
  193. unused_floating_ip = floating_ip
  194. break
  195. if not unused_floating_ip:
  196. pool = conn.ex_list_floating_ip_pools()[0]
  197. print('Allocating new Floating IP from pool: {}'.format(pool))
  198. unused_floating_ip = pool.create_floating_ip()
  199. conn.ex_attach_floating_ip_to_node(instance_worker_1, unused_floating_ip)
  200. print('The worker will be available for SSH at %s' % unused_floating_ip.ip_address)
  201. if __name__ == '__main__':
  202. main()