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.

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