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.

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