Commit d445acfe authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

Merge branch '63-auto-stop-environments' into 'master'

Resolve "auto-stop environments"

Closes #63

See merge request to-be-continuous/terraform!93
parents 93705bfe 244cc491
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -342,6 +342,7 @@ Here are variables supported to configure review environments:
| `TF_REVIEW_PLAN_OPTS`    | Terraform extra [plan options](https://developer.hashicorp.com/terraform/cli/commands/plan) for `review` env | _none_ |
| `TF_REVIEW_APPLY_OPTS`   | Terraform extra [apply options](https://developer.hashicorp.com/terraform/cli/commands/apply) for `review` env | `$TF_APPLY_OPTS` |
| `TF_REVIEW_DESTROY_OPTS` | Terraform extra [destroy options](https://developer.hashicorp.com/terraform/cli/commands/destroy) for `review` env | `$TF_DESTROY_OPTS` |
| `TF_REVIEW_AUTOSTOP_DURATION`| The amount of time before GitLab will automatically stop `review` environments | `4 hours` |

#### Enabling auto-cleanup

@@ -376,6 +377,7 @@ Here are variables supported to configure the integration environment:
| `TF_INTEG_PLAN_OPTS`    | Terraform extra [plan options](https://developer.hashicorp.com/terraform/cli/commands/plan) for `integration` env | _none_ |
| `TF_INTEG_APPLY_OPTS`   | Terraform extra [apply options](https://developer.hashicorp.com/terraform/cli/commands/apply) for `integration` env | `$TF_APPLY_OPTS` |
| `TF_INTEG_DESTROY_OPTS` | Terraform extra [destroy options](https://developer.hashicorp.com/terraform/cli/commands/destroy) for `integration` env | `$TF_DESTROY_OPTS` |
| `TF_INTEG_AUTOSTOP_DURATION`| The amount of time before GitLab will automatically stop the `integration` env | `never` |

### Staging environment configuration

@@ -396,6 +398,7 @@ Here are variables supported to configure the staging environment:
| `TF_STAGING_PLAN_OPTS`    | Terraform extra [plan options](https://developer.hashicorp.com/terraform/cli/commands/plan) for `staging` env | _none_ |
| `TF_STAGING_APPLY_OPTS`   | Terraform extra [apply options](https://developer.hashicorp.com/terraform/cli/commands/apply) for `staging` env | `$TF_APPLY_OPTS` |
| `TF_STAGING_DESTROY_OPTS` | Terraform extra [destroy options](https://developer.hashicorp.com/terraform/cli/commands/destroy) for `staging` env | `$TF_DESTROY_OPTS` |
| `TF_STAGING_AUTOSTOP_DURATION`| The amount of time before GitLab will automatically stop the `staging` env | `never` |

### Production environment configuration

+15 −0
Original line number Diff line number Diff line
@@ -286,6 +286,11 @@
          "description": "Terraform extra [destroy options](https://developer.hashicorp.com/terraform/cli/commands/destroy) for `review` env",
          "default": "$TF_DESTROY_OPTS",
          "advanced": true
        },
        {
          "name": "TF_REVIEW_AUTOSTOP_DURATION",
          "description": "The amount of time before GitLab will automatically stop `review` environments",
          "default": "4 hours"
        }
      ]
    },
@@ -336,6 +341,11 @@
          "description": "Terraform extra [destroy options](https://developer.hashicorp.com/terraform/cli/commands/destroy) for `integration` env",
          "default": "$TF_DESTROY_OPTS",
          "advanced": true
        },
        {
          "name": "TF_INTEG_AUTOSTOP_DURATION",
          "description": "The amount of time before GitLab will automatically stop the `integration` env",
          "default": "never"
        }
      ]
    },
@@ -386,6 +396,11 @@
          "description": "Terraform extra [destroy options](https://developer.hashicorp.com/terraform/cli/commands/destroy) for `staging` env",
          "default": "$TF_DESTROY_OPTS",
          "advanced": true
        },
        {
          "name": "TF_STAGING_AUTOSTOP_DURATION",
          "description": "The amount of time before GitLab will automatically stop the `staging` env",
          "default": "never"
        }
      ]
    },
+25 −18
Original line number Diff line number Diff line
@@ -86,6 +86,11 @@ variables:
  TF_SCRIPTS_DIR: "."
  TF_OUTPUT_DIR: "tf-output"

  # auto-stop environments
  TF_REVIEW_AUTOSTOP_DURATION: "4 hours"
  TF_INTEG_AUTOSTOP_DURATION: "never"
  TF_STAGING_AUTOSTOP_DURATION: "never"

  # separated tf plan is enabled for production by default
  TF_PROD_PLAN_ENABLED: "true"

@@ -372,8 +377,8 @@ stages:
  }

  function tf_init() {
    opts=$1
    extra_opts=$2
    opts=${ENV_INIT_OPTS:-$TF_INIT_OPTS}
    extra_opts=${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}

    log_info "--- \\e[32minit\\e[0m"
    log_info "--- Positioned environment (can also be used as TF vars):"
@@ -463,7 +468,7 @@ stages:
  }

  function tf_workspace_select() {
    workspace=$1
    workspace=${ENV_WORKSPACE:-$TF_WORKSPACE}

    # select workspace if configured
    if [[ -n "$workspace" ]]
@@ -480,10 +485,10 @@ stages:
  }

  tf_plan() {
    opts=$1
    extra_opts=$2
    tf_plan=$3
    gitlab_report=$4
    opts=${ENV_PLAN_OPTS:-$TF_PLAN_OPTS}
    extra_opts=${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}
    tf_plan=${ENV_TYPE}.tfplan
    gitlab_report=${ENV_TYPE}-plan.json

    # shellcheck disable=SC2154
    log_info "--- \\e[32mplan\\e[0m"
@@ -512,11 +517,10 @@ stages:
    terraform show --json "$tf_plan" | jq -r '([.resource_changes[]?.change.actions?]|flatten)|{"create":(map(select(.=="create"))|length),"update":(map(select(.=="update"))|length),"delete":(map(select(.=="delete"))|length)}' > "$gitlab_report"
  }

  # $tf_plan might be propagated by dotenv artifact (optional)
  function tf_apply() {
    opts=$1
    extra_opts=$2
    # optional arg
    tf_plan=$3
    opts=${ENV_APPLY_OPTS:-$TF_APPLY_OPTS}
    extra_opts=${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}

    # shellcheck disable=SC2154
    log_info "--- \\e[32mapply\\e[0m"
@@ -565,8 +569,8 @@ stages:
  }

  function tf_destroy() {
    opts=$1
    extra_opts=$2
    opts=${ENV_DESTROY_OPTS:-$TF_DESTROY_OPTS}
    extra_opts=${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}

    # shellcheck disable=SC2154
    log_info "--- \\e[32mdestroy\\e[0m"
@@ -631,11 +635,11 @@ stages:

.tf-commands:
  default:
    init: tf_init "${ENV_INIT_OPTS:-$TF_INIT_OPTS}" "${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}"
    select_workspace: tf_workspace_select "${ENV_WORKSPACE:-$TF_WORKSPACE}"
    plan: tf_plan "${ENV_PLAN_OPTS:-$TF_PLAN_OPTS}" "${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}" "${ENV_TYPE}.tfplan" "${ENV_TYPE}-plan.json"
    apply: tf_apply "${ENV_APPLY_OPTS:-$TF_APPLY_OPTS}" "${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}" "$tfplan"
    destroy: tf_destroy "${ENV_DESTROY_OPTS:-$TF_DESTROY_OPTS}" "${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}"
    init: tf_init
    select_workspace: tf_workspace_select
    plan: tf_plan
    apply: tf_apply
    destroy: tf_destroy
  init:
    - !reference [ .tf-commands, default, init ]
  select_workspace:
@@ -989,6 +993,7 @@ tf-destroy-review:
  environment:
    name: review/$CI_COMMIT_REF_NAME
    action: stop
    auto_stop_in: "$TF_REVIEW_AUTOSTOP_DURATION"
  resource_group: tf-review/$CI_COMMIT_REF_NAME
  rules:
    # exclude tags
@@ -1064,6 +1069,7 @@ tf-destroy-integration:
  environment:
    name: integration
    action: stop
    auto_stop_in: "$TF_INTEG_AUTOSTOP_DURATION"
  resource_group: tf-integration
  rules:
    # only on integration branch(es), with $TF_INTEG_ENABLED set
@@ -1136,6 +1142,7 @@ tf-destroy-staging:
  environment:
    name: staging
    action: stop
    auto_stop_in: "$TF_STAGING_AUTOSTOP_DURATION"
  resource_group: tf-staging
  rules:
    # only on production branch(es), with $TF_STAGING_ENABLED set