-
Notifications
You must be signed in to change notification settings - Fork 117
/
status.go
186 lines (157 loc) · 5.12 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package project
import (
"fmt"
"strings"
"time"
"github.com/rilldata/rill/cli/pkg/cmdutil"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime"
runtimeclient "github.com/rilldata/rill/runtime/client"
"github.com/spf13/cobra"
)
func StatusCmd(ch *cmdutil.Helper) *cobra.Command {
var name, path string
statusCmd := &cobra.Command{
Use: "status [<project-name>]",
Args: cobra.MaximumNArgs(1),
Short: "Project deployment status",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := ch.Client()
if err != nil {
return err
}
if len(args) > 0 {
name = args[0]
}
if !cmd.Flags().Changed("project") && len(args) == 0 && ch.Interactive {
name, err = ch.InferProjectName(cmd.Context(), ch.Org, path)
if err != nil {
return err
}
}
proj, err := client.GetProject(cmd.Context(), &adminv1.GetProjectRequest{
OrganizationName: ch.Org,
Name: name,
})
if err != nil {
return err
}
// 1. Print project info
ch.PrintfSuccess("Project info\n\n")
fmt.Printf(" Name: %s\n", proj.Project.Name)
fmt.Printf(" Organization: %v\n", proj.Project.OrgName)
fmt.Printf(" Public: %v\n", proj.Project.Public)
fmt.Printf(" Github: %v\n", proj.Project.GithubUrl)
fmt.Printf(" Created: %s\n", proj.Project.CreatedOn.AsTime().Local().Format(time.RFC3339))
fmt.Printf(" Updated: %s\n", proj.Project.UpdatedOn.AsTime().Local().Format(time.RFC3339))
depl := proj.ProdDeployment
if depl == nil {
return nil
}
// 2. Print deployment info
ch.PrintfSuccess("\nDeployment info\n\n")
fmt.Printf(" Web: %s\n", proj.Project.FrontendUrl)
fmt.Printf(" Runtime: %s\n", depl.RuntimeHost)
fmt.Printf(" Instance: %s\n", depl.RuntimeInstanceId)
fmt.Printf(" Driver: %s\n", proj.Project.ProdOlapDriver)
if proj.Project.ProdOlapDsn != "" {
fmt.Printf(" OLAP DSN: %s\n", proj.Project.ProdOlapDsn)
}
fmt.Printf(" Slots: %d\n", depl.Slots)
fmt.Printf(" Branch: %s\n", depl.Branch)
if proj.Project.Subpath != "" {
fmt.Printf(" Subpath: %s\n", proj.Project.Subpath)
}
fmt.Printf(" Created: %s\n", depl.CreatedOn.AsTime().Local().Format(time.RFC3339))
fmt.Printf(" Updated: %s\n", depl.UpdatedOn.AsTime().Local().Format(time.RFC3339))
if depl.Status != adminv1.DeploymentStatus_DEPLOYMENT_STATUS_OK {
fmt.Printf(" Status: %s\n", depl.Status.String())
fmt.Printf(" Status Message: %s\n", depl.StatusMessage)
// Deployment not available
return nil
}
// 3. Print parser and resources info
rt, err := runtimeclient.New(depl.RuntimeHost, proj.Jwt)
if err != nil {
return fmt.Errorf("failed to connect to runtime: %w", err)
}
res, err := rt.ListResources(cmd.Context(), &runtimev1.ListResourcesRequest{InstanceId: depl.RuntimeInstanceId})
if err != nil {
return fmt.Errorf("failed to list resources: %w", err)
}
var parser *runtimev1.ProjectParser
var table []*resourceTableRow
for _, r := range res.Resources {
if r.Meta.Name.Kind == runtime.ResourceKindProjectParser {
parser = r.GetProjectParser()
}
if r.Meta.Hidden {
continue
}
table = append(table, newResourceTableRow(r))
}
ch.PrintfSuccess("\nResources\n\n")
ch.PrintData(table)
if parser.State != nil && len(parser.State.ParseErrors) != 0 {
var table []*parseErrorTableRow
for _, e := range parser.State.ParseErrors {
table = append(table, newParseErrorTableRow(e))
}
ch.PrintfSuccess("\nParse errors\n\n")
ch.PrintData(table)
}
return nil
},
}
statusCmd.Flags().StringVar(&name, "project", "", "Project Name")
statusCmd.Flags().StringVar(&path, "path", ".", "Project directory")
return statusCmd
}
type resourceTableRow struct {
Type string `header:"type"`
Name string `header:"name"`
Status string `header:"status"`
Error string `header:"error"`
}
func newResourceTableRow(r *runtimev1.Resource) *resourceTableRow {
truncErr := r.Meta.ReconcileError
if len(truncErr) > 80 {
truncErr = truncErr[:80] + "..."
}
return &resourceTableRow{
Type: formatResourceKind(r.Meta.Name.Kind),
Name: r.Meta.Name.Name,
Status: formatReconcileStatus(r.Meta.ReconcileStatus),
Error: truncErr,
}
}
func formatResourceKind(k string) string {
k = strings.TrimPrefix(k, "rill.runtime.v1.")
k = strings.TrimSuffix(k, "V2")
return k
}
func formatReconcileStatus(s runtimev1.ReconcileStatus) string {
switch s {
case runtimev1.ReconcileStatus_RECONCILE_STATUS_UNSPECIFIED:
return "Unknown"
case runtimev1.ReconcileStatus_RECONCILE_STATUS_IDLE:
return "Idle"
case runtimev1.ReconcileStatus_RECONCILE_STATUS_PENDING:
return "Pending"
case runtimev1.ReconcileStatus_RECONCILE_STATUS_RUNNING:
return "Running"
default:
panic(fmt.Errorf("unknown reconcile status: %s", s.String()))
}
}
type parseErrorTableRow struct {
Path string `header:"path"`
Error string `header:"error"`
}
func newParseErrorTableRow(e *runtimev1.ParseError) *parseErrorTableRow {
return &parseErrorTableRow{
Path: e.FilePath,
Error: e.Message,
}
}