Exemple #1
0
def image_distribute(deployment, image_name):
    """Distribute an image available on the head region to the other regions
    """
    dep = deployments[deployment]

    head_region = dep.head.region
    other_regions = [r.region for r in dep.regions if r.region != head_region]
    connections = ec2.connect_all(*other_regions)

    if not ec2.list_images(ec2.connect(head_region), image_name):
        print 'image "%s" does not exist on %s!' % (image_name, head_region)
        return

    regions_tocopy = []
    for region, conn in connections.iteritems():
        images = ec2.list_images(conn)
        print "%s:" % (region)
        needcopy = True
        for img in images:
            if img.name == image_name:
                print "\talready available:", img
                needcopy = False
                break
        if needcopy:
            print "\tcopying..."
            regions_tocopy.append(conn)
    for conn in regions_tocopy:
        ec2.copy_image(conn, image_name, head_region)
Exemple #2
0
def image_distribute(deployment, image_name):
    """Distribute an image available on the head region to the other regions
    """
    dep = deployments[deployment]

    head_region = dep.head.region
    other_regions = [r.region for r in dep.regions if r.region != head_region]
    connections = ec2.connect_all(*other_regions)

    if not ec2.list_images(ec2.connect(head_region), image_name):
        print 'image "%s" does not exist on %s!' %(image_name, head_region)
        return

    regions_tocopy = []
    for region, conn in connections.iteritems():
        images = ec2.list_images(conn)
        print '%s:' % (region)
        needcopy = True
        for img in images:
            if img.name == image_name:
                print "\talready available:", img
                needcopy = False
                break
        if needcopy:
            print "\tcopying..."
            regions_tocopy.append(conn)
    for conn in regions_tocopy:
        ec2.copy_image(conn, image_name, head_region)
Exemple #3
0
def image_list(deployment):
    """List images available on each region
    """
    dep = deployments[deployment]
    connections = ec2.connect_all(*[x.region for x in dep.regions])
    for region, conn in connections.iteritems():
        images = ec2.list_images(conn)
        print "%s:" % (region)
        for img in images:
            print " %s %s - %s" % (img.name, img.id, img.state)
Exemple #4
0
def image_list(deployment):
    """List images available on each region
    """
    dep = deployments[deployment]
    connections = ec2.connect_all(*[x.region for x in dep.regions])
    for region, conn in connections.iteritems():
        images = ec2.list_images(conn)
        print '%s:' % (region)
        for img in images:
            print ' %s %s - %s' % (img.name, img.id, img.state)
Exemple #5
0
def inst_start(deployment, image_name, token="A", keyname="macubuntu", security_group="default"):
    """Start and configure instances

    Assumptions:
    - You need to give the image with image_name (AMI-Name) available in all needed regions
    - You have a keypair named 'macubuntu' in all regions (otherwise use the keyname param)
    - You have a security group named 'default' in all regions, with all ports open (otherwise use the security_group param)
    """
    dep = deployments[deployment]
    regions = [x.region for x in dep.regions]
    connections = ec2.connect_all(*regions)
    images = {}
    reservations = {}

    # first check that image is available everywhere
    for region, conn in connections.items():
        imageid = None
        for img in ec2.list_images(conn, name=image_name):
            if img.state == u"available":
                imageid = img.id
                images[region] = imageid
        if not imageid:
            raise Exception("image not available in region " + region)
    # start instances
    for dc in dep.regions:
        conn = connections[dc.region]
        for node in dc.nodes:
            print "* Starting node", node.name, ":", node
            reservation = conn.run_instances(
                images[node.region],
                instance_type=node.type,
                placement=node.region + node.zone,
                key_name=keyname,
                security_groups=[security_group],
                client_token=node.name + token,
                # user_data =
                dry_run=False,
            )
            reservations[node.name] = (node, reservation)
    # wait for instances to be running and tag them
    print "**********************"
    for node, reserv in reservations.values():
        instance = reserv.instances[0]  # each reservation is for a single instance
        status = instance.update()
        while status == "pending":
            time.sleep(5)
            status = instance.update()
        if status == "running":
            print "* Tagging instance: ", node.name
            instance.add_tag("Name", node.name)
        else:
            print "* ERROR: starting node", node, status
    print "* DONE!"
Exemple #6
0
def inst_start(deployment, image_name, token='A', keyname='macubuntu', security_group='default'):
    """Start and configure instances

    Assumptions:
    - You need to give the image with image_name (AMI-Name) available in all needed regions
    - You have a keypair named 'macubuntu' in all regions (otherwise use the keyname param)
    - You have a security group named 'default' in all regions, with all ports open (otherwise use the security_group param)
    """
    dep = deployments[deployment]
    regions = [x.region for x in dep.regions]
    connections = ec2.connect_all(*regions)
    images = {}
    reservations = {}

    # first check that image is available everywhere
    for region, conn in connections.items():
        imageid=None
        for img in ec2.list_images(conn, name=image_name):
            if img.state == u'available':
                imageid=img.id
                images[region] = imageid
        if not imageid:
            raise Exception('image not available in region ' + region)
    # start instances
    for dc in dep.regions:
        conn = connections[dc.region]
        for node in dc.nodes:
            print '* Starting node', node.name, ':', node
            reservation = conn.run_instances(images[node.region],
                                             instance_type = node.type,
                                             placement = node.region + node.zone,
                                             key_name = keyname,
                                             security_groups = [security_group],
                                             client_token = node.name + token,
                                             # user_data =
                                             dry_run = False)
            reservations[node.name] = (node, reservation)
    # wait for instances to be running and tag them
    print '**********************'
    for node, reserv in reservations.values():
        instance = reserv.instances[0] # each reservation is for a single instance
        status = instance.update()
        while status == 'pending':
            time.sleep(5)
            status = instance.update()
        if status == 'running':
            print '* Tagging instance: ', node.name
            instance.add_tag('Name', node.name)
        else:
            print '* ERROR: starting node', node, status
    print '* DONE!'
Exemple #7
0
def head_start(deployment, image_name=None, token="A", keyname="macubuntu", security_group="default"):
    """Start/restart head instance.

    Assumptions:
    - If you are creating a "head" instance, you need to give the image_name (AMI-Name)
    - You have a keypair named 'macubuntu' in the head's region (otherwise use the keyname param)
    - You have a security group named 'default' in the head's region, with all ports open (otherwise use the security_group param)
    """
    dep = deployments[deployment]
    connections = ec2.connect_all(*[dep.head.region])
    instances = ec2.list_instances(*connections.values())
    exists = False
    for worker in instances:
        # ignore nodes named 'head'
        if "Name" in worker.tags and worker.tags["Name"] == "head":
            if worker.state_code == 16:
                print "* Head already running:", worker.region.name, worker.id
            else:
                worker.start()
                exists = True
                break
    # create new instance
    if not exists and image_name:
        conn = connections[dep.head.region]
        imageid = None
        for img in ec2.list_images(conn, name=image_name):
            if img.state == u"available":
                imageid = img.id
                break
        if not imageid:
            raise Exception("image not available in region " + dep.head.region)
        reserv = conn.run_instances(
            imageid,
            instance_type=dep.head.type,
            placement=dep.head.region + dep.head.zone,
            key_name=keyname,
            security_groups=[security_group],
            client_token=dep.head.name + token,
            dry_run=False,
        )
        instance = reserv.instances[0]
        status = instance.update()
        while status == "pending":
            time.sleep(5)
            status = instance.update()
        if status == "running":
            print "* Tagging instance: ", dep.head.name
            instance.add_tag("Name", dep.head.name)
        else:
            print "* ERROR: starting node", dep.head, status
Exemple #8
0
def image_delete(deployment, image_name):
    """Delete the image from all regions in the deployment
    """
    dep = deployments[deployment]
    connections = ec2.connect_all(*[x.region for x in dep.regions])
    for region, conn in connections.iteritems():
        images = ec2.list_images(conn)
        print "%s:" % (region)
        for img in images:
            if img.name == image_name:
                print "removing", img
                try:
                    img.deregister(delete_snapshot=True)
                except:
                    pass
Exemple #9
0
def image_delete(deployment, image_name):
    """Delete the image from all regions in the deployment
    """
    dep = deployments[deployment]
    connections = ec2.connect_all(*[x.region for x in dep.regions])
    for region, conn in connections.iteritems():
        images = ec2.list_images(conn)
        print '%s:' % (region)
        for img in images:
            if img.name == image_name:
                print "removing", img
                try:
                    img.deregister(delete_snapshot=True)
                except:
                    pass
Exemple #10
0
def head_start(deployment, image_name=None, token='A', keyname='macubuntu', security_group='default'):
    """Start/restart head instance.

    Assumptions:
    - If you are creating a "head" instance, you need to give the image_name (AMI-Name)
    - You have a keypair named 'macubuntu' in the head's region (otherwise use the keyname param)
    - You have a security group named 'default' in the head's region, with all ports open (otherwise use the security_group param)
    """
    dep = deployments[deployment]
    connections = ec2.connect_all(*[dep.head.region])
    instances = ec2.list_instances(*connections.values())
    exists=False
    for worker in instances:
        # ignore nodes named 'head'
        if 'Name' in worker.tags and worker.tags['Name'] == 'head':
            if worker.state_code == 16:
                print '* Head already running:', worker.region.name, worker.id
            else:
                worker.start()
                exists=True
                break
    # create new instance
    if not exists and image_name:
        conn = connections[dep.head.region]
        imageid=None
        for img in ec2.list_images(conn, name=image_name):
            if img.state == u'available':
                imageid=img.id
                break
        if not imageid:
            raise Exception('image not available in region ' + dep.head.region)
        reserv = conn.run_instances(imageid,
                                    instance_type = dep.head.type,
                                    placement = dep.head.region + dep.head.zone,
                                    key_name = keyname,
                                    security_groups = [security_group],
                                    client_token = dep.head.name + token,
                                    dry_run = False)
        instance = reserv.instances[0]
        status = instance.update()
        while status == 'pending':
            time.sleep(5)
            status = instance.update()
        if status == 'running':
            print '* Tagging instance: ', dep.head.name
            instance.add_tag('Name', dep.head.name)
        else:
            print '* ERROR: starting node', dep.head, status