-
Notifications
You must be signed in to change notification settings - Fork 0
/
poddy.go
216 lines (194 loc) · 5.02 KB
/
poddy.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
package main
import (
"bytes"
"fmt"
"github.com/AlecAivazis/survey/v2"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)
func main() {
cmd := exec.Command("kubectl", "get", "pods", "--template", "{{range .items}}{{.metadata.name}}{{\";;\"}}{{end}}")
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
return
}
options := strings.Split(out.String(), ";;")
selectedPods := []string{}
podPrompt := &survey.MultiSelect{
Message: "Select pods:",
Options: options,
}
err = survey.AskOne(podPrompt, &selectedPods)
if err != nil {
return
}
cmdType := ""
cmdTypePrompt := &survey.Select{
Message: "Choose action:",
Options: []string{"logs", "exec", "fileUpload", "fileDownload"},
}
err = survey.AskOne(cmdTypePrompt, &cmdType)
if err != nil {
return
}
if cmdType == "logs" {
if saveLogs(selectedPods, cmd) {
return
}
}
if cmdType == "exec" {
runCommandOnPod(selectedPods, cmd)
}
if cmdType == "fileUpload" {
uploadFile(selectedPods, cmd)
}
if cmdType == "fileDownload" {
downloadFile(selectedPods, cmd)
}
}
func downloadFile(selectedPods []string, cmd *exec.Cmd) {
targetLocation := "/"
sourceFile := ""
prompt := &survey.Input{
Message: "Remote file path:",
Default: sourceFile,
}
survey.AskOne(prompt, &sourceFile)
prompt = &survey.Input{
Message: "Target dir:",
Suggest: ListDirectories,
}
survey.AskOne(prompt, &targetLocation)
targetFilename := filepath.Base(sourceFile)
for _, podName := range selectedPods {
target := targetLocation + "/" + podName + "/" + targetFilename
commandToExec := "kubectl cp " + podName + ":" + sourceFile + " " + target
splitCommand := strings.Split(commandToExec, " ")
cmd = exec.Command(splitCommand[0], splitCommand[1:]...)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
fmt.Println("[" + podName + "]")
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
} else {
fmt.Println("File " + sourceFile + " successfully downloaded to " + target)
}
}
}
func uploadFile(selectedPods []string, cmd *exec.Cmd) {
prompt := &survey.Input{
Message: "Choose file to upload:",
Suggest: ListDirTree,
}
fileToUpload := ""
survey.AskOne(prompt, &fileToUpload)
targetLocation := "/"
prompt = &survey.Input{
Message: "Choose target location:",
Default: targetLocation,
}
survey.AskOne(prompt, &targetLocation)
for _, podName := range selectedPods {
commandToExec := "kubectl cp " + fileToUpload + " " + podName + ":" + targetLocation
splitCommand := strings.Split(commandToExec, " ")
cmd = exec.Command(splitCommand[0], splitCommand[1:]...)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
fmt.Println("[" + podName + "]")
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
} else {
fmt.Println("File " + fileToUpload + " successfully uploaded to " + targetLocation)
}
}
}
func runCommandOnPod(selectedPods []string, cmd *exec.Cmd) {
commandToRun := "ls -l"
prompt := &survey.Input{
Message: "Enter command:",
Default: commandToRun,
}
survey.AskOne(prompt, &commandToRun)
commandToExec := ""
for _, podName := range selectedPods {
commandToExec = "kubectl exec " + podName + " -- " + commandToRun
splitCommand := strings.Split(commandToExec, " ")
cmd = exec.Command(splitCommand[0], splitCommand[1:]...)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
fmt.Println("[" + podName + "]")
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
} else {
fmt.Println(out.String())
fmt.Println("")
}
}
}
func saveLogs(selectedPods []string, cmd *exec.Cmd) bool {
dirToSave := "logs"
prompt := &survey.Input{
Message: "Choose dir to save logs:",
Suggest: ListDirectories,
Default: dirToSave,
}
survey.AskOne(prompt, &dirToSave)
for _, podName := range selectedPods {
logFile := dirToSave + "/" + podName
if _, err := os.Stat(dirToSave); os.IsNotExist(err) {
err := os.Mkdir(dirToSave, 0755)
check(err)
}
cmd = exec.Command("kubectl", "logs", podName)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
return true
}
err = ioutil.WriteFile(logFile, out.Bytes(), 0644)
check(err)
fmt.Println("[" + podName + "] - logs saved to " + logFile)
}
return false
}
func ListDirectories(toComplete string) []string {
var directories []string
files, _ := filepath.Glob(toComplete + "*")
for _, path := range files {
fileInfo, _ := os.Stat(path)
if fileInfo.IsDir() {
directories = append(directories, path)
}
}
return directories
}
func ListDirTree(toComplete string) []string {
files, _ := filepath.Glob(toComplete + "*")
return files
}
func check(e error) {
if e != nil {
panic(e)
}
}