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.

350 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. # 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 = X
  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. ubuntu_image_name = "Ubuntu 20.04 - Focal Fossa - 64-bit - Cloud Based Image"
  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 = 'CloudComp30-keypair'
  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. #hsfd_faafo_cloud_init_script = 'https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh'
  195. # testing / faafo dev branch:
  196. hsfd_faafo_cloud_init_script = 'https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/branch/dev_faafo/faafo/contrib/install.sh'
  197. userdata_service = '''#!/usr/bin/env bash
  198. curl -L -s ''' + hsfd_faafo_cloud_init_script + ''' | bash -s -- \
  199. -i database -i messaging
  200. rabbitmqctl add_user faafo guest
  201. rabbitmqctl set_user_tags faafo administrator
  202. rabbitmqctl set_permissions -p / faafo ".*" ".*" ".*"
  203. '''
  204. print('Starting new app-services instance and wait until it is running...')
  205. instance_services = conn.create_node(name='app-services',
  206. image=image,
  207. size=flavor,
  208. networks=[network],
  209. ex_keyname=keypair_name,
  210. ex_userdata=userdata_service,
  211. ex_security_groups=[services_security_group])
  212. instance_services = conn.wait_until_running(nodes=[instance_services], timeout=120,
  213. ssh_interface='private_ips')[0][0]
  214. services_ip = instance_services.private_ips[0]
  215. ###########################################################################
  216. #
  217. # create app-api instances
  218. #
  219. ###########################################################################
  220. userdata_api = '''#!/usr/bin/env bash
  221. curl -L -s ''' + hsfd_faafo_cloud_init_script + ''' | bash -s -- \
  222. -i faafo -r api -m 'amqp://faafo:guest@%(services_ip)s:5672/' \
  223. -d 'mysql+pymysql://faafo:password@%(services_ip)s:3306/faafo'
  224. ''' % {'services_ip': services_ip}
  225. print('Starting new app-api-1 instance and wait until it is running...')
  226. instance_api_1 = conn.create_node(name='app-api-1',
  227. image=image,
  228. size=flavor,
  229. networks=[network],
  230. ex_keyname=keypair_name,
  231. ex_userdata=userdata_api,
  232. ex_security_groups=[api_security_group])
  233. print('Starting new app-api-2 instance and wait until it is running...')
  234. instance_api_2 = conn.create_node(name='app-api-2',
  235. image=image,
  236. size=flavor,
  237. networks=[network],
  238. ex_keyname=keypair_name,
  239. ex_userdata=userdata_api,
  240. ex_security_groups=[api_security_group])
  241. instance_api_1 = conn.wait_until_running(nodes=[instance_api_1], timeout=120,
  242. ssh_interface='private_ips')[0][0]
  243. api_1_ip = instance_api_1.private_ips[0]
  244. instance_api_2 = conn.wait_until_running(nodes=[instance_api_2], timeout=120,
  245. ssh_interface='private_ips')[0][0]
  246. # api_2_ip = instance_api_2.private_ips[0]
  247. for instance in [instance_api_1, instance_api_2]:
  248. floating_ip = get_floating_ip(conn)
  249. conn.ex_attach_floating_ip_to_node(instance, floating_ip)
  250. print(('allocated %(ip)s to %(host)s' % {'ip': floating_ip.ip_address, 'host': instance.name}))
  251. ###########################################################################
  252. #
  253. # create worker instances
  254. #
  255. ###########################################################################
  256. userdata_worker = '''#!/usr/bin/env bash
  257. curl -L -s ''' + hsfd_faafo_cloud_init_script + ''' | bash -s -- \
  258. -i faafo -r worker -e 'http://%(api_1_ip)s' -m 'amqp://faafo:guest@%(services_ip)s:5672/'
  259. ''' % {'api_1_ip': api_1_ip, 'services_ip': services_ip}
  260. # userdata_api-api-2 = '''#!/usr/bin/env bash
  261. # curl -L -s ''' + hsfd_faafo_cloud_init_script + ''' | bash -s -- \
  262. # -i faafo -r worker -e 'http://%(api_2_ip)s' -m 'amqp://faafo:guest@%(services_ip)s:5672/'
  263. # ''' % {'api_2_ip': api_2_ip, 'services_ip': services_ip}
  264. print('Starting new app-worker-1 instance and wait until it is running...')
  265. instance_worker_1 = conn.create_node(name='app-worker-1',
  266. image=image, size=flavor,
  267. networks=[network],
  268. ex_keyname=keypair_name,
  269. ex_userdata=userdata_worker,
  270. ex_security_groups=[worker_security_group])
  271. print('Starting new app-worker-2 instance and wait until it is running...')
  272. instance_worker_2 = conn.create_node(name='app-worker-2',
  273. image=image, size=flavor,
  274. networks=[network],
  275. ex_keyname=keypair_name,
  276. ex_userdata=userdata_worker,
  277. ex_security_groups=[worker_security_group])
  278. # do not start worker 3 initially, can be started using scale-out-add-worker.py demo
  279. #print('Starting new app-worker-3 instance and wait until it is running...')
  280. #instance_worker_3 = conn.create_node(name='app-worker-3',
  281. # image=image, size=flavor,
  282. # networks=[network],
  283. # ex_keyname=keypair_name,
  284. # ex_userdata=userdata_worker,
  285. # ex_security_groups=[worker_security_group])
  286. print(instance_worker_1)
  287. print(instance_worker_2)
  288. #print(instance_worker_3)
  289. if __name__ == '__main__':
  290. main()