Skip to content

Commit

Permalink
Merge pull request #474 from kool-dev/cloud-improvements
Browse files Browse the repository at this point in the history
Kool Cloud improvements
  • Loading branch information
fabriciojs authored Oct 22, 2023
2 parents 8d6091e + c965494 commit 1a18981
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build_artifacts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 65 additions & 9 deletions commands/cloud_setup.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package commands

import (
"bytes"
"fmt"
"kool-dev/kool/core/environment"
"kool-dev/kool/core/shell"
"kool-dev/kool/services/cloud"
"kool-dev/kool/services/compose"
"os"
"strconv"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -87,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 {
Expand All @@ -106,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)
Expand All @@ -126,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))
Expand All @@ -154,12 +210,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)
}
Expand Down
2 changes: 1 addition & 1 deletion core/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion kool.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions services/cloud/api/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"kool-dev/kool/core/environment"
"kool-dev/kool/core/shell"
"mime/multipart"
"net/http"
"os"
Expand All @@ -19,6 +20,7 @@ type Deploy struct {
tarballPath, id string

env environment.EnvStorage
out shell.Shell

Status *StatusResponse
}
Expand All @@ -34,6 +36,7 @@ func NewDeploy(tarballPath string) *Deploy {
return &Deploy{
Endpoint: NewDefaultEndpoint("POST"),
env: environment.NewEnvStorage(),
out: shell.NewShell(),
tarballPath: tarballPath,
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions services/cloud/deploy_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ 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"`
Public []*DeployConfigPublicEntry `yaml:"public,omitempty"`
Environment interface{} `yaml:"environment"`
}

type DeployConfigPublicEntry struct {
Port *string `yaml:"port"`
Port *int `yaml:"port"`
Path *string `yaml:"path,omitempty"`
}

Expand Down

0 comments on commit 1a18981

Please sign in to comment.