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.

307 lines
12 KiB

  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 create start multiple instances splitting up the faafo monolithic application into
  4. # a (minimalistic but already scalable) microservice architecture.
  5. # Also introduces the concept of different security groups and corresponding frontend/backend
  6. # separation.
  7. # Needed if the password should be prompted for:
  8. # import getpass
  9. import os
  10. import sys
  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. # The public key to be used for SSH connection, please make sure, that you have the
  43. # corresponding private key
  44. #
  45. # id_rsa.pub should look like this (standard sshd pubkey format):
  46. # ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAw+J...F3w2mleybgT1w== user@HOSTNAME
  47. KEYPAIR_NAME = 'srieger-pub'
  48. PUB_KEY_FILE = '~/.ssh/id_rsa.pub'
  49. FLAVOR_NAME = 'm1.small'
  50. # default region
  51. REGION_NAME = 'RegionOne'
  52. # domain to use, "default" for local accounts, formerly "hsfulda" for LDAP accounts etc.
  53. # domain_name = "default"
  54. def main(): # noqa: C901 pylint: disable=too-many-branches,too-many-statements,too-many-locals,missing-function-docstring
  55. ###########################################################################
  56. #
  57. # get credentials
  58. #
  59. ###########################################################################
  60. # if "OS_PASSWORD" in os.environ:
  61. # auth_password = os.environ["OS_PASSWORD"]
  62. # else:
  63. # auth_password = getpass.getpass("Enter your OpenStack password:")
  64. auth_password = "demo"
  65. ###########################################################################
  66. #
  67. # create connection
  68. #
  69. ###########################################################################
  70. provider = get_driver(Provider.OPENSTACK)
  71. conn = provider(AUTH_USERNAME,
  72. auth_password,
  73. ex_force_auth_url=AUTH_URL,
  74. ex_force_auth_version='3.x_password',
  75. ex_tenant_name=PROJECT_NAME,
  76. ex_force_service_region=REGION_NAME)
  77. # ex_domain_name=domain_name)
  78. ###########################################################################
  79. #
  80. # get image, flavor, network for instance creation
  81. #
  82. ###########################################################################
  83. images = conn.list_images()
  84. image = ''
  85. for img in images:
  86. if img.name == UBUNTU_IMAGE_NAME:
  87. image = img
  88. flavors = conn.list_sizes()
  89. flavor = ''
  90. for flav in flavors:
  91. if flav.name == FLAVOR_NAME:
  92. flavor = conn.ex_get_size(flav.id)
  93. networks = conn.ex_list_networks()
  94. network = ''
  95. for net in networks:
  96. if net.name == PROJECT_NETWORK:
  97. network = net
  98. ###########################################################################
  99. #
  100. # create keypair dependency
  101. #
  102. ###########################################################################
  103. print('Checking for existing SSH key pair...')
  104. keypair_exists = False
  105. for keypair in conn.list_key_pairs():
  106. if keypair.name == KEYPAIR_NAME:
  107. keypair_exists = True
  108. if keypair_exists:
  109. print('Keypair ' + KEYPAIR_NAME + ' already exists. Skipping import.')
  110. else:
  111. print('adding keypair...')
  112. conn.import_key_pair_from_file(KEYPAIR_NAME, PUB_KEY_FILE)
  113. for keypair in conn.list_key_pairs():
  114. print(keypair)
  115. ###########################################################################
  116. #
  117. # create security group dependency
  118. #
  119. ###########################################################################
  120. print('Checking for existing worker security group...')
  121. security_group_name = 'worker'
  122. security_group_exists = False
  123. worker_security_group = ''
  124. for security_group in conn.ex_list_security_groups():
  125. if security_group.name == security_group_name:
  126. worker_security_group = security_group
  127. security_group_exists = True
  128. if security_group_exists:
  129. print('Worker Security Group ' + worker_security_group.name + ' already exists. '
  130. 'Skipping creation.')
  131. else:
  132. worker_security_group = conn.ex_create_security_group('worker', 'for services '
  133. 'that run on a worker node')
  134. conn.ex_create_security_group_rule(worker_security_group, 'TCP', 22, 22)
  135. print('Checking for existing controller security group...')
  136. security_group_name = 'control'
  137. security_group_exists = False
  138. controller_security_group = ''
  139. for security_group in conn.ex_list_security_groups():
  140. if security_group.name == security_group_name:
  141. controller_security_group = security_group
  142. security_group_exists = True
  143. if security_group_exists:
  144. print('Controller Security Group ' + controller_security_group.name + ' already exists. '
  145. 'Skipping creation.')
  146. else:
  147. controller_security_group = conn.ex_create_security_group('control', 'for services that '
  148. '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. for security_group in conn.ex_list_security_groups():
  154. print(security_group)
  155. ###########################################################################
  156. #
  157. # create app-controller
  158. #
  159. ###########################################################################
  160. userdata = '#!/usr/bin/env bash\n' \
  161. 'curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-' \
  162. 'examples/raw/master/faafo/contrib/install.sh | bash -s -- ' \
  163. '-i messaging -i faafo -r api\n'
  164. print('\nUsing cloud-init userdata for controller:\n"' + userdata + '"\n')
  165. print('Starting new app-controller instance and wait until it is running...')
  166. instance_controller_1 = conn.create_node(name='app-controller',
  167. image=image,
  168. size=flavor,
  169. networks=[network],
  170. ex_keyname=KEYPAIR_NAME,
  171. ex_userdata=userdata,
  172. ex_security_groups=[controller_security_group])
  173. conn.wait_until_running(nodes=[instance_controller_1], timeout=120, ssh_interface='private_ips')
  174. ###########################################################################
  175. #
  176. # assign app-controller floating ip
  177. #
  178. ###########################################################################
  179. print('Checking for unused Floating IP...')
  180. unused_floating_ip = None
  181. for floating_ip in conn.ex_list_floating_ips():
  182. if not floating_ip.node_id:
  183. unused_floating_ip = floating_ip
  184. break
  185. if not unused_floating_ip:
  186. pool = conn.ex_list_floating_ip_pools()[0]
  187. print(f'Allocating new Floating IP from pool: {pool}')
  188. unused_floating_ip = pool.create_floating_ip()
  189. conn.ex_attach_floating_ip_to_node(instance_controller_1, unused_floating_ip)
  190. print(f'Controller Application will be deployed to http://{unused_floating_ip.ip_address}')
  191. actual_ip_address = unused_floating_ip.ip_address
  192. ###########################################################################
  193. #
  194. # getting id and ip address of app-controller instance
  195. #
  196. ###########################################################################
  197. # instance should not have a public ip? floating ips are assigned later
  198. instance_controller_1 = conn.ex_get_node_details(instance_controller_1.id)
  199. ip_controller = ''
  200. if instance_controller_1.public_ips:
  201. ip_controller = instance_controller_1.public_ips[0]
  202. else:
  203. ip_controller = instance_controller_1.private_ips[0]
  204. ###########################################################################
  205. #
  206. # create app-worker-1
  207. #
  208. ###########################################################################
  209. userdata = '#!/usr/bin/env bash\n' \
  210. 'curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-' \
  211. 'examples/raw/master/faafo/contrib/install.sh | bash -s -- ' \
  212. f'-i faafo -r worker -e "http://{ip_controller}" -m "amqp://faafo:guest@' \
  213. f'{ip_controller}:5672/"\n'
  214. print('\nUsing cloud-init userdata for worker:\n"' + userdata + '"\n')
  215. print('Starting new app-worker-1 instance and wait until it is running...')
  216. instance_worker_1 = conn.create_node(name='app-worker-1',
  217. image=image,
  218. size=flavor,
  219. networks=[network],
  220. ex_keyname=KEYPAIR_NAME,
  221. ex_userdata=userdata,
  222. ex_security_groups=[worker_security_group])
  223. conn.wait_until_running(nodes=[instance_worker_1], timeout=120, ssh_interface='private_ips')
  224. ###########################################################################
  225. #
  226. # assign app-worker floating ip
  227. #
  228. ###########################################################################
  229. print('Checking for unused Floating IP...')
  230. unused_floating_ip = None
  231. for floating_ip in conn.ex_list_floating_ips():
  232. if not floating_ip.node_id:
  233. unused_floating_ip = floating_ip
  234. break
  235. if not unused_floating_ip:
  236. pool = conn.ex_list_floating_ip_pools()[0]
  237. print(f'Allocating new Floating IP from pool: {pool}')
  238. unused_floating_ip = pool.create_floating_ip()
  239. conn.ex_attach_floating_ip_to_node(instance_worker_1, unused_floating_ip)
  240. print(f'The worker will be available for SSH at {unused_floating_ip.ip_address}')
  241. print('\n\n#### Deployment finished\n\n')
  242. print('After some minutes, as soon as cloud-init installed required packages and the\n'
  243. 'faafo app, (First App Application For OpenStack) fractals demo will be available\n'
  244. f'at http://{actual_ip_address}\n')
  245. print('You can use ssh to login to the controller using your private key.\n'
  246. f'E.g., "ssh -i ~/.ssh/id_rsa ubuntu@{actual_ip_address}". After login,\n'
  247. 'you can list available fractals using "faafo list". To request the generation of\n'
  248. 'new fractals, you can use "faafo create". \n'
  249. 'You can also see other options to use the faafo example cloud service using '
  250. '"faafo -h".')
  251. if __name__ == '__main__':
  252. main()