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.

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