Browse Source

Merge 726cb59f02 into 877e7415ef

pull/32/merge
Greyson Dehn 8 years ago
committed by GitHub
parent
commit
ad28484945
  1. 30
      DOCS.md
  2. 15
      main.go
  3. 53
      plugin.go

30
DOCS.md

@ -187,3 +187,33 @@ pipeline:
app_version: 1.0.0
parallelism: 2
```
## Submodule Overrides
You may want to override particular configuration values in submodules.
For example, this configuration:
```yaml
pipeline:
terraform:
image: jmccann/drone-terraform:0.5
plan: false
submodules:
the-first:
source: some:neat://path
a-key: key
the-second:
do-the-thing: true
parallelism: 2
```
will add a file named `<random hex>_override.tf` containing:
```hcl-terraform
module "the-first" {
source = "some:neat://path"
a-key = "key"
}
module "the-second" {
do-the-thing = true
}
```

15
main.go

@ -68,17 +68,20 @@ func main() {
Usage: "The number of concurrent operations as Terraform walks its graph",
EnvVar: "PLUGIN_PARALLELISM",
},
cli.StringFlag{
Name: "env-file",
Usage: "source env file",
},
cli.StringSliceFlag{
Name: "targets",
Usage: "targets to run apply or plan on",
EnvVar: "PLUGIN_TARGETS",
},
cli.StringFlag{
Name: "submodules",
Usage: "submodules to override",
EnvVar: "PLUGIN_SUBMODULES",
},
}
if err := app.Run(os.Args); err != nil {
@ -111,6 +114,13 @@ func run(c *cli.Context) error {
}
}
var submodules map[string]map[string]string
if c.String("submodules") != "" {
if err := json.Unmarshal([]byte(c.String("submodules")), &submodules); err != nil {
panic(err)
}
}
plugin := Plugin{
Config: Config{
Remote: remote,
@ -123,6 +133,7 @@ func run(c *cli.Context) error {
RootDir: c.String("root_dir"),
Parallelism: c.Int("parallelism"),
Targets: c.StringSlice("targets"),
Submodules: submodules,
},
}

53
plugin.go

@ -1,17 +1,20 @@
package main
import (
"encoding/hex"
"fmt"
"github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
)
type (
@ -26,6 +29,7 @@ type (
RootDir string
Parallelism int
Targets []string
Submodules map[string]map[string]string
}
Remote struct {
@ -45,18 +49,30 @@ func (p Plugin) Exec() error {
var commands []*exec.Cmd
remote := p.Config.Remote
if len(p.Config.Submodules) != 0 {
overridesFileName := submoduleOverride(p.Config.Submodules)
if overridesFileName != "" {
defer os.Remove(overridesFileName)
}
}
if p.Config.Cacert != "" {
commands = append(commands, installCaCert(p.Config.Cacert))
}
if remote.Backend != "" {
commands = append(commands, deleteCache())
commands = append(commands, remoteConfigCommand(remote))
}
commands = append(commands, getModules())
commands = append(commands, planCommand(p.Config.Vars, p.Config.Secrets, p.Config.Parallelism, p.Config.Targets))
if !p.Config.Plan {
commands = append(commands, applyCommand(p.Config.Parallelism, p.Config.Targets))
}
commands = append(commands, deleteCache())
for _, c := range commands {
@ -94,6 +110,33 @@ func installCaCert(cacert string) *exec.Cmd {
)
}
func submoduleOverride(submodules map[string]map[string]string) string {
allOverrides := []string{}
for moduleName, override := range submodules {
overrideContents := []string{}
for k, v := range override {
overrideContents = append(overrideContents, fmt.Sprintf(` %s = "%s"`, k, v))
}
moduleContents := fmt.Sprintf("module \"%s\" {\n%s\n}", moduleName, strings.Join(overrideContents, "\n"))
allOverrides = append(allOverrides, moduleContents)
}
fileContents := []byte(strings.Join(allOverrides, "\n"))
randBytes := make([]byte, 16)
rand.Read(randBytes)
fileName := hex.EncodeToString(randBytes) + "_override.tf"
if err := ioutil.WriteFile(fileName, fileContents, 0644); err != nil {
logrus.WithFields(logrus.Fields{
"error": err,
}).Fatal("Error writing overrides file!")
}
return fileName
}
func deleteCache() *exec.Cmd {
return exec.Command(
"rm",

Loading…
Cancel
Save