Packer plugin for Drone CI
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
2.7 KiB

package main
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
"github.com/sirupsen/logrus"
)
type (
// Config for the plugin.
Config struct {
Actions []string
Vars map[string]string
Template string
VarFiles []string
SyntaxOnly bool
Except []string
Only []string
Color bool
Debug bool
Parallel bool
Readable bool
}
// Plugin values
Plugin struct {
Config Config
}
)
func pkValidate(config Config) *exec.Cmd {
args := []string{
"validate",
}
for _, v := range config.VarFiles {
args = append(args, "-var-file", fmt.Sprintf("%s", v))
}
for k, v := range config.Vars {
args = append(args, "-var")
args = append(args, fmt.Sprintf("%s=%s", k, v))
}
if len(config.Except) > 0 {
args = append(args, "-except="+strings.Join(config.Except, ","))
}
if len(config.Only) > 0 {
args = append(args, "-only="+strings.Join(config.Only, ","))
}
if config.SyntaxOnly {
args = append(args, "-syntax-only")
}
args = append(args, config.Template)
return exec.Command(
"packer",
args...,
)
}
func pkBuild(config Config) *exec.Cmd {
args := []string{
"build",
}
for _, v := range config.VarFiles {
args = append(args, "-var-file", fmt.Sprintf("%s", v))
}
for k, v := range config.Vars {
args = append(args, "-var")
args = append(args, fmt.Sprintf("%s=%s", k, v))
}
if len(config.Except) > 0 {
args = append(args, "-except="+strings.Join(config.Except, ","))
}
if len(config.Only) > 0 {
args = append(args, "-only="+strings.Join(config.Only, ","))
}
if config.Parallel {
args = append(args, "-parallel=true")
}
if config.Color {
args = append(args, "-color=true")
}
if config.Debug {
args = append(args, "-debug")
}
if config.Readable {
args = append(args, "-machine-readable")
}
args = append(args, config.Template)
return exec.Command(
"packer",
args...,
)
}
// Exec executes the plugin.
func (p *Plugin) Exec() error {
var commands []*exec.Cmd
if p.Config.Template == "" {
return errors.New("you must provide a template file")
}
if len(p.Config.Actions) == 0 {
return errors.New("you must provide packer action")
}
commands = append(commands, exec.Command("packer", "version"))
// Add commands listed from Actions
for _, action := range p.Config.Actions {
switch action {
case "validate":
commands = append(commands, pkValidate(p.Config))
case "build":
commands = append(commands, pkBuild(p.Config))
default:
return fmt.Errorf("valid actions are: validate, build You provided %s", action)
}
}
for _, c := range commands {
c.Stdout = os.Stdout
c.Stderr = os.Stderr
7 years ago
c.Env = os.Environ()
err := c.Run()
if err != nil {
7 years ago
return err
}
logrus.Debug("Command completed successfully")
}
return nil
}