-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add initial migration console snapshot support #751
Changes from all commits
e0ec666
c19a155
6a47641
6a31fab
11dc524
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import logging | ||
from typing import Tuple | ||
from console_link.models.snapshot import Snapshot, SnapshotStatus | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def create(snapshot: Snapshot, *args, **kwargs) -> Tuple[SnapshotStatus, str]: | ||
logger.info(f"Creating snapshot with {args=} and {kwargs=}") | ||
try: | ||
result = snapshot.create(*args, **kwargs) | ||
except Exception as e: | ||
logger.error(f"Failed to create snapshot: {e}") | ||
return SnapshotStatus.FAILED, f"Failure when creating snapshot: {type(e).__name__} {e}" | ||
Check warning on line 14 in TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/logic/snapshot.py Codecov / codecov/patchTrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/logic/snapshot.py#L9-L14
|
||
|
||
if result.success: | ||
return SnapshotStatus.COMPLETED, "Snapshot created successfully." + "\n" + result.value | ||
return SnapshotStatus.FAILED, "Snapshot creation failed." + "\n" + result.value | ||
Check warning on line 18 in TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/logic/snapshot.py Codecov / codecov/patchTrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/logic/snapshot.py#L16-L18
|
||
|
||
|
||
def status(snapshot: Snapshot, *args, **kwargs) -> Tuple[SnapshotStatus, str]: | ||
logger.info("Getting snapshot status") | ||
try: | ||
result = snapshot.status(*args, **kwargs) | ||
except Exception as e: | ||
logger.error(f"Failed to get status of snapshot: {e}") | ||
return SnapshotStatus.FAILED, f"Failure when getting status of snapshot: {type(e).__name__} {e}" | ||
if result.success: | ||
return SnapshotStatus.COMPLETED, result.value | ||
return SnapshotStatus.FAILED, "Snapshot status retrieval failed." + "\n" + result.value | ||
Check warning on line 30 in TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/logic/snapshot.py Codecov / codecov/patchTrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/logic/snapshot.py#L22-L30
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from abc import ABC, abstractmethod | ||
from enum import Enum | ||
import logging | ||
import subprocess | ||
from typing import Dict, Optional | ||
from console_link.models.cluster import Cluster | ||
from console_link.models.command_result import CommandResult | ||
from cerberus import Validator | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
SnapshotStatus = Enum( | ||
"SnapshotStatus", [ | ||
"NOT_STARTED", | ||
"RUNNING", | ||
"COMPLETED", | ||
"FAILED"]) | ||
|
||
|
||
class Snapshot(ABC): | ||
""" | ||
Interface for creating and managing snapshots. | ||
""" | ||
def __init__(self, config: Dict, source_cluster: Cluster, target_cluster: Optional[Cluster] = None) -> None: | ||
self.config = config | ||
self.source_cluster = source_cluster | ||
self.target_cluster = target_cluster | ||
|
||
@abstractmethod | ||
def create(self, *args, **kwargs) -> CommandResult: | ||
"""Create a snapshot.""" | ||
pass | ||
Check warning on line 32 in TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/snapshot.py Codecov / codecov/patchTrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/snapshot.py#L32
|
||
|
||
@abstractmethod | ||
def status(self, *args, **kwargs) -> CommandResult: | ||
"""Get the status of the snapshot.""" | ||
pass | ||
Check warning on line 37 in TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/snapshot.py Codecov / codecov/patchTrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/snapshot.py#L37
|
||
|
||
|
||
S3_SNAPSHOT_SCHEMA = { | ||
'snapshot_name': { | ||
'type': 'string', | ||
'required': True | ||
}, | ||
's3_repo_uri': { | ||
'type': 'string', | ||
'required': True | ||
}, | ||
's3_region': { | ||
'type': 'string', | ||
'required': True | ||
} | ||
} | ||
|
||
|
||
class S3Snapshot(Snapshot): | ||
def __init__(self, config: Dict, source_cluster: Cluster, target_cluster: Optional[Cluster] = None) -> None: | ||
super().__init__(config, source_cluster, target_cluster) | ||
v = Validator(S3_SNAPSHOT_SCHEMA) | ||
if not v.validate(config): | ||
raise ValueError("Invalid config file for snapshot", v.errors) | ||
Check warning on line 61 in TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/snapshot.py Codecov / codecov/patchTrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/snapshot.py#L61
|
||
self.snapshot_name = config['snapshot_name'] | ||
self.s3_repo_uri = config['s3_repo_uri'] | ||
self.s3_region = config['s3_region'] | ||
|
||
def create(self, *args, **kwargs) -> CommandResult: | ||
command = [ | ||
Check warning on line 67 in TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/snapshot.py Codecov / codecov/patchTrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/snapshot.py#L67
|
||
"/root/createSnapshot/bin/CreateSnapshot", | ||
"--snapshot-name", self.snapshot_name, | ||
"--s3-repo-uri", self.s3_repo_uri, | ||
"--s3-region", self.s3_region, | ||
"--source-host", self.source_cluster.endpoint, | ||
"--target-host", self.target_cluster.endpoint, | ||
] | ||
|
||
if self.source_cluster.allow_insecure: | ||
command.append("--source-insecure") | ||
if self.target_cluster.allow_insecure: | ||
command.append("--target-insecure") | ||
Check warning on line 79 in TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/snapshot.py Codecov / codecov/patchTrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/snapshot.py#L76-L79
|
||
|
||
logger.info(f"Creating snapshot with command: {' '.join(command)}") | ||
try: | ||
Check warning on line 82 in TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/snapshot.py Codecov / codecov/patchTrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/snapshot.py#L81-L82
|
||
# Pass None to stdout and stderr to not capture output and show in terminal | ||
subprocess.run(command, stdout=None, stderr=None, text=True, check=True) | ||
logger.info(f"Snapshot {self.config['snapshot_name']} created successfully") | ||
return CommandResult(success=True, value=f"Snapshot {self.config['snapshot_name']} created successfully") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, I kind of forgot that we don't have to do any of the s3 stuff ourselves! 👍 |
||
except subprocess.CalledProcessError as e: | ||
logger.error(f"Failed to create snapshot: {str(e)}") | ||
return CommandResult(success=False, value=f"Failed to create snapshot: {str(e)}") | ||
Check warning on line 89 in TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/snapshot.py Codecov / codecov/patchTrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/snapshot.py#L84-L89
|
||
|
||
def status(self, *args, **kwargs) -> CommandResult: | ||
return CommandResult(success=False, value="Command not implemented") | ||
Check warning on line 92 in TrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/snapshot.py Codecov / codecov/patchTrafficCapture/dockerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/snapshot.py#L92
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Down the line, we likely want to clean this up and use a factory to give us a s3 snapshot vs local vs whatever, but I think it's good for now.