-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial migration console snapshot support
Signed-off-by: Andre Kurait <akurait@amazon.com>
- Loading branch information
1 parent
7ca269f
commit 4916515
Showing
11 changed files
with
249 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...Solution/src/main/docker/migrationConsole/lib/console_link/console_link/logic/snapshot.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
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}" | ||
|
||
if result.success: | ||
return SnapshotStatus.COMPLETED, "Snapshot created successfully." + "\n" + result.value | ||
return SnapshotStatus.FAILED, "Snapshot creation failed." + "\n" + result.value | ||
|
||
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 |
85 changes: 85 additions & 0 deletions
85
...olution/src/main/docker/migrationConsole/lib/console_link/console_link/models/snapshot.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from abc import ABC, abstractmethod | ||
from enum import Enum | ||
import logging | ||
import subprocess | ||
from typing import Dict, Optional, Tuple | ||
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 | ||
|
||
@abstractmethod | ||
def status(self, *args, **kwargs) -> CommandResult: | ||
"""Get the status of the snapshot.""" | ||
pass | ||
|
||
|
||
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) | ||
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 = [ | ||
"/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 or self.target_cluster.allow_insecure: | ||
command.append("--insecure") | ||
logger.info(f"Creating snapshot with command: {' '.join(command)}") | ||
try: | ||
subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, 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") | ||
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)} {e.output} {e.stderr}") | ||
def status(self, *args, **kwargs) -> CommandResult: | ||
return CommandResult(success=False, value=f"Command not implemented") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.