Skip to content

Commit

Permalink
Merge pull request #4 from nirdosh17/release_v1.0.0
Browse files Browse the repository at this point in the history
- Allow `MAX_DELETE_RETRY_COUNT` and `STACK_WAIT_TIME_SECONDS` to be configurable
- Add Integration tests
  • Loading branch information
nirdosh17 authored Aug 15, 2024
2 parents fd18ecf + 8b2e958 commit 4477fae
Show file tree
Hide file tree
Showing 19 changed files with 580 additions and 174 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ jobs:
steps:
-
name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: 1.16
go-version: 1.22
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
uses: goreleaser/goreleaser-action@v6
with:
# either 'goreleaser' (default) or 'goreleaser-pro'
distribution: goreleaser
version: latest
version: "~> v2"
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GIT_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
stack_teardown_details.json
cfn-teardown
dist
21 changes: 15 additions & 6 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# For more info about this file, check the documentation at http://goreleaser.com
version: 2
release:
github:
owner: nirdosh17/cfn-teardown
Expand All @@ -17,12 +18,20 @@ builds:
- windows
- darwin
archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of `uname`.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
# use zip for windows archives
format_overrides:
- goos: windows
format: zip

checksum:
name_template: 'checksums.txt'
snapshot:
Expand Down
14 changes: 10 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
.DEFAULT_GOAL=help

build: ## Download packages and build binary
build: ## install deps and build binary
go mod download && \
go build -o cfn-teardown .

run: build ## Build and run binary
run: build ## build and run binary
./cfn-teardown

# http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
test.start: test.stop ## start integration test
docker build --platform linux/amd64 -t cfn-teardown-test -f test/Dockerfile .
docker compose -f test/docker-compose.yaml up --abort-on-container-exit --remove-orphans

test.stop: ## stop integration test
docker compose -f test/docker-compose.yaml down

help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@sed -ne '/@sed/!s/## //p' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ": "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
3 changes: 1 addition & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ import (
// config vars
var (
cfgFile string
config models.Config
)

var config models.Config

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "cfn-teardown",
Expand Down
22 changes: 21 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/nirdosh17/cfn-teardown

go 1.16
go 1.22

require (
github.com/aws/aws-sdk-go v1.40.22
Expand All @@ -9,3 +9,23 @@ require (
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.8.1
)

require (
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/text v0.3.6 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
125 changes: 0 additions & 125 deletions go.sum

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions models/nuke.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ package models

// Config represents all the parameters supported by cfn-teardown
type Config struct {
AWSProfile string `mapstructure:"AWS_PROFILE"`
AWSRegion string `mapstructure:"AWS_REGION"`
TargetAccountId string `mapstructure:"TARGET_ACCOUNT_ID"`
StackPattern string `mapstructure:"STACK_PATTERN"`
StackWaitTimeSeconds int16 `mapstructure:"STACK_WAIT_TIME_SECONDS"`
MaxDeleteRetryCount int16 `mapstructure:"MAX_DELETE_RETRY_COUNT"`
AbortWaitTimeMinutes int16 `mapstructure:"ABORT_WAIT_TIME_MINUTES"`
SlackWebhookURL string `mapstructure:"SLACK_WEBHOOK_URL"`
RoleARN string `mapstructure:"ROLE_ARN"`
DryRun string `mapstructure:"DRY_RUN"`
AWSProfile string `mapstructure:"AWS_PROFILE"`
AWSRegion string `mapstructure:"AWS_REGION"`
TargetAccountId string `mapstructure:"TARGET_ACCOUNT_ID"`
StackPattern string `mapstructure:"STACK_PATTERN"`
StackWaitTimeSeconds int16 `mapstructure:"STACK_WAIT_TIME_SECONDS"`
MaxDeleteRetryCount int16 `mapstructure:"MAX_DELETE_RETRY_COUNT"`
AbortWaitTimeMinutes int16 `mapstructure:"ABORT_WAIT_TIME_MINUTES"`
SlackWebhookURL string `mapstructure:"SLACK_WEBHOOK_URL"`
RoleARN string `mapstructure:"ROLE_ARN"`
DryRun string `mapstructure:"DRY_RUN"`
EndpointURL *string `mapstructure:"ENDPOINT_URL"`
}
24 changes: 24 additions & 0 deletions test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM ubuntu

ARG GO_VERSION=1.22.0

RUN set -ex \
&& apt-get update \
&& apt-get install wget git python3 python3-pip unzip jq -y

RUN wget -q -o /dev/null "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" \
&& unzip awscli-exe-linux-x86_64.zip -d /awscli > /dev/null \
&& ./awscli/aws/install

WORKDIR /usr/local
RUN wget -q -o /dev/null https://dl.google.com/go/go$GO_VERSION.linux-amd64.tar.gz
RUN tar -xzf go$GO_VERSION.linux-amd64.tar.gz
ENV GOROOT=/usr/local/go
ENV GOPATH=/root/go
ENV PATH=${GOROOT}/bin:${GOPATH}/bin:${PATH}

WORKDIR /app

COPY . /app

CMD ["sh", "-c", "/app/test/test_runner.sh"]
29 changes: 29 additions & 0 deletions test/create_test_stacks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

set -e

LOCALSTACK_ENDPOINT="${LOCALSTACK_ENDPOINT:-'http://localhost:4566'}"

ARGS="--endpoint-url $LOCALSTACK_ENDPOINT --region us-east-1"
ENV_NAME="test"
VPC_STACK_NAME=$ENV_NAME-vpc
DDB_STACK_NAME=$ENV_NAME-dynamodb
LAMBDA_STACK_NAME=$ENV_NAME-lambda

TEMPLATES_FOLDER="test/templates"

echo "--- test stacks creation started ---"

aws cloudformation create-stack --stack-name $VPC_STACK_NAME --template-body file://$TEMPLATES_FOLDER/vpc.yaml $ARGS > /dev/null
aws cloudformation wait stack-create-complete --stack-name $VPC_STACK_NAME $ARGS
echo "vpc created!"

aws cloudformation create-stack --stack-name $DDB_STACK_NAME --template-body file://$TEMPLATES_FOLDER/dynamodb.yaml $ARGS > /dev/null
aws cloudformation wait stack-create-complete --stack-name $DDB_STACK_NAME $ARGS
echo "dynamodb created!"

aws cloudformation create-stack --stack-name $LAMBDA_STACK_NAME --template-body file://$TEMPLATES_FOLDER/lambda.yaml $ARGS > /dev/null
aws cloudformation wait stack-create-complete --stack-name $LAMBDA_STACK_NAME $ARGS
echo "lambda created!"

echo "--- test stacks created! ---"
23 changes: 23 additions & 0 deletions test/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
services:
cfn-teardown-test:
container_name: cfn-teardown-test
image: cfn-teardown-test
depends_on:
- localstack
environment:
- LOCALSTACK_ENDPOINT=http://localstack:4566

localstack:
container_name: localstack-main
image: localstack/localstack
# ports:
# - "127.0.0.1:4566:4566" # LocalStack Gateway
# - "127.0.0.1:4510-4559:4510-4559" # external services port range
environment:
# LocalStack configuration: https://docs.localstack.cloud/references/configuration/
- DEBUG=${DEBUG:-0}
volumes:
# - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
# Mounting the Docker socket /var/run/docker.sock as a volume is required
# for some services that use Docker to provide the emulation, such as AWS Lambda.
- "/var/run/docker.sock:/var/run/docker.sock"
31 changes: 31 additions & 0 deletions test/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

set -e

echo "setting up aws configs.."
mkdir -p ~/.aws

echo "[default]
region = us-east-1" > ~/.aws/config

echo "[default]
aws_access_key_id = randomstringforlocalstack
aws_secret_access_key = randomstringforlocalstack" > ~/.aws/credentials
echo "aws configs written!"

echo "creating test config for cli..."
echo "AWS_REGION: us-east-1
AWS_PROFILE: default
STACK_PATTERN: test-
ENDPOINT_URL: http://localstack:4566
ABORT_WAIT_TIME_MINUTES: 0
STACK_WAIT_TIME_SECONDS: 2" > ~/.cfn-teardown.yaml
echo "configs written at: ~/.cfn-teardown.yaml"

echo "building binary..."
make build
echo "binary built!"

echo "creating test stacks..."
./test/create_test_stacks.sh
echo "stacks created!"
49 changes: 49 additions & 0 deletions test/templates/dynamodb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
AWSTemplateFormatVersion: 2010-09-09
Description: Dynamodb table
Parameters:
EnvironmentName:
Type: String
Description: Environment label e.g. dev, test, prod
Default: test
DynamoDBTableName:
Type: String
Description: The name of the DynamoDB table to be deployed
Default: DynamoDB-Table-CFNExample
DynamoDBPKName:
Type: String
Description: The name of the primary key that will exist in the DynamoDB table
Default: itemId

Resources:
DynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
BillingMode: PROVISIONED
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
AttributeDefinitions:
-
AttributeName: !Ref DynamoDBPKName
AttributeType: "S"
KeySchema:
-
AttributeName: !Ref DynamoDBPKName
KeyType: HASH
TableName: !Ref DynamoDBTableName
Tags:
- Key: cfnteardown
Value: true

Outputs:
DynamoDBTableName:
Description: Dynamodb table name
Value: !Ref DynamoDBTable
Export:
Name: !Join [ ':', [ !Ref EnvironmentName, DynamoDBTableName ] ]
DynamoDBPKName:
Description: Dynamodb table Primary Key
Value: !Ref DynamoDBPKName
Export:
Name: !Join [ ':', [ !Ref EnvironmentName, DynamoDBPKName ] ]
Loading

0 comments on commit 4477fae

Please sign in to comment.