Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Multi-Region CMS Support (hot standby / pilot light) (#134)
Browse files Browse the repository at this point in the history
* Work in progress of multi-region pilot light cms update.

* Fix find bugs issue

* Merge master, update arg mapper tests, add missing license headers

* Update CompositeOperation.java

* Update WhitelistCidrForVpcAccessOperation.java
  • Loading branch information
fieldju authored Apr 15, 2019
1 parent 36dda5b commit 78d7472
Show file tree
Hide file tree
Showing 31 changed files with 467 additions and 178 deletions.
6 changes: 2 additions & 4 deletions src/main/java/com/nike/cerberus/cli/CerberusRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,12 @@
import com.nike.cerberus.command.cms.CreateCmsClusterCommand;
import com.nike.cerberus.command.cms.CreateCmsConfigCommand;
import com.nike.cerberus.command.cms.UpdateCmsConfigCommand;
import com.nike.cerberus.command.composite.CreateEnvironmentCommand;
import com.nike.cerberus.command.composite.DeleteEnvironmentCommand;
import com.nike.cerberus.command.composite.*;
import com.nike.cerberus.command.certificates.GenerateAndRotateCertificatesCommand;
import com.nike.cerberus.command.composite.PrintAllStackInformationCommand;
import com.nike.cerberus.command.certificates.RotateCertificatesCommand;
import com.nike.cerberus.command.core.CreateAlbLogAthenaDbAndTableCommand;
import com.nike.cerberus.command.core.InitializeEnvironmentCommand;
import com.nike.cerberus.command.core.SyncConfigCommand;
import com.nike.cerberus.command.composite.UpdateAllStackTagsCommand;
import com.nike.cerberus.command.rds.CleanUpRdsSnapshotsCommand;
import com.nike.cerberus.command.rds.CopyRdsSnapshotsCommand;
import com.nike.cerberus.command.rds.CreateDatabaseCommand;
Expand Down Expand Up @@ -242,6 +239,7 @@ private void registerAllCommands() {
registerCommand(new UpdateAllStackTagsCommand());
registerCommand(new SyncConfigCommand());
registerCommand(new CreateAlbLogAthenaDbAndTableCommand());
registerCommand(new CreateCmsResourcesForRegionCommand());
}

/**
Expand Down
154 changes: 88 additions & 66 deletions src/main/java/com/nike/cerberus/cli/EnvironmentConfigToArgsMapper.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class UpdateCmsConfigCommand implements Command {
@Parameter(names = FORCE_ARG, description = "Force allow overwriting of system generated property. This may break your configuration.")
private boolean force = false;

@Parameter(names = IGNORE_DEFAULT_CONFIGURATIONS_ARGUMENT, description = "Ignores default configurations of the CMS. ")
@Parameter(names = IGNORE_DEFAULT_CONFIGURATIONS_ARGUMENT, description = "Ignores default configurations of the CMS.")
private boolean ignoreDefaultConfigurations = false;

public String getAdminGroup() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.composite;

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.composite.CreateCmsResourcesForRegionOperation;

import static com.nike.cerberus.command.composite.CreateEnvironmentCommand.COMMAND_NAME;
import static com.nike.cerberus.domain.cloudformation.CloudFormationParametersDelegate.STACK_REGION;
import static com.nike.cerberus.domain.cloudformation.CloudFormationParametersDelegate.STACK_REGION_DESCRIPTION;

@Parameters(
commandNames = {
COMMAND_NAME
},
commandDescription = "Stands up the resources needed to have CMS running in a given region"
)
public class CreateCmsResourcesForRegionCommand implements Command {

public static final String COMMAND_NAME = "create-resources-for-secondary-region";

@Parameter(names = STACK_REGION, description = STACK_REGION_DESCRIPTION, required = true)
private String stackRegion;

public String getStackRegion() {
return stackRegion;
}

@Override
public String getCommandName() {
return COMMAND_NAME;
}

@Override
public Class<? extends Operation<?>> getOperationClass() {
return CreateCmsResourcesForRegionOperation.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@

package com.nike.cerberus.command.core;

import com.amazonaws.regions.Regions;
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.CreateAlbLogAthenaDbAndTableOperation;

import java.util.Optional;

import static com.nike.cerberus.command.audit.CreateAuditAthenaDbAndTableCommand.COMMAND_NAME;
import static com.nike.cerberus.domain.cloudformation.CloudFormationParametersDelegate.STACK_REGION;
import static com.nike.cerberus.domain.cloudformation.CloudFormationParametersDelegate.STACK_REGION_DESCRIPTION;

@Parameters(
commandNames = COMMAND_NAME,
Expand All @@ -31,6 +37,13 @@ public class CreateAlbLogAthenaDbAndTableCommand implements Command {

public static final String COMMAND_NAME = "create-alb-log-athena-db-and-table";

@Parameter(names = STACK_REGION, description = STACK_REGION_DESCRIPTION)
private String stackRegion;

public Optional<Regions> getStackRegion() {
return stackRegion == null ? Optional.empty() : Optional.of(Regions.fromName(stackRegion));
}

@Override
public String getCommandName() {
return COMMAND_NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@

package com.nike.cerberus.command.core;

import com.amazonaws.regions.Regions;
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.CreateEdgeDomainRecordOperation;

import java.util.Optional;

import static com.nike.cerberus.command.core.CreateEdgeDomainRecordCommand.COMMAND_NAME;
import static com.nike.cerberus.domain.cloudformation.CloudFormationParametersDelegate.STACK_REGION;
import static com.nike.cerberus.domain.cloudformation.CloudFormationParametersDelegate.STACK_REGION_DESCRIPTION;

/**
* Command to create the edge domain Route53 record for Cerberus.
Expand All @@ -39,6 +44,9 @@ public class CreateEdgeDomainRecordCommand implements Command {

public static final String EDGE_DOMAIN_NAME_OVERRIDE = "--edge-domain-name-override";

@Parameter(names = STACK_REGION, description = STACK_REGION_DESCRIPTION)
private String stackRegion;

@Parameter(names = BASE_DOMAIN_NAME_LONG_ARG,
description = "The base domain name for Cerberus (e.g. url: https://env.cerberus.example.com => base hostname: cerberus.example.com)",
required = true)
Expand All @@ -53,6 +61,10 @@ public class CreateEdgeDomainRecordCommand implements Command {
required = true)
private String hostedZoneId;

public Optional<Regions> getStackRegion() {
return stackRegion == null ? Optional.empty() : Optional.of(Regions.fromName(stackRegion));
}

public String getBaseDomainName() {
return baseDomainName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.nike.cerberus.util.StackConverter;

import static com.nike.cerberus.command.core.DeleteStackCommand.COMMAND_NAME;
import static com.nike.cerberus.domain.cloudformation.CloudFormationParametersDelegate.STACK_REGION;

@Parameters(
commandNames = {
Expand All @@ -38,8 +39,6 @@ public class DeleteStackCommand implements Command {

public static final String STACK_NAME_LONG_ARG = "--stack-name";

public static final String REGION_LONG_ARG = "--region";

@Parameter(
names = {STACK_NAME_LONG_ARG},
required = true,
Expand All @@ -48,7 +47,7 @@ public class DeleteStackCommand implements Command {
private Stack stack;

@Parameter(
names = {REGION_LONG_ARG},
names = {STACK_REGION},
description = "Region to delete stack in, defaults to primary region"
)
private String region;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.nike.cerberus.command.core;

import com.amazonaws.regions.Regions;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.nike.cerberus.command.Command;
Expand All @@ -24,8 +25,11 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static com.nike.cerberus.command.core.WhitelistCidrForVpcAccessCommand.COMMAND_NAME;
import static com.nike.cerberus.domain.cloudformation.CloudFormationParametersDelegate.STACK_REGION;
import static com.nike.cerberus.domain.cloudformation.CloudFormationParametersDelegate.STACK_REGION_DESCRIPTION;

/**
* Command for granting CIDRs ingress to specific ports within the Cerberus VPC.
Expand All @@ -43,6 +47,9 @@ public class WhitelistCidrForVpcAccessCommand implements Command {
@Parameter(names = PORT_LONG_ARG, description = "The ports to grant ingress on within the Cerberus VPC.")
private List<Integer> ports = new ArrayList<>();

@Parameter(names = STACK_REGION, description = STACK_REGION_DESCRIPTION)
private String stackRegion;

public List<String> getCidrs() {
return cidrs;
}
Expand All @@ -51,6 +58,10 @@ public List<Integer> getPorts() {
return ports;
}

public Optional<Regions> getStackRegion() {
return stackRegion == null ? Optional.empty() : Optional.of(Regions.fromName(stackRegion));
}

@Override
public String getCommandName() {
return COMMAND_NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public String getInstanceClass() {
description = "option for setting a snapshot identifier on the RDS stack to restore from the snapshot " +
"while standing up the new RDS cluster via cloudformation"
)
String snapshotIdentifier;
private String snapshotIdentifier;

public String getSnapshotIdentifier() {
return snapshotIdentifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

package com.nike.cerberus.domain.cloudformation;

import com.amazonaws.regions.Regions;
import com.beust.jcommander.DynamicParameter;
import com.beust.jcommander.Parameter;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
* CloudFormation input parameters common to all Cerberus CloudFormation stacks.
Expand All @@ -29,6 +32,9 @@ public class CloudFormationParametersDelegate {
public static final String TAG_LONG_ARG = "--TAG";
public static final String TAG_SHORT_ARG = "-T";

public static final String STACK_REGION = "--stack-region";
public static final String STACK_REGION_DESCRIPTION = "The region for the stack, ex: us-west-2, will default to the primary region.";

@DynamicParameter(
names = {
TAG_LONG_ARG,
Expand All @@ -38,10 +44,17 @@ public class CloudFormationParametersDelegate {
)
private Map<String, String> tags = new HashMap<>();

@Parameter(names = STACK_REGION, description = STACK_REGION_DESCRIPTION)
private String stackRegion;

public Map<String, String> getTags() {
return tags;
}

public Optional<Regions> getStackRegion() {
return stackRegion == null ? Optional.empty() : Optional.of(Regions.fromName(stackRegion));
}

public String[] getArgs(){
return tags.entrySet()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
public class CmsParameters implements LaunchConfigParameters {

private String baseStackName;
private String cmsIamRoleName;

private String loadBalancerStackName;

Expand All @@ -38,12 +38,12 @@ public class CmsParameters implements LaunchConfigParameters {
@JsonUnwrapped
private LaunchConfigParametersDelegate launchConfigParameters = new LaunchConfigParametersDelegate();

public String getBaseStackName() {
return baseStackName;
public String getCmsIamRoleName() {
return cmsIamRoleName;
}

public CmsParameters setBaseStackName(String baseStackName) {
this.baseStackName = baseStackName;
public CmsParameters setCmsIamRoleName(String cmsIamRoleName) {
this.cmsIamRoleName = cmsIamRoleName;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.google.common.collect.MoreCollectors.onlyElement;

/**
* Stores all YAML data for a given Cerberus environment
Expand Down Expand Up @@ -193,13 +196,19 @@ public RegionSpecificConfigurationInput getPrimaryRegionConfig() {
return getPrimaryEntry().getValue();
}

public RegionSpecificConfigurationInput getRegionConfig(String region) {
return Optional.ofNullable(getRegionSpecificConfiguration().get(region))
.orElseThrow(() -> new RuntimeException(String
.format("Failed to find region config for %s in region specific config", region)));
}

public String getPrimaryRegion() {
return getPrimaryEntry().getKey();
}

private Map.Entry<String, RegionSpecificConfigurationInput> getPrimaryEntry() {
return regionSpecificConfiguration.entrySet().stream()
.filter(entry -> entry.getValue() != null && entry.getValue().isPrimary()).findFirst()
.orElseThrow(() -> new RuntimeException("Failed to find primary region in region specific config"));
.filter(entry -> entry.getValue() != null && entry.getValue().isPrimary())
.collect(onlyElement());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void run(CreateAuditAthenaDbAndTableCommand command) {

log.info("Creating Athena DB");
String createDb = "CREATE DATABASE IF NOT EXISTS " + databaseName + ";";
log.info(athenaService.executeAthenaQuery(createDb, bucketName).toString());
log.info(athenaService.executeAthenaQuery(createDb, bucketName, configStore.getPrimaryRegion()).toString());
log.info("Creating table");
String createAuditTable;
try {
Expand All @@ -91,7 +91,7 @@ public void run(CreateAuditAthenaDbAndTableCommand command) {
} catch (IOException e) {
throw new RuntimeException("failed to load create athena table template", e);
}
log.info(athenaService.executeAthenaQuery(createAuditTable, bucketName).toString());
log.info(athenaService.executeAthenaQuery(createAuditTable, bucketName, configStore.getPrimaryRegion()).toString());
}

@Override
Expand Down
Loading

0 comments on commit 78d7472

Please sign in to comment.