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.

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