-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
117 lines (96 loc) · 2.34 KB
/
app.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
package main
import (
"os"
"os/signal"
"syscall"
"time"
"github.com/kassisol/flip/datasource"
"github.com/kassisol/flip/datasource/driver"
"github.com/kassisol/flip/pkg/docker"
"github.com/kassisol/flip/pkg/ip"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func runApp(cmd *cobra.Command, args []string) {
var i *driver.IP
if cmdDebug {
log.SetLevel(log.DebugLevel)
}
d, err := datasource.NewDriver(cmdDatasource, cmdDatasourceOptions)
if err != nil {
log.Fatal(err)
}
ticker1 := time.NewTicker(time.Second * time.Duration(30))
for _ = range ticker1.C {
if err := d.IsAvailable(); err != nil {
log.Error(err)
}
i, err = d.GetIP()
if err != nil {
log.Error(err)
continue
}
break
}
ticker1.Stop()
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
signal.Notify(ch, syscall.SIGTERM)
ticker2 := time.NewTicker(time.Second * time.Duration(cmdKeepalive))
go func(nic string, ipaddr *driver.IP) {
for _ = range ticker2.C {
d, err := docker.NewDockerClient()
if err != nil {
log.Error(err)
continue
}
if err := d.GetNodeID(); err != nil {
log.Error(err)
continue
}
service, err := d.IsServiceRunning()
if err != nil {
log.Error(err)
continue
}
if err := d.Close(); err != nil {
log.Error(err)
continue
}
if service {
// Check if IP is already set
i := ip.NewIP(nic, ipaddr.Address, ipaddr.Netmask)
if i.IsSet() {
log.Debugf("IP address '%s' is already configured", ipaddr.Address)
continue
}
// Ping IP
if i.Ping() {
log.Debugf("IP address '%s' is reachable and so seems to be already set on another node", ipaddr.Address)
continue
}
// If IP is not set then add it
if err := i.Set(); err != nil {
log.Warning(err)
continue
}
log.Infof("IP address '%s' has been set", ipaddr.Address)
} else {
// Check if IP is already set
i := ip.NewIP(nic, ipaddr.Address, ipaddr.Netmask)
if i.IsSet() {
log.Debugf("IP address '%s' is already configured", ipaddr.Address)
// If IP is set then delete it
if err := i.Unset(); err != nil {
log.Warning(err)
continue
}
log.Infof("IP address '%s' has been unset", ipaddr.Address)
}
}
}
}(cmdInterface, i)
s := <-ch
ticker2.Stop()
log.Infof("Processing signal '%s'", s)
}