2024-02-05 19:44:47 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-02-07 19:32:15 +00:00
|
|
|
"encoding/json"
|
2024-02-05 19:44:47 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2024-05-12 10:03:10 +00:00
|
|
|
"github.com/thegeeklab/wp-opentofu/tofu"
|
2024-05-17 19:55:22 +00:00
|
|
|
plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
|
2024-02-05 19:44:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-02-05 20:50:41 +00:00
|
|
|
ErrTaintedPath = errors.New("filepath is tainted")
|
|
|
|
ErrMaxSizeSizeLimit = errors.New("max size limit of decoded data exceeded")
|
|
|
|
ErrActionUnknown = errors.New("action not found")
|
|
|
|
ErrInvalidTofuVersion = errors.New("invalid version string")
|
2024-02-07 12:17:58 +00:00
|
|
|
ErrHTTPError = errors.New("http error")
|
2024-02-05 19:44:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-02-07 12:17:58 +00:00
|
|
|
maxDecompressionSize = 100 * 1024 * 1024
|
2024-02-05 19:44:47 +00:00
|
|
|
defaultDirPerm = 0o755
|
|
|
|
)
|
|
|
|
|
|
|
|
//nolint:revive
|
|
|
|
func (p *Plugin) run(ctx context.Context) error {
|
2024-02-07 19:32:15 +00:00
|
|
|
if err := p.FlagsFromContext(); err != nil {
|
|
|
|
return fmt.Errorf("validation failed: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-02-05 19:44:47 +00:00
|
|
|
if err := p.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("validation failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.Execute(); err != nil {
|
|
|
|
return fmt.Errorf("execution failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-07 19:32:15 +00:00
|
|
|
func (p *Plugin) FlagsFromContext() error {
|
|
|
|
if p.Context.String("init-option") != "" {
|
2024-05-12 10:03:10 +00:00
|
|
|
initOptions := tofu.InitOptions{}
|
2024-02-07 19:32:15 +00:00
|
|
|
if err := json.Unmarshal([]byte(p.Context.String("init-option")), &initOptions); err != nil {
|
|
|
|
return fmt.Errorf("cannot unmarshal init_option: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-05-12 10:03:10 +00:00
|
|
|
p.Settings.Tofu.InitOptions = initOptions
|
2024-02-07 19:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.Context.String("fmt-option") != "" {
|
2024-05-12 10:03:10 +00:00
|
|
|
fmtOptions := tofu.FmtOptions{}
|
2024-02-07 19:32:15 +00:00
|
|
|
if err := json.Unmarshal([]byte(p.Context.String("fmt-option")), &fmtOptions); err != nil {
|
|
|
|
return fmt.Errorf("cannot unmarshal fmt_option: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-05-12 10:03:10 +00:00
|
|
|
p.Settings.Tofu.FmtOptions = fmtOptions
|
2024-02-07 19:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-05 19:44:47 +00:00
|
|
|
// Validate handles the settings validation of the plugin.
|
|
|
|
func (p *Plugin) Validate() error {
|
|
|
|
p.Settings.DataDir = ".terraform"
|
|
|
|
if value, ok := os.LookupEnv("TF_DATA_DIR"); ok {
|
|
|
|
p.Settings.DataDir = value
|
|
|
|
}
|
|
|
|
|
2024-05-12 10:03:10 +00:00
|
|
|
p.Settings.Tofu.OutFile = "plan.tfout"
|
2024-02-05 19:44:47 +00:00
|
|
|
if p.Settings.DataDir == ".terraform" {
|
2024-05-12 10:03:10 +00:00
|
|
|
p.Settings.Tofu.OutFile = fmt.Sprintf("%s.plan.tfout", p.Settings.DataDir)
|
2024-02-05 19:44:47 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 12:17:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute provides the implementation of the plugin.
|
|
|
|
func (p *Plugin) Execute() error {
|
2024-05-17 19:55:22 +00:00
|
|
|
batchCmd := make([]*plugin_exec.Cmd, 0)
|
2024-05-12 10:03:10 +00:00
|
|
|
batchCmd = append(batchCmd, p.Settings.Tofu.Version())
|
2024-05-04 13:51:32 +00:00
|
|
|
|
2024-02-05 20:50:41 +00:00
|
|
|
if p.Settings.TofuVersion != "" {
|
|
|
|
err := installPackage(p.Plugin.Network.Context, p.Plugin.Network.Client, p.Settings.TofuVersion, maxDecompressionSize)
|
2024-02-05 19:44:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-12 10:03:10 +00:00
|
|
|
batchCmd = append(batchCmd, p.Settings.Tofu.Init())
|
|
|
|
batchCmd = append(batchCmd, p.Settings.Tofu.GetModules())
|
2024-02-05 19:44:47 +00:00
|
|
|
|
|
|
|
for _, action := range p.Settings.Action.Value() {
|
|
|
|
switch action {
|
|
|
|
case "fmt":
|
2024-05-12 10:03:10 +00:00
|
|
|
batchCmd = append(batchCmd, p.Settings.Tofu.Fmt())
|
2024-02-05 19:44:47 +00:00
|
|
|
case "validate":
|
2024-05-12 10:03:10 +00:00
|
|
|
batchCmd = append(batchCmd, p.Settings.Tofu.Validate())
|
2024-02-05 19:44:47 +00:00
|
|
|
case "plan":
|
2024-05-12 10:03:10 +00:00
|
|
|
batchCmd = append(batchCmd, p.Settings.Tofu.Plan(false))
|
2024-02-05 19:44:47 +00:00
|
|
|
case "plan-destroy":
|
2024-05-12 10:03:10 +00:00
|
|
|
batchCmd = append(batchCmd, p.Settings.Tofu.Plan(true))
|
2024-02-05 19:44:47 +00:00
|
|
|
case "apply":
|
2024-05-12 10:03:10 +00:00
|
|
|
batchCmd = append(batchCmd, p.Settings.Tofu.Apply())
|
2024-02-05 19:44:47 +00:00
|
|
|
case "destroy":
|
2024-05-12 10:03:10 +00:00
|
|
|
batchCmd = append(batchCmd, p.Settings.Tofu.Destroy())
|
2024-02-05 19:44:47 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("%w: %s", ErrActionUnknown, action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-12 10:03:10 +00:00
|
|
|
if err := os.RemoveAll(p.Settings.DataDir); err != nil {
|
2024-02-05 19:44:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-12 11:25:37 +00:00
|
|
|
for _, cmd := range batchCmd {
|
|
|
|
if cmd == nil {
|
|
|
|
continue
|
2024-02-05 19:44:47 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 13:51:32 +00:00
|
|
|
if p.Settings.RootDir != "" {
|
2024-05-12 11:25:37 +00:00
|
|
|
cmd.Dir = p.Settings.RootDir
|
2024-05-04 13:51:32 +00:00
|
|
|
}
|
2024-02-05 19:44:47 +00:00
|
|
|
|
2024-05-12 11:25:37 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
2024-02-05 19:44:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-12 10:03:10 +00:00
|
|
|
return os.RemoveAll(p.Settings.DataDir)
|
2024-02-05 19:44:47 +00:00
|
|
|
}
|