Skip to content

Commit

Permalink
Merge pull request #512 from kool-dev/build-local-ping
Browse files Browse the repository at this point in the history
Add ping during local build - handle timeouts/interruptions
  • Loading branch information
fabriciojs authored Aug 28, 2024
2 parents ef79e4e + 84f66a9 commit 2de4f0f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 2 deletions.
28 changes: 26 additions & 2 deletions commands/cloud_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (d *KoolDeploy) Execute(args []string) (err error) {
var (
filename string
deployCreated *api.DeployCreateResponse
isVerbose = d.env.IsTrue("KOOL_VERBOSE")

deployer = cloud.NewDeployer()
)
Expand All @@ -97,10 +98,30 @@ func (d *KoolDeploy) Execute(args []string) (err error) {
}
s.Stop()

var chPingDone = make(chan bool)
go func() {
for {
select {
case <-chPingDone:
if isVerbose {
fmt.Println(" - ending deploy ping routine")
}
return
case <-time.After(30 * time.Second):
if isVerbose {
fmt.Println(" - going to ping API while local build runs")
}
if _, err = deployer.PingDeploy(deployCreated); err != nil {
d.Shell().Error(err)
}
}
}
}()

d.Shell().Info("Building images...")
for svcName, svc := range d.cloudConfig.Cloud.Services {
if svc.Build != nil {
d.Shell().Info(" > Build deploy image for service: ", svcName)
d.Shell().Info(" > Build deploy image for service '", svcName, "'")

if err = cloud.BuildPushImageForDeploy(svcName, svc, deployCreated); err != nil {
if reportErr := deployer.BuildError(deployCreated, err); reportErr != nil {
Expand All @@ -110,10 +131,13 @@ func (d *KoolDeploy) Execute(args []string) (err error) {
return
}

d.Shell().Info(" > Image for service: ", svcName, " built & pushed successfully.")
d.Shell().Info(" > Image for service '", svcName, "' built & pushed successfully.")
}
}

// ends ping
chPingDone <- true

if deployCreated.LogsUrl != "" {
d.Shell().Info(strings.Repeat("-", 40))
d.Shell().Info("Logs available at: ", deployCreated.LogsUrl)
Expand Down
4 changes: 4 additions & 0 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"kool-dev/kool/core/environment"
"kool-dev/kool/core/parser"
"kool-dev/kool/core/shell"
"kool-dev/kool/services/cloud/api"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -52,6 +53,9 @@ var originalWorkingDir = ""

func init() {
AddCommands(rootCmd)

// pass in the version
api.SetCliVersion(version)
}

// NewRootCmd creates the root command
Expand Down
7 changes: 7 additions & 0 deletions services/cloud/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ package api

var (
apiBaseURL string = "https://kool.dev/api"

cliVersion string
)

// SetBaseURL defines the target Kool API URL to be used
// when reaching out endpoints.
func SetBaseURL(url string) {
apiBaseURL = url
}

// SetCliVersion injects version to this package
func SetCliVersion(v string) {
cliVersion = v
}
34 changes: 34 additions & 0 deletions services/cloud/api/deploy_ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package api

import "fmt"

// DeployPingResponse holds data returned from the deploy endpoint
type DeployPingResponse struct {
ID int `json:"id"`
Status string `json:"status"`
}

// DeployPing consumes the API endpoint to create a new deployment
type DeployPing struct {
Endpoint
}

// NewDeployPing creates a new DeployStart instance
func NewDeployPing(created *DeployCreateResponse) (c *DeployPing) {
c = &DeployPing{
Endpoint: NewDefaultEndpoint("POST"),
}

c.SetPath("deploy/ping")
c.Body().Set("id", fmt.Sprintf("%d", created.Deploy.ID))

return
}

// Run calls deploy/ping in the Kool Dev API
func (c *DeployPing) Run() (resp *DeployPingResponse, err error) {
resp = &DeployPingResponse{}
c.SetResponseReceiver(resp)
err = c.DoCall()
return
}
2 changes: 2 additions & 0 deletions services/cloud/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ func (e *DefaultEndpoint) DoCall() (err error) {
return
}

request.Header.Add("x-kool-cli-version", cliVersion)

if e.contentType != "" {
request.Header.Add("Content-type", e.contentType)
}
Expand Down
7 changes: 7 additions & 0 deletions services/cloud/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ func (d *Deployer) StartDeploy(created *api.DeployCreateResponse) (started *api.
return
}

func (d *Deployer) PingDeploy(created *api.DeployCreateResponse) (pinged *api.DeployPingResponse, err error) {
var ping = api.NewDeployPing(created)

pinged, err = ping.Run()
return
}

func (d *Deployer) BuildError(created *api.DeployCreateResponse, gotErr error) (err error) {
var buildErr = api.NewDeployError(created, gotErr)

Expand Down

0 comments on commit 2de4f0f

Please sign in to comment.