This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cross region RDS cluster restore command (#137)
* Add cross region RDS cluster restore command * Change command name to better reflect it's nature
- Loading branch information
1 parent
a518dae
commit c442a75
Showing
9 changed files
with
323 additions
and
13 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
71 changes: 71 additions & 0 deletions
71
src/main/java/com/nike/cerberus/command/rds/XRegionDatabaseReplicationCommand.java
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,71 @@ | ||
/* | ||
* Copyright (c) 2019 Nike, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.nike.cerberus.command.rds; | ||
|
||
import com.beust.jcommander.Parameter; | ||
import com.beust.jcommander.Parameters; | ||
import com.nike.cerberus.command.Command; | ||
import com.nike.cerberus.operation.Operation; | ||
import com.nike.cerberus.operation.rds.XRegionDatabaseReplicationOperation; | ||
|
||
import static com.nike.cerberus.command.rds.CopyRdsSnapshotsCommand.COMMAND_NAME; | ||
|
||
@Parameters( | ||
commandNames = COMMAND_NAME, | ||
commandDescription = "Restores RDS cluster in the target region for this environment from " + | ||
"a fresh RDS cluster snapshot created in the source region" | ||
) | ||
public class XRegionDatabaseReplicationCommand implements Command { | ||
|
||
public static final String COMMAND_NAME = "x-region-database-replication"; | ||
|
||
public static final String TARGET_REGION_LONG_ARG = "--target-region"; | ||
|
||
public static final String SOURCE_REGION_LONG_ARG = "--source-region"; | ||
|
||
@Parameter( | ||
names = TARGET_REGION_LONG_ARG, | ||
description = "The AWS Region to restore RDS cluster snapshot in", | ||
required = true | ||
) | ||
private String targetRegion; | ||
|
||
public String getTargetRegion() { | ||
return targetRegion; | ||
} | ||
|
||
@Parameter( | ||
names = SOURCE_REGION_LONG_ARG, | ||
description = "The AWS Region to create RDS cluster snapshot in", | ||
required = true | ||
) | ||
private String sourceRegion; | ||
|
||
public String getSourceRegion() { | ||
return sourceRegion; | ||
} | ||
|
||
@Override | ||
public String getCommandName() { | ||
return COMMAND_NAME; | ||
} | ||
|
||
@Override | ||
public Class<? extends Operation<?>> getOperationClass() { | ||
return XRegionDatabaseReplicationOperation.class; | ||
} | ||
} |
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
146 changes: 146 additions & 0 deletions
146
src/main/java/com/nike/cerberus/operation/rds/XRegionDatabaseReplicationOperation.java
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,146 @@ | ||
/* | ||
* Copyright (c) 2019 Nike, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.nike.cerberus.operation.rds; | ||
|
||
import com.amazonaws.AmazonServiceException; | ||
import com.amazonaws.regions.Regions; | ||
import com.amazonaws.services.cloudformation.model.StackResourceSummary; | ||
import com.amazonaws.services.rds.model.DBClusterSnapshot; | ||
import com.google.common.collect.ImmutableList; | ||
import com.nike.cerberus.command.rds.XRegionDatabaseReplicationCommand; | ||
import com.nike.cerberus.domain.environment.Stack; | ||
import com.nike.cerberus.operation.Operation; | ||
import com.nike.cerberus.service.CloudFormationService; | ||
import com.nike.cerberus.service.RdsService; | ||
import com.nike.cerberus.store.ConfigStore; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static com.nike.cerberus.module.CerberusModule.ENV_NAME; | ||
import static com.google.common.collect.MoreCollectors.onlyElement; | ||
|
||
/** | ||
* Operation for XRegionDatabaseReplicationCommand | ||
* | ||
* Restores rds cluster in the target region for this environment from a fresh RDS cluster snapshot created in the source region | ||
*/ | ||
public class XRegionDatabaseReplicationOperation implements Operation<XRegionDatabaseReplicationCommand> { | ||
|
||
private final Logger log = LoggerFactory.getLogger(getClass()); | ||
|
||
private final ConfigStore configStore; | ||
private final String environmentName; | ||
private final RdsService rdsService; | ||
private final CloudFormationService cloudFormationService; | ||
|
||
@Inject | ||
public XRegionDatabaseReplicationOperation(ConfigStore configStore, | ||
@Named(ENV_NAME) String environmentName, | ||
RdsService rdsService, | ||
CloudFormationService cloudFormationService) { | ||
|
||
this.configStore = configStore; | ||
this.environmentName = environmentName; | ||
this.rdsService = rdsService; | ||
this.cloudFormationService = cloudFormationService; | ||
} | ||
|
||
@Override | ||
public void run(XRegionDatabaseReplicationCommand command) { | ||
Regions sourceRegion = Regions.fromName(command.getSourceRegion()); | ||
Regions targetRegion = Regions.fromName(command.getTargetRegion()); | ||
|
||
String stackName = Stack.DATABASE.getFullName(environmentName); | ||
List<StackResourceSummary> stackResources = cloudFormationService.getStackResources(sourceRegion, stackName); | ||
StackResourceSummary sourceDbCluster = stackResources | ||
.stream() | ||
.filter(resource -> "CmsDatabaseCluster".equals(resource.getLogicalResourceId())) | ||
.collect(onlyElement()); | ||
String sourceDbClusterId = sourceDbCluster.getPhysicalResourceId(); | ||
|
||
log.info("Preparing to create snapshot of RDS cluster in region: {}", sourceRegion); | ||
DBClusterSnapshot sourceSnapshot = rdsService.createSnapshot(sourceDbClusterId, sourceRegion); | ||
rdsService.waitForSnapshotsToBecomeAvailable(sourceSnapshot, sourceRegion); | ||
|
||
log.info("Preparing to initiate copy of RDS DB snapshot: {} located in region: {} to region: {}", | ||
sourceSnapshot.getDBClusterSnapshotIdentifier(), sourceRegion.getName(), targetRegion.getName()); | ||
DBClusterSnapshot copiedSnapshot = rdsService.copySnapshot(sourceSnapshot, sourceRegion, targetRegion); | ||
rdsService.waitForSnapshotsToBecomeAvailable(copiedSnapshot, targetRegion); | ||
|
||
rdsService.deleteSnapshot(sourceSnapshot, sourceRegion); | ||
|
||
String databasePassword = configStore.getCmsDatabasePassword() | ||
.orElseThrow(() -> new RuntimeException("Expected the database password to exist")); | ||
Map<String, String> parameters = cloudFormationService.getStackParameters(targetRegion, stackName); | ||
parameters.put("snapshotIdentifier", copiedSnapshot.getDBClusterSnapshotIdentifier()); | ||
parameters.put("cmsDbMasterPassword", databasePassword); | ||
|
||
try { | ||
log.info("Preparing to initiate restore of RDS DB snapshot {} in region {}", | ||
copiedSnapshot.getDBClusterSnapshotIdentifier(), | ||
targetRegion); | ||
|
||
cloudFormationService.updateStackAndWait( | ||
targetRegion, | ||
Stack.DATABASE, | ||
parameters, | ||
true, | ||
false, | ||
null, | ||
true); | ||
|
||
log.info("Restore complete."); | ||
} catch (AmazonServiceException ase) { | ||
if (ase.getStatusCode() == 400 && | ||
StringUtils.equalsIgnoreCase(ase.getErrorMessage(), "No updates are to be performed.")) { | ||
log.warn("CloudFormation reported no changes detected."); | ||
} else { | ||
throw ase; | ||
} | ||
} | ||
|
||
rdsService.deleteSnapshot(copiedSnapshot, targetRegion); | ||
} | ||
|
||
|
||
|
||
@Override | ||
public boolean isRunnable(XRegionDatabaseReplicationCommand command) { | ||
boolean isRunnable = true; | ||
Regions targetRegion = Regions.fromName(command.getTargetRegion()); | ||
Regions sourceRegion = Regions.fromName(command.getSourceRegion()); | ||
|
||
ImmutableList<Regions> regions = ImmutableList.of(targetRegion, sourceRegion); | ||
if (!configStore.getCmsRegions().containsAll(regions)) { | ||
log.error("The source and target regions must be configured for the environment"); | ||
isRunnable = false; | ||
} | ||
|
||
if (isRunnable && !cloudFormationService.isStackPresent(targetRegion, Stack.DATABASE.getFullName(environmentName))) { | ||
log.error("The Database stack must exist in the target region in order to restore snapshot"); | ||
isRunnable = false; | ||
} | ||
|
||
return isRunnable; | ||
} | ||
} |
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.