-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
94 lines (74 loc) · 1.86 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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"time"
"github.com/LINBIT/golinstor/client"
"github.com/sirupsen/logrus"
)
var (
Version = "unknown"
)
type WaitFunc func(ctx context.Context, lc *client.Client) error
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
log := logrus.WithField("version", Version)
if len(os.Args) < 2 {
log.Fatal("expected at least 1 argument")
}
var err error
var waitCheck WaitFunc
switch os.Args[1] {
case "api-online":
waitCheck, err = waitApiOnline(os.Args[2:]...)
case "satellite-online":
waitCheck, err = waitSatelliteOnline(os.Args[2:]...)
default:
log.Fatalf("unknown argument %s", os.Args[1])
}
if err != nil {
log.WithError(err).Fatal("error parsing arguments")
}
lc, err := client.NewClient(client.Log(log))
if err != nil {
log.WithError(err).Fatal("failed to create LINSTOR client")
}
for {
err := waitCheck(ctx, lc)
if err == nil {
return
}
if ctx.Err() != nil {
log.WithError(err).Fatal("context cancelled")
}
log.WithError(err).Info("not ready")
time.Sleep(10 * time.Second)
}
}
func waitApiOnline(args ...string) (WaitFunc, error) {
if len(args) != 0 {
return nil, fmt.Errorf("expected 0 arguments, got %d", len(args))
}
return func(ctx context.Context, lc *client.Client) error {
_, err := lc.Controller.GetVersion(ctx)
return err
}, nil
}
func waitSatelliteOnline(args ...string) (WaitFunc, error) {
if len(args) != 1 {
return nil, fmt.Errorf("expected exactly 1 argument (satellite-name), got %d", len(args))
}
return func(ctx context.Context, lc *client.Client) error {
node, err := lc.Nodes.Get(ctx, args[0])
if err != nil {
return err
}
if node.ConnectionStatus != "ONLINE" {
return fmt.Errorf("satellite %s is not ONLINE: %s", node.Name, node.ConnectionStatus)
}
return nil
}, nil
}