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.

346 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 = "auto-sync/ubuntu-jammy-22.04-amd64-server-20240319-disk1.img"
  24. UBUNTU_IMAGE_NAME = "ubuntu-22.04-jammy-x86_64"
  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 = 'CloudComp30-keypair'
  30. keypair_name = "srieger-pub"
  31. pub_key_file = '~/.ssh/id_rsa.pub'
  32. flavor_name = 'm1.small'
  33. # default region
  34. region_name = 'RegionOne'
  35. # domain to use, "default" for local accounts, "hsfulda" for RZ LDAP, e.g., using fdaiXXXX as auth_username
  36. domain_name = "default"
  37. def main():
  38. ###########################################################################
  39. #
  40. # get credentials
  41. #
  42. ###########################################################################
  43. # if "OS_PASSWORD" in os.environ:
  44. # auth_password = os.environ["OS_PASSWORD"]
  45. # else:
  46. # auth_password = getpass.getpass("Enter your OpenStack password:")
  47. auth_password = "demo"
  48. ###########################################################################
  49. #
  50. # create connection
  51. #
  52. ###########################################################################
  53. # libcloud.security.VERIFY_SSL_CERT = False
  54. provider = get_driver(Provider.OPENSTACK)
  55. conn = provider(auth_username,
  56. auth_password,
  57. ex_force_auth_url=auth_url,
  58. ex_force_auth_version='3.x_password',
  59. ex_tenant_name=project_name,
  60. ex_force_service_region=region_name,
  61. ex_domain_name=domain_name)
  62. ###########################################################################
  63. #
  64. # get image, flavor, network for instance creation
  65. #
  66. ###########################################################################
  67. images = conn.list_images()
  68. image = ''
  69. for img in images:
  70. if img.name == ubuntu_image_name:
  71. image = img
  72. flavors = conn.list_sizes()
  73. flavor = ''
  74. for flav in flavors:
  75. if flav.name == flavor_name:
  76. flavor = conn.ex_get_size(flav.id)
  77. networks = conn.ex_list_networks()
  78. network = ''
  79. for net in networks:
  80. if net.name == project_network:
  81. network = net
  82. ###########################################################################
  83. #
  84. # create keypair dependency
  85. #
  86. ###########################################################################
  87. print('Checking for existing SSH key pair...')
  88. keypair_exists = False
  89. for keypair in conn.list_key_pairs():
  90. if keypair.name == keypair_name:
  91. keypair_exists = True
  92. if keypair_exists:
  93. print(('Keypair ' + keypair_name + ' already exists. Skipping import.'))
  94. else:
  95. print('adding keypair...')
  96. conn.import_key_pair_from_file(keypair_name, pub_key_file)
  97. for keypair in conn.list_key_pairs():
  98. print(keypair)
  99. ###########################################################################
  100. #
  101. # clean up resources from previous demos
  102. #
  103. ###########################################################################
  104. # destroy running demo instances
  105. for instance in conn.list_nodes():
  106. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-worker-3', 'app-controller',
  107. 'app-services', 'app-api-1', 'app-api-2']:
  108. print(('Destroying Instance: %s' % instance.name))
  109. conn.destroy_node(instance)
  110. # wait until all nodes are destroyed to be able to remove depended security groups
  111. nodes_still_running = True
  112. while nodes_still_running:
  113. nodes_still_running = False
  114. time.sleep(3)
  115. instances = conn.list_nodes()
  116. for instance in instances:
  117. # if we see any demo instances still running continue to wait for them to stop
  118. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-controller']:
  119. nodes_still_running = True
  120. print('There are still instances running, waiting for them to be destroyed...')
  121. # delete security groups
  122. for group in conn.ex_list_security_groups():
  123. if group.name in ['control', 'worker', 'api', 'services']:
  124. print(('Deleting security group: %s' % group.name))
  125. conn.ex_delete_security_group(group)
  126. ###########################################################################
  127. #
  128. # create security group dependency
  129. #
  130. ###########################################################################
  131. def get_security_group(connection, security_group_name):
  132. """A helper function to check if security group already exists"""
  133. print(('Checking for existing ' + security_group_name + ' security group...'))
  134. for security_grp in connection.ex_list_security_groups():
  135. if security_grp.name == security_group_name:
  136. print(('Security Group ' + security_group_name + ' already exists. Skipping creation.'))
  137. return worker_security_group
  138. return False
  139. if not get_security_group(conn, "api"):
  140. api_security_group = conn.ex_create_security_group('api', 'for API services only')
  141. conn.ex_create_security_group_rule(api_security_group, 'TCP', 80, 80)
  142. conn.ex_create_security_group_rule(api_security_group, 'TCP', 22, 22)
  143. else:
  144. api_security_group = get_security_group(conn, "api")
  145. if not get_security_group(conn, "worker"):
  146. worker_security_group = conn.ex_create_security_group('worker', 'for services that run on a worker node')
  147. conn.ex_create_security_group_rule(worker_security_group, 'TCP', 22, 22)
  148. else:
  149. worker_security_group = get_security_group(conn, "worker")
  150. if not get_security_group(conn, "control"):
  151. controller_security_group = conn.ex_create_security_group('control', 'for services that run on a control node')
  152. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 22, 22)
  153. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 80, 80)
  154. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 5672, 5672,
  155. source_security_group=worker_security_group)
  156. if not get_security_group(conn, "services"):
  157. services_security_group = conn.ex_create_security_group('services', 'for DB and AMQP services only')
  158. conn.ex_create_security_group_rule(services_security_group, 'TCP', 22, 22)
  159. conn.ex_create_security_group_rule(services_security_group, 'TCP', 3306, 3306,
  160. source_security_group=api_security_group)
  161. conn.ex_create_security_group_rule(services_security_group, 'TCP', 5672, 5672,
  162. source_security_group=worker_security_group)
  163. conn.ex_create_security_group_rule(services_security_group, 'TCP', 5672, 5672,
  164. source_security_group=api_security_group)
  165. else:
  166. services_security_group = get_security_group(conn, "services")
  167. for security_group in conn.ex_list_security_groups():
  168. print(security_group)
  169. ###########################################################################
  170. #
  171. # get floating ip helper function
  172. #
  173. ###########################################################################
  174. def get_floating_ip(connection):
  175. """A helper function to re-use available Floating IPs"""
  176. unused_floating_ip = None
  177. for float_ip in connection.ex_list_floating_ips():
  178. if not float_ip.node_id:
  179. unused_floating_ip = float_ip
  180. break
  181. if not unused_floating_ip:
  182. pool = connection.ex_list_floating_ip_pools()[0]
  183. unused_floating_ip = pool.create_floating_ip()
  184. return unused_floating_ip
  185. ###########################################################################
  186. #
  187. # create app-services instance (database & messaging)
  188. #
  189. ###########################################################################
  190. #hsfd_faafo_cloud_init_script = 'https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh'
  191. # testing / faafo dev branch:
  192. 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'
  193. userdata_service = '''#!/usr/bin/env bash
  194. curl -L -s ''' + hsfd_faafo_cloud_init_script + ''' | 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 ''' + hsfd_faafo_cloud_init_script + ''' | 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 ''' + hsfd_faafo_cloud_init_script + ''' | 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 ''' + hsfd_faafo_cloud_init_script + ''' | 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()