vincbeck commented on code in PR #35770:
URL: https://github.com/apache/airflow/pull/35770#discussion_r1400713847


##########
airflow/providers/amazon/aws/operators/ec2.py:
##########
@@ -254,3 +256,100 @@ def execute(self, context: Context):
                         "MaxAttempts": self.max_attempts,
                     },
                 )
+
+class EC2RebootInstanceOperator(BaseOperator):
+    """
+    Reboot AWS EC2 instance using boto3.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the 
guide:
+        :ref:`howto/operator:EC2RebootInstanceOperator`
+
+    :param instance_id: id of the AWS EC2 instance
+    :param aws_conn_id: aws connection to use
+    :param region_name: (optional) aws region name associated with the client
+    :param check_interval: time in seconds that the job should wait in
+        between each instance state checks until operation is completed
+    """
+
+    template_fields: Sequence[str] = ("instance_id", "region_name")
+    ui_color = "#eeaa11"
+    ui_fgcolor = "#ffffff"
+
+    def __init__(
+        self,
+        *,
+        instance_id: str,
+        aws_conn_id: str = "aws_default",
+        region_name: str | None = None,
+        check_interval: float = 15,
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.instance_id = instance_id
+        self.aws_conn_id = aws_conn_id
+        self.region_name = region_name
+        self.check_interval = check_interval
+
+    def execute(self, context: Context):
+        ec2_hook = EC2Hook(aws_conn_id=self.aws_conn_id, 
region_name=self.region_name)
+        self.log.info("Rebooting EC2 instance %s", self.instance_id)
+        instance = ec2_hook.get_instance(instance_id=self.instance_id)
+        instance.reboot()
+        ec2_hook.wait_for_state(
+            instance_id=self.instance_id,
+            target_state="running",
+            check_interval=self.check_interval,
+        )
+
+class EC2HibernateInstanceOperator(BaseOperator):
+    """
+    Hibernate AWS EC2 instance using boto3.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the 
guide:
+        :ref:`howto/operator:EC2HibernateInstanceOperator`
+
+    :param instance_id: id of the AWS EC2 instance
+    :param aws_conn_id: aws connection to use
+    :param region_name: (optional) aws region name associated with the client
+    :param check_interval: time in seconds that the job should wait in
+        between each instance state checks until operation is completed
+    """
+
+    template_fields: Sequence[str] = ("instance_id", "region_name")
+    ui_color = "#eeaa11"
+    ui_fgcolor = "#ffffff"
+
+    def __init__(
+        self,
+        *,
+        instance_id: str,
+        aws_conn_id: str = "aws_default",
+        region_name: str | None = None,
+        check_interval: float = 15,
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.instance_id = instance_id
+        self.aws_conn_id = aws_conn_id
+        self.region_name = region_name
+        self.check_interval = check_interval
+
+    def execute(self, context: Context):
+        ec2_hook = EC2Hook(aws_conn_id=self.aws_conn_id, 
region_name=self.region_name)
+        self.log.info("Hibernating EC2 instance %s", self.instance_id)
+        instance = ec2_hook.get_instance(instance_id=self.instance_id)
+        
+        hibernation_options = instance.hibernation_options
+        if not hibernation_options or not hibernation_options["Configured"]:
+            raise EC2HibernationError(f"Instance {self.instance_id} is not 
configured for hibernation")
+        
+        instance.stop(
+            Hibernate = True
+        )

Review Comment:
   nit
   
   ```suggestion
           instance.stop(Hibernate=True)
   ```



##########
tests/system/providers/amazon/aws/example_ec2.py:
##########
@@ -150,6 +153,22 @@ def parse_response(instance_ids: list):
     )
     # [END howto_operator_ec2_terminate_instance]
     terminate_instance.trigger_rule = TriggerRule.ALL_DONE
+
+    # [START howto_operator_ec2_hibernate_instance]
+    hibernate_instance = EC2HibernateInstanceOperator(
+        task_id="hibernate_instace",
+        instance_id=instance_id,
+    )
+    # [END howto_operator_ec2_hibernate_instance]
+    hibernate_instance.trigger_rule = TriggerRule.ALL_DONE
+    
+    # [START howto_operator_ec2_reboot_instance]
+    reboot_instance = EC2RebootInstanceOperator(
+        task_id="reboot_instace",
+        instance_id=instance_id,
+    )
+    # [END howto_operator_ec2_reboot_instance]
+    reboot_instance.trigger_rule = TriggerRule.ALL_DONE

Review Comment:
   I dont think this sequence would work. At that point the instance is 
terminated, hence you cannot hibernate it. I would also reboot it before 
hibernating it. I would follow this order:
   - reboot
   - hibernate
   - terminate



##########
airflow/providers/amazon/aws/operators/ec2.py:
##########
@@ -254,3 +256,100 @@ def execute(self, context: Context):
                         "MaxAttempts": self.max_attempts,
                     },
                 )
+
+class EC2RebootInstanceOperator(BaseOperator):
+    """
+    Reboot AWS EC2 instance using boto3.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the 
guide:
+        :ref:`howto/operator:EC2RebootInstanceOperator`
+
+    :param instance_id: id of the AWS EC2 instance
+    :param aws_conn_id: aws connection to use
+    :param region_name: (optional) aws region name associated with the client
+    :param check_interval: time in seconds that the job should wait in
+        between each instance state checks until operation is completed
+    """
+
+    template_fields: Sequence[str] = ("instance_id", "region_name")
+    ui_color = "#eeaa11"
+    ui_fgcolor = "#ffffff"
+
+    def __init__(
+        self,
+        *,
+        instance_id: str,
+        aws_conn_id: str = "aws_default",
+        region_name: str | None = None,
+        check_interval: float = 15,
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.instance_id = instance_id
+        self.aws_conn_id = aws_conn_id
+        self.region_name = region_name
+        self.check_interval = check_interval
+
+    def execute(self, context: Context):
+        ec2_hook = EC2Hook(aws_conn_id=self.aws_conn_id, 
region_name=self.region_name)
+        self.log.info("Rebooting EC2 instance %s", self.instance_id)
+        instance = ec2_hook.get_instance(instance_id=self.instance_id)
+        instance.reboot()
+        ec2_hook.wait_for_state(
+            instance_id=self.instance_id,
+            target_state="running",
+            check_interval=self.check_interval,
+        )
+
+class EC2HibernateInstanceOperator(BaseOperator):
+    """
+    Hibernate AWS EC2 instance using boto3.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the 
guide:
+        :ref:`howto/operator:EC2HibernateInstanceOperator`
+
+    :param instance_id: id of the AWS EC2 instance
+    :param aws_conn_id: aws connection to use
+    :param region_name: (optional) aws region name associated with the client
+    :param check_interval: time in seconds that the job should wait in
+        between each instance state checks until operation is completed
+    """
+
+    template_fields: Sequence[str] = ("instance_id", "region_name")
+    ui_color = "#eeaa11"
+    ui_fgcolor = "#ffffff"
+
+    def __init__(
+        self,
+        *,
+        instance_id: str,
+        aws_conn_id: str = "aws_default",
+        region_name: str | None = None,
+        check_interval: float = 15,
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.instance_id = instance_id
+        self.aws_conn_id = aws_conn_id
+        self.region_name = region_name
+        self.check_interval = check_interval
+
+    def execute(self, context: Context):
+        ec2_hook = EC2Hook(aws_conn_id=self.aws_conn_id, 
region_name=self.region_name)
+        self.log.info("Hibernating EC2 instance %s", self.instance_id)
+        instance = ec2_hook.get_instance(instance_id=self.instance_id)
+        
+        hibernation_options = instance.hibernation_options
+        if not hibernation_options or not hibernation_options["Configured"]:
+            raise EC2HibernationError(f"Instance {self.instance_id} is not 
configured for hibernation")

Review Comment:
   I am not strongly against it but what's the value of using a custom 
exception instead of one like `AirflowException`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to