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
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 = 2
  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 20.04 - Focal Fossa - 64-bit - Cloud Based Image"
  23. ubuntu_image_name = "Ubuntu 22.04 - Jammy Jellyfish - 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. #hsfd_faafo_cloud_init_script = 'https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh'
  190. # testing / faafo dev branch:
  191. 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'
  192. userdata_service = '''#!/usr/bin/env bash
  193. curl -L -s ''' + hsfd_faafo_cloud_init_script + ''' | 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 ''' + hsfd_faafo_cloud_init_script + ''' | 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 ''' + hsfd_faafo_cloud_init_script + ''' | 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 ''' + hsfd_faafo_cloud_init_script + ''' | 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()