forked from cloudfoundry/cf-acceptance-tests
/
linux.go
62 lines (49 loc) · 1.32 KB
/
linux.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
package linux
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os/exec"
"syscall"
"github.com/gorilla/mux"
)
func ReleaseHandler(res http.ResponseWriter, req *http.Request) {
cmd := exec.Command("lsb_release", "--all")
outBytes, _ := cmd.Output()
res.Write(outBytes)
}
func MyIPHandler(res http.ResponseWriter, req *http.Request) {
cmd := exec.Command("bash", "-c", "ip route get 1 | awk '{print $NF;exit}'")
outBytes, _ := cmd.Output()
res.Write(outBytes)
}
func CurlHandler(res http.ResponseWriter, req *http.Request) {
host := mux.Vars(req)["host"]
port := mux.Vars(req)["port"]
if port == "" {
port = "80"
}
cmd := exec.Command("curl", "-m", "3", "-v", "-i", fmt.Sprintf("%s:%s", host, port))
outBuf := bytes.NewBuffer([]byte{})
errBuf := bytes.NewBuffer([]byte{})
cmd.Stdout = outBuf
cmd.Stderr = errBuf
err := cmd.Run()
exitCode := 0
if e, ok := err.(*exec.ExitError); ok {
exitCode = e.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
}
curlOutput := struct {
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
ReturnCode int `json:"return_code"`
}{
Stdout: outBuf.String(),
Stderr: errBuf.String(),
ReturnCode: exitCode,
}
curlOutputJSON, _ := json.Marshal(curlOutput)
res.Header().Add("Content-Type", "application/json")
res.Write(curlOutputJSON)
}