Skip to content

Commit 86c1b22

Browse files
committed
add ping during local build
1 parent 6511719 commit 86c1b22

File tree

6 files changed

+80
-2
lines changed

6 files changed

+80
-2
lines changed

commands/cloud_deploy.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func (d *KoolDeploy) Execute(args []string) (err error) {
7575
var (
7676
filename string
7777
deployCreated *api.DeployCreateResponse
78+
isVerbose = d.env.IsTrue("KOOL_VERBOSE")
7879

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

101+
var chPingDone = make(chan bool)
102+
go func() {
103+
select {
104+
case <-chPingDone:
105+
if isVerbose {
106+
fmt.Println(" - ending deploy ping routine")
107+
}
108+
return
109+
case <-time.After(30 * time.Second):
110+
if isVerbose {
111+
fmt.Println(" - going to ping API while local build runs")
112+
}
113+
if _, err = deployer.PingDeploy(deployCreated); err != nil {
114+
d.Shell().Error(err)
115+
return
116+
}
117+
break
118+
}
119+
}()
120+
100121
d.Shell().Info("Building images...")
101122
for svcName, svc := range d.cloudConfig.Cloud.Services {
102123
if svc.Build != nil {
103-
d.Shell().Info(" > Build deploy image for service: ", svcName)
124+
d.Shell().Info(" > Build deploy image for service '", svcName, "'")
104125

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

113-
d.Shell().Info(" > Image for service: ", svcName, " built & pushed successfully.")
134+
d.Shell().Info(" > Image for service '", svcName, "' built & pushed successfully.")
114135
}
115136
}
116137

138+
// ends ping
139+
chPingDone <- true
140+
117141
if deployCreated.LogsUrl != "" {
118142
d.Shell().Info(strings.Repeat("-", 40))
119143
d.Shell().Info("Logs available at: ", deployCreated.LogsUrl)

commands/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"kool-dev/kool/core/environment"
77
"kool-dev/kool/core/parser"
88
"kool-dev/kool/core/shell"
9+
"kool-dev/kool/services/cloud/api"
910
"os"
1011
"path"
1112
"path/filepath"
@@ -52,6 +53,9 @@ var originalWorkingDir = ""
5253

5354
func init() {
5455
AddCommands(rootCmd)
56+
57+
// pass in the version
58+
api.SetCliVersion(version)
5559
}
5660

5761
// NewRootCmd creates the root command

services/cloud/api/api.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ package api
22

33
var (
44
apiBaseURL string = "https://kool.dev/api"
5+
6+
cliVersion string
57
)
68

79
// SetBaseURL defines the target Kool API URL to be used
810
// when reaching out endpoints.
911
func SetBaseURL(url string) {
1012
apiBaseURL = url
1113
}
14+
15+
// SetCliVersion injects version to this package
16+
func SetCliVersion(v string) {
17+
cliVersion = v
18+
}

services/cloud/api/deploy_ping.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package api
2+
3+
import "fmt"
4+
5+
// DeployPingResponse holds data returned from the deploy endpoint
6+
type DeployPingResponse struct {
7+
ID int `json:"id"`
8+
Status string `json:"status"`
9+
}
10+
11+
// DeployPing consumes the API endpoint to create a new deployment
12+
type DeployPing struct {
13+
Endpoint
14+
}
15+
16+
// NewDeployPing creates a new DeployStart instance
17+
func NewDeployPing(created *DeployCreateResponse) (c *DeployPing) {
18+
c = &DeployPing{
19+
Endpoint: NewDefaultEndpoint("POST"),
20+
}
21+
22+
c.SetPath("deploy/ping")
23+
c.Body().Set("id", fmt.Sprintf("%d", created.Deploy.ID))
24+
25+
return
26+
}
27+
28+
// Run calls deploy/ping in the Kool Dev API
29+
func (c *DeployPing) Run() (resp *DeployPingResponse, err error) {
30+
resp = &DeployPingResponse{}
31+
c.SetResponseReceiver(resp)
32+
err = c.DoCall()
33+
return
34+
}

services/cloud/api/endpoint.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ func (e *DefaultEndpoint) DoCall() (err error) {
189189
return
190190
}
191191

192+
request.Header.Add("x-kool-cli-version", cliVersion)
193+
192194
if e.contentType != "" {
193195
request.Header.Add("Content-type", e.contentType)
194196
}

services/cloud/deployer.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ func (d *Deployer) StartDeploy(created *api.DeployCreateResponse) (started *api.
4646
return
4747
}
4848

49+
func (d *Deployer) PingDeploy(created *api.DeployCreateResponse) (pinged *api.DeployPingResponse, err error) {
50+
var ping = api.NewDeployPing(created)
51+
52+
pinged, err = ping.Run()
53+
return
54+
}
55+
4956
func (d *Deployer) BuildError(created *api.DeployCreateResponse, gotErr error) (err error) {
5057
var buildErr = api.NewDeployError(created, gotErr)
5158

0 commit comments

Comments
 (0)