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.

216 lines
8.2 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 add an additional worker to a running faafo example application
  4. # Needed if the password should be prompted for:
  5. # import getpass
  6. import os
  7. import sys
  8. # import time
  9. from libcloud.compute.providers import get_driver
  10. from libcloud.compute.types import Provider
  11. # For our new Charmed OpenStack private cloud, we need to specify the path to the root
  12. # CA certificate
  13. import libcloud.security
  14. libcloud.security.CA_CERTS_PATH = ['./root-ca.crt']
  15. # Disable SSL certificate verification (not recommended for production)
  16. # libcloud.security.VERIFY_SSL_CERT = False
  17. # Please use 1-29 as environment variable GROUP_NUMBER to specify your group number.
  18. # (will be used for the username, project etc., as coordinated in the lab sessions)
  19. group_number = os.environ.get('GROUP_NUMBER')
  20. if group_number is None:
  21. sys.exit('Please set the GROUP_NUMBER environment variable to your group number,\n'
  22. 'e.g., on Windows:\n'
  23. ' "$env:GROUP_NUMBER=0" or "set GROUP_NUMBER=0"\n'
  24. 'or on Linux/MacOS:\n'
  25. ' "export GROUP_NUMBER=0" or "set GROUP_NUMBER=0"')
  26. # web service endpoint of the private cloud infrastructure
  27. # auth_url = 'https://private-cloud.informatik.hs-fulda.de:5000'
  28. AUTH_URL = 'https://10.32.4.182:5000'
  29. # auth_url = 'https://private-cloud2.informatik.hs-fulda.de:5000'
  30. # your username in OpenStack
  31. AUTH_USERNAME = 'CloudComp' + str(group_number)
  32. print(f'Using username: {AUTH_USERNAME}\n')
  33. # your project in OpenStack
  34. PROJECT_NAME = 'CloudComp' + str(group_number)
  35. # A network in the project the started instance will be attached to
  36. PROJECT_NETWORK = 'CloudComp' + str(group_number) + '-net'
  37. # The image to look for and use for the started instance
  38. # ubuntu_image_name = "Ubuntu 18.04 - Bionic Beaver - 64-bit - Cloud Based Image"
  39. #UBUNTU_IMAGE_NAME = "auto-sync/ubuntu-jammy-22.04-amd64-server-20240319-disk1.img"
  40. UBUNTU_IMAGE_NAME = "ubuntu-22.04-jammy-x86_64"
  41. # The public key to be used for SSH connection, please make sure, that you have the
  42. # corresponding private key
  43. #
  44. # id_rsa.pub should look like this (standard sshd pubkey format):
  45. # ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAw+J...F3w2mleybgT1w== user@HOSTNAME
  46. KEYPAIR_NAME = 'srieger-pub'
  47. PUB_KEY_FILE = '~/.ssh/id_rsa.pub'
  48. FLAVOR_NAME = 'm1.small'
  49. # default region
  50. REGION_NAME = 'RegionOne'
  51. # domain to use, "default" for local accounts, formerly "hsfulda" for LDAP accounts etc.
  52. # domain_name = "default"
  53. def main(): # noqa: C901 pylint: disable=too-many-branches,too-many-statements,too-many-locals,missing-function-docstring
  54. ###########################################################################
  55. #
  56. # get credentials
  57. #
  58. ###########################################################################
  59. # if "OS_PASSWORD" in os.environ:
  60. # auth_password = os.environ["OS_PASSWORD"]
  61. # else:
  62. # auth_password = getpass.getpass("Enter your OpenStack password:")
  63. auth_password = "demo"
  64. ###########################################################################
  65. #
  66. # create connection
  67. #
  68. ###########################################################################
  69. provider = get_driver(Provider.OPENSTACK)
  70. conn = provider(AUTH_USERNAME,
  71. auth_password,
  72. ex_force_auth_url=AUTH_URL,
  73. ex_force_auth_version='3.x_password',
  74. ex_tenant_name=PROJECT_NAME,
  75. ex_force_service_region=REGION_NAME)
  76. # ex_domain_name=domain_name)
  77. ###########################################################################
  78. #
  79. # get image, flavor, network for instance creation
  80. #
  81. ###########################################################################
  82. images = conn.list_images()
  83. image = ''
  84. for img in images:
  85. if img.name == UBUNTU_IMAGE_NAME:
  86. image = img
  87. flavors = conn.list_sizes()
  88. flavor = ''
  89. for flav in flavors:
  90. if flav.name == FLAVOR_NAME:
  91. flavor = conn.ex_get_size(flav.id)
  92. networks = conn.ex_list_networks()
  93. network = ''
  94. for net in networks:
  95. if net.name == PROJECT_NETWORK:
  96. network = net
  97. ###########################################################################
  98. #
  99. # get fixed a ip for service and api instance
  100. # (better would be shared IP for the cluster etc.)
  101. #
  102. ###########################################################################
  103. # find service instance
  104. for instance in conn.list_nodes():
  105. if instance.name == 'app-services':
  106. services_ip = instance.private_ips[0]
  107. print(('Found app-services fixed IP to be: ', services_ip))
  108. if instance.name == 'app-api-1':
  109. api_1_ip = instance.private_ips[0]
  110. print(('Found app-api-1 fixed IP to be: ', api_1_ip))
  111. ###########################################################################
  112. #
  113. # create keypair dependency
  114. #
  115. ###########################################################################
  116. print('Checking for existing SSH key pair...')
  117. keypair_exists = False
  118. for keypair in conn.list_key_pairs():
  119. if keypair.name == KEYPAIR_NAME:
  120. keypair_exists = True
  121. if keypair_exists:
  122. print(('Keypair ' + KEYPAIR_NAME + ' already exists. Skipping import.'))
  123. else:
  124. print('adding keypair...')
  125. conn.import_key_pair_from_file(KEYPAIR_NAME, PUB_KEY_FILE)
  126. for keypair in conn.list_key_pairs():
  127. print(keypair)
  128. ###########################################################################
  129. #
  130. # create security group dependency
  131. #
  132. ###########################################################################
  133. def get_security_group(connection, security_group_name):
  134. """A helper function to check if security group already exists"""
  135. print(('Checking for existing ' + security_group_name + ' security group...'))
  136. for security_grp in connection.ex_list_security_groups():
  137. if security_grp.name == security_group_name:
  138. print(('Security Group ' + security_group_name +
  139. ' already exists. Skipping creation.'))
  140. return security_grp
  141. return False
  142. if not get_security_group(conn, "worker"):
  143. worker_security_group = conn.ex_create_security_group(
  144. 'worker', 'for services that run on a worker node')
  145. conn.ex_create_security_group_rule(worker_security_group, 'TCP', 22, 22)
  146. else:
  147. worker_security_group = get_security_group(conn, "worker")
  148. for security_group in conn.ex_list_security_groups():
  149. print(security_group)
  150. ###########################################################################
  151. #
  152. # create worker instances
  153. #
  154. ###########################################################################
  155. userdata_worker = '#!/usr/bin/env bash\n' \
  156. 'curl -L -s https://gogs.informatik.hs-fulda.de/srieger/' \
  157. 'cloud-computing-msc-ai-examples/raw/master/faafo/contrib/' \
  158. 'install.sh | bash -s -- ' \
  159. f'-i faafo -r worker -e "http://{api_1_ip}" '\
  160. f'-m "amqp://faafo:guest@{services_ip}:5672/"'
  161. print('\nUsing cloud-init userdata for worker:\n"' + userdata_worker + '"\n')
  162. # userdata-api-2 = '''#!/usr/bin/env bash
  163. # curl -L -s ''' + hsfd_faafo_cloud_init_script + ''' | bash -s -- \
  164. # -i faafo -r worker -e 'http://%(api_2_ip)s' -m 'amqp://faafo:guest@%(services_ip)s:5672/'
  165. # ''' % {'api_2_ip': api_2_ip, 'services_ip': services_ip}
  166. print('Starting new app-worker-3 instance and wait until it is running...')
  167. instance_worker_3 = conn.create_node(name='app-worker-3',
  168. image=image, size=flavor,
  169. networks=[network],
  170. ex_keyname=KEYPAIR_NAME,
  171. ex_userdata=userdata_worker,
  172. ex_security_groups=[worker_security_group])
  173. print(instance_worker_3)
  174. if __name__ == '__main__':
  175. main()