2019-12-09 17:56:02 +00:00
|
|
|
package helm
|
|
|
|
|
|
|
|
import (
|
2020-04-05 12:22:46 +00:00
|
|
|
"errors"
|
2019-12-10 00:25:47 +00:00
|
|
|
"fmt"
|
2020-01-14 18:32:20 +00:00
|
|
|
"github.com/pelotech/drone-helm3/internal/env"
|
2019-12-09 17:56:02 +00:00
|
|
|
"github.com/pelotech/drone-helm3/internal/run"
|
2019-12-10 00:25:47 +00:00
|
|
|
"os"
|
2019-12-09 17:56:02 +00:00
|
|
|
)
|
|
|
|
|
2019-12-23 20:43:17 +00:00
|
|
|
const (
|
|
|
|
kubeConfigTemplate = "/root/.kube/config.tpl"
|
|
|
|
kubeConfigFile = "/root/.kube/config"
|
|
|
|
)
|
2019-12-12 18:20:11 +00:00
|
|
|
|
2019-12-09 18:52:41 +00:00
|
|
|
// A Step is one step in the plan.
|
2019-12-09 17:56:02 +00:00
|
|
|
type Step interface {
|
2020-01-17 18:13:53 +00:00
|
|
|
Prepare() error
|
2020-01-16 23:30:21 +00:00
|
|
|
Execute() error
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 18:52:41 +00:00
|
|
|
// A Plan is a series of steps to perform.
|
2019-12-09 17:56:02 +00:00
|
|
|
type Plan struct {
|
2020-01-17 18:13:53 +00:00
|
|
|
steps []Step
|
|
|
|
cfg env.Config
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 18:52:41 +00:00
|
|
|
// NewPlan makes a plan for running a helm operation.
|
2020-01-14 18:32:20 +00:00
|
|
|
func NewPlan(cfg env.Config) (*Plan, error) {
|
2019-12-12 18:20:11 +00:00
|
|
|
p := Plan{
|
|
|
|
cfg: cfg,
|
2019-12-10 00:25:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 12:22:46 +00:00
|
|
|
if cfg.UpdateDependencies && cfg.DependenciesAction != "" {
|
|
|
|
return nil, errors.New("update_dependencies is deprecated and cannot be provided together with dependencies_action")
|
|
|
|
}
|
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
p.steps = (*determineSteps(cfg))(cfg)
|
|
|
|
|
|
|
|
for i, step := range p.steps {
|
|
|
|
if cfg.Debug {
|
|
|
|
fmt.Fprintf(os.Stderr, "calling %T.Prepare (step %d)\n", step, i)
|
|
|
|
}
|
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
if err := step.Prepare(); err != nil {
|
2019-12-12 18:20:11 +00:00
|
|
|
err = fmt.Errorf("while preparing %T step: %w", step, err)
|
2019-12-09 17:56:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-12 18:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// determineSteps is primarily for the tests' convenience: it allows testing the "which stuff should
|
|
|
|
// we do" logic without building a config that meets all the steps' requirements.
|
2020-01-14 18:32:20 +00:00
|
|
|
func determineSteps(cfg env.Config) *func(env.Config) []Step {
|
2019-12-12 18:20:11 +00:00
|
|
|
switch cfg.Command {
|
|
|
|
case "upgrade":
|
|
|
|
return &upgrade
|
2019-12-19 23:02:49 +00:00
|
|
|
case "uninstall", "delete":
|
|
|
|
return &uninstall
|
2019-12-09 17:56:02 +00:00
|
|
|
case "lint":
|
2019-12-18 01:14:39 +00:00
|
|
|
return &lint
|
2019-12-09 17:56:02 +00:00
|
|
|
case "help":
|
2019-12-12 18:20:11 +00:00
|
|
|
return &help
|
2019-12-09 17:56:02 +00:00
|
|
|
default:
|
|
|
|
switch cfg.DroneEvent {
|
|
|
|
case "push", "tag", "deployment", "pull_request", "promote", "rollback":
|
2019-12-12 18:20:11 +00:00
|
|
|
return &upgrade
|
2019-12-19 00:58:31 +00:00
|
|
|
case "delete":
|
2019-12-19 23:02:49 +00:00
|
|
|
return &uninstall
|
2019-12-09 17:56:02 +00:00
|
|
|
default:
|
2019-12-26 18:47:42 +00:00
|
|
|
return &help
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 18:52:41 +00:00
|
|
|
// Execute runs each step in the plan, aborting and reporting on error
|
2019-12-09 17:56:02 +00:00
|
|
|
func (p *Plan) Execute() error {
|
2019-12-12 18:20:11 +00:00
|
|
|
for i, step := range p.steps {
|
|
|
|
if p.cfg.Debug {
|
2019-12-24 19:08:09 +00:00
|
|
|
fmt.Fprintf(p.cfg.Stderr, "calling %T.Execute (step %d)\n", step, i)
|
2019-12-12 18:20:11 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 23:30:21 +00:00
|
|
|
if err := step.Execute(); err != nil {
|
2019-12-24 23:49:47 +00:00
|
|
|
return fmt.Errorf("while executing %T step: %w", step, err)
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-14 18:32:20 +00:00
|
|
|
var upgrade = func(cfg env.Config) []Step {
|
2020-01-16 21:57:28 +00:00
|
|
|
var steps []Step
|
2020-08-25 02:59:06 +00:00
|
|
|
if !cfg.SkipKubeconfig {
|
2020-08-21 08:30:22 +00:00
|
|
|
steps = append(steps, run.NewInitKube(cfg, kubeConfigTemplate, kubeConfigFile))
|
|
|
|
}
|
2020-01-16 21:57:28 +00:00
|
|
|
for _, repo := range cfg.AddRepos {
|
2020-01-17 18:13:53 +00:00
|
|
|
steps = append(steps, run.NewAddRepo(cfg, repo))
|
2020-01-16 21:57:28 +00:00
|
|
|
}
|
2020-04-05 12:22:46 +00:00
|
|
|
|
|
|
|
if cfg.DependenciesAction != "" {
|
|
|
|
steps = append(steps, run.NewDepAction(cfg))
|
|
|
|
}
|
|
|
|
|
2019-12-27 23:06:32 +00:00
|
|
|
if cfg.UpdateDependencies {
|
2020-01-16 21:57:28 +00:00
|
|
|
steps = append(steps, run.NewDepUpdate(cfg))
|
2019-12-27 23:06:32 +00:00
|
|
|
}
|
2020-04-05 12:22:46 +00:00
|
|
|
|
2020-01-16 21:50:04 +00:00
|
|
|
steps = append(steps, run.NewUpgrade(cfg))
|
2019-12-09 17:56:02 +00:00
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
return steps
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
2019-12-10 00:25:47 +00:00
|
|
|
|
2020-01-14 18:32:20 +00:00
|
|
|
var uninstall = func(cfg env.Config) []Step {
|
2020-01-16 21:57:28 +00:00
|
|
|
var steps []Step
|
2020-08-25 02:59:06 +00:00
|
|
|
if !cfg.SkipKubeconfig {
|
2020-08-21 08:30:22 +00:00
|
|
|
steps = append(steps, run.NewInitKube(cfg, kubeConfigTemplate, kubeConfigFile))
|
|
|
|
}
|
2019-12-27 23:06:32 +00:00
|
|
|
if cfg.UpdateDependencies {
|
2020-01-16 21:57:28 +00:00
|
|
|
steps = append(steps, run.NewDepUpdate(cfg))
|
2019-12-27 23:06:32 +00:00
|
|
|
}
|
2020-01-16 21:50:04 +00:00
|
|
|
steps = append(steps, run.NewUninstall(cfg))
|
2019-12-19 01:13:17 +00:00
|
|
|
|
|
|
|
return steps
|
|
|
|
}
|
|
|
|
|
2020-01-14 18:32:20 +00:00
|
|
|
var lint = func(cfg env.Config) []Step {
|
2020-01-16 21:57:28 +00:00
|
|
|
var steps []Step
|
|
|
|
for _, repo := range cfg.AddRepos {
|
2020-01-17 18:13:53 +00:00
|
|
|
steps = append(steps, run.NewAddRepo(cfg, repo))
|
2020-01-16 21:57:28 +00:00
|
|
|
}
|
2019-12-27 23:06:32 +00:00
|
|
|
if cfg.UpdateDependencies {
|
2020-01-16 21:57:28 +00:00
|
|
|
steps = append(steps, run.NewDepUpdate(cfg))
|
2019-12-18 01:14:39 +00:00
|
|
|
}
|
2020-01-16 21:50:04 +00:00
|
|
|
steps = append(steps, run.NewLint(cfg))
|
2019-12-27 23:06:32 +00:00
|
|
|
return steps
|
2019-12-18 01:14:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 18:32:20 +00:00
|
|
|
var help = func(cfg env.Config) []Step {
|
2020-01-16 21:50:04 +00:00
|
|
|
return []Step{run.NewHelp(cfg)}
|
2019-12-19 01:13:17 +00:00
|
|
|
}
|