mirror of
https://github.com/foomo/squadron.git
synced 2025-10-16 12:35:42 +00:00
Merge branch 'master' into feature/resource-validation
This commit is contained in:
commit
41145c3c2d
@ -11,7 +11,7 @@ var buildCmd = &cobra.Command{
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
_, err := build(args[0], flagTag, flagDir, flagVerbose)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.WithError(err).Fatalf("Build failed")
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
@ -15,15 +15,11 @@ var initCmd = &cobra.Command{
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
_, err := initialize(args[0], flagDir, flagVerbose)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.WithError(err).Fatalf("Initialization failed")
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func initialize(name, dir string, flagVerbose bool) (string, error) {
|
||||
output, err := configurd.Init(log, path.Join(dir, name), flagVerbose)
|
||||
if err != nil {
|
||||
return output, outputErrorf(output, err, "could not initialize an example configuraiton")
|
||||
}
|
||||
return output, nil
|
||||
return configurd.Init(log, path.Join(dir, name))
|
||||
}
|
||||
|
||||
@ -61,22 +61,17 @@ func install(group, namespace, tag, workDir, outputDir, service string, buildSer
|
||||
if buildService {
|
||||
log.Printf("Building services")
|
||||
for _, si := range sis {
|
||||
_, err := build(si.Name, tag, workDir, verbose)
|
||||
output, err := build(si.Name, tag, workDir, verbose)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return output, err
|
||||
}
|
||||
}
|
||||
}
|
||||
output, err := cnf.Install(configurd.InstallConfiguration{
|
||||
return cnf.Install(configurd.InstallConfiguration{
|
||||
ServiceItems: sis,
|
||||
BasePath: workDir,
|
||||
OutputDir: outputDir,
|
||||
Tag: tag,
|
||||
Verbose: verbose,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return output, outputErrorf(output, err, "could not install group: %v", group)
|
||||
}
|
||||
return output, nil
|
||||
}
|
||||
|
||||
@ -1,10 +1,8 @@
|
||||
package actions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/foomo/configurd"
|
||||
"github.com/sirupsen/logrus"
|
||||
@ -69,10 +67,6 @@ func Execute() {
|
||||
|
||||
}
|
||||
|
||||
func outputErrorf(output string, err error, format string, args ...interface{}) error {
|
||||
return fmt.Errorf("%v\nerror: %v\noutput: %v", fmt.Sprintf(format, args...), err, strings.ReplaceAll(output, "\n", " "))
|
||||
}
|
||||
|
||||
func newLogger(verbose bool) *logrus.Logger {
|
||||
logger := logrus.New()
|
||||
if verbose {
|
||||
|
||||
@ -17,7 +17,7 @@ var (
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
_, err := uninstall(args[0], flagNamespace, flagTag, flagDir, flagVerbose)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.WithError(err).Fatalf("Uninstallation failed")
|
||||
}
|
||||
},
|
||||
}
|
||||
@ -40,9 +40,5 @@ func uninstall(group, namespace, tag, dir string, verbose bool) (string, error)
|
||||
return "", err
|
||||
}
|
||||
|
||||
output, err := cnf.Uninstall(sis, namespace, verbose)
|
||||
if err != nil {
|
||||
return output, outputErrorf(output, err, "could not uninstall service group: %v", group)
|
||||
}
|
||||
return output, nil
|
||||
return cnf.Uninstall(sis, namespace, verbose)
|
||||
}
|
||||
|
||||
42
configurd.go
42
configurd.go
@ -1,6 +1,7 @@
|
||||
package configurd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@ -258,14 +259,7 @@ func (c Configurd) Build(s Service) (string, error) {
|
||||
}
|
||||
l.Infof("Building service: %v", s.Name)
|
||||
|
||||
output, err := runCommand(c.config.BasePath, args...)
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
|
||||
l.Trace(output)
|
||||
|
||||
return output, err
|
||||
return runCommand(c.config.Log, c.config.BasePath, args...)
|
||||
}
|
||||
|
||||
func loadService(reader io.Reader, name, defaultTag string) (Service, error) {
|
||||
@ -282,19 +276,37 @@ func loadService(reader io.Reader, name, defaultTag string) (Service, error) {
|
||||
return wrapper.Service, nil
|
||||
}
|
||||
|
||||
func Init(log *logrus.Logger, dir string, _ bool) (string, error) {
|
||||
func Init(log *logrus.Logger, dir string) (string, error) {
|
||||
log.Infof("Downloading example configuration into dir: %q", dir)
|
||||
return "done with export", exampledata.RestoreAssets(dir, "")
|
||||
return "", exampledata.RestoreAssets(dir, "")
|
||||
}
|
||||
|
||||
func runCommand(cwd string, command ...string) (string, error) {
|
||||
func runCommand(log *logrus.Logger, cwd string, command ...string) (string, error) {
|
||||
cmd := exec.Command(command[0], command[1:]...)
|
||||
cmd.Dir = cwd
|
||||
out, err := cmd.CombinedOutput()
|
||||
output := strings.Replace(string(out), "\n", "\n\t", -1)
|
||||
if out == nil && err != nil {
|
||||
output = err.Error()
|
||||
|
||||
cmdReader, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
cmd.Stderr = cmd.Stdout
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var out []string
|
||||
scanner := bufio.NewScanner(cmdReader)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
log.Trace(line)
|
||||
out = append(out, line)
|
||||
}
|
||||
output := strings.Join(out, "\n")
|
||||
if err := cmd.Wait(); err != nil {
|
||||
return output, fmt.Errorf("%s, %s", err.Error(), output)
|
||||
}
|
||||
|
||||
return output, err
|
||||
}
|
||||
|
||||
|
||||
@ -43,6 +43,17 @@ spec:
|
||||
port: container-port
|
||||
resources:
|
||||
{{- toYaml .Values.resources | nindent 12 }}
|
||||
volumeMounts:
|
||||
{{- range .Values.volumes }}
|
||||
- name: {{ .name }}
|
||||
mountPath: {{ .mount }}
|
||||
{{- end }}
|
||||
volumes:
|
||||
{{- range .Values.volumes }}
|
||||
- name: {{ .name }}
|
||||
hostPath:
|
||||
path: {{ .host }}
|
||||
{{- end }}
|
||||
{{- with .Values.nodeSelector }}
|
||||
nodeSelector:
|
||||
{{- toYaml . | nindent 8 }}
|
||||
|
||||
@ -10,6 +10,10 @@ group:
|
||||
ingress:
|
||||
path: /services/hello
|
||||
host: localhost
|
||||
volumes:
|
||||
- name: config
|
||||
host: ./application
|
||||
mount: /config/
|
||||
hi-service:
|
||||
overrides:
|
||||
args:
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
service:
|
||||
build:
|
||||
image: mongo
|
||||
image: mongo
|
||||
chart: mongodb
|
||||
|
||||
46
helm.go
46
helm.go
@ -7,14 +7,21 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/otiai10/copy"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Volume struct {
|
||||
Name string
|
||||
Host string
|
||||
Mount string
|
||||
}
|
||||
|
||||
type ServiceItem struct {
|
||||
Name string
|
||||
Overrides interface{}
|
||||
Overrides map[string]interface{}
|
||||
namespace string
|
||||
group string
|
||||
chart string
|
||||
@ -45,6 +52,15 @@ func generateYaml(_ *logrus.Logger, path string, data interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func fixVolumeRelativePath(basePath string, volumes interface{}) []Volume {
|
||||
var vs []Volume
|
||||
mapstructure.Decode(volumes, &vs)
|
||||
for i := 0; i < len(vs); i++ {
|
||||
vs[i].Host = strings.Replace(vs[i].Host, "./", fmt.Sprintf("%v/", basePath), 1)
|
||||
}
|
||||
return vs
|
||||
}
|
||||
|
||||
func generate(log *logrus.Logger, si ServiceItem, basePath, outputDir string) error {
|
||||
outputPath := path.Join(basePath, defaultOutputDir, outputDir, si.Name)
|
||||
log.Infof("Creating dir: %q", path.Join(outputDir, si.Name))
|
||||
@ -75,6 +91,10 @@ func generate(log *logrus.Logger, si ServiceItem, basePath, outputDir string) er
|
||||
return fmt.Errorf("could not copy template files: %w", err)
|
||||
}
|
||||
|
||||
if _, ok := si.Overrides["volumes"]; ok {
|
||||
si.Overrides["volumes"] = fixVolumeRelativePath(basePath, si.Overrides["volumes"])
|
||||
}
|
||||
|
||||
log.Printf("Generating yaml file: %q", path.Join(outputDir, si.Name, defaultOverridesFile))
|
||||
err = generateYaml(log, path.Join(outputPath, defaultOverridesFile), si.Overrides)
|
||||
if err != nil {
|
||||
@ -85,6 +105,7 @@ func generate(log *logrus.Logger, si ServiceItem, basePath, outputDir string) er
|
||||
|
||||
func helmInstall(log *logrus.Logger, si ServiceItem, service Service, outputDir string) (string, error) {
|
||||
log.Infof("Running helm install for service: %v", si.Name)
|
||||
|
||||
chartPath := path.Join(outputDir, si.Name)
|
||||
cmd := []string{
|
||||
"helm", "upgrade", si.Name, chartPath,
|
||||
@ -92,19 +113,17 @@ func helmInstall(log *logrus.Logger, si ServiceItem, service Service, outputDir
|
||||
"-n", si.namespace,
|
||||
"--install",
|
||||
"--set", fmt.Sprintf("group=%v", si.group),
|
||||
"--set", fmt.Sprintf("image.repository=%s", service.Image),
|
||||
"--set", fmt.Sprintf("image.tag=%s", service.Tag),
|
||||
"--set", fmt.Sprintf("metadata.name=%s", service.Name),
|
||||
"--set", fmt.Sprintf("metadata.component=%s", si.group),
|
||||
"--set", fmt.Sprintf("metadata.namespace=%s", si.namespace),
|
||||
}
|
||||
|
||||
output, err := runCommand("", cmd...)
|
||||
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("could not install a helm chart for service %v", si.Name)
|
||||
if service.Image != "" {
|
||||
cmd = append(cmd, "--set", fmt.Sprintf("image.repository=%s", service.Image))
|
||||
}
|
||||
return output, nil
|
||||
if service.Tag != "" {
|
||||
cmd = append(cmd, "--set", fmt.Sprintf("image.tag=%s", service.Tag))
|
||||
}
|
||||
return runCommand(log, "", cmd...)
|
||||
}
|
||||
|
||||
func helmUninstall(log *logrus.Logger, si ServiceItem) (string, error) {
|
||||
@ -115,12 +134,7 @@ func helmUninstall(log *logrus.Logger, si ServiceItem) (string, error) {
|
||||
"-n", si.namespace,
|
||||
si.Name,
|
||||
}
|
||||
output, err := runCommand("", cmd...)
|
||||
|
||||
if err != nil {
|
||||
return output, fmt.Errorf("could not uninstall a helm chart for service: %v, namespace: %v", si.Name, si.namespace)
|
||||
}
|
||||
return output, nil
|
||||
return runCommand(log, "", cmd...)
|
||||
}
|
||||
|
||||
type InstallConfiguration struct {
|
||||
@ -164,7 +178,6 @@ func (c Configurd) Install(cnf InstallConfiguration) (string, error) {
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
logger.Trace(out)
|
||||
output = append(output, out)
|
||||
}
|
||||
|
||||
@ -181,7 +194,6 @@ func (c Configurd) Uninstall(sis []ServiceItem, namespace string, verbose bool)
|
||||
return out, err
|
||||
}
|
||||
outputs = append(outputs, out)
|
||||
logger.Trace(out)
|
||||
}
|
||||
return strings.Join(outputs, "\n"), nil
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user