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.

307 lines
14 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import configparser
  2. from os.path import expanduser
  3. from libcloud.compute.base import NodeImage
  4. from libcloud.compute.providers import get_driver
  5. from libcloud.compute.types import Provider
  6. home = expanduser("~")
  7. # requirements:
  8. # services: EC2
  9. # resources: 2 instances (1 keypair, 2 security groups)
  10. # optionally also elastic ip (comparable to floating ip) offering a persistent public
  11. # IP, but elastic ips are expensive - make sure to delete them after you used them
  12. # The image to look for and use for the started instance
  13. ubuntu_image_name = 'ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-20210128'
  14. # TODO: 18.04, currently still needed for faafo, need to port faafo demo app to 20.04 or higher and python3...
  15. # The public key to be used for SSH connection, please make sure, that you have the corresponding private key
  16. #
  17. # id_rsa.pub should look like this (standard sshd pubkey format):
  18. # ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAw+J...F3w2mleybgT1w== user@HOSTNAME
  19. keypair_name = 'srieger-pub'
  20. pub_key_file = home + '/.ssh/id_rsa.pub'
  21. flavor_name = 't2.nano'
  22. # default region
  23. # region_name = 'eu-central-1'
  24. # region_name = 'ap-south-1'
  25. # AWS Academy Labs only allow us-east-1 see our AWS Academy Lab Guide, https://awsacademy.instructure.com/login/
  26. region_name = 'us-east-1'
  27. # starting instances in AWS Academy takes significantly longer compared to paid AWS accounts, allow ~ >2 minutes timeout
  28. timeout = 600
  29. def main():
  30. ###########################################################################
  31. #
  32. # get credentials
  33. #
  34. ###########################################################################
  35. # see AWS Academy Lab for Account Details
  36. # read credentials from file
  37. config = configparser.ConfigParser()
  38. config.read_file(open(home + '/.aws/credentials'))
  39. aws_access_key_id = config['default']['aws_access_key_id']
  40. aws_secret_access_key = config['default']['aws_secret_access_key']
  41. aws_session_token = config['default']['aws_session_token']
  42. # hard coded AWS credentials using vars
  43. # aws_access_key_id = "ASIAX..."
  44. # aws_secret_access_key = "WLxxXK+..."
  45. # aws_session_token = "FwoGZXIvYXdzEMb//////////wEaDE5rX.......0SleZ+L75I9iEri9LA4hovWul8HvexhCBK8.......................Ae/T+VkUbcQRtJEDwg+gYCABuk0JlSj5Wk7YA65r3BSNJXZFpkhbek6VBjvE/cEt5fKZEhENcdFxjAcAJLd6bOWi/oGXU5e3PX3mcXgm0oJpz6h3wqD1LvSDtw5GDwn0BHiF1Mu.......................cm/VukK5F"
  46. ###########################################################################
  47. #
  48. # create connection
  49. #
  50. ###########################################################################
  51. provider = get_driver(Provider.EC2)
  52. conn = provider(aws_access_key_id,
  53. aws_secret_access_key,
  54. token=aws_session_token,
  55. region=region_name)
  56. ###########################################################################
  57. #
  58. # get image, flavor, network for instance creation
  59. #
  60. ###########################################################################
  61. print("Search for AMI...")
  62. image = conn.list_images(ex_filters={"name": ubuntu_image_name})[0]
  63. print("Using image: %s" % image)
  64. # print("Fetching images (AMI) list from AWS region. This will take a lot of seconds (AWS has a very long list of "
  65. # "supported operating systems and versions)... please be patient...")
  66. # image = ''
  67. # for img in images:
  68. # # if img.name == ubuntu_image_name:
  69. # if img.extra['owner_alias'] == 'amazon':
  70. # print(img)
  71. # if img.id == ubuntu_image_name:
  72. # image = img
  73. # fetch/select the image referenced with ubuntu_image_name above
  74. # image = [i for i in images if i.name == ubuntu_image_name][0]
  75. # print(image)
  76. # select image directly to save time, as retrieving the image list takes several minutes now,
  77. # need to change ami id here if updated or for other regions, id is working for course in
  78. # summer term 2022, in region: us-east-1 and pointing to ubuntu 18.04 used in the instance wizard,
  79. # to update AMI id use the create instance wizard and copy amd64 image id for ubuntu 18.04 in the
  80. # desired region
  81. # image = NodeImage(id="ami-0e472ba40eb589f49",
  82. # name=ubuntu_image_name,
  83. # driver="hvm")
  84. flavors = conn.list_sizes()
  85. flavor = [s for s in flavors if s.id == flavor_name][0]
  86. print(flavor)
  87. # networks = conn.ex_list_networks()
  88. # network = ''
  89. # for net in networks:
  90. # if net.name == project_network:
  91. # network = net
  92. ###########################################################################
  93. #
  94. # create keypair dependency
  95. #
  96. ###########################################################################
  97. print('Checking for existing SSH key pair...')
  98. keypair_exists = False
  99. for keypair in conn.list_key_pairs():
  100. if keypair.name == keypair_name:
  101. keypair_exists = True
  102. if keypair_exists:
  103. print('Keypair ' + keypair_name + ' already exists. Skipping import.')
  104. else:
  105. print('adding keypair...')
  106. conn.import_key_pair_from_file(keypair_name, pub_key_file)
  107. for keypair in conn.list_key_pairs():
  108. print(keypair)
  109. ###########################################################################
  110. #
  111. # create security group dependency
  112. #
  113. ###########################################################################
  114. print('Checking for existing worker security group...')
  115. worker_security_group_exists = False
  116. worker_security_group_name = 'worker'
  117. for security_group in conn.ex_get_security_groups():
  118. if security_group.name == worker_security_group_name:
  119. worker_security_group_id = security_group.id
  120. worker_security_group_exists = True
  121. if worker_security_group_exists:
  122. print('Worker Security Group ' + worker_security_group_name + ' already exists. Skipping creation.')
  123. else:
  124. worker_security_group_result = conn.ex_create_security_group('worker', 'for services that run on a worker node')
  125. worker_security_group_id = worker_security_group_result['group_id']
  126. conn.ex_authorize_security_group_ingress(worker_security_group_id, 22, 22, cidr_ips=['0.0.0.0/0'],
  127. protocol='tcp')
  128. print('Checking for existing controller security group...')
  129. controller_security_group_exists = False
  130. controller_security_group_name = 'control'
  131. controller_security_group_id = ''
  132. for security_group in conn.ex_get_security_groups():
  133. if security_group.name == controller_security_group_name:
  134. controller_security_group_id = security_group.id
  135. controller_security_group_exists = True
  136. if controller_security_group_exists:
  137. print('Controller Security Group ' + controller_security_group_name + ' already exists. Skipping creation.')
  138. else:
  139. controller_security_group_result = conn.ex_create_security_group('control',
  140. 'for services that run on a control node')
  141. controller_security_group_id = controller_security_group_result['group_id']
  142. conn.ex_authorize_security_group_ingress(controller_security_group_id, 22, 22, cidr_ips=['0.0.0.0/0'],
  143. protocol='tcp')
  144. conn.ex_authorize_security_group_ingress(controller_security_group_id, 80, 80, cidr_ips=['0.0.0.0/0'],
  145. protocol='tcp')
  146. conn.ex_authorize_security_group_ingress(controller_security_group_id, 5672, 5672,
  147. group_pairs=[{'group_id': worker_security_group_id}], protocol='tcp')
  148. # for security_group in conn.ex_list_security_groups():
  149. # print(security_group)
  150. ###########################################################################
  151. #
  152. # create app-controller
  153. #
  154. ###########################################################################
  155. # https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh
  156. # is currently broken, hence the "rabbitctl" lines were added in the example
  157. # below, see also https://bugs.launchpad.net/faafo/+bug/1679710
  158. #
  159. # Thanks to Stefan Friedmann for finding this fix ;)
  160. userdata = '''#!/usr/bin/env bash
  161. curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install-aws.sh | bash -s -- \
  162. -i messaging -i faafo -r api
  163. rabbitmqctl add_user faafo guest
  164. rabbitmqctl set_user_tags faafo administrator
  165. rabbitmqctl set_permissions -p / faafo ".*" ".*" ".*"
  166. '''
  167. print('Starting new app-controller instance and wait until it is running (can take several minutes in free AWS'
  168. ' academy accounts compared to seconds when using a regular paid AWS account), timeout %i seconds...'
  169. % timeout)
  170. instance_controller_1 = conn.create_node(name='app-controller',
  171. image=image,
  172. size=flavor,
  173. ex_keyname=keypair_name,
  174. ex_userdata=userdata,
  175. ex_security_groups=[controller_security_group_name])
  176. wait_until_running_result = conn.wait_until_running(nodes=[instance_controller_1], timeout=timeout, ssh_interface='public_ips')
  177. instance_controller_1, node_addresses = wait_until_running_result[0]
  178. ###########################################################################
  179. #
  180. # assign app-controller elastic ip
  181. #
  182. ###########################################################################
  183. # AWS offers elastic ips, that have the same function as floating IPs in OpenStack. However, elastic IPs cost money,
  184. # and instances typically already have public IP in AWS, what a luxury ;) so I commented out elastic IP creation to
  185. # save your AWS academy budget
  186. # print('Checking for unused Elastic IP...')
  187. # unused_elastic_ip = None
  188. # for elastic_ip in conn.ex_describe_all_addresses():
  189. # if not elastic_ip.instance_id:
  190. # unused_elastic_ip = elastic_ip
  191. # break
  192. #
  193. # if not unused_elastic_ip:
  194. # print('Allocating new Elastic IP')
  195. # unused_elastic_ip = conn.ex_allocate_address()
  196. # conn.ex_associate_address_with_node(instance_controller_1, unused_elastic_ip)
  197. # print('Controller Application will be deployed to http://%s' % unused_elastic_ip.ip)
  198. ###########################################################################
  199. #
  200. # getting id and ip address of app-controller instance
  201. #
  202. ###########################################################################
  203. # instance_controller_1 = conn.list_nodes(ex_node_ids=instance_controller_1.id)
  204. public_ip_controller = instance_controller_1.public_ips[0]
  205. print('Controller Application %s will be reachable after cloud-init has run at http://%s, you can also connect'
  206. % (instance_controller_1.id, public_ip_controller) +
  207. ' via ssh ubuntu@%s' % public_ip_controller)
  208. # get private IP of instance to use in worker/service instances to connect to the controller
  209. private_ip_controller = instance_controller_1.private_ips[0]
  210. ###########################################################################
  211. #
  212. # create app-worker-1
  213. #
  214. ###########################################################################
  215. userdata = '''#!/usr/bin/env bash
  216. curl -L -s https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/faafo/contrib/install-aws.sh | bash -s -- \
  217. -i faafo -r worker -e 'http://%(ip_controller)s' -m 'amqp://faafo:guest@%(ip_controller)s:5672/'
  218. ''' % {'ip_controller': private_ip_controller}
  219. print('Starting new app-worker-1 instance and wait until it is running...')
  220. instance_worker_1 = conn.create_node(name='app-worker-1',
  221. image=image,
  222. size=flavor,
  223. ex_keyname=keypair_name,
  224. ex_userdata=userdata,
  225. ex_security_groups=[worker_security_group_name])
  226. wait_until_running_result = conn.wait_until_running(nodes=[instance_worker_1], timeout=timeout, ssh_interface='public_ips')
  227. instance_worker_1, node_addresses = wait_until_running_result[0]
  228. print('Worker instance %s has private ip: %s ' % (instance_worker_1.id, instance_worker_1.private_ips[0]))
  229. ###########################################################################
  230. #
  231. # assign app-worker elastic ip
  232. #
  233. ###########################################################################
  234. # AWS offers elastic ips, that have the same function as floating IPs in OpenStack. However, elastic IPs cost money,
  235. # and instances typically already have public IP in AWS, what a luxury ;) so I commented out elastic IP creation to
  236. # save your AWS academy budget
  237. # print('Checking for unused Elastic IP...')
  238. # unused_elastic_ip = None
  239. # for elastic_ip in conn.ex_describe_all_addresses():
  240. # if not elastic_ip.instance_id:
  241. # unused_elastic_ip = elastic_ip
  242. # break
  243. #
  244. # if not unused_elastic_ip:
  245. # print('Allocating new Elastic IP')
  246. # unused_elastic_ip = conn.ex_allocate_address()
  247. # conn.ex_associate_address_with_node(instance_worker_1, unused_elastic_ip)
  248. # print('The worker will be available for SSH at %s' % unused_elastic_ip.ip)
  249. #
  250. print('You can use ssh to login to the controller using your private key. After login, you can list available '
  251. 'fractals using "faafo list". To request the generation of new fractals, you can use "faafo create". '
  252. 'You can also see other options to use the faafo example cloud service using "faafo -h".')
  253. if __name__ == '__main__':
  254. main()