Пример #1
0
def start_instance(conn, inst_id, debug=False, sleep=10, max_tries=50):
    """Starts up an existing EC2 instance given its id."""
    if debug:
        print 'starting a instance (id=%s)' % inst_id
        
    insts = conn.start_instances(instance_ids=[inst_id])
    inst = insts[0]
    
    check_inst_state(inst, u'running', debug=debug)
    if inst.state != u'running':
        raise RuntimeError("instance with id '%s' failed to start" % inst_id)
    
    if debug:
        print "instance at address '%s' is running" % inst.public_dns_name
        
    for i in range(max_tries):
        time.sleep(sleep)
        if debug: 
            print "testing ssh connection (try #%d)" % (i+1)
        # even though the instance is running, it takes a while before
        # sshd is running, so we have to wait a bit longer
        if ssh_test(inst.public_dns_name):
            break
    else:
        raise RuntimeError("instance '%s' ran but ssh connection attempts"
                           " failed (%d attempts)" % (inst_id, max_tries))

    return inst
Пример #2
0
def start_instance(conn, inst_id, debug=False, sleep=10, max_tries=50):
    """Starts up an existing EC2 instance given its id"""
    if debug:
        print 'starting a instance (id=%s)' % inst_id

    insts = conn.start_instances(instance_ids=[inst_id])
    inst = insts[0]

    check_inst_state(inst, u'running', debug=debug)
    if inst.state != u'running':
        raise RuntimeError("instance with id '%s' failed to start" % inst_id)

    if debug:
        print "instance at address '%s' is running" % inst.public_dns_name

    for i in range(max_tries):
        time.sleep(sleep)
        if debug:
            print "testing ssh connection (try #%d)" % (i + 1)
        # even though the instance is running, it takes a while before
        # sshd is running, so we have to wait a bit longer
        if ssh_test(inst.public_dns_name):
            break
    else:
        raise RuntimeError("instance '%s' ran but ssh connection attempts"
                           " failed (%d attempts)" % (inst_id, max_tries))

    return inst
Пример #3
0
def start_instance_from_image(conn, config, name, sleep=10, max_tries=50,
                              stream=sys.stdout):
    """Starts up an EC2 instance having the specified 'short' name and
    returns the instance.
    """
    debug = config.getboolean(name, 'debug')
    img_id = config.get(name, 'image_id')
    img = conn.get_image(img_id)
    instance_type = config.get(name, 'instance_type')
    platform = config.get(name, 'platform')
    identity = config.get(name, 'identity')
    py = config.get(name, 'py')
    key_name = os.path.splitext(os.path.basename(identity))[0]
    security_groups = [s.strip() for s in config.get(name, 'security_groups').split()
                       if len(s.strip())>0]
    
    print 'starting instance of image %s' % name
    print "   image id: %s" % img_id
    print "   location: %s" % img.location
    print "   platform: %s" % platform
    print "   identity: %s" % identity
    print "   key name: %s" % key_name
    print "   security_groups: %s" % security_groups
    print "   python: %s" % py
        
    reservation = img.run(key_name=key_name, 
                          security_groups=security_groups,
                          instance_type=instance_type)
        
    inst = reservation.instances[0]
    check_inst_state(inst, u'running', imgname=name, debug=debug)
    if inst.state != u'running':
        raise RuntimeError("instance of '%s' failed to run" % name)
    
    if debug:
        print "instance at address '%s' is running" % inst.public_dns_name
        
    for i in range(max_tries):
        time.sleep(sleep)
        if debug: 
            print "testing ssh connection (try #%d)" % (i+1)
        # even though the instance is running, it takes a while before
        # sshd is running, so we have to wait a bit longer
        if ssh_test(inst.public_dns_name):
            break
    else:
        stream.write("\nssh connection to %s failed after %d attempts."
                     "  terminating...\n" % (name, max_tries))
        terminate_instance(inst, name, stream, debug)
        raise RuntimeError("couldn't connect to %s via ssh" % name)

    time.sleep(20)
    
    try:
        conn.create_tags([inst.id], {'Name': "%s_%s" % (get_username(), name)} )
    except Exception as err:
        stream.write(str(err))
    return inst
Пример #4
0
def start_instance_from_image(conn, config, name, sleep=10, max_tries=50):
    """Starts up an EC2 instance having the specified 'short' name and
    returns the instance.
    """
    debug = config.getboolean(name, 'debug')
    img_id = config.get(name, 'image_id')
    img = conn.get_image(img_id)
    instance_type = config.get(name, 'instance_type')
    platform = config.get(name, 'platform')
    identity = config.get(name, 'identity')
    key_name = os.path.splitext(os.path.basename(identity))[0]
    security_groups = [
        s.strip() for s in config.get(name, 'security_groups').split()
        if len(s.strip()) > 0
    ]

    print 'starting instance of image %s' % name
    print "   image id: %s" % img_id
    print "   location: %s" % img.location
    print "   platform: %s" % platform
    print "   identity: %s" % identity
    print "   key name: %s" % key_name
    print "   security_groups: %s" % security_groups

    reservation = img.run(key_name=key_name,
                          security_groups=security_groups,
                          instance_type=instance_type)

    inst = reservation.instances[0]
    check_inst_state(inst, u'running', imgname=name, debug=debug)
    if inst.state != u'running':
        raise RuntimeError("instance of '%s' failed to run" % name)

    if debug:
        print "instance at address '%s' is running" % inst.public_dns_name

    for i in range(max_tries):
        time.sleep(sleep)
        if debug:
            print "testing ssh connection (try #%d)" % (i + 1)
        # even though the instance is running, it takes a while before
        # sshd is running, so we have to wait a bit longer
        if ssh_test(inst.public_dns_name):
            break
    else:
        raise RuntimeError(
            "instance of '%s' ran but ssh connection attempts failed (%d attempts)"
            % (name, max_tries))

    time.sleep(20)

    try:
        conn.create_tags([inst.id], {'Name': "%s_%s" % (get_username(), name)})
    except Exception as err:
        print str(err)
    return inst
Пример #5
0
def start_instance_from_image(conn, config, name, sleep=10, max_tries=50):
    """Starts up an EC2 instance having the specified 'short' name and
    returns the instance.
    """
    debug = config.getboolean(name, 'debug')
    img_id = config.get(name, 'image_id')
    img = conn.get_image(img_id)
    instance_type = config.get(name, 'instance_type')
    platform = config.get(name, 'platform')
    identity = config.get(name, 'identity')
    key_name = os.path.splitext(os.path.basename(identity))[0]
    security_groups = [s.strip() for s in config.get(name, 'security_groups').split()
                       if len(s.strip())>0]
    
    print 'starting instance of image %s' % name
    print "   image id: %s" % img_id
    print "   location: %s" % img.location
    print "   platform: %s" % platform
    print "   identity: %s" % identity
    print "   key name: %s" % key_name
    print "   security_groups: %s" % security_groups
        
    reservation = img.run(key_name=key_name, 
                          security_groups=security_groups,
                          instance_type=instance_type)
    
    inst = reservation.instances[0]
    check_inst_state(inst, u'running', imgname=name, debug=debug)
    if inst.state != u'running':
        raise RuntimeError("instance of '%s' failed to run" % name)
    
    if debug:
        print "instance at address '%s' is running" % inst.public_dns_name
        
    for i in range(max_tries):
        time.sleep(sleep)
        if debug: 
            print "testing ssh connection (try #%d)" % (i+1)
        # even though the instance is running, it takes a while before
        # sshd is running, so we have to wait a bit longer
        if ssh_test(inst.public_dns_name):
            break
    else:
        raise RuntimeError("instance of '%s' ran but ssh connection attempts failed (%d attempts)" % (name,max_tries))

    time.sleep(20)
        
    return inst
Пример #6
0
def start_instance_from_image(conn,
                              config,
                              name,
                              sleep=10,
                              max_tries=50,
                              stream=sys.stdout):
    """Starts up an EC2 instance having the specified 'short' name and
    returns the instance.
    """
    debug = config.getboolean(name, 'debug')
    img_id = config.get(name, 'image_id')
    img = conn.get_image(img_id)
    instance_type = config.get(name, 'instance_type')
    platform = config.get(name, 'platform')
    identity = config.get(name, 'identity')
    py = config.get(name, 'py')
    key_name = os.path.splitext(os.path.basename(identity))[0]
    security_groups = [
        s.strip() for s in config.get(name, 'security_groups').split()
        if len(s.strip()) > 0
    ]

    print 'starting instance of image %s' % name
    print "   image id: %s" % img_id
    print "   location: %s" % img.location
    print "   platform: %s" % platform
    print "   identity: %s" % identity
    print "   key name: %s" % key_name
    print "   security_groups: %s" % security_groups
    print "   python: %s" % py

    reservation = img.run(key_name=key_name,
                          security_groups=security_groups,
                          instance_type=instance_type)

    inst = reservation.instances[0]
    check_inst_state(inst, u'running', imgname=name, debug=debug)
    if inst.state != u'running':
        raise RuntimeError("instance of '%s' failed to run" % name)

    if debug:
        print "instance at address '%s' is running" % inst.public_dns_name

    if platform == 'windows':
        # Extremely slow startup, don't even try for a minute.
        print 'pausing for lethargic windows...'
        time.sleep(60)

    successes = 0
    for i in range(max_tries):
        time.sleep(sleep)
        if debug:
            print "testing ssh connection (try #%d)" % (i + 1)
        # even though the instance is running, it takes a while before
        # sshd is running, so we have to wait a bit longer
        # In addition, sometimes the initial success is a fluke and the
        # next connection fails, so we wait for three successes in a row.
        if ssh_test(inst.public_dns_name):
            successes += 1
            if successes > 2:
                break
        else:
            successes = 0
    else:
        stream.write("\nssh connection to %s failed after %d attempts."
                     "  terminating...\n" % (name, max_tries))
        terminate_instance(inst, name, stream, debug)
        raise RuntimeError("couldn't connect to %s via ssh" % name)

    time.sleep(20)

    try:
        conn.create_tags([inst.id], {'Name': "%s_%s" % (get_username(), name)})
    except Exception as err:
        stream.write(str(err))
    return inst
Пример #7
0
def start_instance_from_image(conn, config, name, sleep=10, max_tries=50,
                              stream=sys.stdout):
    """Starts up an EC2 instance having the specified 'short' name and
    returns the instance.
    """
    debug = config.getboolean(name, 'debug')
    img_id = config.get(name, 'image_id')
    # Sometimes requires retry due to:
    # SSLError: [Errno 8] _ssl.c:504: EOF occurred in violation of protocol
    for retry in range(3):
        try:
            img = conn.get_image(img_id)
        except Exception as exc:
            print 'get_image(%s) failed: %s' % (img_id, exc)
            time.sleep(1)
        else:
            break
    else:
        raise RuntimeError("Can't get image for %s" % img_id)
    
    instance_type = config.get(name, 'instance_type')
    platform = config.get(name, 'platform')
    identity = config.get(name, 'identity')
    py = config.get(name, 'py')
    key_name = os.path.splitext(os.path.basename(identity))[0]
    security_groups = [s.strip() for s in config.get(name, 'security_groups').split()
                       if len(s.strip())>0]
    
    print 'starting instance of image %s' % name
    print "   image id: %s" % img_id
    print "   location: %s" % img.location
    print "   platform: %s" % platform
    print "   identity: %s" % identity
    print "   key name: %s" % key_name
    print "   security_groups: %s" % security_groups
    print "   python: %s" % py
        
    reservation = img.run(key_name=key_name, 
                          security_groups=security_groups,
                          instance_type=instance_type)
        
    inst = reservation.instances[0]
    check_inst_state(inst, u'running', imgname=name, debug=debug)
    if inst.state != u'running':
        raise RuntimeError("instance of '%s' failed to run" % name)
    
    if debug:
        print "instance at address '%s' is running" % inst.public_dns_name
        
    if platform == 'windows':
        # Extremely slow startup, don't even try for a minute.
        print 'pausing for lethargic windows...'
        time.sleep(60)

    successes = 0
    for i in range(max_tries):
        time.sleep(sleep)
        if debug: 
            print "testing ssh connection (try #%d)" % (i+1)
        # even though the instance is running, it takes a while before
        # sshd is running, so we have to wait a bit longer
        # In addition, sometimes the initial success is a fluke and the
        # next connection fails, so we wait for three successes in a row.
        if ssh_test(inst.public_dns_name):
            successes += 1
            if successes > 2:
                break
        else:
            successes = 0
    else:
        stream.write("\nssh connection to %s failed after %d attempts."
                     "  terminating...\n" % (name, max_tries))
        terminate_instance(inst, name, stream, debug)
        raise RuntimeError("couldn't connect to %s via ssh" % name)

    time.sleep(20)
    
    try:
        conn.create_tags([inst.id], {'Name': "%s_%s" % (get_username(), name)} )
    except Exception as err:
        stream.write(str(err))
    return inst