-
Notifications
You must be signed in to change notification settings - Fork 1
/
cf2go.go
264 lines (213 loc) · 6.83 KB
/
cf2go.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
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"sort"
"github.com/olekukonko/tablewriter"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
app = kingpin.New("cf2go", "A command line toolset to manage CF landscapes")
list = app.Command("list", "List landscapes configuration")
jump = app.Command("jump", "SSH to a jumpbox system")
jumpLandscapeID = jump.Arg("landscape-id", "Call 'cf2go list' to get available landscape ids").Required().String()
tunnel = app.Command("tunnel", "SSH tunnel via jumpbox to target")
tunnelLandscapeID = tunnel.Arg("landscape-id", "Call 'cf2go list' to get available landscape ids").Required().String()
tunnelTarget = tunnel.Arg("target", "Supported targets: [director|concourse], director is default").String()
details = app.Command("details", "Display landscape details")
detailsLandscapeID = details.Arg("landscape-id", "Call 'cf2go list' to get available landscape ids").Required().String()
login = app.Command("login", "login to cf api endpoint")
loginLandscapeID = login.Arg("landscape-id", "Call 'cf2go list' to get available landscape ids").Required().String()
url = app.Flag("url", "URL from which the landscape configuration is requested").Default("https://raw.githubusercontent.com/sklevenz/cf2go/master/landscape.json").String()
)
type detailsCommandStruct struct{}
type listCommandStruct struct{}
type loginCommandStruct struct{}
type jumpCommandStruct struct{}
type tunnelCommandStruct struct{}
type landscapeStruct struct {
LandscapeType string `json:"type"`
Description string `json:"description"`
Owner string `json:"owner"`
JumpboxIP string `json:"jumpbox"`
ConcourseIP string `json:"concourse"`
DirectorIP string `json:"director"`
Domain string `json:"domain"`
}
func (n *tunnelCommandStruct) run(c *kingpin.ParseContext) error {
config := parseConfiguration(readConfiguration())
landscape, ok := (*config)[*tunnelLandscapeID]
if !ok {
log.Printf("Unknown landscape id: %v", *tunnelLandscapeID)
os.Exit(1)
}
s := *tunnelTarget
var param string
switch {
case s == "" || s == "director":
param = fmt.Sprintf("localhost:25555:%v:25555", landscape.DirectorIP)
case s == "concourse":
param = fmt.Sprintf("localhost:8080:%v:8080", landscape.ConcourseIP)
default:
log.Printf("Unknown tunnel target: %v", *tunnelTarget)
os.Exit(1)
}
log.Println("Tunnel open: " + param)
cmdStr := "ssh"
cmd := exec.Command(cmdStr, "-nNTL", param, landscape.JumpboxIP)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Printf("Execute: %v %v", cmdStr, landscape.JumpboxIP)
log.Printf("Error: %v", err)
os.Exit(1)
}
return nil
}
func (n *loginCommandStruct) run(c *kingpin.ParseContext) error {
config := parseConfiguration(readConfiguration())
landscape, ok := (*config)[*loginLandscapeID]
if !ok {
log.Printf("Unknown landscape id: %v", *loginLandscapeID)
os.Exit(1)
}
cmdStr := "cf"
cmd := exec.Command(cmdStr, "api", fmt.Sprintf("https://api.cf.%s", landscape.Domain), "--skip-ssl-validation")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Printf("Execute: %v %v", cmdStr, landscape.JumpboxIP)
log.Printf("Error: %v", err)
os.Exit(1)
}
return nil
}
func (n *jumpCommandStruct) run(c *kingpin.ParseContext) error {
config := parseConfiguration(readConfiguration())
landscape, ok := (*config)[*jumpLandscapeID]
if !ok {
log.Printf("Unknown landscape id: %v", *jumpLandscapeID)
os.Exit(1)
}
cmdStr := "ssh"
cmd := exec.Command(cmdStr, landscape.JumpboxIP)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Printf("Execute: %v %v", cmdStr, landscape.JumpboxIP)
log.Printf("Error: %v", err)
os.Exit(1)
}
return nil
}
func (n *detailsCommandStruct) run(c *kingpin.ParseContext) error {
config := parseConfigurationRaw(readConfiguration())
landscape, ok := (*config)[*detailsLandscapeID]
if !ok {
log.Printf("Unknown landscape id: %v", *detailsLandscapeID)
os.Exit(1)
}
var keys []string
landscapeMap := landscape.(map[string]interface{})
for k := range landscapeMap {
keys = append(keys, k)
}
sort.Strings(keys)
fmt.Println("details called: " + *detailsLandscapeID)
fmt.Printf("landscape - %v: \n", landscape)
fmt.Printf("value: %v \n", landscapeMap["domain"])
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"property", "value"})
for _, k := range keys {
v := (landscapeMap)[k]
table.Append([]string{k, v.(string)})
}
table.Render() // Send output
return nil
}
func (n *listCommandStruct) run(c *kingpin.ParseContext) error {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"landscape-id", "type", "description", "owner", "jumpbox ip", "concourse ip", "director ip", "domain"})
config := parseConfiguration(readConfiguration())
var keys []string
for k := range *config {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := (*config)[k]
table.Append([]string{k, v.LandscapeType, v.Description, v.Owner, v.JumpboxIP, v.ConcourseIP, v.DirectorIP, v.Domain})
}
table.Render() // Send output
return nil
}
func init() {
}
func parseConfiguration(jsonValue string) *map[string]landscapeStruct {
var cfg map[string]landscapeStruct
err := json.Unmarshal([]byte(jsonValue), &cfg)
if err != nil {
log.Printf("json = '%s'", jsonValue)
log.Println("error:", err)
os.Exit(1)
}
return &cfg
}
func parseConfigurationRaw(jsonValue string) *map[string]interface{} {
var cfg map[string]interface{}
err := json.Unmarshal([]byte(jsonValue), &cfg)
if err != nil {
log.Printf("json = '%s'", jsonValue)
log.Println("error:", err)
os.Exit(1)
}
return &cfg
}
func readConfiguration() string {
response, err := http.Get(*url)
if err != nil {
log.Printf("Error: %v for url: %s", err, *url)
os.Exit(1)
}
if response.StatusCode != 200 {
log.Printf("Http status (%v): '%v' for requesting url: %s", response.StatusCode, response.Status, *url)
os.Exit(1)
}
defer response.Body.Close()
content, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Printf("Error while reading content: %s", err)
os.Exit(1)
}
return string(content)
}
func main() {
app.Version("0.1." + REVISION).Author("Stephan Klevenz")
kingpin.CommandLine.HelpFlag.Short('h') // not working
listCommand := &listCommandStruct{}
list.Action(listCommand.run)
jumpCommand := &jumpCommandStruct{}
jump.Action(jumpCommand.run)
loginCommand := &loginCommandStruct{}
login.Action(loginCommand.run)
tunnelCommand := &tunnelCommandStruct{}
tunnel.Action(tunnelCommand.run)
detailsCommand := &detailsCommandStruct{}
details.Action(detailsCommand.run)
_, err := app.Parse(os.Args[1:])
if err != nil {
log.Printf("Error: %v", err)
os.Exit(1)
}
}