The fence_aws_vpc_net agent is a network and power fencing agent for AWS VPC that operates by manipulating security groups. This document outlines the design and architecture of the agent.
## Class Diagram
```mermaid
classDiagram
class FenceAWSVPCNet {
-logger: Logger
-conn: boto3.resource
-options: dict
+main()
+define_new_opts()
+process_input()
+check_input()
+get_power_status()
+set_power_status()
}
class InstanceOperations {
+get_instance_id()
+get_instance_details()
+shutdown_instance()
+get_nodes_list()
}
class SecurityGroupOperations {
+modify_security_groups()
+create_backup_tag()
+restore_security_groups()
-validate_sg_changes()
}
class TagOperations {
+set_lastfence_tag()
+create_backup_tag()
+restore_from_backup()
-handle_chunked_tags()
}
class LoggingManager {
+configure_logging()
+configure_boto3_logging()
+handle_debug_file()
}
FenceAWSVPCNet --> InstanceOperations
FenceAWSVPCNet --> SecurityGroupOperations
FenceAWSVPCNet --> TagOperations
FenceAWSVPCNet --> LoggingManager
SecurityGroupOperations --> TagOperations
```
## Sequence Diagrams
### Fence Operation (Power Off)
```mermaid
sequenceDiagram
participant Client
participant FenceAgent
participant AWS
participant SecurityGroups
participant Tags
Client->>FenceAgent: Execute fence operation
FenceAgent->>AWS: Validate AWS credentials
AWS-->>FenceAgent: Credentials valid
opt skip-race-check not set
FenceAgent->>AWS: Get self instance ID
AWS-->>FenceAgent: Instance ID
FenceAgent->>FenceAgent: Check for self-fencing
end
FenceAgent->>AWS: Get instance details
AWS-->>FenceAgent: Instance details
alt Instance is running
FenceAgent->>SecurityGroups: Backup current security groups
SecurityGroups-->>FenceAgent: Backup created
alt ignore-tag-write-failure not set
FenceAgent->>Tags: Create lastfence tag
Tags-->>FenceAgent: Tag created
end
FenceAgent->>SecurityGroups: Modify security groups
SecurityGroups-->>FenceAgent: Groups modified
opt onfence-poweroff enabled
FenceAgent->>AWS: Initiate shutdown
AWS-->>FenceAgent: Shutdown initiated
end
FenceAgent-->>Client: Success
else Instance not running
FenceAgent-->>Client: Fail - Instance not running
end
```
### Unfence Operation (Power On)
```mermaid
sequenceDiagram
participant Client
participant FenceAgent
participant AWS
participant SecurityGroups
participant Tags
Client->>FenceAgent: Execute unfence operation
FenceAgent->>AWS: Validate AWS credentials
AWS-->>FenceAgent: Credentials valid
alt unfence-ignore-restore not set
FenceAgent->>Tags: Get lastfence tag
Tags-->>FenceAgent: Lastfence tag
FenceAgent->>Tags: Get backup tags
Tags-->>FenceAgent: Backup tags
alt Valid backup found
FenceAgent->>SecurityGroups: Restore original security groups
SecurityGroups-->>FenceAgent: Groups restored
FenceAgent->>Tags: Cleanup backup tags
Tags-->>FenceAgent: Tags cleaned
FenceAgent-->>Client: Success
else No valid backup
FenceAgent-->>Client: Fail - No valid backup found
end
else
FenceAgent-->>Client: Success - Restore skipped
end
```
## Component Details
### 1. Main Controller (FenceAWSVPCNet)
- **Purpose**: Main entry point and orchestration
- **Key Responsibilities**:
- Process command line options
- Initialize AWS connection
- Execute fence operations
- Handle logging and errors
- Manage self-fencing prevention
- Support tag write failure handling
### 2. Instance Operations
- **Purpose**: Handle EC2 instance operations
- **Key Responsibilities**:
- Get instance details and metadata
- Handle instance power operations
- Validate instance states
- List and filter instances
- Handle instance shutdown
### 3. Security Group Operations
- **Purpose**: Manage security group operations
- **Key Responsibilities**:
- Modify security groups (remove or keep-only modes)