Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## 8.4-1.3.1 (2022-10-06)
* Update embedded TF to `1.3.1`
* Add the ability to `skip_init`
* Doesn't run `init` pre-cleanup of `.terraform/`
* Handful together with `s3-cache` plugin
* Add the ability to `skip_cleanup`
* Doesn't remove `.terraform/` on finish
* Handful together with `s3-cache` plugin
* Integrate [`tf-summarize`](https://github.com/dineshba/tf-summarize) into the plugin
* New `summarize` action - requires a generated plan file to be present
* Has bool options `draw`, `md`, `tree` and `separate_tree` in the `summarize_options` plugin setting to tune the output

## 8.3-1.0.2 (2021-07-09)
* Update embedded TF to `1.0.2`
* A continuation of `v0.15` release line but now following semver
Expand Down
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ ARG terraform_version
RUN wget -q https://releases.hashicorp.com/terraform/${terraform_version}/terraform_${terraform_version}_linux_amd64.zip -O terraform.zip && \
unzip terraform.zip -d /bin && \
rm -f terraform.zip
ARG summarize_version
RUN curl -sL https://github.com/dineshba/tf-summarize/releases/download/v${summarize_version}/tf-summarize_linux_amd64.zip -osummarize.zip && \
unzip summarize.zip tf-summarize -d /bin && \
rm -f summarize.zip

COPY --from=builder /go/bin/drone-terraform /bin/
ENTRYPOINT ["/bin/drone-terraform"]
6 changes: 4 additions & 2 deletions build-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ fi
major=$(echo $tag | awk -F. '{print $1}')
minor=$(echo $tag | awk -F. '{print $2}')

tf_ver="1.0.2"
tf_ver="1.3.1"
sum_ver="0.2.2"

echo "Confirm building images for:"
echo " MAJOR: ${major}"
echo " MINOR: ${minor}"
echo " TF_VERSION: ${tf_ver}"
echo " SUM_VERSION: ${sum_ver}"

read -p "Proceed? [Y/N] " ans

Expand All @@ -27,7 +29,7 @@ if [[ "$ans" != "Y" && "$ans" != "y" ]]; then
fi

set -x
docker build -t jmccann/drone-terraform:latest --build-arg terraform_version=${tf_ver} .
docker build -t jmccann/drone-terraform:latest --build-arg terraform_version=${tf_ver} --build-arg summarize_version=${sum_ver}.

docker tag jmccann/drone-terraform:latest jmccann/drone-terraform:${major}
docker tag jmccann/drone-terraform:latest jmccann/drone-terraform:${major}.${minor}
Expand Down
20 changes: 20 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ func main() {
Usage: "options for the init command. See https://www.terraform.io/docs/commands/init.html",
EnvVar: "PLUGIN_INIT_OPTIONS",
},
cli.StringFlag{
Name: "summarize_options",
Usage: "options for the tf-summarize command. See https://github.com/dineshba/tf-summarize#usage",
EnvVar: "PLUGIN_SUMMARIZE_OPTIONS",
},
cli.StringFlag{
Name: "fmt_options",
Usage: "options for the fmt command. See https://www.terraform.io/docs/commands/fmt.html",
Expand All @@ -54,6 +59,16 @@ func main() {
Usage: "The number of concurrent operations as Terraform walks its graph",
EnvVar: "PLUGIN_PARALLELISM",
},
cli.BoolFlag{
Name: "skip_init",
Usage: "skip terraform init (useful for usage with s3-cache)",
EnvVar: "PLUGIN_SKIP_INIT",
},
cli.BoolFlag{
Name: "skip_cleanup",
Usage: "skip removal of .terraform/ (useful for usage with s3-cache)",
EnvVar: "PLUGIN_SKIP_CLEANUP",
},
cli.StringFlag{
Name: "netrc.machine",
Usage: "netrc machine",
Expand Down Expand Up @@ -152,6 +167,8 @@ func run(c *cli.Context) error {
json.Unmarshal([]byte(c.String("init_options")), &initOptions)
fmtOptions := FmtOptions{}
json.Unmarshal([]byte(c.String("fmt_options")), &fmtOptions)
summarizeOptions := SummarizeOptions{}
json.Unmarshal([]byte(c.String("summarize_options")), &summarizeOptions)

plugin := Plugin{
Config: Config{
Expand All @@ -160,10 +177,13 @@ func run(c *cli.Context) error {
Secrets: secrets,
InitOptions: initOptions,
FmtOptions: fmtOptions,
SummarizeOptions: summarizeOptions,
Cacert: c.String("ca_cert"),
Sensitive: c.Bool("sensitive"),
RoleARN: c.String("role_arn_to_assume"),
RootDir: c.String("root_dir"),
SkipInit: c.Bool("skip_init"),
SkipCleanup: c.Bool("skip_cleanup"),
Parallelism: c.Int("parallelism"),
Targets: c.StringSlice("targets"),
VarFiles: c.StringSlice("var_files"),
Expand Down
67 changes: 63 additions & 4 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ type (
Secrets map[string]string
InitOptions InitOptions
FmtOptions FmtOptions
SummarizeOptions SummarizeOptions
Cacert string
Sensitive bool
SkipInit bool
SkipCleanup bool
RoleARN string
RootDir string
Parallelism int
Expand Down Expand Up @@ -59,6 +62,14 @@ type (
Check *bool `json:"check"`
}

// SummarizeOptions for tf-summarize
SummarizeOptions struct {
Draw *bool `json:"draw"`
Md *bool `json:"md"`
Tree *bool `json:"tree"`
SeparateTree *bool `json:"separate_tree"`
}

// Plugin represents the plugin instance to be executed
Plugin struct {
Config Config
Expand Down Expand Up @@ -104,9 +115,11 @@ func (p Plugin) Exec() error {
commands = append(commands, installCaCert(p.Config.Cacert))
}

commands = append(commands, deleteCache(terraformDataDir))
commands = append(commands, initCommand(p.Config.InitOptions))
commands = append(commands, getModules())
if !p.Config.SkipInit {
commands = append(commands, deleteCache(terraformDataDir))
commands = append(commands, initCommand(p.Config.InitOptions))
commands = append(commands, getModules())
}

// Add commands listed from Actions
for _, action := range p.Config.Actions {
Expand All @@ -119,6 +132,8 @@ func (p Plugin) Exec() error {
commands = append(commands, tfPlan(p.Config, false))
case "plan-destroy":
commands = append(commands, tfPlan(p.Config, true))
case "summarize":
commands = append(commands, tfSummary(p.Config))
case "apply":
commands = append(commands, tfApply(p.Config))
case "destroy":
Expand All @@ -128,7 +143,9 @@ func (p Plugin) Exec() error {
}
}

commands = append(commands, deleteCache(terraformDataDir))
if !p.Config.SkipCleanup {
commands = append(commands, deleteCache(terraformDataDir))
}

for _, c := range commands {
if c.Dir == "" {
Expand Down Expand Up @@ -331,6 +348,48 @@ func tfPlan(config Config, destroy bool) *exec.Cmd {
)
}

func tfSummary(config Config) *exec.Cmd {
json_cmd := exec.Command("terraform", "show", "-json", getTfoutPath())
json_path := fmt.Sprintf("%s.json", getTfoutPath())

json_file, err := os.Create(json_path)
if err != nil {
logrus.WithFields(logrus.Fields{
"error": err,
}).Fatal("Failed to create JSON state file")
}
logrus.Debug("JSON state file created successfully")

json_cmd.Stdout = json_file
defer json_file.Close()

err = json_cmd.Run()
if err != nil {
logrus.WithFields(logrus.Fields{
"error": err,
}).Fatal("Failed to execute a command")
}

var args []string

if config.SummarizeOptions.Draw != nil {
args = append(args, "-draw")
}
if config.SummarizeOptions.Md != nil {
args = append(args, "-md")
}
if config.SummarizeOptions.Tree != nil {
args = append(args, "-tree")
}
if config.SummarizeOptions.SeparateTree != nil {
args = append(args, "-separate-tree")
}

args = append(args, json_path)

return exec.Command("tf-summarize", args...)
}

func tfValidate() *exec.Cmd {
return exec.Command(
"terraform",
Expand Down