-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
108 lines (92 loc) · 2.64 KB
/
common.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
package prometheus_grafana
import (
"fmt"
"os"
"os/exec"
"strings"
"net/http"
"io/ioutil"
)
/////////////////////////////////////////Check cmd output///////////
func printCommand(cmd *exec.Cmd) {
fmt.Printf("==> Executing: %s\n", strings.Join(cmd.Args, " "))
}
func printError(err error) {
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("==> Error: %s\n", err.Error()))
}
}
func printOutput(outs []byte) {
if len(outs) > 0 {
fmt.Printf("==> Output: %s\n", string(outs))
}
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////Commend line////////////////
func connectToClusterCmd() {
//Connect to the Cluster
cmd := exec.Command("bash", "-c", "ACCOUNT=$(gcloud info --format='value(config.account)')")
printCommand(cmd)
output, err := cmd.CombinedOutput()
printError(err)
printOutput(output)
setKubectlAcountCmd := exec.Command("kubectl", "create", "clusterrolebinding", "owner-cluster-admin-binding",
"--clusterrole", "cluster-admin", "--user", "$ACCOUNT")
printCommand(setKubectlAcountCmd)
output, setKubectlAcountErr := setKubectlAcountCmd.CombinedOutput()
printError(setKubectlAcountErr)
printOutput(output)
}
func createNamespaceCmd(namespace_name string) {
cmd := exec.Command("kubectl", "create", "namespace", namespace_name)
printCommand(cmd)
output, err := cmd.CombinedOutput()
printError(err)
printOutput(output)
}
func applyYamlFileCmd(fileName string, option string, namespace_name string) {
var cmd *exec.Cmd
if strings.Compare(option, "--namespace=") == 0 {
cmd = exec.Command("kubectl", "apply", "-f", fileName, option+namespace_name)
} else {
cmd = exec.Command("kubectl", "apply", "-f", fileName)
}
printCommand(cmd)
output, err := cmd.CombinedOutput()
printError(err)
printOutput(output)
}
///////////////////////////////////////////////////////////////////////
//
// /////delete yaml file
// func deleteFile(fileName string) {
// // delete file
// var err = os.Remove(fileName)
// if isError(err) { return }
//
// fmt.Println("==> done deleting file")
// }
//
// func isError(err error) bool {
// if err != nil {
// fmt.Println(err.Error())
// }
//
// return (err != nil)
// }
////////////Read yaml
func getYaml(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", fmt.Errorf("GET error: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("Status error: %v", resp.StatusCode)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("Read body: %v", err)
}
return string(data), nil
}