-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
152 lines (129 loc) · 3.94 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"regexp"
"time"
)
var appVersionFile = "./VERSION"
func readVersion(appVersionFile string) string {
dat, _ := ioutil.ReadFile(appVersionFile)
return string(dat)
}
// PathDetector detects if binaries are in path
type PathDetector interface {
inPath(command string) bool
}
type localPathDetector struct {
}
func (pathDetector localPathDetector) inPath(command string) bool {
_, err := exec.LookPath(command)
if err != nil {
return false
}
return true
}
type logWriter struct {
}
func (writer logWriter) Write(bytes []byte) (int, error) {
return fmt.Print(string(bytes))
}
type arrayFlags []string
func (i *arrayFlags) String() string {
return "String"
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
func waitFor(waitsFlags arrayFlags, commandFlags arrayFlags, timeoutFlag int, intervalFlag int, shell string) {
for _, wait := range waitsFlags {
processWait(wait, timeoutFlag, intervalFlag, shell)
}
for _, command := range commandFlags {
processCommandExec(command, timeoutFlag, intervalFlag, shell)
}
}
func processWait(wait string, timeoutFlag int, intervalFlag int, shell string) {
pattern, _ := regexp.Compile("(.*):(.*)")
matches := pattern.FindAllStringSubmatch(wait, -1)
if len(matches) > 0 {
dbHost := matches[0][1]
dbPort := matches[0][2]
for {
conn, err := net.DialTimeout("tcp", net.JoinHostPort(dbHost, dbPort), time.Duration(intervalFlag)*time.Second)
if err != nil {
log.Println(err)
log.Printf("Sleeping %d seconds waiting for host\n", intervalFlag)
time.Sleep(time.Duration(intervalFlag) * time.Second)
}
if conn != nil {
conn.Close()
break
}
}
} else {
for {
out, err := exec.Command(shell, "-c", wait).Output()
if err != nil {
log.Printf("Sleeping %d seconds waiting for command - %s - to return\n", intervalFlag, wait)
time.Sleep(time.Duration(intervalFlag) * time.Second)
} else {
log.Println(string(out))
break
}
}
}
}
func processCommandExec(command string, timeoutFlag int, intervalFlag int, shell string) {
out, err := exec.Command(shell, "-c", command).Output()
if err != nil {
log.Printf("Sleeping %d seconds waiting for command - %s - to return\n", intervalFlag, command)
time.Sleep(time.Duration(intervalFlag) * time.Second)
} else {
log.Println(string(out))
}
}
func chooseShell(pathDetector PathDetector) string {
if pathDetector.inPath("bash") {
return "bash"
} else if pathDetector.inPath("sh") {
return "sh"
} else {
panic("Neither bash or sh present on system")
}
}
func mainExecution(waitsFlags arrayFlags, commandFlags arrayFlags, timeoutFlag int, intervalFlag int, version bool, pathDetector localPathDetector) int {
if version {
log.Println(readVersion(appVersionFile))
return 0
}
if len(waitsFlags) == 0 || len(commandFlags) == 0 {
log.Println("You must specify at least a wait and a command. Please see --help for more information.")
return 1
}
shell := chooseShell(pathDetector)
waitFor(waitsFlags, commandFlags, timeoutFlag, intervalFlag, shell)
return 2
}
func main() {
var pathDetector = localPathDetector{}
// Set custom logger
log.SetFlags(0)
log.SetOutput(new(logWriter))
var waitsFlags arrayFlags
var commandFlags arrayFlags
flag.Var(&waitsFlags, "wait", "You can specify the HOST and TCP PORT using the format HOST:PORT, or you can specify a command that should return an output. Multiple wait flags can be added.")
flag.Var(&commandFlags, "command", "Command that should be run when all waits are accessible. Multiple commands can be added.")
timeoutFlag := flag.Int("timeout", 600, "Timeout untill script is killed.")
intervalFlag := flag.Int("interval", 15, "Interval between calls")
version := flag.Bool("version", false, "Prints current version")
flag.Parse()
returnValue := mainExecution(waitsFlags, commandFlags, *timeoutFlag, *intervalFlag, *version, pathDetector)
os.Exit(returnValue)
}