0
0
mirror of https://github.com/thegeeklab/wp-opentofu.git synced 2024-11-22 00:30:40 +00:00

Add .netrc support

This commit is contained in:
Florian Bertholin 2018-04-06 13:37:39 +02:00
parent 082b4073ad
commit 0ef1c728e1
2 changed files with 64 additions and 0 deletions

20
main.go
View File

@ -53,6 +53,21 @@ func main() {
Usage: "whether or not to suppress terraform commands to stdout", Usage: "whether or not to suppress terraform commands to stdout",
EnvVar: "PLUGIN_SENSITIVE", EnvVar: "PLUGIN_SENSITIVE",
}, },
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",
},
cli.StringFlag{ cli.StringFlag{
Name: "role_arn_to_assume", Name: "role_arn_to_assume",
Usage: "A role to assume before running the terraform commands", Usage: "A role to assume before running the terraform commands",
@ -142,6 +157,11 @@ func run(c *cli.Context) error {
VarFiles: c.StringSlice("var_files"), VarFiles: c.StringSlice("var_files"),
Destroy: c.Bool("destroy"), Destroy: c.Bool("destroy"),
}, },
Netrc: Netrc{
Login: c.String("netrc.username"),
Machine: c.String("netrc.machine"),
Password: c.String("netrc.password"),
},
Terraform: Terraform{ Terraform: Terraform{
Version: c.String("tf.version"), Version: c.String("tf.version"),
}, },

View File

@ -5,6 +5,8 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"os/user"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
"time" "time"
@ -33,6 +35,12 @@ type (
Destroy bool Destroy bool
} }
Netrc struct {
Machine string
Login string
Password string
}
// InitOptions include options for the Terraform's init command // InitOptions include options for the Terraform's init command
InitOptions struct { InitOptions struct {
BackendConfig []string `json:"backend-config"` BackendConfig []string `json:"backend-config"`
@ -43,6 +51,7 @@ type (
// Plugin represents the plugin instance to be executed // Plugin represents the plugin instance to be executed
Plugin struct { Plugin struct {
Config Config Config Config
Netrc Netrc
Terraform Terraform Terraform Terraform
} }
) )
@ -62,6 +71,12 @@ func (p Plugin) Exec() error {
assumeRole(p.Config.RoleARN) assumeRole(p.Config.RoleARN)
} }
// writing the .netrc file with Github credentials in it.
err := writeNetrc(p.Netrc.Machine, p.Netrc.Login, p.Netrc.Password)
if err != nil {
return err
}
var commands []*exec.Cmd var commands []*exec.Cmd
commands = append(commands, exec.Command("terraform", "version")) commands = append(commands, exec.Command("terraform", "version"))
@ -303,3 +318,32 @@ func assumeRole(roleArn string) {
func trace(cmd *exec.Cmd) { func trace(cmd *exec.Cmd) {
fmt.Println("$", strings.Join(cmd.Args, " ")) fmt.Println("$", strings.Join(cmd.Args, " "))
} }
// helper function to write a netrc file.
// The following code comes from the official Git plugin for Drone:
// https://github.com/drone-plugins/drone-git/blob/8386effd2fe8c8695cf979427f8e1762bd805192/utils.go#L43-L68
func writeNetrc(machine, login, password string) error {
if machine == "" {
return nil
}
out := fmt.Sprintf(
netrcFile,
machine,
login,
password,
)
home := "/root"
u, err := user.Current()
if err == nil {
home = u.HomeDir
}
path := filepath.Join(home, ".netrc")
return ioutil.WriteFile(path, []byte(out), 0600)
}
const netrcFile = `
machine %s
login %s
password %s
`