def terminate(self, aws_tags: List[Dict]) -> None:
        """Aws spot instance scheduler function.

        Terminate spot instances by using the defined tag.

        :param list[map] aws_tags:
            Aws tags to use for filter resources.
            For example:
            [
                {
                    'Key': 'string',
                    'Values': [
                        'string',
                    ]
                }
            ]
        """
        tag_key = aws_tags[0]["Key"]
        tag_value = "".join(aws_tags[0]["Values"])
        for spot_instance in self.list_spot(tag_key, tag_value):
            try:
                self.ec2.terminate_instances(InstanceIds=[spot_instance])
                print("Terminate spot instance {0}".format(spot_instance))
            except ClientError as exc:
                ec2_exception("spot instance", spot_instance, exc)
Esempio n. 2
0
    def start(self, aws_tags: List[Dict]) -> None:
        """Aws ec2 instance start function.

        Start ec2 instances with defined tags.

        Aws tags to use for filter resources
            Aws tags to use for filter resources.
            For example:
            [
                {
                    'Key': 'string',
                    'Values': [
                        'string',
                    ]
                }
            ]
        """
        for instance_arn in self.tag_api.get_resources("ec2:instance",
                                                       aws_tags):
            instance_id = instance_arn.split("/")[-1]
            try:
                if not self.asg.describe_auto_scaling_instances(
                        InstanceIds=[instance_id])["AutoScalingInstances"]:
                    self.ec2.start_instances(InstanceIds=[instance_id])
                    print("Start instances {0}".format(instance_id))
            except ClientError as exc:
                ec2_exception("instance", instance_id, exc)
Esempio n. 3
0
    def instance_running(self, instance_ids: List[str]) -> None:
        """Aws waiter for instance running.

        Wait ec2 instances are in running state.

        :param list instance_ids:
            The instance IDs to wait.
        """
        if instance_ids:
            try:
                instance_waiter = self.ec2.get_waiter("instance_running")
                instance_waiter.wait(
                    InstanceIds=instance_ids,
                    WaiterConfig={"Delay": 15, "MaxAttempts": 15},
                )
            except ClientError as exc:
                ec2_exception("waiter", instance_waiter, exc)
Esempio n. 4
0
    def start(self, aws_tags: List[Dict]) -> None:
        """Aws autoscaling resume function.

        Resume autoscaling group and start its instances
        with defined tag.

        :param list[map] aws_tags:
            Aws tags to use for filter resources
            For example:
            [
                {
                    'Key': 'string',
                    'Values': [
                        'string',
                    ]
                }
            ]
        """
        tag_key = aws_tags[0]["Key"]
        tag_value = "".join(aws_tags[0]["Values"])
        asg_name_list = self.list_groups(tag_key, tag_value)
        instance_id_list = self.list_instances(asg_name_list)
        instance_running_ids = []

        # Start autoscaling instance
        for instance_id in instance_id_list:
            try:
                self.ec2.start_instances(InstanceIds=[instance_id])
                print("Start autoscaling instances {0}".format(instance_id))
            except ClientError as exc:
                ec2_exception("instance", instance_id, exc)
            else:
                instance_running_ids.append(instance_id)

        self.waiter.instance_running(instance_ids=instance_running_ids)

        for asg_name in asg_name_list:
            try:
                self.asg.resume_processes(AutoScalingGroupName=asg_name)
                print("Resume autoscaling group {0}".format(asg_name))
            except ClientError as exc:
                ec2_exception("autoscaling group", asg_name, exc)
Esempio n. 5
0
    def stop(self, aws_tags: List[Dict]) -> None:
        """Aws autoscaling suspend function.

        Suspend autoscaling group and stop its instances
        with defined tag.

        :param list[map] aws_tags:
            Aws tags to use for filter resources.
            For example:
            [
                {
                    'Key': 'string',
                    'Values': [
                        'string',
                    ]
                }
            ]
        """
        tag_key = aws_tags[0]["Key"]
        tag_value = "".join(aws_tags[0]["Values"])
        asg_name_list = self.list_groups(tag_key, tag_value)
        instance_id_list = self.list_instances(asg_name_list)

        for asg_name in asg_name_list:
            try:
                self.asg.suspend_processes(AutoScalingGroupName=asg_name)
                print("Suspend autoscaling group {0}".format(asg_name))
            except ClientError as exc:
                ec2_exception("instance", asg_name, exc)

        # Stop autoscaling instance
        for instance_id in instance_id_list:
            try:
                self.ec2.stop_instances(InstanceIds=[instance_id])
                print("Stop autoscaling instances {0}".format(instance_id))
            except ClientError as exc:
                ec2_exception("autoscaling group", instance_id, exc)