2015-11-09 19:23:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-10-08 15:34:43 +00:00
|
|
|
"encoding/json"
|
2015-11-09 19:23:42 +00:00
|
|
|
"os"
|
|
|
|
|
2016-10-08 15:34:43 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-12-21 01:43:45 +00:00
|
|
|
"github.com/joho/godotenv"
|
2016-10-08 15:34:43 +00:00
|
|
|
"github.com/urfave/cli"
|
2016-02-21 11:41:22 +00:00
|
|
|
)
|
|
|
|
|
2016-10-09 05:08:37 +00:00
|
|
|
var revision string // build number set at compile-time
|
2015-11-09 19:23:42 +00:00
|
|
|
|
|
|
|
func main() {
|
2016-10-08 15:34:43 +00:00
|
|
|
app := cli.NewApp()
|
|
|
|
app.Name = "terraform plugin"
|
|
|
|
app.Usage = "terraform plugin"
|
|
|
|
app.Action = run
|
2016-10-09 05:08:37 +00:00
|
|
|
app.Version = revision
|
2016-10-08 15:34:43 +00:00
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
|
|
|
|
//
|
|
|
|
// plugin args
|
|
|
|
//
|
|
|
|
|
2018-02-14 15:39:20 +00:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "actions",
|
|
|
|
Usage: "a list of actions to have terraform perform",
|
|
|
|
EnvVar: "PLUGIN_ACTIONS",
|
2018-02-14 16:47:32 +00:00
|
|
|
Value: &cli.StringSlice{"validate", "plan", "apply"},
|
2016-10-08 15:34:43 +00:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
2018-02-14 15:39:20 +00:00
|
|
|
Name: "ca_cert",
|
|
|
|
Usage: "ca cert to add to your environment to allow terraform to use internal/private resources",
|
|
|
|
EnvVar: "PLUGIN_CA_CERT",
|
2016-10-08 15:34:43 +00:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
2018-02-14 15:39:20 +00:00
|
|
|
Name: "env-file",
|
|
|
|
Usage: "source env file",
|
2016-10-08 15:34:43 +00:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
2018-02-14 15:39:20 +00:00
|
|
|
Name: "init_options",
|
|
|
|
Usage: "options for the init command. See https://www.terraform.io/docs/commands/init.html",
|
|
|
|
EnvVar: "PLUGIN_INIT_OPTIONS",
|
2016-10-08 15:34:43 +00:00
|
|
|
},
|
2018-02-14 15:39:20 +00:00
|
|
|
cli.IntFlag{
|
|
|
|
Name: "parallelism",
|
|
|
|
Usage: "The number of concurrent operations as Terraform walks its graph",
|
|
|
|
EnvVar: "PLUGIN_PARALLELISM",
|
2016-10-08 15:34:43 +00:00
|
|
|
},
|
2018-04-06 11:37:39 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "netrc.machine",
|
|
|
|
Usage: "netrc machine",
|
|
|
|
EnvVar: "DRONE_NETRC_MACHINE",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "netrc.username",
|
|
|
|
Usage: "netrc username",
|
|
|
|
EnvVar: "DRONE_NETRC_USERNAME",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "netrc.password",
|
|
|
|
Usage: "netrc password",
|
|
|
|
EnvVar: "DRONE_NETRC_PASSWORD",
|
|
|
|
},
|
2016-10-08 15:34:43 +00:00
|
|
|
cli.StringFlag{
|
2016-12-21 01:43:45 +00:00
|
|
|
Name: "role_arn_to_assume",
|
|
|
|
Usage: "A role to assume before running the terraform commands",
|
2016-10-08 15:34:43 +00:00
|
|
|
EnvVar: "PLUGIN_ROLE_ARN_TO_ASSUME",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
2016-12-21 01:43:45 +00:00
|
|
|
Name: "root_dir",
|
|
|
|
Usage: "The root directory where the terraform files live. When unset, the top level directory will be assumed",
|
2016-10-08 15:34:43 +00:00
|
|
|
EnvVar: "PLUGIN_ROOT_DIR",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
2018-02-14 15:39:20 +00:00
|
|
|
Name: "secrets",
|
|
|
|
Usage: "a map of secrets to pass to the Terraform `plan` and `apply` commands. Each value is passed as a `<key>=<ENV>` option",
|
|
|
|
EnvVar: "PLUGIN_SECRETS",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "sensitive",
|
|
|
|
Usage: "whether or not to suppress terraform commands to stdout",
|
|
|
|
EnvVar: "PLUGIN_SENSITIVE",
|
2016-10-08 15:34:43 +00:00
|
|
|
},
|
2016-12-21 01:43:45 +00:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "targets",
|
|
|
|
Usage: "targets to run apply or plan on",
|
|
|
|
EnvVar: "PLUGIN_TARGETS",
|
|
|
|
},
|
2017-09-06 18:52:01 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "tf.version",
|
|
|
|
Usage: "terraform version to use",
|
|
|
|
EnvVar: "PLUGIN_TF_VERSION",
|
|
|
|
},
|
2018-02-14 15:39:20 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "vars",
|
|
|
|
Usage: "a map of variables to pass to the Terraform `plan` and `apply` commands. Each value is passed as a `<key>=<value>` option",
|
|
|
|
EnvVar: "PLUGIN_VARS",
|
|
|
|
},
|
2018-02-14 15:35:07 +00:00
|
|
|
cli.StringSliceFlag{
|
2018-02-14 15:39:20 +00:00
|
|
|
Name: "var_files",
|
|
|
|
Usage: "a list of var files to use. Each value is passed as -var-file=<value>",
|
|
|
|
EnvVar: "PLUGIN_VAR_FILES",
|
2018-02-14 15:35:07 +00:00
|
|
|
},
|
2016-03-23 12:43:20 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 15:34:43 +00:00
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
logrus.Fatal(err)
|
2015-11-10 01:07:05 +00:00
|
|
|
}
|
2016-02-09 19:27:12 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 15:34:43 +00:00
|
|
|
func run(c *cli.Context) error {
|
2016-10-09 05:08:37 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"Revision": revision,
|
|
|
|
}).Info("Drone Terraform Plugin Version")
|
|
|
|
|
2016-10-08 15:34:43 +00:00
|
|
|
if c.String("env-file") != "" {
|
|
|
|
_ = godotenv.Load(c.String("env-file"))
|
2015-11-10 01:07:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 15:34:43 +00:00
|
|
|
var vars map[string]string
|
2016-10-20 14:14:40 +00:00
|
|
|
if c.String("vars") != "" {
|
|
|
|
if err := json.Unmarshal([]byte(c.String("vars")), &vars); err != nil {
|
2016-10-20 13:46:56 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2016-06-10 12:38:54 +00:00
|
|
|
}
|
2016-10-08 15:34:43 +00:00
|
|
|
var secrets map[string]string
|
2016-10-20 14:14:40 +00:00
|
|
|
if c.String("secrets") != "" {
|
|
|
|
if err := json.Unmarshal([]byte(c.String("secrets")), &secrets); err != nil {
|
2016-10-20 13:46:56 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2016-06-10 12:38:54 +00:00
|
|
|
}
|
2015-11-09 19:23:42 +00:00
|
|
|
|
2017-05-08 15:40:14 +00:00
|
|
|
initOptions := InitOptions{}
|
|
|
|
json.Unmarshal([]byte(c.String("init_options")), &initOptions)
|
|
|
|
|
2016-10-08 15:34:43 +00:00
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
2018-02-14 16:06:42 +00:00
|
|
|
Actions: c.StringSlice("actions"),
|
2016-10-08 15:34:43 +00:00
|
|
|
Vars: vars,
|
|
|
|
Secrets: secrets,
|
2017-05-08 15:40:14 +00:00
|
|
|
InitOptions: initOptions,
|
2016-10-20 14:14:40 +00:00
|
|
|
Cacert: c.String("ca_cert"),
|
|
|
|
Sensitive: c.Bool("sensitive"),
|
|
|
|
RoleARN: c.String("role_arn_to_assume"),
|
|
|
|
RootDir: c.String("root_dir"),
|
|
|
|
Parallelism: c.Int("parallelism"),
|
2016-12-21 01:43:45 +00:00
|
|
|
Targets: c.StringSlice("targets"),
|
2017-02-07 00:39:00 +00:00
|
|
|
VarFiles: c.StringSlice("var_files"),
|
2016-10-08 15:34:43 +00:00
|
|
|
},
|
2018-04-06 11:37:39 +00:00
|
|
|
Netrc: Netrc{
|
|
|
|
Login: c.String("netrc.username"),
|
|
|
|
Machine: c.String("netrc.machine"),
|
|
|
|
Password: c.String("netrc.password"),
|
|
|
|
},
|
2017-09-06 18:52:01 +00:00
|
|
|
Terraform: Terraform{
|
|
|
|
Version: c.String("tf.version"),
|
|
|
|
},
|
2016-04-04 21:24:02 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 15:34:43 +00:00
|
|
|
return plugin.Exec()
|
2015-11-09 19:23:42 +00:00
|
|
|
}
|