Skip to content

Commit

Permalink
push down verbosity level to kernel command, improve logging around v…
Browse files Browse the repository at this point in the history
…pc lookup
  • Loading branch information
cwensel committed Aug 10, 2023
1 parent e567cc2 commit 2d59c81
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ public abstract class ProcessExec {
private static final Logger LOG = LogManager.getLogger(ProcessExec.class);
protected Supplier<Boolean> dryRun = () -> false;
protected Supplier<Boolean> retry = () -> false;
protected Supplier<Integer> verbosity = () -> 0;

public ProcessExec() {
}

public ProcessExec(Supplier<Boolean> dryRun) {
public ProcessExec(Supplier<Boolean> dryRun, Supplier<Integer> verbosity) {
this.dryRun = dryRun;
this.verbosity = verbosity;
}

public ProcessExec(Supplier<Boolean> dryRun, Supplier<Boolean> retry) {
public ProcessExec(Supplier<Boolean> dryRun, Supplier<Boolean> retry, Supplier<Integer> verbosity) {
this.dryRun = dryRun;
this.retry = retry;
this.verbosity = verbosity;
}

public boolean dryRun() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,47 @@
package clusterless.substrate.aws.resources;

import clusterless.naming.Ref;
import clusterless.substrate.aws.managed.ManagedComponentContext;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awscdk.Fn;
import software.amazon.awscdk.services.ec2.IVpc;
import software.amazon.awscdk.services.ec2.Vpc;
import software.amazon.awscdk.services.ec2.VpcLookupOptions;
import software.constructs.Construct;

import java.util.Objects;

public class Vpcs {
private static final Logger LOG = LoggerFactory.getLogger(Vpcs.class);

public static final String COMMON_VPC = "CommonVpc";
public static final String VPC = "vpc";

public static @NotNull IVpc lookupVpc(Construct scope, @NotNull ManagedComponentContext context) {
// need to be careful where we look up region, as it may not be set yet
return lookupVpc(scope, context.deployable().placement().region());
}

public static @NotNull IVpc lookupVpc(Construct scope, String region) {
String vpcName = Vpcs.bootstrapVPCName(scope);
LOG.info("looking up vpc: {}, in region: {}", vpcName, region);

IVpc vpcLookup = Vpc.fromLookup(
scope,
"VpcLookup",
VpcLookupOptions.builder()
.region(region)
.vpcName(vpcName)
.build()
);

LOG.info("found vpc id: {}", vpcLookup.getVpcId());

return vpcLookup;
}

public static String bootstrapVPCName(Construct scope) {
Objects.requireNonNull(scope, "scope is null");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import org.jetbrains.annotations.NotNull;
import software.amazon.awscdk.services.batch.alpha.FargateComputeEnvironment;
import software.amazon.awscdk.services.batch.alpha.IManagedComputeEnvironment;
import software.amazon.awscdk.services.ec2.Vpc;
import software.amazon.awscdk.services.ec2.VpcLookupOptions;
import software.constructs.Construct;

/**
Expand All @@ -32,26 +30,19 @@ public ComputeResourceConstruct(@NotNull ManagedComponentContext context, @NotNu
String name = Resources.regionallyUniqueProjectName(this, model().computeEnvironmentName());

computeEnvironment = constructWithinHandler(() ->
FargateComputeEnvironment.Builder.create(this, id(model().computeEnvironmentName()))
.computeEnvironmentName(name) // globally unique
.replaceComputeEnvironment(false)
.maxvCpus(4096)
.spot(false)
{
return FargateComputeEnvironment.Builder.create(this, id(model().computeEnvironmentName()))
.computeEnvironmentName(name) // globally unique
.replaceComputeEnvironment(false)
.maxvCpus(4096)
.spot(false)
// .terminateOnUpdate(false)
// .updateTimeout()
// .vpcSubnets()
.vpc(
Vpc.fromLookup(
this,
"VpcLookup",
VpcLookupOptions.builder()
.region(context.managedProject().getRegion())
.vpcName(Vpcs.bootstrapVPCName(this))
.build()
)
)
.enabled(true)
.build()
.vpc(Vpcs.lookupVpc(this, context))
.enabled(true)
.build();
}
);

TagsUtil.applyTags(computeEnvironment, model().tags());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ protected CommonConfig getCommonConfig() {
protected AwsConfig getProviderConfig() {
return kernel.configurations().get(Provider.NAME);
}

protected int verbosityLevel() {
return kernel.verbosity().level();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ public class CDKProcessExec extends ProcessExec {
public CDKProcessExec() {
}

public CDKProcessExec(CommonCommandOptions commandOptions) {
super(commandOptions::dryRun);
public CDKProcessExec(CommonCommandOptions commandOptions, Supplier<Integer> verbosity) {
super(commandOptions::dryRun, verbosity);
}

public CDKProcessExec(Supplier<Boolean> dryRun, Supplier<Boolean> retry) {
super(dryRun, retry);
public CDKProcessExec(Supplier<Boolean> dryRun, Supplier<Boolean> retry, Supplier<Integer> verbosity) {
super(dryRun, retry, verbosity);
}

public String cdk() {
Expand Down Expand Up @@ -137,6 +137,7 @@ public Integer executeLifecycleProcess(@NotNull CommonConfig commonConfig, @NotN
kernelArgs.addAll(Lists.list(OrderedSafeMaps.of("--exclude-arc", commandOptions.excludeArcNames().isEmpty() ? null : String.join(",", commandOptions.excludeArcNames()))));
kernelArgs.addAll(Lists.list(OrderedSafeMaps.of("--only-resource", commandOptions.onlyResourceNames().isEmpty() ? null : String.join(",", commandOptions.onlyResourceNames()))));
kernelArgs.addAll(Lists.list(OrderedSafeMaps.of("--exclude-all-tags", commandOptions.excludeAllTags().map(b -> Boolean.toString(b)).orElse(null))));
kernelArgs.addAll(SafeList.of(verbosity.get() == 0 ? null : String.format("-%s", "v".repeat(verbosity.get()))));

return executeCDKApp(commonConfig, awsConfig, cdkCommand, cdkCommandArgs, "synth", kernelArgs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class Bootstrap extends BaseCDKCommand implements Callable<Integer> {
@CommandLine.Mixin
BootstrapCommandOptions commandOptions = new BootstrapCommandOptions();
@CommandLine.Mixin
CDKProcessExec processExec = new CDKProcessExec(commandOptions::dryRun, commandOptions::retry);
CDKProcessExec processExec = new CDKProcessExec(commandOptions::dryRun, commandOptions::retry, this::verbosityLevel);

@Override
public Integer call() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import clusterless.substrate.aws.cdk.BaseCDKCommand;
import clusterless.substrate.aws.cdk.CDKCommand;
import clusterless.substrate.aws.cdk.CDKProcessExec;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import picocli.CommandLine;

import java.util.concurrent.Callable;
Expand All @@ -22,11 +20,10 @@
name = "deploy"
)
public class Deploy extends BaseCDKCommand implements Callable<Integer> {
private static final Logger LOG = LogManager.getLogger(Deploy.class);
@CommandLine.Mixin
DeployCommandOptions commandOptions = new DeployCommandOptions();
@CommandLine.Mixin
CDKProcessExec processExec = new CDKProcessExec(commandOptions);
CDKProcessExec processExec = new CDKProcessExec(commandOptions, this::verbosityLevel);

@Override
public Integer call() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Destroy extends BaseCDKCommand implements Callable<Integer> {
@CommandLine.Mixin
DestroyCommandOptions commandOptions = new DestroyCommandOptions();
@CommandLine.Mixin
CDKProcessExec processExec = new CDKProcessExec(commandOptions::dryRun, commandOptions::retry);
CDKProcessExec processExec = new CDKProcessExec(commandOptions::dryRun, commandOptions::retry, this::verbosityLevel);

@Override
public Integer call() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Diff extends BaseCDKCommand implements Callable<Integer> {
@CommandLine.Mixin
DiffCommandOptions commandOptions = new DiffCommandOptions();
@CommandLine.Mixin
CDKProcessExec processExec = new CDKProcessExec(commandOptions);
CDKProcessExec processExec = new CDKProcessExec(commandOptions, this::verbosityLevel);

@Override
public Integer call() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Import extends BaseCDKCommand implements Callable<Integer> {
@CommandLine.Mixin
ImportCommandOptions commandOptions = new ImportCommandOptions();
@CommandLine.Mixin
CDKProcessExec processExec = new CDKProcessExec(commandOptions);
CDKProcessExec processExec = new CDKProcessExec(commandOptions, this::verbosityLevel);

@Override
public Integer call() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Verify extends BaseCDKCommand implements Callable<Integer> {
@CommandLine.Mixin
VerifyCommandOptions commandOptions = new VerifyCommandOptions();
@CommandLine.Mixin
CDKProcessExec processExec = new CDKProcessExec(commandOptions);
CDKProcessExec processExec = new CDKProcessExec(commandOptions, this::verbosityLevel);

public Verify() {
}
Expand Down

0 comments on commit 2d59c81

Please sign in to comment.