/
status.go
102 lines (91 loc) · 3.29 KB
/
status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package deploy
import (
"context"
"fmt"
deploy "github.com/armory-io/deploy-engine/pkg"
"github.com/spf13/cobra"
_nethttp "net/http"
"time"
)
const (
deployStatusShort = "Watch deployment on Armory Cloud"
deployStatusLong = "Watch deployment on Armory Cloud"
deployStatusExample = "armory deploy status [options]"
)
type deployStatusOptions struct {
*deployOptions
}
type FormattableDeployStatus struct {
DeployResp deploy.DeploymentV2DeploymentStatusResponse `json:"deployment"`
httpResponse *_nethttp.Response
err error
}
func (u FormattableDeployStatus) Get() interface{} {
return u.DeployResp
}
func (u FormattableDeployStatus) GetHttpResponse() *_nethttp.Response {
return u.httpResponse
}
func (u FormattableDeployStatus) GetFetchError() error {
return u.err
}
func newDeployStatusResponseWrapper(raw deploy.DeploymentV2DeploymentStatusResponse, response *_nethttp.Response, err error) FormattableDeployStatus {
wrapper := FormattableDeployStatus{
DeployResp: raw,
httpResponse: response,
err: err,
}
return wrapper
}
func (u FormattableDeployStatus) String() string {
ret := ""
now := time.Now().Format(time.RFC3339)
ret += fmt.Sprintf("[%v] application: %s, started: %s\n", now, u.DeployResp.GetApplication(), u.DeployResp.GetStartedAtIso8601())
ret += fmt.Sprintf("[%v] status: ", now)
switch status := u.DeployResp.GetStatus(); status {
case deploy.DEPLOYMENTV2DEPLOYMENTSTATUSRESPONSESTATUS_PAUSED:
end := u.DeployResp.Kubernetes.Canary.PauseInfo.GetEndTimeIso8601()
reason := u.DeployResp.Kubernetes.Canary.PauseInfo.GetReason()
if reason == "" {
reason = "unspecified"
}
ret += fmt.Sprintf("[%s] msg: Paused until %s for reason: %s. You can skip the pause in the cloud console or CLI\n", status, end, reason)
case deploy.DEPLOYMENTV2DEPLOYMENTSTATUSRESPONSESTATUS_AWAITING_APPROVAL:
ret += fmt.Sprintf("[%s] msg: Paused for Manual Judgment. You can approve the rollout and continue the deployment in the cloud console or CLI.\n", status)
default:
ret += string(status) + "\n"
}
return ret
}
func NewDeployStatusCmd(deployOptions *deployOptions) *cobra.Command {
options := &deployStatusOptions{
deployOptions: deployOptions,
}
cmd := &cobra.Command{
Use: "status --deploymentId [deploymentId]",
Aliases: []string{"status"},
Short: deployStatusShort,
Long: deployStatusLong,
Example: deployStatusExample,
RunE: func(cmd *cobra.Command, args []string) error {
return status(cmd, options)
},
}
cmd.Flags().StringVarP(&options.deploymentId, "deploymentId", "i", "", "(Required) The ID of an existing deployment.")
cmd.MarkFlagRequired("deploymentId")
return cmd
}
func status(cmd *cobra.Command, options *deployStatusOptions) error {
ctx, cancel := context.WithTimeout(options.DeployClient.Context, time.Second * 5)
defer cancel()
req := options.DeployClient.DeploymentServiceApi.DeploymentServiceStatus(ctx, options.deploymentId)
deployResp, response, err := req.Execute()
dataFormat, err := options.Output.Formatter(newDeployStatusResponseWrapper(deployResp, response, err))
// if we've made it this far, the command is valid. if an error occurs it isn't a usage error
cmd.SilenceUsage = true
if err != nil {
return fmt.Errorf("error trying to parse respone: %s", err)
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), dataFormat)
return err
}