Ejemplo n.º 1
0
def copy_snapshot(ticket):
    # EC2 resource of the destination region.
    response = ec2_common.getEC2(ticket).meta.client.copy_snapshot(
        # See https://github.com/boto/boto3/issues/886
        # DestinationRegion is not used actually.
        #DestinationRegion   = ticket["regionName"],
        SourceRegion        = ticket["srcRegionName"],
        SourceSnapshotId    = ticket["snapshot"]["snapshotId"],
    )
    target = ec2_common.getEC2(ticket).Snapshot(response.get("SnapshotId"))
    target.reload()
    print "Snapshot [" + target.id + "] under creation in " + ticket.get("regionName")
    ec2_common.tag(ticket["regionName"], target.id, ticket["snapshot"]["tags"])
    return(target)
Ejemplo n.º 2
0
def create_snapshot(ticket):
    volume = ticket["volume"]
    snapshot = ec2_common.getEC2(ticket).create_snapshot(
        VolumeId    = volume["volumeId"],
        #Description = ticket.get("desc")
    )
    print "Snapshot [" + snapshot.id + "] under creation in " + ticket.get("regionName")

    #wait_snapshot(ticket)
    ec2_common.tag(ticket["regionName"], snapshot.id, volume["tags"])
    return(snapshot)
def stop_instances(ticket):
    """
    :param ticket: 
    {
        "regionName": "us-east-2",
        "instanceIds": [
            "i-0cc705bce867c7fe6"
        ], 
        "event": "instance_stopped"
    }
    :return: 
    """
    print "--------------------------------------------------------------------------------"
    print inspect.stack()[0][3] + " ticket: " + json_dump(ticket)
    print "--------------------------------------------------------------------------------"

    order = copy.copy(ticket)
    order["event"] = "instance_stopped"
    getEC2(ticket).meta.client.stop_instances(
        InstanceIds = order["instanceIds"],
    )
    return(wait_instances(order))
def list_instances(ticket):
    """
    :param ticket: 
    {
        "regionName": "us-east-2",
        "filters": [
            {
                "Name": "tag:Name", 
                "Values": [
                    "gitlab-dr"
                ]
            }, 
            {
                "Name": "instance-id",
                "Values": [
                    "i-0cc705bce867c7fe6"
                ]
            },
            ...
        ], 
    }
    :return: 
    """
    print "--------------------------------------------------------------------------------"
    print inspect.stack()[0][3] + " ticket: " + json_dump(ticket)
    print "--------------------------------------------------------------------------------"

    reservations = getEC2(ticket).meta.client.describe_instances(
        Filters = ticket.get("filters")
    )["Reservations"]
    instances = sum(
        [
            [i for i in r["Instances"]]
            for r in reservations
        ],
        []
    )
    return(instances)
def wait_instances(ticket):
    """
    
    :param ticket: 
    {
        "regionName": "us-east-2",
        "event": "instance_running", 
        "instanceIds": [
            "i-0cc705bce867c7fe6"
        ], 
    }
    :return: 
    """
    print "--------------------------------------------------------------------------------"
    print inspect.stack()[0][3] + " ticket: " + json_dump(ticket)
    print "--------------------------------------------------------------------------------"

    wait_args = {}
    wait_args["InstanceIds"]  = ticket["instanceIds"]
    wait_resource(ticket["regionName"], ticket["event"], **wait_args)
    return(getEC2(ticket).meta.client.describe_instances(
        InstanceIds = ticket["instanceIds"]
    ))
Ejemplo n.º 6
0
def create_volumes_from_snapshots(ticket):
    """
    Creates an EBS volume from a snapshot that can be attached to an instance in the same Availability Zone. 
    http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-creating-volume.html
    volume = ec2.create_volume(
        AvailabilityZone='string',
        Encrypted=True|False,
        KmsKeyId='string',
        Size=123,
        SnapshotId='string',
        VolumeType='standard'|'io1'|'gp2'|'sc1'|'st1',
        TagSpecifications=[
            {
                'ResourceType': 'instance'|'volume',
                'Tags': [
                    {
                        'Key': 'string',
                        'Value': 'string'
                    },
                ]
            },
        ]
    )
    :param ticket: 
    {
        "regionName": "us-east-2", 
        "snapshots": [
            {
                "deviceName": "/dev/sdb", 
                "snapshotId": "snap-03a49438c6485a4e1", 
                "srcSnapshotId": "snap-0e2f318bc176804e7", 
                "srcVolumeId": "vol-094812e2e62d7845e", 
                "volumeType": "standard",
                "availabilityZone": "us-east-2c", 
                "tags": [
                    {
                        "Key": "origin-region", 
                        "Value": "us-east-1"
                    }, 
                    {
                        "Key": "origin-device", 
                        "Value": "/dev/sdb"
                    }, 
                    {
                        "Key": "origin-volumeId", 
                        "Value": "vol-094812e2e62d7845e"
                    }
                ], 
            }
        ]
    }
    :return: 
    {
        "regionName": "us-east-2", 
        "volumes": [
            {
                "volumeId": "vol-0f11c4abffcf9521a", <---- Volume created
                "volumeType": "standard"
                "availabilityZone": "us-east-2c", 
                "deviceName": "/dev/sdb", 
                "snapshotId": "snap-03a49438c6485a4e1", 
                "srcSnapshotId": "snap-0e2f318bc176804e7", 
                "srcVolumeId": "vol-094812e2e62d7845e", 
                "tags": [
                    {
                        "Key": "origin-region", 
                        "Value": "us-east-1"
                    }, 
                    {
                        "Key": "origin-device", 
                        "Value": "/dev/sdb"
                    }, 
                    {
                        "Key": "origin-volumeId", 
                        "Value": "vol-094812e2e62d7845e"
                    }
                ], 
            }
        ]
    }
    """

    print "--------------------------------------------------------------------------------"
    print inspect.stack()[0][3] + " ticket: " + json_dump(ticket)
    print "--------------------------------------------------------------------------------"
    volumeIds = []

    ticket = copy.deepcopy(ticket)
    snapshots = ticket["snapshots"]
    for s in snapshots:
        volume = ec2_common.getEC2(ticket).create_volume(
            SnapshotId          = s["snapshotId"],
            AvailabilityZone    = s["availabilityZone"],
            VolumeType          = s["volumeType"],
            TagSpecifications=[
                {
                    'ResourceType': "volume",
                    'Tags': s["tags"]
                },
            ]
        )
        s["volumeId"] = volume.id
        volumeIds.extend([volume.id])

    ticket["volumes"] = ticket.pop("snapshots")
    ticket["event"] = "volume_available"
    ticket["filters"]= [
        {
            "Name" : "status",
            "Values": [
                "available"
            ]
        }
    ]
    print json_dump(ec2_volume.wait_volumes(ticket))
    return(ticket)