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.

261 lines
9.9 KiB

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