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.

193 lines
7.2 KiB

  1. #Python 2 to Python 3 conversion made by "Usama Tahir"
  2. # import getpass
  3. # import os
  4. # import libcloud.security
  5. import time
  6. from libcloud.compute.providers import get_driver
  7. from libcloud.compute.types import Provider
  8. # reqs:
  9. # services: nova, glance, neutron
  10. # resources: 2 instances (m1.small), 2 floating ips (1 keypair, 2 security groups)
  11. # Please use 1-29 for X in the following variable to specify your group number. (will be used for the username,
  12. # project etc., as coordinated in the lab sessions)
  13. group_number = 30
  14. # web service endpoint of the private cloud infrastructure
  15. auth_url = 'https://private-cloud.informatik.hs-fulda.de:5000'
  16. # your username in OpenStack
  17. auth_username = 'CloudComp' + str(group_number)
  18. # your project in OpenStack
  19. project_name = 'CloudComp' + str(group_number)
  20. # A network in the project the started instance will be attached to
  21. project_network = 'CloudComp' + str(group_number) + '-net'
  22. # The image to look for and use for the started instance
  23. ubuntu_image_name = "Ubuntu 18.04 - Bionic Beaver - 64-bit - Cloud Based Image"
  24. # TODO: Ubuntu >18.04 would require major updates to faafo example again/better option: complete rewrite of example?
  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 = '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. userdata_worker = '''#!/usr/bin/env bash
  138. curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh | bash -s -- \
  139. -i faafo -r worker -e 'http://%(api_1_ip)s' -m 'amqp://faafo:guest@%(services_ip)s:5672/'
  140. ''' % {'api_1_ip': api_1_ip, 'services_ip': services_ip}
  141. # userdata-api-2 = '''#!/usr/bin/env bash
  142. # curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh | bash -s -- \
  143. # -i faafo -r worker -e 'http://%(api_2_ip)s' -m 'amqp://faafo:guest@%(services_ip)s:5672/'
  144. # ''' % {'api_2_ip': api_2_ip, 'services_ip': services_ip}
  145. print('Starting new app-worker-3 instance and wait until it is running...')
  146. instance_worker_3 = conn.create_node(name='app-worker-3',
  147. image=image, size=flavor,
  148. networks=[network],
  149. ex_keyname=keypair_name,
  150. ex_userdata=userdata_worker,
  151. ex_security_groups=[worker_security_group])
  152. if __name__ == '__main__':
  153. main()