Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wait flag for Services #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/koyeb/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func NewServiceCmd() *cobra.Command {
}
addServiceDefinitionFlags(createServiceCmd.Flags())
createServiceCmd.Flags().StringP("app", "a", "", "App")
createServiceCmd.Flags().BoolP("wait", "w", false, "Wait for the service deployment to be completed")
createServiceCmd.MarkFlagRequired("app")
serviceCmd.AddCommand(createServiceCmd)

Expand Down Expand Up @@ -106,6 +107,7 @@ func NewServiceCmd() *cobra.Command {
},
}
addServiceDefinitionFlags(updateServiceCmd.Flags())
updateServiceCmd.Flags().BoolP("wait", "w", false, "Wait for the service deployment to be completed")
serviceCmd.AddCommand(updateServiceCmd)

redeployServiceCmd := &cobra.Command{
Expand All @@ -114,6 +116,7 @@ func NewServiceCmd() *cobra.Command {
Args: cobra.ExactArgs(1),
RunE: h.ReDeploy,
}
redeployServiceCmd.Flags().BoolP("wait", "w", false, "Wait for the service deployment to be completed")
serviceCmd.AddCommand(redeployServiceCmd)

deleteServiceCmd := &cobra.Command{
Expand Down Expand Up @@ -178,6 +181,7 @@ func addServiceDefinitionFlags(flags *pflag.FlagSet) {
flags.String("instance-type", "nano", "Instance type")
flags.Int64("min-scale", 1, "Min scale")
flags.Int64("max-scale", 1, "Max scale")

}

func parseServiceDefinitionFlags(flags *pflag.FlagSet, definition *koyeb.ServiceDefinition, useDefault bool) error {
Expand Down
13 changes: 12 additions & 1 deletion pkg/koyeb/services_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,20 @@ func (h *ServiceHandler) Create(cmd *cobra.Command, args []string, createService

log.Infof("Service deployment in progress. Access deployment logs running: koyeb service logs %s.", res.Service.GetId()[:8])

wait := GetBoolFlags(cmd, "wait")
if wait {
watchDeployment(h, res.Service.GetLatestDeploymentId())
}

full := GetBoolFlags(cmd, "full")
output := GetStringFlags(cmd, "output")
getServiceReply := NewGetServiceReply(h.mapper, &koyeb.GetServiceReply{Service: res.Service}, full)

gRes, gResp, err := h.client.ServicesApi.GetService(h.ctx, res.Service.GetId()).Execute()
if err != nil {
fatalApiError(err, gResp)
}

getServiceReply := NewGetServiceReply(h.mapper, &koyeb.GetServiceReply{Service: gRes.Service}, full)

return renderer.NewDescribeRenderer(getServiceReply).Render(output)
}
24 changes: 23 additions & 1 deletion pkg/koyeb/services_redeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package koyeb

import (
"github.com/koyeb/koyeb-api-client-go/api/v1/koyeb"
"github.com/koyeb/koyeb-cli/pkg/koyeb/renderer"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -13,5 +14,26 @@ func (h *ServiceHandler) ReDeploy(cmd *cobra.Command, args []string) error {
fatalApiError(err, resp)
}
log.Infof("Service %s redeployed.", args[0])
return nil

gRes, gResp, err := h.client.ServicesApi.GetService(h.ctx, h.ResolveServiceArgs(args[0])).Execute()
if err != nil {
fatalApiError(err, gResp)
}

wait := GetBoolFlags(cmd, "wait")
if wait {
watchDeployment(h, gRes.Service.GetLatestDeploymentId())
}

full := GetBoolFlags(cmd, "full")
output := GetStringFlags(cmd, "output")

gRes, gResp, err = h.client.ServicesApi.GetService(h.ctx, h.ResolveServiceArgs(args[0])).Execute()
if err != nil {
fatalApiError(err, gResp)
}

getServiceReply := NewGetServiceReply(h.mapper, &koyeb.GetServiceReply{Service: gRes.Service}, full)

return renderer.NewDescribeRenderer(getServiceReply).Render(output)
}
15 changes: 13 additions & 2 deletions pkg/koyeb/services_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,20 @@ func (h *ServiceHandler) Update(cmd *cobra.Command, args []string, updateService
}
log.Infof("Service deployment in progress. Access deployment logs running: koyeb service logs %s.", res.Service.GetId()[:8])

wait := GetBoolFlags(cmd, "wait")
if wait {
watchDeployment(h, res.Service.GetLatestDeploymentId())
}

full := GetBoolFlags(cmd, "full")
output := GetStringFlags(cmd, "output")
getServiceReply := NewGetServiceReply(h.mapper, &koyeb.GetServiceReply{Service: res.Service}, full)

return renderer.NewDescribeItemRenderer(getServiceReply).Render(output)
gRes, gResp, err := h.client.ServicesApi.GetService(h.ctx, res.Service.GetId()).Execute()
if err != nil {
fatalApiError(err, gResp)
}

getServiceReply := NewGetServiceReply(h.mapper, &koyeb.GetServiceReply{Service: gRes.Service}, full)

return renderer.NewDescribeRenderer(getServiceReply).Render(output)
}
29 changes: 29 additions & 0 deletions pkg/koyeb/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"errors"
"io/ioutil"
"strings"
"time"

"github.com/ghodss/yaml"
"github.com/koyeb/koyeb-api-client-go/api/v1/koyeb"
log "github.com/sirupsen/logrus"
)

func isYaml(file string) bool {
Expand Down Expand Up @@ -69,3 +72,29 @@ func loadYaml(file string) (string, error) {
}
return "", errors.New("Unknown format")
}

func watchDeployment(h *ServiceHandler, deploymentId string) {
edouardb marked this conversation as resolved.
Show resolved Hide resolved
numAttemptsOnUnhealthy := 12
retryCount := 0
retryInterval := 5

for {
res, resp, err := h.client.DeploymentsApi.GetDeployment(h.ctx, deploymentId).Execute()
if err != nil {
fatalApiError(err, resp)
}
currentStatus := res.Deployment.GetStatus()

log.Infof("Service deployment in progress. Deployment status is %s. Next update in %d seconds.", currentStatus, retryInterval)
edouardb marked this conversation as resolved.
Show resolved Hide resolved

if currentStatus == koyeb.DEPLOYMENTSTATUS_ERROR || currentStatus == koyeb.DEPLOYMENTSTATUS_HEALTHY || retryCount >= numAttemptsOnUnhealthy {
break
} else if currentStatus == koyeb.DEPLOYMENTSTATUS_UNHEALTHY {
retryCount += 1
retryInterval = 10
time.Sleep(time.Duration(retryInterval) * time.Second)
} else {
time.Sleep(time.Duration(retryInterval) * time.Second)
}
}
}