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.

322 lines
14 KiB

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. # import libcloud.security
  4. import time
  5. from libcloud.compute.providers import get_driver
  6. from libcloud.compute.types import Provider
  7. # reqs:
  8. # services: nova, glance, neutron
  9. # resources: 2 instances (m1.small), 2 floating ips (1 keypair, 2 security groups)
  10. # Please use 1-25 for X in username, project etc., as coordinated in the lab sessions
  11. # web service endpoint of the private cloud infrastructure
  12. auth_url = 'https://private-cloud2.informatik.hs-fulda.de:5000'
  13. # your username in OpenStack
  14. auth_username = 'CloudCompX'
  15. # your project in OpenStack
  16. project_name = 'CloudCompGrpX'
  17. # default region
  18. region_name = 'RegionOne'
  19. # domain to use, "default" for local accounts, "hsfulda" for LDAP of DVZ, e.g., using fdaiXXXX as auth_username
  20. domain_name = "default"
  21. ubuntu_image_name = "Ubuntu 14.04 - Trusty Tahr - 64-bit - Cloud Based Image"
  22. flavor_name = 'm1.small'
  23. network_name = "CloudCompGrpX-net"
  24. keypair_name = 'srieger-pub'
  25. pub_key_file = '~/.ssh/id_rsa.pub'
  26. def main():
  27. ###########################################################################
  28. #
  29. # get credentials
  30. #
  31. ###########################################################################
  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. auth_password = "demo"
  37. ###########################################################################
  38. #
  39. # create connection
  40. #
  41. ###########################################################################
  42. # libcloud.security.VERIFY_SSL_CERT = False
  43. provider = get_driver(Provider.OPENSTACK)
  44. conn = provider(auth_username,
  45. auth_password,
  46. ex_force_auth_url=auth_url,
  47. ex_force_auth_version='3.x_password',
  48. ex_tenant_name=project_name,
  49. ex_force_service_region=region_name,
  50. ex_domain_name=domain_name)
  51. ###########################################################################
  52. #
  53. # get image, flavor, network for instance creation
  54. #
  55. ###########################################################################
  56. images = conn.list_images()
  57. image = ''
  58. for img in images:
  59. if img.name == ubuntu_image_name:
  60. image = img
  61. flavors = conn.list_sizes()
  62. flavor = ''
  63. for flav in flavors:
  64. if flav.name == flavor_name:
  65. flavor = conn.ex_get_size(flav.id)
  66. networks = conn.ex_list_networks()
  67. network = ''
  68. for net in networks:
  69. if net.name == network_name:
  70. network = net
  71. ###########################################################################
  72. #
  73. # create keypair dependency
  74. #
  75. ###########################################################################
  76. print('Checking for existing SSH key pair...')
  77. keypair_exists = False
  78. for keypair in conn.list_key_pairs():
  79. if keypair.name == keypair_name:
  80. keypair_exists = True
  81. if keypair_exists:
  82. print('Keypair ' + keypair_name + ' already exists. Skipping import.')
  83. else:
  84. print('adding keypair...')
  85. conn.import_key_pair_from_file(keypair_name, pub_key_file)
  86. for keypair in conn.list_key_pairs():
  87. print(keypair)
  88. ###########################################################################
  89. #
  90. # clean up resources from previous demos
  91. #
  92. ###########################################################################
  93. # destroy running demo instances
  94. for instance in conn.list_nodes():
  95. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-worker-3', 'app-controller',
  96. 'app-services', 'app-api-1', 'app-api-2']:
  97. print('Destroying Instance: %s' % instance.name)
  98. conn.destroy_node(instance)
  99. # wait until all nodes are destroyed to be able to remove depended security groups
  100. nodes_still_running = True
  101. while nodes_still_running:
  102. nodes_still_running = False
  103. time.sleep(3)
  104. instances = conn.list_nodes()
  105. for instance in instances:
  106. # if we see any demo instances still running continue to wait for them to stop
  107. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-controller']:
  108. nodes_still_running = True
  109. print('There are still instances running, waiting for them to be destroyed...')
  110. # delete security groups
  111. for group in conn.ex_list_security_groups():
  112. if group.name in ['control', 'worker', 'api', 'services']:
  113. print('Deleting security group: %s' % group.name)
  114. conn.ex_delete_security_group(group)
  115. ###########################################################################
  116. #
  117. # create security group dependency
  118. #
  119. ###########################################################################
  120. def get_security_group(connection, security_group_name):
  121. """A helper function to check if security group already exists"""
  122. print('Checking for existing ' + security_group_name + ' security group...')
  123. for security_grp in connection.ex_list_security_groups():
  124. if security_grp.name == security_group_name:
  125. print('Security Group ' + security_group_name + ' already exists. Skipping creation.')
  126. return worker_security_group
  127. return False
  128. if not get_security_group(conn, "api"):
  129. api_security_group = conn.ex_create_security_group('api', 'for API services only')
  130. conn.ex_create_security_group_rule(api_security_group, 'TCP', 80, 80)
  131. conn.ex_create_security_group_rule(api_security_group, 'TCP', 22, 22)
  132. else:
  133. api_security_group = get_security_group(conn, "api")
  134. if not get_security_group(conn, "worker"):
  135. worker_security_group = conn.ex_create_security_group('worker', 'for services that run on a worker node')
  136. conn.ex_create_security_group_rule(worker_security_group, 'TCP', 22, 22)
  137. else:
  138. worker_security_group = get_security_group(conn, "worker")
  139. if not get_security_group(conn, "control"):
  140. controller_security_group = conn.ex_create_security_group('control', 'for services that run on a control node')
  141. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 22, 22)
  142. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 80, 80)
  143. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 5672, 5672,
  144. source_security_group=worker_security_group)
  145. if not get_security_group(conn, "services"):
  146. services_security_group = conn.ex_create_security_group('services', 'for DB and AMQP services only')
  147. conn.ex_create_security_group_rule(services_security_group, 'TCP', 22, 22)
  148. conn.ex_create_security_group_rule(services_security_group, 'TCP', 3306, 3306,
  149. source_security_group=api_security_group)
  150. conn.ex_create_security_group_rule(services_security_group, 'TCP', 5672, 5672,
  151. source_security_group=worker_security_group)
  152. conn.ex_create_security_group_rule(services_security_group, 'TCP', 5672, 5672,
  153. source_security_group=api_security_group)
  154. else:
  155. services_security_group = get_security_group(conn, "services")
  156. for security_group in conn.ex_list_security_groups():
  157. print(security_group)
  158. ###########################################################################
  159. #
  160. # get floating ip helper function
  161. #
  162. ###########################################################################
  163. def get_floating_ip(connection):
  164. """A helper function to re-use available Floating IPs"""
  165. unused_floating_ip = None
  166. for float_ip in connection.ex_list_floating_ips():
  167. if not float_ip.node_id:
  168. unused_floating_ip = float_ip
  169. break
  170. if not unused_floating_ip:
  171. pool = connection.ex_list_floating_ip_pools()[0]
  172. unused_floating_ip = pool.create_floating_ip()
  173. return unused_floating_ip
  174. ###########################################################################
  175. #
  176. # create app-services instance (database & messaging)
  177. #
  178. ###########################################################################
  179. userdata = '''#!/usr/bin/env bash
  180. curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \
  181. -i database -i messaging
  182. '''
  183. print('Starting new app-services instance and wait until it is running...')
  184. instance_services = conn.create_node(name='app-services',
  185. image=image,
  186. size=flavor,
  187. networks=[network],
  188. ex_keyname=keypair_name,
  189. ex_userdata=userdata,
  190. ex_security_groups=[services_security_group])
  191. instance_services = conn.wait_until_running(nodes=[instance_services], timeout=120,
  192. ssh_interface='private_ips')[0][0]
  193. services_ip = instance_services.private_ips[0]
  194. ###########################################################################
  195. #
  196. # create app-api instances
  197. #
  198. ###########################################################################
  199. userdata = '''#!/usr/bin/env bash
  200. curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \
  201. -i faafo -r api -m 'amqp://guest:guest@%(services_ip)s:5672/' \
  202. -d 'mysql+pymysql://faafo:password@%(services_ip)s:3306/faafo'
  203. ''' % {'services_ip': services_ip}
  204. print('Starting new app-api-1 instance and wait until it is running...')
  205. instance_api_1 = conn.create_node(name='app-api-1',
  206. image=image,
  207. size=flavor,
  208. networks=[network],
  209. ex_keyname=keypair_name,
  210. ex_userdata=userdata,
  211. ex_security_groups=[api_security_group])
  212. print('Starting new app-api-2 instance and wait until it is running...')
  213. instance_api_2 = conn.create_node(name='app-api-2',
  214. image=image,
  215. size=flavor,
  216. networks=[network],
  217. ex_keyname=keypair_name,
  218. ex_userdata=userdata,
  219. ex_security_groups=[api_security_group])
  220. instance_api_1 = conn.wait_until_running(nodes=[instance_api_1], timeout=120,
  221. ssh_interface='private_ips')[0][0]
  222. api_1_ip = instance_api_1.private_ips[0]
  223. instance_api_2 = conn.wait_until_running(nodes=[instance_api_2], timeout=120,
  224. ssh_interface='private_ips')[0][0]
  225. # api_2_ip = instance_api_2.private_ips[0]
  226. for instance in [instance_api_1, instance_api_2]:
  227. floating_ip = get_floating_ip(conn)
  228. conn.ex_attach_floating_ip_to_node(instance, floating_ip)
  229. print('allocated %(ip)s to %(host)s' % {'ip': floating_ip.ip_address, 'host': instance.name})
  230. ###########################################################################
  231. #
  232. # create worker instances
  233. #
  234. ###########################################################################
  235. userdata_api_1 = '''#!/usr/bin/env bash
  236. curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \
  237. -i faafo -r worker -e 'http://%(api_1_ip)s' -m 'amqp://guest:guest@%(services_ip)s:5672/'
  238. ''' % {'api_1_ip': api_1_ip, 'services_ip': services_ip}
  239. # userdata-api-2 = '''#!/usr/bin/env bash
  240. # curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \
  241. # -i faafo -r worker -e 'http://%(api_2_ip)s' -m 'amqp://guest:guest@%(services_ip)s:5672/'
  242. # ''' % {'api_2_ip': api_2_ip, 'services_ip': services_ip}
  243. print('Starting new app-worker-1 instance and wait until it is running...')
  244. instance_worker_1 = conn.create_node(name='app-worker-1',
  245. image=image, size=flavor,
  246. networks=[network],
  247. ex_keyname=keypair_name,
  248. ex_userdata=userdata_api_1,
  249. ex_security_groups=[worker_security_group])
  250. print('Starting new app-worker-1 instance and wait until it is running...')
  251. instance_worker_2 = conn.create_node(name='app-worker-2',
  252. image=image, size=flavor,
  253. networks=[network],
  254. ex_keyname=keypair_name,
  255. ex_userdata=userdata_api_1,
  256. ex_security_groups=[worker_security_group])
  257. print('Starting new app-worker-1 instance and wait until it is running...')
  258. instance_worker_3 = conn.create_node(name='app-worker-3',
  259. image=image, size=flavor,
  260. networks=[network],
  261. ex_keyname=keypair_name,
  262. ex_userdata=userdata_api_1,
  263. ex_security_groups=[worker_security_group])
  264. print(instance_worker_1)
  265. print(instance_worker_2)
  266. print(instance_worker_3)
  267. if __name__ == '__main__':
  268. main()