|
|
import getpass # import os # import libcloud.security
import time from libcloud.compute.providers import get_driver from libcloud.compute.types import Provider, NodeState
# reqs: # services: EC2 (nova, glance, neutron) # resources: 2 instances (m1.small), 2 elastic ips (1 keypair, 2 security groups)
# default region # region_name = 'eu-central-1' # region_name = 'ap-south-1'
# AWS Educate only allows us-east-1 see our AWS classroom at https://www.awseducate.com # e.g., https://www.awseducate.com/student/s/launch-classroom?classroomId=a1v3m000005mNm6AAE
region_name = 'us-east-1'
def main(): ########################################################################### # # get credentials # ###########################################################################
# see AWS Educate classroom, Account Details
# access_id = getpass.win_getpass("Enter your access_id:") # secret_key = getpass.win_getpass("Enter your secret_key:") # session_token = getpass.win_getpass("Enter your session_token:") access_id = "ASIAX..." secret_key = "eGwE12j..." session_token = "FwoGZXIvYXdzEK///////////wEaDE..."
########################################################################### # # create connection # ###########################################################################
provider = get_driver(Provider.EC2) conn = provider(access_id, secret_key, token=session_token, region=region_name)
########################################################################### # # clean up resources from previous demos # ###########################################################################
# destroy running demo instances for instance in conn.list_nodes(): if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-worker-3', 'app-controller', 'app-services', 'app-api-1', 'app-api-2']: print('Destroying Instance: %s' % instance.name) conn.destroy_node(instance)
# wait until all nodes are destroyed to be able to remove depended security groups nodes_still_running = True while nodes_still_running: nodes_still_running = False time.sleep(3) instances = conn.list_nodes() for instance in instances: # if we see any demo instances still running continue to wait for them to stop if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-worker-3', 'app-controller', 'app-services', 'app-api-1', 'app-api-2']: if instance.state.value != 'terminated': nodes_still_running = True print('There are still instances running, waiting for them to be destroyed...')
# delete security groups for group in conn.ex_list_security_groups(): if group in ['control', 'worker', 'api', 'services']: print('Deleting security group: %s' % group) conn.ex_delete_security_group(group)
# release elastic ips for elastic_ip in conn.ex_describe_all_addresses(): if elastic_ip.instance_id is None: print('Releasing unused elastic ip %s' % elastic_ip) conn.ex_release_address(elastic_ip, domain=elastic_ip.domain)
if __name__ == '__main__': main()
|