-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (88 loc) · 2.68 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
package main
import (
"context"
"flag"
"fmt"
"github.com/anjmao/vcheck/client"
"log"
"os"
"time"
)
var (
target = flag.String("target", "", "Version check endpoint target")
expectedBuildVersion = flag.String("expect", "", "Expected version")
method = flag.String("method", "/version.Version/GetVersion", "Version check endpoint path")
clientType = flag.String("client", "grpc", "Version check client type")
checkCount = flag.Int("count", 12, "Check count")
sleepAfterCheck = flag.Int("sleep", 5, "Sleep after check in seconds")
printHelp = flag.Bool("help", false, "Print help")
printVersion = flag.Bool("version", false, "Print vcheck version")
)
var Version = "dev"
const checkTimeout = 10 * time.Second
const usageStr = `
Usage: vcheck [options]
Options:
--target <target> Target host including port. (e.g --target service.mydomain.com:443)
--expect <version> Expected version (e.g -v 1.2.3)
--method <method> Version check endpoint (default: /debug.Debug/GetVersion)
--client <client> Client type (grpc, http)
--count <count> Check count (default: 12)
--sleep <sleep> Sleep duration after check in seconds (default: 5)
Other options:
--help Print help
--version Print vcheck util version
`
func usage() {
fmt.Printf("%s\n", usageStr)
os.Exit(0)
}
func main() {
flag.Parse()
if *printVersion {
fmt.Println(Version)
os.Exit(0)
}
if *printHelp || *target == "" || *expectedBuildVersion == "" {
usage()
}
sleep := time.Duration(*sleepAfterCheck) * time.Second
var actualVersion string
c := getClient(*clientType, *target, *method)
for i := 0; i < *checkCount; i++ {
v, err := getVersion(c)
if err != nil {
fmt.Printf("could not get version: %v\n", err)
time.Sleep(sleep)
continue
}
actualVersion = v.BuildVersion
fmt.Printf("expected version %s, got %s\n", *expectedBuildVersion, actualVersion)
if actualVersion == *expectedBuildVersion {
fmt.Println("deployment successful")
return
}
time.Sleep(sleep)
}
log.Fatalf("deployment failed: expected version %s, got %s\n", *expectedBuildVersion, actualVersion)
}
func getVersion(c client.Client) (*client.GetVersionReply, error) {
ctx, cancel := context.WithTimeout(context.Background(), checkTimeout)
defer cancel()
v, err := c.GetVersion(ctx)
if err != nil {
return nil, err
}
return v, nil
}
func getClient(clientType, target, method string) client.Client {
switch clientType {
case "http":
return client.NewHTTP(target, method)
case "grpc":
return client.NewGRPC(target, method)
default:
log.Fatalf("unknow client type: %s", clientType)
}
return nil
}