Ejemplo n.º 1
0
def start_ec2():
    """
    This code is from Amazon's EC2 example.
    Do a dryrun first to verify permissions.
    Try to start the EC2 instance.
    """
    print("------------------------------")
    print("Try to start the EC2 instance.")
    print("------------------------------")

    try:
        print("Start dry run...")
        ec2.start_instances(InstanceIds=[Mem.instance_id], DryRun=True)
    except ClientError as e:
        if 'DryRunOperation' not in str(e):
            raise

    # Dry run succeeded, run start_instances without dryrun
    try:
        print("Start instance without dry run...")
        response = ec2.start_instances(InstanceIds=[Mem.instance_id], DryRun=False)
        print(response)
        fetch_public_ip()
    except ClientError as e:
        print(e)
def startInstance(I_NAME):
    ec2 = boto3.client('ec2')
    resource = boto3.resource('ec2')
    print("Starting the instance...")
    RI_ID = None
    instance = [
        i for i in resource.instances.filter(Filters=[{
            'Name': 'tag:Name',
            'Values': [I_NAME]
        }, {
            'Name': 'instance-state-name',
            'Values': ['stopped']
        }])
    ]
    for i in instance:
        RI_ID = i.id
    if RI_ID != None:
        print("\nPlease wait while " + I_NAME + " is being started")

    else:
        print("\nSeems somthing is wrong..failed..")
        sys.exit(1)

    # change instance ID appropriately

    try:
        ec2.start_instances(InstanceIds=[
            RI_ID,
        ], DryRun=False)
    except:
        print("\nFailed because of wrong input. Please recheck and try")
        sys.exit(1)

    for server in instance:
        while server.state['Name'] not in ('running'):
            print("Please wait...")
            time.sleep(5)
            server.load()
        print('\nInstance is ready to use. Login using private ip: ',
              server.private_ip_address)
Ejemplo n.º 3
0
def main():
    # read arguments from the command line and
    # check whether at least two elements were entered
    if len(sys.argv) < 2:
	print ("Usage: python aws.py {start|stop}\n")
	sys.exit(0)
    else:
	action = sys.argv[1]

    if action == "start":
	startInstance()
    elif action == "stop":
    	stopInstance()
    else:
    	print ("Usage: python aws.py {start|stop}\n"

def startInstance():
    print ("Starting the instance...")

    # change "eu-west-1" region if different
    try:
        ec2 = boto.ec2.connect_to_region("eu-west-1", **auth)

    except Exception, e1:
        error1 = "Error1: %s" % str(e1)
        print(error1)
        sys.exit(0)

    # change instance ID appropriately
    try:
         ec2.start_instances(instance_ids="i-12345678")

    except Exception, e2:
        error2 = "Error2: %s" % str(e2)
        print(error2)
        sys.exit(0)

def stopInstance():
    print "Stopping the instance..."

    try:
        ec2 = boto.ec2.connect_to_region("eu-west-1", **auth)

    except Exception, e1:
        error1 = "Error1: %s" % str(e1)
        print(error1)
        sys.exit(0)

    try:
         ec2.stop_instances(instance_ids="i-12345678")# /usr/bin/python2.7
# written by Tomas (www.lisenet.com) on 05/11/2012
# copyleft free software

import boto.ec2
import sys

# specify AWS keys
auth = {"aws_access_key_id": "<key_id>", "aws_secret_access_key": "<access_key>"}

def main():
    # read arguments from the command line and
    # check whether at least two elements were entered
    if len(sys.argv) < 2:
	print "Usage: python aws.py {start|stop}\n"
	sys.exit(0)
    else:
	action = sys.argv[1]

    if action == "start":
	startInstance()
    elif action == "stop":
    	stopInstance()
    else:
    	print "Usage: python aws.py {start|stop}\n"

def startInstance():
    print "Starting the instance..."

    # change "eu-west-1" region if different
    try:
        ec2 = boto.ec2.connect_to_region("eu-west-1", **auth)

    except Exception, e1:
        error1 = "Error1: %s" % str(e1)
        print(error1)
        sys.exit(0)

    # change instance ID appropriately
    try:
         ec2.start_instances(instance_ids="i-12345678")

    except Exception, e2:
        error2 = "Error2: %s" % str(e2)
        print(error2)
        sys.exit(0)

def stopInstance():
    print "Stopping the instance..."

    try:
        ec2 = boto.ec2.connect_to_region("eu-west-1", **auth)

    except Exception, e1:
        error1 = "Error1: %s" % str(e1)
        print(error1)
        sys.exit(0)

    try:
         ec2.stop_instances(instance_ids="i-12345678")

    except Exception, e2:
        error2 = "Error2: %s" % str(e2)
        print(error2)
        sys.exit(0)

if __name__ == '__main__':
    main()

    except Exception, e2:
        error2 = "Error2: %s" % str(e2)
        print(error2)
        sys.exit(0)

if __name__ == '__main__':
    main()
Ejemplo n.º 4
0
import boto3.ec2
from botocore.exceptions import ClientError

ec2 = boto3.client('ec2')

try:
    instanceid =''
    response = ec2.start_instances(InstanceIds=instanceid, DryRun=False)
    print(response)
except ClientError as e:
    print(e)