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.

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