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.

317 lines
13 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. # https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh
  161. # is currently broken, hence the "rabbitctl" lines were added in the example
  162. # below, see also https://bugs.launchpad.net/faafo/+bug/1679710
  163. #
  164. # Thanks to Stefan Friedmann for finding this fix ;)
  165. # TODO: still needed for new version of faafo and Ubuntu 22.04?
  166. userdata = '#!/usr/bin/env bash\n' \
  167. 'curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-' \
  168. 'examples/raw/master/faafo/contrib/install.sh | bash -s -- ' \
  169. '-i messaging -i faafo -r api\n' \
  170. 'rabbitmqctl add_user faafo guest\n' \
  171. 'rabbitmqctl set_user_tags faafo administrator\n' \
  172. 'rabbitmqctl set_permissions -p / faafo ".*" ".*" ".*"\n'
  173. print('\nUsing cloud-init userdata:\n"' + userdata + '"\n')
  174. print('Starting new app-controller instance and wait until it is running...')
  175. instance_controller_1 = conn.create_node(name='app-controller',
  176. image=image,
  177. size=flavor,
  178. networks=[network],
  179. ex_keyname=KEYPAIR_NAME,
  180. ex_userdata=userdata,
  181. ex_security_groups=[controller_security_group])
  182. conn.wait_until_running(nodes=[instance_controller_1], timeout=120, ssh_interface='private_ips')
  183. ###########################################################################
  184. #
  185. # assign app-controller floating ip
  186. #
  187. ###########################################################################
  188. print('Checking for unused Floating IP...')
  189. unused_floating_ip = None
  190. for floating_ip in conn.ex_list_floating_ips():
  191. if not floating_ip.node_id:
  192. unused_floating_ip = floating_ip
  193. break
  194. if not unused_floating_ip:
  195. pool = conn.ex_list_floating_ip_pools()[0]
  196. print(f'Allocating new Floating IP from pool: {pool}')
  197. unused_floating_ip = pool.create_floating_ip()
  198. conn.ex_attach_floating_ip_to_node(instance_controller_1, unused_floating_ip)
  199. print(f'Controller Application will be deployed to http://{unused_floating_ip.ip_address}')
  200. actual_ip_address = unused_floating_ip.ip_address
  201. ###########################################################################
  202. #
  203. # getting id and ip address of app-controller instance
  204. #
  205. ###########################################################################
  206. # instance should not have a public ip? floating ips are assigned later
  207. instance_controller_1 = conn.ex_get_node_details(instance_controller_1.id)
  208. ip_controller = ''
  209. if instance_controller_1.public_ips:
  210. ip_controller = instance_controller_1.public_ips[0]
  211. else:
  212. ip_controller = instance_controller_1.private_ips[0]
  213. ###########################################################################
  214. #
  215. # create app-worker-1
  216. #
  217. ###########################################################################
  218. userdata = '#!/usr/bin/env bash\n' \
  219. 'curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-' \
  220. 'examples/raw/master/faafo/contrib/install.sh | bash -s -- ' \
  221. f'-i faafo -r worker -e "http://{ip_controller}" -m "amqp://faafo:guest@' \
  222. f'{ip_controller}:5672/\n'
  223. print('\nUsing cloud-init userdata:\n"' + userdata + '"\n')
  224. print('Starting new app-worker-1 instance and wait until it is running...')
  225. instance_worker_1 = conn.create_node(name='app-worker-1',
  226. image=image,
  227. size=flavor,
  228. networks=[network],
  229. ex_keyname=KEYPAIR_NAME,
  230. ex_userdata=userdata,
  231. ex_security_groups=[worker_security_group])
  232. conn.wait_until_running(nodes=[instance_worker_1], timeout=120, ssh_interface='private_ips')
  233. ###########################################################################
  234. #
  235. # assign app-worker floating ip
  236. #
  237. ###########################################################################
  238. print('Checking for unused Floating IP...')
  239. unused_floating_ip = None
  240. for floating_ip in conn.ex_list_floating_ips():
  241. if not floating_ip.node_id:
  242. unused_floating_ip = floating_ip
  243. break
  244. if not unused_floating_ip:
  245. pool = conn.ex_list_floating_ip_pools()[0]
  246. print(f'Allocating new Floating IP from pool: {pool}')
  247. unused_floating_ip = pool.create_floating_ip()
  248. conn.ex_attach_floating_ip_to_node(instance_worker_1, unused_floating_ip)
  249. print(f'The worker will be available for SSH at {unused_floating_ip.ip_address}')
  250. print('\n\n#### Deployment finished\n\n')
  251. print('After some minutes, as soon as cloud-init installed required packages and the\n'
  252. 'faafo app, (First App Application For OpenStack) fractals demo will be available\n'
  253. f'at http://{actual_ip_address}\n')
  254. print('You can use ssh to login to the controller using your private key.\n'
  255. f'E.g., "ssh -i ~/.ssh/id_rsa ubuntu@{actual_ip_address}". After login,\n'
  256. 'you can list available fractals using "faafo list". To request the generation of\n'
  257. 'new fractals, you can use "faafo create". \n'
  258. 'You can also see other options to use the faafo example cloud service using '
  259. '"faafo -h".')
  260. if __name__ == '__main__':
  261. main()