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 command to sync configs between regions (#120)
* add command to sync configs between regions
- Loading branch information
1 parent
c253bc2
commit af7381e
Showing
10 changed files
with
193 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,4 @@ | |
|
||
group=com.nike | ||
artifactId=cerberus-lifecycle-cli | ||
version=4.4.0 | ||
version=4.5.0 |
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
69 changes: 69 additions & 0 deletions
69
src/main/java/com/nike/cerberus/command/core/SyncConfigCommand.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,69 @@ | ||
/* | ||
* Copyright (c) 2018 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.core; | ||
|
||
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.core.SyncConfigOperation; | ||
|
||
import static com.nike.cerberus.command.core.UpdateStackCommand.COMMAND_NAME; | ||
|
||
|
||
/** | ||
* Command for syncing configs between regions. | ||
*/ | ||
@Parameters(commandNames = COMMAND_NAME, commandDescription = "Syncs configs between regions. Recursively copies all files from the source region's config bucket (as defined in --region) to the destination region's config bucket.") | ||
public class SyncConfigCommand implements Command { | ||
|
||
public static final String COMMAND_NAME = "sync-config"; | ||
public static final String DESTINATION_REGION_LONG_ARG = "--destination-region"; | ||
public static final String DRY_LONG_ARG = "--dry"; | ||
public static final String ALL_LONG_ARG = "--all"; | ||
|
||
@Parameter(names = {DESTINATION_REGION_LONG_ARG}, description = "The destination region") | ||
private String destinationRegionName; | ||
|
||
@Parameter(names = {DRY_LONG_ARG}, description = "Displays destination buckets and files to be copied over without actually running them") | ||
private boolean dryrun; | ||
|
||
@Parameter(names = {ALL_LONG_ARG}, description = "Sync up all regions as defines in the environment data") | ||
private boolean all; | ||
|
||
public String getDestinationRegionName() { | ||
return destinationRegionName; | ||
} | ||
|
||
public boolean isDryrun() { | ||
return dryrun; | ||
} | ||
|
||
public boolean isAll() { | ||
return all; | ||
} | ||
|
||
@Override | ||
public String getCommandName() { | ||
return COMMAND_NAME; | ||
} | ||
|
||
@Override | ||
public Class<? extends Operation<?>> getOperationClass() { | ||
return SyncConfigOperation.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
80 changes: 80 additions & 0 deletions
80
src/main/java/com/nike/cerberus/operation/core/SyncConfigOperation.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,80 @@ | ||
/* | ||
* Copyright (c) 2018 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.core; | ||
|
||
import com.amazonaws.regions.Regions; | ||
import com.nike.cerberus.command.core.SyncConfigCommand; | ||
import com.nike.cerberus.operation.Operation; | ||
import com.nike.cerberus.store.ConfigStore; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.nike.cerberus.module.CerberusModule.CONFIG_REGION; | ||
|
||
/** | ||
* Operation for syncing configs between regions. | ||
*/ | ||
public class SyncConfigOperation implements Operation<SyncConfigCommand> { | ||
|
||
|
||
private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
private final ConfigStore configStore; | ||
|
||
private Regions configRegion; | ||
|
||
@Inject | ||
public SyncConfigOperation(ConfigStore configStore, @Named(CONFIG_REGION) String configRegion) { | ||
this.configStore = configStore; | ||
this.configRegion = Regions.fromName(configRegion); | ||
} | ||
|
||
@Override | ||
public void run(SyncConfigCommand command) { | ||
List<Regions> destinationRegions = command.isAll() ? configStore.getSyncDestinationRegions() : Arrays.asList(Regions.fromName(command.getDestinationRegionName())); | ||
|
||
if (command.isDryrun()){ | ||
logger.info("Destination buckets: {}", destinationRegions.stream() | ||
.map(region -> configStore.getConfigBucketForRegion(region)).collect(Collectors.joining(", "))); | ||
logger.info("Files to be copied over:"); | ||
configStore.listKeys().forEach(key -> logger.info(key)); | ||
} else { | ||
for (Regions region: destinationRegions) { | ||
logger.info("Destination bucket: {}", configStore.getConfigBucketForRegion(region)); | ||
configStore.sync(region); | ||
} | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public boolean isRunnable(SyncConfigCommand command) { | ||
boolean isRunnable = true; | ||
if (!command.isAll() && configRegion.equals(Regions.fromName(command.getDestinationRegionName()))){ | ||
isRunnable = false; | ||
logger.error("The source and destination region must be different."); | ||
} | ||
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
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