/
govc.go
183 lines (157 loc) · 3.94 KB
/
govc.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
package action_govc
import (
"bufio"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/Mrpye/go-action-workflow/workflow"
)
type GovcSchema struct {
}
// GetFunctionMap returns the function map for this schema
func (s GovcSchema) GetFunctionMap() map[string]workflow.FunctionSchema {
return map[string]workflow.FunctionSchema{}
}
// GetTargetSchema returns the target schema for this action
func (s GovcSchema) GetTargetSchema() map[string]workflow.TargetSchema {
//Build a test schema
return map[string]workflow.TargetSchema{
"action_govc.vcenter": workflow.BuildTargetConfig("vcenter", "vcenter", &VCenter{}),
}
}
// GetActions returns the actions for this schema
func (s GovcSchema) GetActionSchema() map[string]workflow.ActionSchema {
//no short d f h
return map[string]workflow.ActionSchema{
"govc": {
Action: Action_GOVC,
Short: "Run GOVC commands",
Long: "Run GOVC commands",
ProcessResults: true,
Target: "action_govc.vcenter",
ConfigSchema: map[string]*workflow.Schema{
"target_name": {
Type: workflow.TypeString,
Partial: false,
Required: false,
Description: "The target to use",
ConfigKey: "target_name",
Short: "t",
Default: "",
},
"command": {
Type: workflow.TypeString,
Partial: false,
Required: true,
Description: "Command to run",
Short: "c",
Default: "",
},
},
},
}
}
// GetAction returns the action for the target
func GetSchema() workflow.SchemaEndpoint {
return GovcSchema{}
}
func PrintSomethingOut(out io.Writer) {
fmt.Fprintln(out, "print something to io.Writer")
}
/*func capture() func() (string, error) {
r, w, err := os.Pipe()
if err != nil {
panic(err)
}
done := make(chan error, 1)
save := os.Stdout
os.Stdout = w
var buf strings.Builder
go func() {
_, err := io.Copy(&buf, r)
r.Close()
done <- err
}()
return func() (string, error) {
os.Stdout = save
w.Close()
err := <-done
return buf.String(), err
}
}*/
func Action_GOVC(w *workflow.Workflow, m *workflow.TemplateData) error {
govc_obj, err := w.MapTargetConfigValue(m, &VCenter{})
if err != nil {
return err
}
vcenter_target := govc_obj.(*VCenter)
//**********************
//Get the command string
//**********************
command, err := w.GetConfigTokenString("command", m, true)
if err != nil {
return err
}
//remove the carriage character
command = strings.ReplaceAll(command, "\n", " ")
//***********************
//Run the vcenter command
//***********************
rescueStdout := os.Stdout
rescueStderr := os.Stderr
//Error Pipe
er, ew, _ := os.Pipe()
os.Stderr = ew
//******************************************************************************
// Read the output in a separate goroutine so printing can't block indefinitely.
//******************************************************************************
var buf strings.Builder
scanner := bufio.NewScanner(io.Reader(rescueStdout))
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
m := scanner.Text()
buf.WriteString(string(m))
fmt.Println(string(m))
}
//******************
//Run the command
//******************
err = vcenter_target.Run(command, false)
if err != nil {
return err
}
//******************
//Close the pipes
//******************
ew.Close()
err_out, _ := ioutil.ReadAll(er)
os.Stderr = rescueStderr
os.Stdout = rescueStdout
//******************
//Display the result
//******************
if w.LogLevel > workflow.LOG_QUIET {
fmt.Println(string(buf.String()))
fmt.Println(string(err_out))
}
//******************
//Check for errors
//******************
if string(err_out) != "" {
return errors.New(string(err_out))
}
//*******************
//Process the results
//*******************
err = w.ActionProcessResults(m, buf.String())
if err != nil {
return err
}
//**************
//Return the err
//**************
return nil
}