From 33b9e3039d49f213d8fa7baba25c36e59c0e386c Mon Sep 17 00:00:00 2001 From: fabriciojs Date: Fri, 15 Sep 2023 22:03:43 -0300 Subject: [PATCH 1/5] disable vcs stamping --- build_artifacts.sh | 2 +- kool.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build_artifacts.sh b/build_artifacts.sh index f43fa9b9..d1d5d309 100755 --- a/build_artifacts.sh +++ b/build_artifacts.sh @@ -57,7 +57,7 @@ for i in "${!BUILD[@]}"; do $flags \ --env CGO_ENABLED=0 \ -v $(pwd):/code -w /code $GO_IMAGE \ - go build -a -tags 'osusergo netgo static_build' \ + go build -buildvcs=false -a -tags 'osusergo netgo static_build' \ -ldflags '-X kool-dev/kool/commands.version='$BUILD_VERSION' -extldflags "-static"' \ -o $dist done diff --git a/kool.yml b/kool.yml index 02d3b0ee..343ec985 100644 --- a/kool.yml +++ b/kool.yml @@ -12,7 +12,7 @@ scripts: # file properly setting GOOS=darwin so you will be able to use the binary. compile: - kool run fmt - - kool run go build -o kool-cli + - kool run go build -buildvcs=false -o kool-cli install: mv ./kool-cli /usr/local/bin/kool fmt: kool run go:linux fmt ./... lint: kool docker --volume=kool_gopath:/go golangci/golangci-lint:v1.54.1 golangci-lint run -v From ee49416a4b80293c1c45b9a6a0fd75cb10ff5edb Mon Sep 17 00:00:00 2001 From: fabriciojs Date: Fri, 15 Sep 2023 22:04:01 -0300 Subject: [PATCH 2/5] fix kool cloud yml port type --- commands/cloud_setup.go | 9 +++++---- services/cloud/deploy_validator.go | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/commands/cloud_setup.go b/commands/cloud_setup.go index e673120d..4b0093f5 100644 --- a/commands/cloud_setup.go +++ b/commands/cloud_setup.go @@ -7,6 +7,7 @@ import ( "kool-dev/kool/services/cloud" "kool-dev/kool/services/compose" "os" + "strconv" "strings" "github.com/spf13/cobra" @@ -154,12 +155,12 @@ func (s *KoolCloudSetup) Execute(args []string) (err error) { answer = potentialPorts[0] } - deployConfig.Services[serviceName].Port = new(string) - *deployConfig.Services[serviceName].Port = answer + deployConfig.Services[serviceName].Port = new(int) + *deployConfig.Services[serviceName].Port, _ = strconv.Atoi(answer) public := &cloud.DeployConfigPublicEntry{} - public.Port = new(string) - *public.Port = answer + public.Port = new(int) + *public.Port = *deployConfig.Services[serviceName].Port deployConfig.Services[serviceName].Public = append(deployConfig.Services[serviceName].Public, public) } diff --git a/services/cloud/deploy_validator.go b/services/cloud/deploy_validator.go index 2672d774..06d41f66 100644 --- a/services/cloud/deploy_validator.go +++ b/services/cloud/deploy_validator.go @@ -17,13 +17,13 @@ type DeployConfig struct { type DeployConfigService struct { Image *string `yaml:"image,omitempty"` Build *string `yaml:"build,omitempty"` - Port *string `yaml:"port,omitempty"` + Port *int `yaml:"port,omitempty"` Public []*DeployConfigPublicEntry `yaml:"public,omitempty"` } type DeployConfigPublicEntry struct { - Port *string `yaml:"port"` + Port *int `yaml:"port"` Path *string `yaml:"path,omitempty"` } From f8da94a77ec3d214f42b4118bdfe0cf6e38a8640 Mon Sep 17 00:00:00 2001 From: fabriciojs Date: Sat, 16 Sep 2023 22:40:59 -0300 Subject: [PATCH 3/5] improve deploy error displaying --- services/cloud/api/deploy.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/services/cloud/api/deploy.go b/services/cloud/api/deploy.go index 0a16f56f..36735c8a 100644 --- a/services/cloud/api/deploy.go +++ b/services/cloud/api/deploy.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "kool-dev/kool/core/environment" + "kool-dev/kool/core/shell" "mime/multipart" "net/http" "os" @@ -19,6 +20,7 @@ type Deploy struct { tarballPath, id string env environment.EnvStorage + out shell.Shell Status *StatusResponse } @@ -34,6 +36,7 @@ func NewDeploy(tarballPath string) *Deploy { return &Deploy{ Endpoint: NewDefaultEndpoint("POST"), env: environment.NewEnvStorage(), + out: shell.NewShell(), tarballPath: tarballPath, } } @@ -63,8 +66,14 @@ func (d *Deploy) SendFile() (err error) { if errAPI.Status == http.StatusUnauthorized { err = ErrUnauthorized } else if errAPI.Status == http.StatusUnprocessableEntity { - fmt.Println(errAPI.Message) - fmt.Println(errAPI.Errors) + d.out.Error(errors.New(errAPI.Message)) + for field, apiErr := range errAPI.Errors { + if apiErrs, ok := apiErr.([]interface{}); ok { + for _, apiErrStr := range apiErrs { + d.out.Error(fmt.Errorf("\t[%s] -> %v", field, apiErrStr)) + } + } + } err = ErrPayloadValidation } else if errAPI.Status != http.StatusOK && errAPI.Status != http.StatusCreated { err = ErrBadResponseStatus @@ -99,7 +108,7 @@ func (d *Deploy) getPayload() (body io.Reader, err error) { } fi, _ := file.Stat() - fmt.Printf("Release tarball got %.2fMBs...\n", float64(fi.Size())/1024/1024) + d.out.Printf("Release tarball got %.2fMBs...\n", float64(fi.Size())/1024/1024) if fw, err = w.CreateFormFile("deploy", "deploy.tgz"); err != nil { return From ec0091df0a6b23832bc7c0083252de8af9deb98f Mon Sep 17 00:00:00 2001 From: fabriciojs Date: Sat, 16 Sep 2023 22:41:29 -0300 Subject: [PATCH 4/5] improve color on shell output --- core/shell/shell.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shell/shell.go b/core/shell/shell.go index 0f65bfaf..daeb2bdb 100644 --- a/core/shell/shell.go +++ b/core/shell/shell.go @@ -252,7 +252,7 @@ func (s *DefaultShell) Success(out ...interface{}) { // Info info message func (s *DefaultShell) Info(out ...interface{}) { - fmt.Fprintln(s.OutStream(), color.New(color.Blue).Sprint(out...)) + fmt.Fprintln(s.OutStream(), color.New(color.Cyan).Sprint(out...)) } // Exec will execute the given command silently and return the combined From c965494197a6112eabc1e2467ef62a565872e875 Mon Sep 17 00:00:00 2001 From: fabriciojs Date: Sat, 16 Sep 2023 22:58:15 -0300 Subject: [PATCH 5/5] improve cloud setup - dockerfile suggestion --- commands/cloud_setup.go | 65 +++++++++++++++++++++++++++--- services/cloud/deploy_validator.go | 3 +- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/commands/cloud_setup.go b/commands/cloud_setup.go index 4b0093f5..8796754a 100644 --- a/commands/cloud_setup.go +++ b/commands/cloud_setup.go @@ -1,6 +1,7 @@ package commands import ( + "bytes" "fmt" "kool-dev/kool/core/environment" "kool-dev/kool/core/shell" @@ -88,7 +89,11 @@ func (s *KoolCloudSetup) Execute(args []string) (err error) { } s.Shell().Info(fmt.Sprintf("Setting up service container '%s' for deployment", serviceName)) - deployConfig.Services[serviceName] = &cloud.DeployConfigService{} + deployConfig.Services[serviceName] = &cloud.DeployConfigService{ + Environment: map[string]string{ + "FOO": "bar", + }, + } // handle image/build config if len(composeService.Volumes) == 0 && composeService.Build == nil { @@ -107,7 +112,7 @@ func (s *KoolCloudSetup) Execute(args []string) (err error) { // if it's a string, that should be the build path... if build, isString := (*composeService.Build).(string); isString { if build != "." { - err = fmt.Errorf("service '%s' got a build dockerfile on path '%s'. Please move to the root folder/context to be able to deploy.", serviceName, build) + err = fmt.Errorf("service '%s' got a build dockerfile on path '%s'. Please move to the root folder/context to be able to deploy", serviceName, build) return } deployConfig.Services[serviceName].Build = new(string) @@ -127,9 +132,59 @@ func (s *KoolCloudSetup) Execute(args []string) (err error) { } } } else { - // no build config, so we'll have to build it - deployConfig.Services[serviceName].Build = new(string) - *deployConfig.Services[serviceName].Build = "Dockerfile" + // no build config, so we'll need to build + + if len(composeService.Volumes) == 0 { + if image, isString := (*composeService.Image).(string); isString { + deployConfig.Services[serviceName].Image = new(string) + *deployConfig.Services[serviceName].Image = image + } else { + err = fmt.Errorf("unable to parse image configuration for service '%s'", serviceName) + return + } + } else { + if answer, err = s.promptSelect.Ask(fmt.Sprintf("Do you want to use a Dockerfile for deploying service '%s'?", serviceName), []string{"Yes", "No"}); err != nil { + return + } + + if answer == "Yes" { + // so here we should build the basic/simplest Dockerfile + deployConfig.Services[serviceName].Build = new(string) + *deployConfig.Services[serviceName].Build = "Dockerfile" + + if _, errStat := os.Stat("Dockerfile"); os.IsNotExist(errStat) { + // we don't have a Dockerfile, let's make a basic one! + var ( + dockerfile *os.File + content bytes.Buffer + ) + + if dockerfile, err = os.Create("Dockerfile"); err != nil { + return + } + + content.WriteString(fmt.Sprintf("FROM %s\n", (*composeService.Image).(string))) + + for _, vol := range composeService.Volumes { + volParts := strings.Split(vol, ":") + + if answer, err = s.promptSelect.Ask(fmt.Sprintf("Do you want to add folder '%s' onto '%s' in the Dockerfile for deploying service '%s'?", volParts[0], volParts[1], serviceName), []string{"Yes", "No"}); err != nil { + return + } + + if answer == "Yes" { + content.WriteString(fmt.Sprintf("\nCOPY %s %s\n", volParts[0], volParts[1])) + } + } + + if _, err = dockerfile.Write(content.Bytes()); err != nil { + return + } + + _ = dockerfile.Close() + } + } + } postInstructions = append(postInstructions, func() { s.Shell().Info(fmt.Sprintf("⇒ Service '%s' needs to be built. Make sure to create the necessary Dockerfile.", serviceName)) diff --git a/services/cloud/deploy_validator.go b/services/cloud/deploy_validator.go index 06d41f66..f8fa478d 100644 --- a/services/cloud/deploy_validator.go +++ b/services/cloud/deploy_validator.go @@ -19,7 +19,8 @@ type DeployConfigService struct { Build *string `yaml:"build,omitempty"` Port *int `yaml:"port,omitempty"` - Public []*DeployConfigPublicEntry `yaml:"public,omitempty"` + Public []*DeployConfigPublicEntry `yaml:"public,omitempty"` + Environment interface{} `yaml:"environment"` } type DeployConfigPublicEntry struct {