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.

368 lines
16 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
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. """Example for Cloud Computing Course Master AI / GSD"""
  2. # This script demonstrates how to use libcloud to start an instance in an OpenStack environment.
  3. # The script will start multiple instances splitting up the faafo monolithic application into
  4. # a microservice architecture with scalable api (controller) and worker instances using a
  5. # message queue and a database
  6. # Needed if the password should be prompted for:
  7. # import getpass
  8. import os
  9. import sys
  10. import time
  11. from libcloud.compute.providers import get_driver
  12. from libcloud.compute.types import Provider
  13. # For our new Charmed OpenStack private cloud, we need to specify the path to the root
  14. # CA certificate
  15. import libcloud.security
  16. libcloud.security.CA_CERTS_PATH = ['./root-ca.crt']
  17. # Disable SSL certificate verification (not recommended for production)
  18. # libcloud.security.VERIFY_SSL_CERT = False
  19. # Please use 1-29 as environment variable GROUP_NUMBER to specify your group number.
  20. # (will be used for the username, project etc., as coordinated in the lab sessions)
  21. group_number = os.environ.get('GROUP_NUMBER')
  22. if group_number is None:
  23. sys.exit('Please set the GROUP_NUMBER environment variable to your group number,\n'
  24. 'e.g., on Windows:\n'
  25. ' "$env:GROUP_NUMBER=0" or "set GROUP_NUMBER=0"\n'
  26. 'or on Linux/MacOS:\n'
  27. ' "export GROUP_NUMBER=0" or "set GROUP_NUMBER=0"')
  28. # web service endpoint of the private cloud infrastructure
  29. # auth_url = 'https://private-cloud.informatik.hs-fulda.de:5000'
  30. AUTH_URL = 'https://10.32.4.182:5000'
  31. # auth_url = 'https://private-cloud2.informatik.hs-fulda.de:5000'
  32. # your username in OpenStack
  33. AUTH_USERNAME = 'CloudComp' + str(group_number)
  34. print(f'Using username: {AUTH_USERNAME}\n')
  35. # your project in OpenStack
  36. PROJECT_NAME = 'CloudComp' + str(group_number)
  37. # A network in the project the started instance will be attached to
  38. PROJECT_NETWORK = 'CloudComp' + str(group_number) + '-net'
  39. # The image to look for and use for the started instance
  40. # ubuntu_image_name = "Ubuntu 18.04 - Bionic Beaver - 64-bit - Cloud Based Image"
  41. #UBUNTU_IMAGE_NAME = "auto-sync/ubuntu-jammy-22.04-amd64-server-20240319-disk1.img"
  42. UBUNTU_IMAGE_NAME = "ubuntu-22.04-jammy-x86_64"
  43. # The public key to be used for SSH connection, please make sure, that you have the
  44. # corresponding private key
  45. #
  46. # id_rsa.pub should look like this (standard sshd pubkey format):
  47. # ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAw+J...F3w2mleybgT1w== user@HOSTNAME
  48. KEYPAIR_NAME = 'srieger-pub'
  49. PUB_KEY_FILE = '~/.ssh/id_rsa.pub'
  50. FLAVOR_NAME = 'm1.small'
  51. # default region
  52. REGION_NAME = 'RegionOne'
  53. # domain to use, "default" for local accounts, formerly "hsfulda" for LDAP accounts etc.
  54. # domain_name = "default"
  55. def main(): # noqa: C901 pylint: disable=too-many-branches,too-many-statements,too-many-locals,missing-function-docstring
  56. ###########################################################################
  57. #
  58. # get credentials
  59. #
  60. ###########################################################################
  61. # if "OS_PASSWORD" in os.environ:
  62. # auth_password = os.environ["OS_PASSWORD"]
  63. # else:
  64. # auth_password = getpass.getpass("Enter your OpenStack password:")
  65. auth_password = "demo"
  66. ###########################################################################
  67. #
  68. # create connection
  69. #
  70. ###########################################################################
  71. provider = get_driver(Provider.OPENSTACK)
  72. conn = provider(AUTH_USERNAME,
  73. auth_password,
  74. ex_force_auth_url=AUTH_URL,
  75. ex_force_auth_version='3.x_password',
  76. ex_tenant_name=PROJECT_NAME,
  77. ex_force_service_region=REGION_NAME)
  78. # ex_domain_name=domain_name)
  79. ###########################################################################
  80. #
  81. # get image, flavor, network for instance creation
  82. #
  83. ###########################################################################
  84. images = conn.list_images()
  85. image = ''
  86. for img in images:
  87. if img.name == UBUNTU_IMAGE_NAME:
  88. image = img
  89. flavors = conn.list_sizes()
  90. flavor = ''
  91. for flav in flavors:
  92. if flav.name == FLAVOR_NAME:
  93. flavor = conn.ex_get_size(flav.id)
  94. networks = conn.ex_list_networks()
  95. network = ''
  96. for net in networks:
  97. if net.name == PROJECT_NETWORK:
  98. network = net
  99. ###########################################################################
  100. #
  101. # create keypair dependency
  102. #
  103. ###########################################################################
  104. print('Checking for existing SSH key pair...')
  105. keypair_exists = False
  106. for keypair in conn.list_key_pairs():
  107. if keypair.name == KEYPAIR_NAME:
  108. keypair_exists = True
  109. if keypair_exists:
  110. print(('Keypair ' + KEYPAIR_NAME + ' already exists. Skipping import.'))
  111. else:
  112. print('adding keypair...')
  113. conn.import_key_pair_from_file(KEYPAIR_NAME, PUB_KEY_FILE)
  114. for keypair in conn.list_key_pairs():
  115. print(keypair)
  116. ###########################################################################
  117. #
  118. # clean up resources from previous demos
  119. #
  120. ###########################################################################
  121. # destroy running demo instances
  122. for instance in conn.list_nodes():
  123. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2',
  124. 'app-worker-3', 'app-controller',
  125. 'app-services', 'app-api-1', 'app-api-2']:
  126. print(f'Destroying Instance: ${instance.name}')
  127. conn.destroy_node(instance)
  128. # wait until all nodes are destroyed to be able to remove depended security groups
  129. nodes_still_running = True
  130. while nodes_still_running:
  131. nodes_still_running = False
  132. time.sleep(3)
  133. instances = conn.list_nodes()
  134. for instance in instances:
  135. # if we see any demo instances still running continue to wait for them to stop
  136. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-controller']:
  137. nodes_still_running = True
  138. print('There are still instances running, waiting for them to be destroyed...')
  139. # delete security groups
  140. for group in conn.ex_list_security_groups():
  141. if group.name in ['control', 'worker', 'api', 'services']:
  142. print(f'Deleting security group: ${group.name}')
  143. conn.ex_delete_security_group(group)
  144. ###########################################################################
  145. #
  146. # create security group dependency
  147. #
  148. ###########################################################################
  149. def get_security_group(connection, security_group_name):
  150. """A helper function to check if security group already exists"""
  151. print(('Checking for existing ' + security_group_name + ' security group...'))
  152. for security_grp in connection.ex_list_security_groups():
  153. if security_grp.name == security_group_name:
  154. print(('Security Group ' + security_group_name +
  155. ' already exists. Skipping creation.'))
  156. return worker_security_group
  157. return False
  158. if not get_security_group(conn, "api"):
  159. api_security_group = conn.ex_create_security_group('api', 'for API services only')
  160. conn.ex_create_security_group_rule(api_security_group, 'TCP', 80, 80)
  161. conn.ex_create_security_group_rule(api_security_group, 'TCP', 22, 22)
  162. else:
  163. api_security_group = get_security_group(conn, "api")
  164. if not get_security_group(conn, "worker"):
  165. worker_security_group = conn.ex_create_security_group(
  166. 'worker', 'for services that run on a worker node')
  167. conn.ex_create_security_group_rule(worker_security_group, 'TCP', 22, 22)
  168. else:
  169. worker_security_group = get_security_group(conn, "worker")
  170. if not get_security_group(conn, "control"):
  171. controller_security_group = conn.ex_create_security_group(
  172. 'control', 'for services that run on a control node')
  173. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 22, 22)
  174. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 80, 80)
  175. conn.ex_create_security_group_rule(controller_security_group, 'TCP', 5672, 5672,
  176. source_security_group=worker_security_group)
  177. if not get_security_group(conn, "services"):
  178. services_security_group = conn.ex_create_security_group(
  179. 'services', 'for DB and AMQP services only')
  180. conn.ex_create_security_group_rule(services_security_group, 'TCP', 22, 22)
  181. conn.ex_create_security_group_rule(services_security_group, 'TCP', 3306, 3306,
  182. source_security_group=api_security_group)
  183. conn.ex_create_security_group_rule(services_security_group, 'TCP', 5672, 5672,
  184. source_security_group=worker_security_group)
  185. conn.ex_create_security_group_rule(services_security_group, 'TCP', 5672, 5672,
  186. source_security_group=api_security_group)
  187. else:
  188. services_security_group = get_security_group(conn, "services")
  189. for security_group in conn.ex_list_security_groups():
  190. print(security_group)
  191. ###########################################################################
  192. #
  193. # get floating ip helper function
  194. #
  195. ###########################################################################
  196. def get_floating_ip(connection):
  197. """A helper function to re-use available Floating IPs"""
  198. unused_floating_ip = None
  199. for float_ip in connection.ex_list_floating_ips():
  200. if not float_ip.node_id:
  201. unused_floating_ip = float_ip
  202. break
  203. if not unused_floating_ip:
  204. pool = connection.ex_list_floating_ip_pools()[0]
  205. unused_floating_ip = pool.create_floating_ip()
  206. return unused_floating_ip
  207. ###########################################################################
  208. #
  209. # create app-services instance (database & messaging)
  210. #
  211. ###########################################################################
  212. userdata_service = '#!/usr/bin/env bash\n' \
  213. 'curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-' \
  214. 'examples/raw/master/faafo/contrib/install.sh | bash -s -- ' \
  215. '-i database -i messaging\n'
  216. print('\nUsing cloud-init userdata for service:\n"' + userdata_service + '"\n')
  217. print('Starting new app-services instance and wait until it is running...')
  218. instance_services = conn.create_node(name='app-services',
  219. image=image,
  220. size=flavor,
  221. networks=[network],
  222. ex_keyname=KEYPAIR_NAME,
  223. ex_userdata=userdata_service,
  224. ex_security_groups=[services_security_group])
  225. instance_services = conn.wait_until_running(nodes=[instance_services], timeout=120,
  226. ssh_interface='private_ips')[0][0]
  227. services_ip = instance_services.private_ips[0]
  228. ###########################################################################
  229. #
  230. # create app-api instances
  231. #
  232. ###########################################################################
  233. userdata_api = '#!/usr/bin/env bash\n' \
  234. 'curl -L -s https://gogs.informatik.hs-fulda.de/srieger/' \
  235. 'cloud-computing-msc-ai-examples/raw/master/faafo/contrib/' \
  236. 'install.sh | bash -s -- ' \
  237. f'-i faafo -r api -m "amqp://faafo:guest@{services_ip}:5672/" ' \
  238. f'-d "mysql+pymysql://faafo:password@{services_ip}:3306/faafo"'
  239. print('\nUsing cloud-init userdata for api:\n"' + userdata_api + '"\n')
  240. print('Starting new app-api-1 instance and wait until it is running...')
  241. instance_api_1 = conn.create_node(name='app-api-1',
  242. image=image,
  243. size=flavor,
  244. networks=[network],
  245. ex_keyname=KEYPAIR_NAME,
  246. ex_userdata=userdata_api,
  247. ex_security_groups=[api_security_group])
  248. print('Starting new app-api-2 instance and wait until it is running...')
  249. instance_api_2 = conn.create_node(name='app-api-2',
  250. image=image,
  251. size=flavor,
  252. networks=[network],
  253. ex_keyname=KEYPAIR_NAME,
  254. ex_userdata=userdata_api,
  255. ex_security_groups=[api_security_group])
  256. instance_api_1 = conn.wait_until_running(nodes=[instance_api_1], timeout=120,
  257. ssh_interface='private_ips')[0][0]
  258. api_1_ip = instance_api_1.private_ips[0]
  259. instance_api_2 = conn.wait_until_running(nodes=[instance_api_2], timeout=120,
  260. ssh_interface='private_ips')[0][0]
  261. # api_2_ip = instance_api_2.private_ips[0]
  262. for instance in [instance_api_1, instance_api_2]:
  263. floating_ip = get_floating_ip(conn)
  264. conn.ex_attach_floating_ip_to_node(instance, floating_ip)
  265. print(('allocated %(ip)s to %(host)s' % {'ip': floating_ip.ip_address,
  266. 'host': instance.name}))
  267. ###########################################################################
  268. #
  269. # create worker instances
  270. #
  271. ###########################################################################
  272. userdata_worker = '#!/usr/bin/env bash\n' \
  273. 'curl -L -s https://gogs.informatik.hs-fulda.de/srieger/' \
  274. 'cloud-computing-msc-ai-examples/raw/master/faafo/contrib/' \
  275. 'install.sh | bash -s -- ' \
  276. f'-i faafo -r worker -e "http://{api_1_ip}" '\
  277. f'-m "amqp://faafo:guest@{services_ip}:5672/"'
  278. print('\nUsing cloud-init userdata for worker:\n"' + userdata_worker + '"\n')
  279. # userdata_api-api-2 = '''#!/usr/bin/env bash
  280. # curl -L -s ''' + hsfd_faafo_cloud_init_script + ''' | bash -s -- \
  281. # -i faafo -r worker -e 'http://%(api_2_ip)s' -m 'amqp://faafo:guest@%(services_ip)s:5672/'
  282. # ''' % {'api_2_ip': api_2_ip, 'services_ip': services_ip}
  283. print('Starting new app-worker-1 instance and wait until it is running...')
  284. instance_worker_1 = conn.create_node(name='app-worker-1',
  285. image=image, size=flavor,
  286. networks=[network],
  287. ex_keyname=KEYPAIR_NAME,
  288. ex_userdata=userdata_worker,
  289. ex_security_groups=[worker_security_group])
  290. print('Starting new app-worker-2 instance and wait until it is running...')
  291. instance_worker_2 = conn.create_node(name='app-worker-2',
  292. image=image, size=flavor,
  293. networks=[network],
  294. ex_keyname=KEYPAIR_NAME,
  295. ex_userdata=userdata_worker,
  296. ex_security_groups=[worker_security_group])
  297. # do not start worker 3 initially, can be started using scale-out-add-worker.py demo
  298. #print('Starting new app-worker-3 instance and wait until it is running...')
  299. #instance_worker_3 = conn.create_node(name='app-worker-3',
  300. # image=image, size=flavor,
  301. # networks=[network],
  302. # ex_keyname=keypair_name,
  303. # ex_userdata=userdata_worker,
  304. # ex_security_groups=[worker_security_group])
  305. print(instance_worker_1)
  306. print(instance_worker_2)
  307. #print(instance_worker_3)
  308. if __name__ == '__main__':
  309. main()