forked from rexray/rexray
/
cmds_module.go
208 lines (169 loc) · 4.79 KB
/
cmds_module.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
package cli
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
log "github.com/Sirupsen/logrus"
"github.com/akutz/gotil"
"github.com/spf13/cobra"
)
func (c *CLI) initModuleCmdsAndFlags() {
c.initModuleCmds()
c.initModuleFlags()
}
func (c *CLI) initModuleCmds() {
c.moduleCmd = &cobra.Command{
Use: "module",
Short: "The module manager",
Run: func(cmd *cobra.Command, args []string) {
cmd.Usage()
},
}
c.serviceCmd.AddCommand(c.moduleCmd)
c.moduleTypesCmd = &cobra.Command{
Use: "types",
Short: "List the available module types and their IDs",
Run: func(cmd *cobra.Command, args []string) {
_, addr, addrErr := gotil.ParseAddress(c.host())
if addrErr != nil {
panic(addrErr)
}
u := fmt.Sprintf("http://%s/r/module/types", addr)
client := &http.Client{}
resp, respErr := client.Get(u)
if respErr != nil {
panic(respErr)
}
defer resp.Body.Close()
body, bodyErr := ioutil.ReadAll(resp.Body)
if bodyErr != nil {
panic(bodyErr)
}
fmt.Println(string(body))
},
}
c.moduleCmd.AddCommand(c.moduleTypesCmd)
c.moduleInstancesCmd = &cobra.Command{
Use: "instance",
Short: "The module instance manager",
Run: func(cmd *cobra.Command, args []string) {
cmd.Usage()
},
}
c.moduleCmd.AddCommand(c.moduleInstancesCmd)
c.moduleInstancesListCmd = &cobra.Command{
Use: "get",
Aliases: []string{"ls", "list"},
Short: "List the running module instances",
Run: func(cmd *cobra.Command, args []string) {
_, addr, addrErr := gotil.ParseAddress(c.host())
if addrErr != nil {
panic(addrErr)
}
u := fmt.Sprintf("http://%s/r/module/instances", addr)
client := &http.Client{}
resp, respErr := client.Get(u)
if respErr != nil {
panic(respErr)
}
defer resp.Body.Close()
body, bodyErr := ioutil.ReadAll(resp.Body)
if bodyErr != nil {
panic(bodyErr)
}
fmt.Println(string(body))
},
}
c.moduleInstancesCmd.AddCommand(c.moduleInstancesListCmd)
c.moduleInstancesCreateCmd = &cobra.Command{
Use: "create",
Aliases: []string{"new"},
Short: "Create a new module instance",
Run: func(cmd *cobra.Command, args []string) {
_, addr, addrErr := gotil.ParseAddress(c.host())
if addrErr != nil {
panic(addrErr)
}
if c.moduleTypeID == -1 || c.moduleInstanceAddress == "" {
cmd.Usage()
return
}
modTypeIDStr := fmt.Sprintf("%d", c.moduleTypeID)
modInstStartStr := fmt.Sprintf("%v", c.moduleInstanceStart)
u := fmt.Sprintf("http://%s/r/module/instances", addr)
cfgJSON, cfgJSONErr := c.r.Config.ToJSON()
if cfgJSONErr != nil {
panic(cfgJSONErr)
}
log.WithFields(log.Fields{
"url": u,
"typeId": modTypeIDStr,
"address": c.moduleInstanceAddress,
"start": modInstStartStr,
"config": cfgJSON}).Debug("post create module instance")
client := &http.Client{}
resp, respErr := client.PostForm(u,
url.Values{
"typeId": {modTypeIDStr},
"address": {c.moduleInstanceAddress},
"start": {modInstStartStr},
"config": {cfgJSON},
})
if respErr != nil {
panic(respErr)
}
defer resp.Body.Close()
body, bodyErr := ioutil.ReadAll(resp.Body)
if bodyErr != nil {
panic(bodyErr)
}
fmt.Println(string(body))
},
}
c.moduleInstancesCmd.AddCommand(c.moduleInstancesCreateCmd)
c.moduleInstancesStartCmd = &cobra.Command{
Use: "start",
Short: "Starts a module instance",
Run: func(cmd *cobra.Command, args []string) {
_, addr, addrErr := gotil.ParseAddress(c.host())
if addrErr != nil {
panic(addrErr)
}
if c.moduleInstanceID == -1 {
cmd.Usage()
return
}
u := fmt.Sprintf(
"http://%s/r/module/instances/%d/start", addr, c.moduleInstanceID)
client := &http.Client{}
resp, respErr := client.Get(u)
if respErr != nil {
panic(respErr)
}
defer resp.Body.Close()
body, bodyErr := ioutil.ReadAll(resp.Body)
if bodyErr != nil {
panic(bodyErr)
}
fmt.Println(string(body))
},
}
c.moduleInstancesCmd.AddCommand(c.moduleInstancesStartCmd)
}
func (c *CLI) initModuleFlags() {
c.moduleInstancesCreateCmd.Flags().Int32VarP(&c.moduleTypeID, "id",
"i", -1, "The ID of the module type to instance")
c.moduleInstancesCreateCmd.Flags().StringVarP(&c.moduleInstanceAddress,
"address", "a", "",
"The network address at which the module will be exposed")
c.moduleInstancesCreateCmd.Flags().BoolVarP(&c.moduleInstanceStart,
"start", "s", false,
"A flag indicating whether or not to start the module upon creation")
c.moduleInstancesCreateCmd.Flags().StringSliceVarP(&c.moduleConfig,
"options", "o", nil,
"A comma-seperated string of key=value pairs used by some module "+
"types for custom configuraitons.")
c.moduleInstancesStartCmd.Flags().Int32VarP(&c.moduleInstanceID, "id",
"i", -1, "The ID of the module instance to start")
}