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.

345 lines
15 KiB

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