コード例 #1
0
ファイル: actions.py プロジェクト: 0rc/chaostoolkit-aws
def stop_instances_any_type(instance_types: dict, force, client: boto3.client):
    """
    Stop instances regardless of the instance type (ondemand, spot)
    """

    if 'normal' in instance_types:
        logger.debug("Stopping instances: {}".format(instance_types['normal']))
        client.stop_instances(InstanceIds=instance_types['normal'],
                              Force=force)

    if 'spot' in instance_types:
        # TODO: proper support for spot fleets
        # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-fleet.html

        # To properly stop spot instances have to cancel spot requests first
        spot_request_ids = get_spot_request_ids_from_response(
            client.describe_instances(InstanceIds=instance_types['spot']))

        logger.debug("Canceling spot requests: {}".format(spot_request_ids))
        client.cancel_spot_instance_requests(
            SpotInstanceRequestIds=spot_request_ids)
        logger.debug("Terminating spot instances: {}".format(
            instance_types['spot']))
        client.terminate_instances(InstanceIds=instance_types['spot'])

    if 'scheduled' in instance_types:
        # TODO: add support for scheduled inststances
        # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-scheduled-instances.html

        raise FailedActivity("Scheduled instances support is not implemented")
コード例 #2
0
def stop_instances_any_type(
    instance_types: dict = None, force: bool = False, client: boto3.client = None
) -> List[AWSResponse]:
    """
    Stop instances regardless of the instance type (on demand, spot)
    """

    response = []
    if "normal" in instance_types:
        logger.debug("Stopping instances: {}".format(instance_types["normal"]))

        response.append(
            client.stop_instances(InstanceIds=instance_types["normal"], Force=force)
        )

    if "spot" in instance_types:
        # TODO: proper support for spot fleets
        # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-fleet.html

        # To properly stop spot instances have to cancel spot requests first
        spot_request_ids = get_spot_request_ids_from_response(
            client.describe_instances(InstanceIds=instance_types["spot"])
        )

        logger.debug(f"Canceling spot requests: {spot_request_ids}")
        client.cancel_spot_instance_requests(SpotInstanceRequestIds=spot_request_ids)
        logger.debug("Terminating spot instances: {}".format(instance_types["spot"]))

        response.append(client.terminate_instances(InstanceIds=instance_types["spot"]))

    if "scheduled" in instance_types:
        # TODO: add support for scheduled instances
        # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-scheduled-instances.html
        raise FailedActivity("Scheduled instances support is not implemented")
    return response
コード例 #3
0
ファイル: actions.py プロジェクト: xpdable/chaostoolkit-aws
def terminate_instances_any_type(instance_types: dict = None,
                                 client: boto3.client = None
                                 ) -> List[AWSResponse]:
    """
    Terminates instance(s) regardless of type
    """
    response = []

    for k, v in instance_types.items():
        logger.debug('Terminating %s instance(s): %s' % (k, instance_types[k]))
        if k == 'spot':
            instances = get_spot_request_ids_from_response(
                client.describe_instances(InstanceIds=v))
            # Cancel spot request prior to termination
            client.cancel_spot_instance_requests(
                SpotInstanceRequestIds=instances)
            response.append(client.terminate_instances(InstanceIds=v))
            continue
        response.append(client.terminate_instances(InstanceIds=v))
    return response