85 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			85 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
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 = 'us-east-2'
 | 
						|
#region_name = 'ap-south-1'
 | 
						|
 | 
						|
 | 
						|
def main():
 | 
						|
    ###########################################################################
 | 
						|
    #
 | 
						|
    # get credentials
 | 
						|
    #
 | 
						|
    ###########################################################################
 | 
						|
 | 
						|
    access_id = getpass.win_getpass("Enter your access_id:")
 | 
						|
    secret_key = getpass.win_getpass("Enter your secret_key:")
 | 
						|
    # access_id = "AXY..."
 | 
						|
    # secret_key = "j1zomy61..."
 | 
						|
 | 
						|
    ###########################################################################
 | 
						|
    #
 | 
						|
    # create connection
 | 
						|
    #
 | 
						|
    ###########################################################################
 | 
						|
 | 
						|
    provider = get_driver(Provider.EC2)
 | 
						|
    conn = provider(access_id,
 | 
						|
                    secret_key,
 | 
						|
                    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 is not '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()
 |