|
| 1 | +package deploy |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os/exec" |
| 6 | +) |
| 7 | + |
| 8 | +func checkDependency(name string) error { |
| 9 | + |
| 10 | + _, err := exec.LookPath(name) |
| 11 | + if err != nil { |
| 12 | + return fmt.Errorf("%s not found in PATH. please install it to continue", name) |
| 13 | + } |
| 14 | + |
| 15 | + return nil |
| 16 | +} |
| 17 | + |
| 18 | +func Run(envName string, dryRun bool) error { |
| 19 | + |
| 20 | + environment, err := LoadEnvironment(envName) |
| 21 | + if err != nil { |
| 22 | + return err |
| 23 | + } |
| 24 | + |
| 25 | + strategy := ResolveStrategy(environment) |
| 26 | + |
| 27 | + executor := Executor{ |
| 28 | + DryRun: dryRun, |
| 29 | + } |
| 30 | + |
| 31 | + switch strategy { |
| 32 | + |
| 33 | + case StrategyHelm: |
| 34 | + |
| 35 | + if err := checkDependency("helm"); err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + |
| 39 | + args := []string{ |
| 40 | + "upgrade", |
| 41 | + "--install", |
| 42 | + environment.Helm.Release, |
| 43 | + environment.Helm.Chart, |
| 44 | + "--namespace", |
| 45 | + environment.K8s.Namespace, |
| 46 | + } |
| 47 | + |
| 48 | + // inject kube-context if provided |
| 49 | + if environment.K8s.Context != "" { |
| 50 | + args = append(args, "--kube-context", environment.K8s.Context) |
| 51 | + } |
| 52 | + |
| 53 | + return executor.Run("helm", args...) |
| 54 | + |
| 55 | + case StrategyKubectl: |
| 56 | + |
| 57 | + if err := checkDependency("kubectl"); err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + args := []string{ |
| 62 | + "apply", |
| 63 | + "-f", |
| 64 | + "k8s/", |
| 65 | + "-n", |
| 66 | + environment.K8s.Namespace, |
| 67 | + } |
| 68 | + |
| 69 | + if environment.K8s.Context != "" { |
| 70 | + args = append(args, "--context", environment.K8s.Context) |
| 71 | + } |
| 72 | + |
| 73 | + return executor.Run("kubectl", args...) |
| 74 | + |
| 75 | + default: |
| 76 | + return fmt.Errorf("unknown deployment strategy") |
| 77 | + } |
| 78 | +} |
0 commit comments