Skip to content

Commit

Permalink
Merge pull request #260 from kmanning/issue_88
Browse files Browse the repository at this point in the history
Issue 88: hold off on DESTROY decoration and Strategies
  • Loading branch information
kmanning authored Jul 31, 2020
2 parents baeaea4 + 024b9f5 commit 9bac14b
Show file tree
Hide file tree
Showing 22 changed files with 294 additions and 168 deletions.
2 changes: 0 additions & 2 deletions src/AnsiColorPlugin.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import static TerraformEnvironmentStage.PLAN
import static TerraformEnvironmentStage.APPLY
import static TerraformEnvironmentStage.DESTROY

class AnsiColorPlugin implements TerraformEnvironmentStagePlugin {

Expand All @@ -12,7 +11,6 @@ class AnsiColorPlugin implements TerraformEnvironmentStagePlugin {
public void apply(TerraformEnvironmentStage stage) {
stage.decorate(PLAN, addColor())
stage.decorate(APPLY, addColor())
stage.decorate(DESTROY, addColor())
}

public static Closure addColor() {
Expand Down
2 changes: 0 additions & 2 deletions src/ConditionalApplyPlugin.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import static TerraformEnvironmentStage.CONFIRM
import static TerraformEnvironmentStage.APPLY
import static TerraformEnvironmentStage.DESTROY

public class ConditionalApplyPlugin implements TerraformEnvironmentStagePlugin {

Expand All @@ -14,7 +13,6 @@ public class ConditionalApplyPlugin implements TerraformEnvironmentStagePlugin {
public void apply(TerraformEnvironmentStage stage) {
stage.decorateAround(CONFIRM, onlyOnExpectedBranch())
stage.decorateAround(APPLY, onlyOnExpectedBranch())
stage.decorateAround(DESTROY, onlyOnExpectedBranch())
}

public Closure onlyOnExpectedBranch() {
Expand Down
1 change: 0 additions & 1 deletion src/CrqPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class CrqPlugin implements TerraformEnvironmentStagePlugin {
def environment = stage.getEnvironment()

stage.decorate(TerraformEnvironmentStage.APPLY, addCrq(environment))
stage.decorate(TerraformEnvironmentStage.DESTROY, addCrq(environment))
}

public String getCrqEnvironment(String environment) {
Expand Down
53 changes: 0 additions & 53 deletions src/DefaultStrategy.groovy

This file was deleted.

30 changes: 22 additions & 8 deletions src/DestroyPlugin.groovy
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
class DestroyPlugin implements TerraformEnvironmentStagePlugin {
class DestroyPlugin implements TerraformPlanCommandPlugin,
TerraformApplyCommandPlugin {

private static arguments = []
public static DESTROY_CONFIRM_MESSAGE = 'WARNING! Are you absolutely sure the plan above is correct? Your environment will be IMMEDIATELY DESTROYED via "terraform destroy"'
public static DESTROY_OK_MESSAGE = "Run terraform DESTROY now"

public static void init() {
DestroyPlugin plugin = new DestroyPlugin()

ConfirmApplyPlugin.withConfirmMessage('WARNING! Are you absolutely sure the plan above is correct? Your environment will be IMMEDIATELY DESTROYED via "terraform destroy"')
ConfirmApplyPlugin.withOkMessage("Run terraform DESTROY now")
ConfirmApplyPlugin.withConfirmMessage(DESTROY_CONFIRM_MESSAGE)
ConfirmApplyPlugin.withOkMessage(DESTROY_OK_MESSAGE)
TerraformEnvironmentStage.withStageNamePattern { options -> "${options['command']}-DESTROY-${options['environment']}" }

TerraformEnvironmentStage.addPlugin(plugin)
TerraformPlanCommand.addPlugin(plugin)
TerraformApplyCommand.addPlugin(plugin)
}

public void apply(TerraformPlanCommand command) {
command.withArgument('-destroy')
}

public void apply(TerraformApplyCommand command) {
command.withCommand('destroy')
for (arg in arguments) {
command.withArgument(arg)
}
}

public static withArgument(String arg) {
arguments << arg
return this
}

@Override
public void apply(TerraformEnvironmentStage stage) {
stage.withStrategy(new DestroyStrategy(arguments))
public static reset() {
arguments = []
}

}
64 changes: 0 additions & 64 deletions src/DestroyStrategy.groovy

This file was deleted.

1 change: 1 addition & 0 deletions src/Jenkinsfile.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,6 @@ class Jenkinsfile {
public static reset() {
instance = new Jenkinsfile()
original = null
defaultNodeName = null
}
}
63 changes: 55 additions & 8 deletions src/TerraformEnvironmentStage.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ class TerraformEnvironmentStage implements Stage {
private String environment
private StageDecorations decorations
private localPlugins
private static strategy = new DefaultStrategy()

private static final DEFAULT_PLUGINS = [ new ConditionalApplyPlugin(), new ConfirmApplyPlugin(), new DefaultEnvironmentPlugin() ]
private static globalPlugins = DEFAULT_PLUGINS.clone()
private static Closure stageNamePattern

public static final String ALL = 'all'
public static final String PLAN = 'plan'
Expand Down Expand Up @@ -45,13 +45,55 @@ class TerraformEnvironmentStage implements Stage {
Jenkinsfile.build(pipelineConfiguration())
}

public void withStrategy(newStrategy) {
this.strategy = newStrategy
}
public Closure pipelineConfiguration() {
def initCommand = TerraformInitCommand.instanceFor(environment)
def planCommand = TerraformPlanCommand.instanceFor(environment)
def applyCommand = TerraformApplyCommand.instanceFor(environment)

private Closure pipelineConfiguration() {
applyPlugins()
return strategy.createPipelineClosure(environment, decorations)

def String environment = this.environment
return { ->
node(jenkinsfile.getNodeName()) {
deleteDir()
checkout(scm)

decorations.apply(ALL) {
stage(getStageNameFor(PLAN)) {
decorations.apply(PLAN) {
sh initCommand.toString()
sh planCommand.toString()
}
}

decorations.apply("Around-${CONFIRM}") {
// The stage name needs to be editable
stage(getStageNameFor(CONFIRM)) {
decorations.apply(CONFIRM) {
echo "Approved"
}
}
}

decorations.apply("Around-${APPLY}") {
// The stage name needs to be editable
stage(getStageNameFor(APPLY)) {
decorations.apply(APPLY) {
sh initCommand.toString()
sh applyCommand.toString()
}
}
}
}
}
}
}

public String getStageNameFor(String command) {
def pattern = stageNamePattern ?: { options -> "${options['command']}-${options['environment']}" }
def options = [ command: command, environment: environment ]

return pattern.call(options)
}

public void decorate(Closure decoration) {
Expand Down Expand Up @@ -137,8 +179,13 @@ class TerraformEnvironmentStage implements Stage {
return globalPlugins
}

public static void resetPlugins() {
this.globalPlugins = DEFAULT_PLUGINS.clone()
public static withStageNamePattern(Closure stageNamePattern) {
this.stageNamePattern = stageNamePattern
}

public static void reset() {
// This totally jacks with localPlugins
this.globalPlugins = DEFAULT_PLUGINS.clone()
this.stageNamePattern = null
}
}
2 changes: 1 addition & 1 deletion test/AgentNodePluginTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AgentNodePluginTest {
@After
void resetPlugins() {
TerraformValidateStage.resetPlugins()
TerraformEnvironmentStage.resetPlugins()
TerraformEnvironmentStage.reset()
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion test/AnsiColorPluginTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AnsiColorPluginTest {
public class Init {
@After
void resetPlugins() {
TerraformEnvironmentStage.resetPlugins()
TerraformEnvironmentStage.reset()
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion test/CredentialsPluginTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CredentialsPluginTest {
void resetPlugins() {
BuildStage.resetPlugins()
RegressionStage.resetPlugins()
TerraformEnvironmentStage.resetPlugins()
TerraformEnvironmentStage.reset()
TerraformValidateStage.resetPlugins()
CredentialsPlugin.reset()
}
Expand Down
2 changes: 1 addition & 1 deletion test/CrqPluginTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CrqPluginTest {
public class Init {
@After
void resetPlugins() {
TerraformEnvironmentStage.resetPlugins()
TerraformEnvironmentStage.reset()
}

@Test
Expand Down
Loading

0 comments on commit 9bac14b

Please sign in to comment.