Пример #1
0
def f_start(ec2, vpc, objects):
    # Create list of objects to print
    instance_list, instance_print, vpn_list, vpn_print = c_object_list(objects)

    if instance_print:
        # Start instance
        print('Instance Starting Process >>>')
        instances = ec2.get_all_instances()
        for inst in instances:
            if inst.instances[0].id in instance_list or len(instance_list[0]) == 0:
                if inst.instances[0].state == 'running':
                    print('ID: ' + inst.instances[0].id + ' is already running.')
                elif inst.instances[0].state == 'stopped':
                    ec2.start_instances(instance_ids=inst.instances[0].id)
                    print('ID: ' + inst.instances[0].id + ' has been started.')
                else:
                    print('ID: ' + inst.instances[0].id + ' is under the ' + inst.instances[0].state + ' state.')

    if vpn_print:
        # Create vpn (The list of vpn IDs is irrelevant in this case as they are being created)
        print('\n' + 'Vpn Starting Process >>>')
        vpn_available = False
        vpns = vpc.get_all_vpn_connections()
        for v in vpns:
            if v.vpn_gateway_id == 'vgw-2271ac3c' and v.state == 'available':
                vpn_available = True
                print('ID: ' + v.id + ' is already available.')
                break

        if not vpn_available:
            vpc.create_vpn_connection('ipsec.1', 'cgw-5172af4f', 'vgw-2271ac3c', static_routes_only=True, dry_run=False)
            # Wait 30s before start trying to set the static route
            time.sleep(30)
            for tries in range(45):     # This give a maximum waiting time of 300s
                vpns_retry = vpc.get_all_vpn_connections()
                for v_retry in vpns_retry:
                    if v_retry.vpn_gateway_id == 'vgw-2271ac3c' and v_retry.state == 'available':
                        vpn_available = True
                        print('ID: ' + v_retry.id + ' is available.')
                        # Create static route for the local network
                        vpc.create_vpn_connection_route('10.0.0.0/16', v_retry.id, dry_run=False)
                        # Exit for loop
                        break

                if vpn_available:
                    # Exit for loop
                    break
                else:
                    # Sleep 6s before another try
                    time.sleep(6)
    return
Пример #2
0
def f_stop(ec2, vpc, objects):
    # Create list of objects to print
    instance_list, instance_print, vpn_list, vpn_print = c_object_list(objects)

    if instance_print:
        # Stop instance
        print('Instance Stopping Process >>>')
        instances = ec2.get_all_instances()
        for inst in instances:
            if inst.instances[0].id in instance_list or len(instance_list[0]) == 0:
                if inst.instances[0].state == 'running':
                    ec2.stop_instances(instance_ids=inst.instances[0].id)
                    print('ID: ' + inst.instances[0].id + ' has been stopped.')
                elif inst.instances[0].state == 'stopped':
                    print('ID: ' + inst.instances[0].id + ' is already stopped.')
                else:
                    print('ID: ' + inst.instances[0].id + ' is under the ' + inst.instances[0].state + ' state.')

    if vpn_print:
        # Print vpn information
        print('\n' + 'Vpn Stopping Process >>>')
        vpns = vpc.get_all_vpn_connections()
        for v in vpns:
            if v.state == 'available':
                vpc.delete_vpn_connection(v.id, dry_run=False)
                print('ID: ' + v.id + ' has been deleted.')
            elif v.state == 'deleted':
                print('ID: ' + v.id + ' is already deleted.')
            else:
                print('ID: ' + v.id + ' is under the ' + v.state + ' state.')

    return
Пример #3
0
def f_status(ec2, vpc, objects):
    # Create list of objects to print
    instance_list, instance_print, vpn_list, vpn_print = c_object_list(objects)

    if instance_print:
        # Print instance information
        print('Instance Status >>>')
        instances = ec2.get_all_instances()
        for inst in instances:
            if inst.instances[0].id in instance_list or len(instance_list[0]) == 0:
                print('ID: ' + inst.instances[0].id + '\t\t\t\t' + 'State: ' + inst.instances[0].state)

    if vpn_print:
        # Print vpn information
        print('\n' + 'Vpn Status >>>')
        vpns = vpc.get_all_vpn_connections()
        for v in vpns:
            print('ID: ' + v.id + '\t\t\t' + 'State: ' + v.state)

    return