-
Notifications
You must be signed in to change notification settings - Fork 24
/
cc.go
175 lines (148 loc) · 4.04 KB
/
cc.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
package cc
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"time"
"github.com/aserto-dev/clui"
"github.com/aserto-dev/topaz/pkg/cli/cc/iostream"
"github.com/aserto-dev/topaz/pkg/cli/dockerx"
"github.com/docker/docker/api/types"
"github.com/fullstorydev/grpcurl"
"github.com/samber/lo"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)
var (
ErrNotRunning = errors.New("topaz is not running, use 'topaz start' or 'topaz run' to start")
ErrIsRunning = errors.New("topaz is already running, use 'topaz stop' to stop")
ErrNotServing = errors.New("topaz gRPC endpoint not SERVING")
)
type CommonCtx struct {
Context context.Context
UI *clui.UI
Config *CLIConfig
}
type CLIConfig struct {
Version int `json:"version"`
Active ActiveConfig `json:"active"`
Running RunningConfig `json:"running"`
Defaults DefaultsConfig `json:"defaults"`
}
type ActiveConfig struct {
Config string `json:"config"`
ConfigFile string `json:"config_file"`
}
type RunningConfig struct {
ActiveConfig
ContainerName string `json:"container_name"`
}
type DefaultsConfig struct {
NoCheck bool `json:"no_check"`
ContainerRegistry string `json:"container_registry"`
ContainerImage string `json:"container_image"`
ContainerTag string `json:"container_tag"`
ContainerPlatform string `json:"container_platform"`
}
type runStatus int
const (
StatusNotRunning runStatus = iota
StatusRunning
)
func NewCommonContext(noCheck bool, configFilePath string) (*CommonCtx, error) {
ctx := &CommonCtx{
Context: context.Background(),
UI: iostream.NewUI(iostream.DefaultIO()),
Config: &CLIConfig{
Version: 1,
Active: ActiveConfig{
ConfigFile: filepath.Join(GetTopazCfgDir(), "config.yaml"),
Config: "config.yaml",
},
Defaults: DefaultsConfig{
NoCheck: noCheck,
ContainerRegistry: ContainerRegistry(),
ContainerImage: ContainerImage(),
ContainerTag: ContainerTag(),
ContainerPlatform: ContainerPlatform(),
},
Running: RunningConfig{},
},
}
if _, err := os.Stat(configFilePath); err == nil {
data, err := os.ReadFile(configFilePath)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, ctx.Config)
if err != nil {
return nil, err
}
}
return ctx, nil
}
func (c *CommonCtx) CheckRunStatus(containerName string, expectedStatus runStatus) bool {
if c.Config.Defaults.NoCheck {
return false
}
// set default container name if not specified.
if containerName == "" {
containerName = ContainerName(c.Config.Active.ConfigFile)
}
dc, err := dockerx.New()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return false
}
running, err := dc.IsRunning(containerName)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return false
}
return lo.Ternary(running, StatusRunning, StatusNotRunning) == expectedStatus
}
func (c *CommonCtx) GetRunningContainers() ([]*types.Container, error) {
dc, err := dockerx.New()
if err != nil {
return nil, err
}
topazContainers, err := dc.GetRunningTopazContainers()
if err != nil {
return nil, err
}
return topazContainers, nil
}
func (c *CommonCtx) IsServing(grpcAddress string) bool {
if c.Config.Defaults.NoCheck {
return true
}
tlsConf, err := grpcurl.ClientTLSConfig(true, "", "", "")
if err != nil {
return false
}
creds := credentials.NewTLS(tlsConf)
opts := []grpc.DialOption{
grpc.WithUserAgent("topaz/dev-build (no version set)"),
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_, err = grpcurl.BlockingDial(ctx, "tcp", grpcAddress, creds, opts...)
return err == nil
}
func (c *CommonCtx) SaveContextConfig(configurationFile string) error {
cliConfig := filepath.Join(GetTopazDir(), configurationFile)
kongConfigBytes, err := json.Marshal(c.Config)
if err != nil {
return err
}
err = os.WriteFile(cliConfig, kongConfigBytes, 0666) // nolint
if err != nil {
return err
}
return nil
}