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.

321 lines
14 KiB

6 years ago
6 years ago
6 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 = "ai-netlab-pro-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. ###########################################################################
  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. # clean up resources from previous demos
  90. #
  91. ###########################################################################
  92. # destroy running demo instances
  93. for instance in conn.list_nodes():
  94. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-worker-3', 'app-controller',
  95. 'app-services', 'app-api-1', 'app-api-2']:
  96. print('Destroying Instance: %s' % instance.name)
  97. conn.destroy_node(instance)
  98. # wait until all nodes are destroyed to be able to remove depended security groups
  99. nodes_still_running = True
  100. while nodes_still_running:
  101. nodes_still_running = False
  102. time.sleep(3)
  103. instances = conn.list_nodes()
  104. for instance in instances:
  105. # if we see any demo instances still running continue to wait for them to stop
  106. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-controller']:
  107. nodes_still_running = True
  108. print('There are still instances running, waiting for them to be destroyed...')
  109. # delete security groups
  110. for group in conn.ex_list_security_groups():
  111. if group.name in ['control', 'worker', 'api', 'services']:
  112. print('Deleting security group: %s' % group.name)
  113. conn.ex_delete_security_group(group)
  114. ###########################################################################
  115. #
  116. # create security group dependency
  117. #
  118. ###########################################################################
  119. def get_security_group(connection, security_group_name):
  120. """A helper function to check if security group already exists"""
  121. print('Checking for existing ' + security_group_name + ' security group...')
  122. for security_grp in connection.ex_list_security_groups():
  123. if security_grp.name == security_group_name:
  124. print('Security Group ' + security_group_name + ' already exists. Skipping creation.')
  125. return worker_security_group
  126. return False
  127. if not get_security_group(conn, "api"):
  128. api_security_group = conn.ex_create_security_group('api', 'for API services only')
  129. conn.ex_create_security_group_rule(api_security_group, 'TCP', 80, 80)
  130. conn.ex_create_security_group_rule(api_security_group, 'TCP', 22, 22)
  131. else:
  132. api_security_group = get_security_group(conn, "api")
  133. if not get_security_group(conn, "worker"):
  134. worker_security_group = conn.ex_create_security_group('worker', 'for services that run on a worker node')
  135. conn.ex_create_security_group_rule(worker_security_group, 'TCP', 22, 22)
  136. else:
  137. worker_security_group = get_security_group(conn, "worker")
  138. if not get_security_group(conn, "control"):
  139. controller_security_group = conn.ex_create_security_group('control', 'for services that run on a control node')
  140. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 22, 22)
  141. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 80, 80)
  142. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 5672, 5672,
  143. source_security_group=worker_security_group)
  144. if not get_security_group(conn, "services"):
  145. services_security_group = conn.ex_create_security_group('services', 'for DB and AMQP services only')
  146. conn.ex_create_security_group_rule(services_security_group, 'TCP', 22, 22)
  147. conn.ex_create_security_group_rule(services_security_group, 'TCP', 3306, 3306,
  148. source_security_group=api_security_group)
  149. conn.ex_create_security_group_rule(services_security_group, 'TCP', 5672, 5672,
  150. source_security_group=worker_security_group)
  151. conn.ex_create_security_group_rule(services_security_group, 'TCP', 5672, 5672,
  152. source_security_group=api_security_group)
  153. else:
  154. services_security_group = get_security_group(conn, "services")
  155. for security_group in conn.ex_list_security_groups():
  156. print(security_group)
  157. ###########################################################################
  158. #
  159. # get floating ip helper function
  160. #
  161. ###########################################################################
  162. def get_floating_ip(connection):
  163. """A helper function to re-use available Floating IPs"""
  164. unused_floating_ip = None
  165. for float_ip in connection.ex_list_floating_ips():
  166. if not float_ip.node_id:
  167. unused_floating_ip = float_ip
  168. break
  169. if not unused_floating_ip:
  170. pool = connection.ex_list_floating_ip_pools()[0]
  171. unused_floating_ip = pool.create_floating_ip()
  172. return unused_floating_ip
  173. ###########################################################################
  174. #
  175. # create app-services instance (database & messaging)
  176. #
  177. ###########################################################################
  178. userdata = '''#!/usr/bin/env bash
  179. curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \
  180. -i database -i messaging
  181. '''
  182. print('Starting new app-services instance and wait until it is running...')
  183. instance_services = conn.create_node(name='app-services',
  184. image=image,
  185. size=flavor,
  186. networks=[network],
  187. ex_keyname=keypair_name,
  188. ex_userdata=userdata,
  189. ex_security_groups=[services_security_group])
  190. instance_services = conn.wait_until_running(nodes=[instance_services], timeout=120,
  191. ssh_interface='private_ips')[0][0]
  192. services_ip = instance_services.private_ips[0]
  193. ###########################################################################
  194. #
  195. # create app-api instances
  196. #
  197. ###########################################################################
  198. userdata = '''#!/usr/bin/env bash
  199. curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \
  200. -i faafo -r api -m 'amqp://guest:guest@%(services_ip)s:5672/' \
  201. -d 'mysql+pymysql://faafo:password@%(services_ip)s:3306/faafo'
  202. ''' % {'services_ip': services_ip}
  203. print('Starting new app-api-1 instance and wait until it is running...')
  204. instance_api_1 = conn.create_node(name='app-api-1',
  205. image=image,
  206. size=flavor,
  207. networks=[network],
  208. ex_keyname=keypair_name,
  209. ex_userdata=userdata,
  210. ex_security_groups=[api_security_group])
  211. print('Starting new app-api-2 instance and wait until it is running...')
  212. instance_api_2 = conn.create_node(name='app-api-2',
  213. image=image,
  214. size=flavor,
  215. networks=[network],
  216. ex_keyname=keypair_name,
  217. ex_userdata=userdata,
  218. ex_security_groups=[api_security_group])
  219. instance_api_1 = conn.wait_until_running(nodes=[instance_api_1], timeout=120,
  220. ssh_interface='private_ips')[0][0]
  221. api_1_ip = instance_api_1.private_ips[0]
  222. instance_api_2 = conn.wait_until_running(nodes=[instance_api_2], timeout=120,
  223. ssh_interface='private_ips')[0][0]
  224. # api_2_ip = instance_api_2.private_ips[0]
  225. for instance in [instance_api_1, instance_api_2]:
  226. floating_ip = get_floating_ip(conn)
  227. conn.ex_attach_floating_ip_to_node(instance, floating_ip)
  228. print('allocated %(ip)s to %(host)s' % {'ip': floating_ip.ip_address, 'host': instance.name})
  229. ###########################################################################
  230. #
  231. # create worker instances
  232. #
  233. ###########################################################################
  234. userdata_api_1 = '''#!/usr/bin/env bash
  235. curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \
  236. -i faafo -r worker -e 'http://%(api_1_ip)s' -m 'amqp://guest:guest@%(services_ip)s:5672/'
  237. ''' % {'api_1_ip': api_1_ip, 'services_ip': services_ip}
  238. # userdata-api-2 = '''#!/usr/bin/env bash
  239. # curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \
  240. # -i faafo -r worker -e 'http://%(api_2_ip)s' -m 'amqp://guest:guest@%(services_ip)s:5672/'
  241. # ''' % {'api_2_ip': api_2_ip, 'services_ip': services_ip}
  242. print('Starting new app-worker-1 instance and wait until it is running...')
  243. instance_worker_1 = conn.create_node(name='app-worker-1',
  244. image=image, size=flavor,
  245. networks=[network],
  246. ex_keyname=keypair_name,
  247. ex_userdata=userdata_api_1,
  248. ex_security_groups=[worker_security_group])
  249. print('Starting new app-worker-1 instance and wait until it is running...')
  250. instance_worker_2 = conn.create_node(name='app-worker-2',
  251. image=image, size=flavor,
  252. networks=[network],
  253. ex_keyname=keypair_name,
  254. ex_userdata=userdata_api_1,
  255. ex_security_groups=[worker_security_group])
  256. print('Starting new app-worker-1 instance and wait until it is running...')
  257. instance_worker_3 = conn.create_node(name='app-worker-3',
  258. image=image, size=flavor,
  259. networks=[network],
  260. ex_keyname=keypair_name,
  261. ex_userdata=userdata_api_1,
  262. ex_security_groups=[worker_security_group])
  263. print(instance_worker_1)
  264. print(instance_worker_2)
  265. print(instance_worker_3)
  266. if __name__ == '__main__':
  267. main()