From 86c1b22bdfc02d43947eb75927963cb3a02b8d7b Mon Sep 17 00:00:00 2001 From: fabriciojs Date: Tue, 20 Aug 2024 23:55:06 -0300 Subject: [PATCH 1/2] add ping during local build --- commands/cloud_deploy.go | 28 +++++++++++++++++++++++-- commands/root.go | 4 ++++ services/cloud/api/api.go | 7 +++++++ services/cloud/api/deploy_ping.go | 34 +++++++++++++++++++++++++++++++ services/cloud/api/endpoint.go | 2 ++ services/cloud/deployer.go | 7 +++++++ 6 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 services/cloud/api/deploy_ping.go diff --git a/commands/cloud_deploy.go b/commands/cloud_deploy.go index 16938270..b3ea7afa 100644 --- a/commands/cloud_deploy.go +++ b/commands/cloud_deploy.go @@ -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() ) @@ -97,10 +98,30 @@ func (d *KoolDeploy) Execute(args []string) (err error) { } s.Stop() + var chPingDone = make(chan bool) + go func() { + 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) + return + } + break + } + }() + 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 { @@ -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) diff --git a/commands/root.go b/commands/root.go index 3c078e1b..7cbd52a2 100644 --- a/commands/root.go +++ b/commands/root.go @@ -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" @@ -52,6 +53,9 @@ var originalWorkingDir = "" func init() { AddCommands(rootCmd) + + // pass in the version + api.SetCliVersion(version) } // NewRootCmd creates the root command diff --git a/services/cloud/api/api.go b/services/cloud/api/api.go index e500410e..9ffeb908 100644 --- a/services/cloud/api/api.go +++ b/services/cloud/api/api.go @@ -2,6 +2,8 @@ package api var ( apiBaseURL string = "https://kool.dev/api" + + cliVersion string ) // SetBaseURL defines the target Kool API URL to be used @@ -9,3 +11,8 @@ var ( func SetBaseURL(url string) { apiBaseURL = url } + +// SetCliVersion injects version to this package +func SetCliVersion(v string) { + cliVersion = v +} diff --git a/services/cloud/api/deploy_ping.go b/services/cloud/api/deploy_ping.go new file mode 100644 index 00000000..d99449c1 --- /dev/null +++ b/services/cloud/api/deploy_ping.go @@ -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 +} diff --git a/services/cloud/api/endpoint.go b/services/cloud/api/endpoint.go index 75601d12..bf73c65d 100644 --- a/services/cloud/api/endpoint.go +++ b/services/cloud/api/endpoint.go @@ -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) } diff --git a/services/cloud/deployer.go b/services/cloud/deployer.go index 28c2d14d..367e37ec 100644 --- a/services/cloud/deployer.go +++ b/services/cloud/deployer.go @@ -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) From 84f66a9ec088947c7b6e94d65116dba8edd9e4d5 Mon Sep 17 00:00:00 2001 From: fabriciojs Date: Tue, 20 Aug 2024 23:58:11 -0300 Subject: [PATCH 2/2] fix reading channels --- commands/cloud_deploy.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/commands/cloud_deploy.go b/commands/cloud_deploy.go index b3ea7afa..753a76ba 100644 --- a/commands/cloud_deploy.go +++ b/commands/cloud_deploy.go @@ -100,21 +100,21 @@ func (d *KoolDeploy) Execute(args []string) (err error) { var chPingDone = make(chan bool) go func() { - 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) + 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) + } } - break } }()