power_off_alibaba_instance
Forcefully stop Alibaba Cloud ECS instances with triple confirmation for added security and accuracy using the Cloud Manage MCP Server. Manages cloud server operations efficiently.
Instructions
强制停止阿里云ECS实例(需要三次确认)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| instance_id | Yes | ||
| ip_confirmation | No | ||
| name_confirmation | No | ||
| operation_confirmation | No |
Implementation Reference
- main.py:585-598 (registration)MCP tool registration for power_off_alibaba_instance. This is the entry point handler that delegates to the Alibaba provider's power_off_instance method.@mcp.tool() def power_off_alibaba_instance( instance_id: str, ip_confirmation: str = "", name_confirmation: str = "", operation_confirmation: str = "" ) -> Dict: """ 强制停止阿里云ECS实例(需要三次确认) """ return alibaba_provider.power_off_instance( instance_id, ip_confirmation, name_confirmation, operation_confirmation )
- providers/alibaba_provider.py:216-229 (handler)Provider-specific handler for powering off Alibaba ECS instance. Delegates to the internal _execute_power_operation with 'stop' action.def power_off_instance( self, instance_id: str, ip_confirmation: str = "", name_confirmation: str = "", operation_confirmation: str = "" ) -> Dict: """ 强制停止ECS实例(需要三次确认) """ return self._execute_power_operation( instance_id, 'stop', ip_confirmation, name_confirmation, operation_confirmation )
- providers/alibaba_provider.py:244-358 (handler)Core helper function that implements the power off logic: fetches instance info, performs triple confirmation via security utils, and calls Alibaba ECS StopInstance API with force_stop=True.def _execute_power_operation( self, instance_id: str, operation: str, ip_confirmation: str, name_confirmation: str, operation_confirmation: str ) -> Dict: """ 执行电源操作的通用函数 """ if not self.available: return { 'error': f'阿里云服务不可用: {getattr(self, "error", "未知错误")}', 'provider': 'alibaba' } # 首先获取实例信息 try: request = ecs_models.DescribeInstancesRequest( region_id=self.region_id, instance_ids=json.dumps([instance_id]) ) response = self.client.describe_instances(request) if not response.body.instances or not response.body.instances.instance: return { 'error': f'未找到ID为 {instance_id} 的ECS实例', 'provider': 'alibaba' } instance = response.body.instances.instance[0] # 格式化实例信息用于确认 instance_info = self._format_instance_for_confirmation(instance) except Exception as e: return { 'error': f'获取ECS实例信息时发生错误: {str(e)}', 'provider': 'alibaba' } # 检查是否提供了确认信息 if not ip_confirmation or not name_confirmation or not operation_confirmation: # 转换操作名称 operation_mapping = { 'start': 'power_on', 'stop': 'power_off', 'reboot': 'reboot' } mapped_operation = operation_mapping.get(operation, operation) return require_triple_confirmation(instance_info, mapped_operation) # 验证确认信息 security = SecurityConfirmation() operation_mapping = { 'start': 'power_on', 'stop': 'power_off', 'reboot': 'reboot' } mapped_operation = operation_mapping.get(operation, operation) is_valid, error_message = security.validate_power_operation( instance_info, mapped_operation, ip_confirmation, name_confirmation, operation_confirmation ) if not is_valid: return { 'error': f'确认验证失败: {error_message}', 'provider': 'alibaba', 'requires_confirmation': True } # 执行实际操作 try: if operation == 'start': request = ecs_models.StartInstanceRequest( instance_id=instance_id ) response = self.client.start_instance(request) elif operation == 'stop': request = ecs_models.StopInstanceRequest( instance_id=instance_id, force_stop=True ) response = self.client.stop_instance(request) elif operation == 'reboot': request = ecs_models.RebootInstanceRequest( instance_id=instance_id, force_stop=True ) response = self.client.reboot_instance(request) else: return { 'error': f'不支持的操作类型: {operation}', 'provider': 'alibaba' } return { 'provider': 'alibaba', 'instance_id': instance_id, 'operation_success': True, 'operation': operation, 'request_id': response.body.request_id, 'message': f'已成功提交 {operation} 操作', 'confirmation_validated': True } except Exception as e: return { 'error': f'执行 {operation} 操作时发生错误: {str(e)}', 'provider': 'alibaba' }