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.

196 lines
7.3 KiB

  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 = X
  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 = "Ubuntu 22.04 - Jammy Jellyfish - 64-bit - Cloud Based Image"
  24. # The public key to be used for SSH connection, please make sure, that you have the corresponding private key
  25. #
  26. # id_rsa.pub should look like this (standard sshd pubkey format):
  27. # ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAw+J...F3w2mleybgT1w== user@HOSTNAME
  28. #keypair_name = 'CloudComp30-keypair'
  29. keypair_name = "srieger-pub"
  30. pub_key_file = '~/.ssh/id_rsa.pub'
  31. flavor_name = 'm1.small'
  32. # default region
  33. region_name = 'RegionOne'
  34. # domain to use, "default" for local accounts, "hsfulda" for RZ LDAP, e.g., using fdaiXXXX as auth_username
  35. domain_name = "default"
  36. def main():
  37. ###########################################################################
  38. #
  39. # get credentials
  40. #
  41. ###########################################################################
  42. # if "OS_PASSWORD" in os.environ:
  43. # auth_password = os.environ["OS_PASSWORD"]
  44. # else:
  45. # auth_password = getpass.getpass("Enter your OpenStack password:")
  46. auth_password = "demo"
  47. ###########################################################################
  48. #
  49. # create connection
  50. #
  51. ###########################################################################
  52. # libcloud.security.VERIFY_SSL_CERT = False
  53. provider = get_driver(Provider.OPENSTACK)
  54. conn = provider(auth_username,
  55. auth_password,
  56. ex_force_auth_url=auth_url,
  57. ex_force_auth_version='3.x_password',
  58. ex_tenant_name=project_name,
  59. ex_force_service_region=region_name,
  60. ex_domain_name=domain_name)
  61. ###########################################################################
  62. #
  63. # get image, flavor, network for instance creation
  64. #
  65. ###########################################################################
  66. images = conn.list_images()
  67. image = ''
  68. for img in images:
  69. if img.name == ubuntu_image_name:
  70. image = img
  71. flavors = conn.list_sizes()
  72. flavor = ''
  73. for flav in flavors:
  74. if flav.name == flavor_name:
  75. flavor = conn.ex_get_size(flav.id)
  76. networks = conn.ex_list_networks()
  77. network = ''
  78. for net in networks:
  79. if net.name == project_network:
  80. network = net
  81. ###########################################################################
  82. #
  83. # get fixed a ip for service and api instance
  84. # (better would be shared IP for the cluster etc.)
  85. #
  86. ###########################################################################
  87. # find service instance
  88. for instance in conn.list_nodes():
  89. if instance.name == 'app-services':
  90. services_ip = instance.private_ips[0]
  91. print(('Found app-services fixed IP to be: ', services_ip))
  92. if instance.name == 'app-api-1':
  93. api_1_ip = instance.private_ips[0]
  94. print(('Found app-api-1 fixed IP to be: ', api_1_ip))
  95. ###########################################################################
  96. #
  97. # create keypair dependency
  98. #
  99. ###########################################################################
  100. print('Checking for existing SSH key pair...')
  101. keypair_exists = False
  102. for keypair in conn.list_key_pairs():
  103. if keypair.name == keypair_name:
  104. keypair_exists = True
  105. if keypair_exists:
  106. print(('Keypair ' + keypair_name + ' already exists. Skipping import.'))
  107. else:
  108. print('adding keypair...')
  109. conn.import_key_pair_from_file(keypair_name, pub_key_file)
  110. for keypair in conn.list_key_pairs():
  111. print(keypair)
  112. ###########################################################################
  113. #
  114. # create security group dependency
  115. #
  116. ###########################################################################
  117. def get_security_group(connection, security_group_name):
  118. """A helper function to check if security group already exists"""
  119. print(('Checking for existing ' + security_group_name + ' security group...'))
  120. for security_grp in connection.ex_list_security_groups():
  121. if security_grp.name == security_group_name:
  122. print(('Security Group ' + security_group_name + ' already exists. Skipping creation.'))
  123. return security_grp
  124. return False
  125. if not get_security_group(conn, "worker"):
  126. worker_security_group = conn.ex_create_security_group('worker', 'for services that run on a worker node')
  127. conn.ex_create_security_group_rule(worker_security_group, 'TCP', 22, 22)
  128. else:
  129. worker_security_group = get_security_group(conn, "worker")
  130. for security_group in conn.ex_list_security_groups():
  131. print(security_group)
  132. ###########################################################################
  133. #
  134. # create worker instances
  135. #
  136. ###########################################################################
  137. #hsfd_faafo_cloud_init_script = 'https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh'
  138. # testing / faafo dev branch:
  139. 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'
  140. userdata_worker = '''#!/usr/bin/env bash
  141. curl -L -s ''' + hsfd_faafo_cloud_init_script + ''' | bash -s -- \
  142. -i faafo -r worker -e 'http://%(api_1_ip)s' -m 'amqp://faafo:guest@%(services_ip)s:5672/'
  143. ''' % {'api_1_ip': api_1_ip, 'services_ip': services_ip}
  144. # userdata-api-2 = '''#!/usr/bin/env bash
  145. # curl -L -s ''' + hsfd_faafo_cloud_init_script + ''' | bash -s -- \
  146. # -i faafo -r worker -e 'http://%(api_2_ip)s' -m 'amqp://faafo:guest@%(services_ip)s:5672/'
  147. # ''' % {'api_2_ip': api_2_ip, 'services_ip': services_ip}
  148. print('Starting new app-worker-3 instance and wait until it is running...')
  149. instance_worker_3 = conn.create_node(name='app-worker-3',
  150. image=image, size=flavor,
  151. networks=[network],
  152. ex_keyname=keypair_name,
  153. ex_userdata=userdata_worker,
  154. ex_security_groups=[worker_security_group])
  155. print(instance_worker_3)
  156. if __name__ == '__main__':
  157. main()