/
agent_repo.go
251 lines (228 loc) · 7.68 KB
/
agent_repo.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Copyright (c) 2024 Yunshan Networks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ctl
import (
"bytes"
"errors"
"fmt"
"io"
"mime/multipart"
"os"
"os/exec"
"path"
"regexp"
"strings"
"github.com/spf13/cobra"
"github.com/deepflowio/deepflow/cli/ctl/common"
"github.com/deepflowio/deepflow/cli/ctl/common/jsonparser"
"github.com/deepflowio/deepflow/cli/ctl/common/printutil"
)
var (
BranchRegex = regexp.MustCompile(`Branch:[\s](.*)`)
RevCountRegex = regexp.MustCompile(`RevCount:[\s](.*)`)
CommitIDRegex = regexp.MustCompile(`CommitId:[\s](.*)`)
)
func RegisterRepoCommand() *cobra.Command {
repo := &cobra.Command{
Use: "repo",
Short: "repo operation commands",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("please run with 'agent'.\n")
},
}
repo.AddCommand(registerAgentCommand())
return repo
}
func registerAgentCommand() *cobra.Command {
agent := &cobra.Command{
Use: "agent",
Short: "repo agent operation commands",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("please run with 'list | create | delete'.\n")
},
}
var arch, image, versionImage string
timeout := common.DefaultTimeout
create := &cobra.Command{
Use: "create",
Short: "create repo agent",
Example: "deepflow-ctl repo agent create --arch x86 --image deepflow-agent",
Run: func(cmd *cobra.Command, args []string) {
if _, err := os.Stat(image); errors.Is(err, os.ErrNotExist) {
fmt.Printf("file %s not found\n", image)
return
}
if strings.HasSuffix(image, ".exe") {
if versionImage == "" {
printutil.ErrorWithColor("version-image must be set when uploading a window image")
return
}
if _, err := os.Stat(versionImage); errors.Is(err, os.ErrNotExist) {
fmt.Printf("file %s not found\n", versionImage)
return
}
printutil.WarnfWithColor("make sure %s and %s have the same version", image, versionImage)
}
if err := createRepoAgent(cmd, arch, image, versionImage); err != nil {
fmt.Println(err)
}
},
}
create.Flags().StringVarP(&arch, "arch", "", "", "arch of deepflow-agent")
create.Flags().StringVarP(&image, "image", "", "", "deepflow-agent image to upload")
create.Flags().StringVarP(&versionImage, "version-image", "", "", "deepflow-agent image to get branch, rev_count and commit_id")
create.Flags().DurationVar(&timeout, "timeout", 0, "timeout duration(default: 30s), e.g., 1s 1m 1h")
create.MarkFlagsRequiredTogether("arch", "image")
list := &cobra.Command{
Use: "list",
Short: "list repo agent",
Example: "deepflow-ctl repo agent list",
Run: func(cmd *cobra.Command, args []string) {
listRepoAgent(cmd)
},
}
delete := &cobra.Command{
Use: "delete",
Short: "delete repo agent",
Example: "deepflow-ctl repo agent delete <name>",
Run: func(cmd *cobra.Command, args []string) {
if err := deleteRepoAgent(cmd, args); err != nil {
fmt.Println(err)
}
},
}
agent.AddCommand(create)
agent.AddCommand(list)
agent.AddCommand(delete)
return agent
}
func createRepoAgent(cmd *cobra.Command, arch, image, versionImage string) error {
execImage := image
if versionImage != "" {
execImage = versionImage
}
agentOutput, err := getAgentOutput(execImage)
if err != nil {
return err
}
branch, revCount, commitID := getAgentInfo(agentOutput)
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
bodyWriter.WriteField("NAME", path.Base(image))
bodyWriter.WriteField("ARCH", arch)
bodyWriter.WriteField("BRANCH", branch)
bodyWriter.WriteField("REV_COUNT", revCount)
bodyWriter.WriteField("COMMIT_ID", commitID)
osStr := "Linux"
if strings.HasSuffix(image, ".exe") {
osStr = "Windows"
}
bodyWriter.WriteField("OS", osStr)
fileWriter, err := bodyWriter.CreateFormFile("IMAGE", path.Base(image))
f, err := os.Open(image)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(fileWriter, f); err != nil {
return err
}
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()
server := common.GetServerInfo(cmd)
url := fmt.Sprintf("http://%s:%d/v1/vtap-repo/", server.IP, server.Port)
resp, err := common.CURLPostFormData(url, contentType, bodyBuf, []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
if err != nil {
return err
}
data := resp.Get("DATA")
fmt.Printf("created successfully, os: %s, branch: %s, rev_count: %s, commit_id: %s\n", data.Get("OS").MustString(),
data.Get("BRANCH").MustString(), data.Get("REV_COUNT").MustString(), data.Get("COMMIT_ID").MustString())
return nil
}
func getAgentOutput(image string) (string, error) {
if !path.IsAbs(image) {
image = "./" + image
}
command := image + " -v"
output, err := exec.Command("/usr/bin/bash", "-c", command).CombinedOutput()
if err != nil {
return "", fmt.Errorf("command(%v) failed; result: %v, error:%v", command, string(output), err)
}
return string(output), nil
}
func getAgentInfo(s string) (branch, revCount, commitID string) {
branchSubmatch := BranchRegex.FindStringSubmatch(s)
if len(branchSubmatch) > 1 {
branch = branchSubmatch[1]
}
revCountSubmatch := RevCountRegex.FindStringSubmatch(s)
if len(revCountSubmatch) > 1 {
revCount = revCountSubmatch[1]
}
commitIDSubmatch := CommitIDRegex.FindStringSubmatch(s)
if len(commitIDSubmatch) > 1 {
commitID = commitIDSubmatch[1]
}
return
}
func listRepoAgent(cmd *cobra.Command) {
server := common.GetServerInfo(cmd)
url := fmt.Sprintf("http://%s:%d/v1/vtap-repo/", server.IP, server.Port)
response, err := common.CURLPerform("GET", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
if err != nil {
fmt.Println(err)
return
}
data := response.Get("DATA")
var (
nameMaxSize = jsonparser.GetTheMaxSizeOfAttr(data, "NAME")
archMaxSize = jsonparser.GetTheMaxSizeOfAttr(data, "ARCH")
osMaxSize = jsonparser.GetTheMaxSizeOfAttr(data, "OS")
branchMaxSize = jsonparser.GetTheMaxSizeOfAttr(data, "BRANCH")
revCountMaxSize = jsonparser.GetTheMaxSizeOfAttr(data, "REV_COUNT")
commitIDMaxSize = jsonparser.GetTheMaxSizeOfAttr(data, "COMMIT_ID")
)
cmdFormat := "%-*s %-*s %-*s %-*s %-*s %-19s %-*s\n"
fmt.Printf(cmdFormat, nameMaxSize, "NAME", archMaxSize, "ARCH", osMaxSize, "OS", branchMaxSize, "BRANCH",
revCountMaxSize, "REV_COUNT", "UPDATED_AT", commitIDMaxSize, "COMMIT_ID")
for i := range data.MustArray() {
d := data.GetIndex(i)
fmt.Printf(cmdFormat,
nameMaxSize, d.Get("NAME").MustString(),
archMaxSize, d.Get("ARCH").MustString(),
osMaxSize, d.Get("OS").MustString(),
branchMaxSize, d.Get("BRANCH").MustString(),
revCountMaxSize, d.Get("REV_COUNT").MustString(),
d.Get("UPDATED_AT").MustString(),
commitIDMaxSize, d.Get("COMMIT_ID").MustString(),
)
}
}
func deleteRepoAgent(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("must specify name.\nExample: %s", cmd.Example)
} else if len(args) > 1 {
return fmt.Errorf("must specify one name.\nExample: %s", cmd.Example)
}
server := common.GetServerInfo(cmd)
url := fmt.Sprintf("http://%s:%d/v1/vtap-repo/%s/", server.IP, server.Port, args[0])
_, err := common.CURLPerform("DELETE", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
if err != nil {
return err
}
return nil
}