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.

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