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.

235 lines
9.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. # import getpass
  2. # import os
  3. from libcloud.compute.providers import get_driver
  4. from libcloud.compute.types import Provider
  5. # Please use 1-29 for X in the following variable to specify your group number. (will be used for the username,
  6. # project etc., as coordinated in the lab sessions)
  7. group_number = X
  8. # web service endpoint of the private cloud infrastructure
  9. auth_url = 'https://private-cloud.informatik.hs-fulda.de:5000'
  10. # your username in OpenStack
  11. auth_username = 'CloudComp' + str(group_number)
  12. # your project in OpenStack
  13. project_name = 'CloudComp' + str(group_number)
  14. # A network in the project the started instance will be attached to
  15. project_network = 'CloudComp' + str(group_number) + '-net'
  16. # The image to look for and use for the started instance
  17. #ubuntu_image_name = "Ubuntu 18.04 - Bionic Beaver - 64-bit - Cloud Based Image"
  18. ubuntu_image_name = "Ubuntu 20.04 - Focal Fossa - 64-bit - Cloud Based Image"
  19. # The public key to be used for SSH connection, please make sure, that you have the corresponding private key
  20. #
  21. # id_rsa.pub should look like this (standard sshd pubkey format):
  22. # ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAw+J...F3w2mleybgT1w== user@HOSTNAME
  23. #keypair_name = 'CloudComp30-keypair'
  24. keypair_name = "srieger-pub"
  25. pub_key_file = '~/.ssh/id_rsa.pub'
  26. flavor_name = 'm1.small'
  27. # default region
  28. region_name = 'RegionOne'
  29. # domain to use, "default" for local accounts, "hsfulda" for RZ LDAP, e.g., using fdaiXXXX as auth_username
  30. # domain_name = "default"
  31. def main():
  32. ###########################################################################
  33. #
  34. # get credentials
  35. #
  36. ###########################################################################
  37. # if "OS_PASSWORD" in os.environ:
  38. # auth_password = os.environ["OS_PASSWORD"]
  39. # else:
  40. # auth_password = getpass.getpass("Enter your OpenStack password:")
  41. auth_password = "demo"
  42. ###########################################################################
  43. #
  44. # create connection
  45. #
  46. ###########################################################################
  47. provider = get_driver(Provider.OPENSTACK)
  48. conn = provider(auth_username,
  49. auth_password,
  50. ex_force_auth_url=auth_url,
  51. ex_force_auth_version='3.x_password',
  52. ex_tenant_name=project_name,
  53. ex_force_service_region=region_name)
  54. # ex_domain_name=domain_name)
  55. ###########################################################################
  56. #
  57. # get image, flavor, network for instance creation
  58. #
  59. ###########################################################################
  60. images = conn.list_images()
  61. image = ''
  62. for img in images:
  63. if img.name == ubuntu_image_name:
  64. image = img
  65. flavors = conn.list_sizes()
  66. flavor = ''
  67. for flav in flavors:
  68. if flav.name == flavor_name:
  69. flavor = conn.ex_get_size(flav.id)
  70. networks = conn.ex_list_networks()
  71. network = ''
  72. for net in networks:
  73. if net.name == project_network:
  74. network = net
  75. ###########################################################################
  76. #
  77. # create keypair dependency
  78. #
  79. ###########################################################################
  80. print('Checking for existing SSH key pair...')
  81. keypair_exists = False
  82. for keypair in conn.list_key_pairs():
  83. if keypair.name == keypair_name:
  84. keypair_exists = True
  85. if keypair_exists:
  86. print(('Keypair ' + keypair_name + ' already exists. Skipping import.'))
  87. else:
  88. print('adding keypair...')
  89. conn.import_key_pair_from_file(keypair_name, pub_key_file)
  90. for keypair in conn.list_key_pairs():
  91. print(keypair)
  92. ###########################################################################
  93. #
  94. # create security group dependency
  95. #
  96. ###########################################################################
  97. print('Checking for existing security group...')
  98. security_group_name = 'all-in-one'
  99. security_group_exists = False
  100. all_in_one_security_group = ''
  101. for security_group in conn.ex_list_security_groups():
  102. if security_group.name == security_group_name:
  103. all_in_one_security_group = security_group
  104. security_group_exists = True
  105. if security_group_exists:
  106. print(('Security Group ' + all_in_one_security_group.name + ' already exists. Skipping creation.'))
  107. else:
  108. all_in_one_security_group = conn.ex_create_security_group(security_group_name,
  109. 'network access for all-in-one application.')
  110. conn.ex_create_security_group_rule(all_in_one_security_group, 'TCP', 80, 80)
  111. conn.ex_create_security_group_rule(all_in_one_security_group, 'TCP', 22, 22)
  112. for security_group in conn.ex_list_security_groups():
  113. print(security_group)
  114. ###########################################################################
  115. #
  116. # create all-in-one instance
  117. #
  118. ###########################################################################
  119. #hsfd_faafo_cloud_init_script = 'https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install.sh'
  120. # testing / faafo dev branch:
  121. 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'
  122. userdata = '''#!/usr/bin/env bash
  123. curl -L -s ''' + hsfd_faafo_cloud_init_script + ''' | bash -s -- \
  124. -i faafo -i messaging -r api -r worker -r demo
  125. '''
  126. print('Checking for existing instance...')
  127. instance_name = 'all-in-one'
  128. instance_exists = False
  129. testing_instance = ''
  130. for instance in conn.list_nodes():
  131. if instance.name == instance_name:
  132. testing_instance = instance
  133. instance_exists = True
  134. if instance_exists:
  135. print(('Instance ' + testing_instance.name + ' already exists. Skipping creation.'))
  136. exit()
  137. else:
  138. print('Starting new all-in-one instance and wait until it is running...')
  139. testing_instance = conn.create_node(name=instance_name,
  140. image=image,
  141. size=flavor,
  142. networks=[network],
  143. ex_keyname=keypair_name,
  144. ex_userdata=userdata,
  145. ex_security_groups=[all_in_one_security_group])
  146. conn.wait_until_running(nodes=[testing_instance], timeout=120, ssh_interface='private_ips')
  147. ###########################################################################
  148. #
  149. # assign all-in-one instance floating ip
  150. #
  151. ###########################################################################
  152. private_ip = None
  153. if len(testing_instance.private_ips):
  154. private_ip = testing_instance.private_ips[0]
  155. print(('Private IP found: {}'.format(private_ip)))
  156. public_ip = None
  157. if len(testing_instance.public_ips):
  158. public_ip = testing_instance.public_ips[0]
  159. print(('Public IP found: {}'.format(public_ip)))
  160. print('Checking for unused Floating IP...')
  161. unused_floating_ip = None
  162. for floating_ip in conn.ex_list_floating_ips():
  163. if not floating_ip.node_id:
  164. unused_floating_ip = floating_ip
  165. break
  166. if not unused_floating_ip and len(conn.ex_list_floating_ip_pools()):
  167. pool = conn.ex_list_floating_ip_pools()[0]
  168. print(('Allocating new Floating IP from pool: {}'.format(pool)))
  169. unused_floating_ip = pool.create_floating_ip()
  170. if public_ip:
  171. print(('Instance ' + testing_instance.name + ' already has a public ip. Skipping attachment.'))
  172. elif unused_floating_ip:
  173. conn.ex_attach_floating_ip_to_node(testing_instance, unused_floating_ip)
  174. actual_ip_address = None
  175. if public_ip:
  176. actual_ip_address = public_ip
  177. elif unused_floating_ip:
  178. actual_ip_address = unused_floating_ip.ip_address
  179. elif private_ip:
  180. actual_ip_address = private_ip
  181. print('\n')
  182. print(('The Fractals app will be deployed to http://{}\n'.format(actual_ip_address)))
  183. print('You can use ssh to login to the instance using your private key. Default user name for official Ubuntu\n'
  184. 'Cloud Images is: ubuntu, so you can use, e.g.: "ssh -i ~/.ssh/id_rsa ubuntu@<floating-ip>" if your private\n'
  185. 'key is in the default location.\n\n'
  186. 'After login, you can list or "ssh ubuntu@<floating-ip>" available fractals using "faafo list". To request\n'
  187. 'the generation of new fractals, you can use "faafo create".\n\n'
  188. 'You can also see other options to use the faafo example cloud service using "faafo -h".\n\n'
  189. 'If you cannot start faafo command and/or do not see the webpage, you can check the Instance Console Log of\n'
  190. 'the instance, e.g., in OpenStack web interface.')
  191. if __name__ == '__main__':
  192. main()