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.

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