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.

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