-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
158 lines (133 loc) · 4.05 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
153
154
155
156
157
158
package main
import (
"code.cloudfoundry.org/filelock"
"code.cloudfoundry.org/lager"
"errors"
"flag"
"fmt"
"github.com/coreos/go-iptables/iptables"
"io/ioutil"
"lib/rules"
"log"
"net"
"net/http"
neturl "net/url"
"os"
"strconv"
"strings"
"sync"
"syscall"
"time"
)
var logger lager.Logger
func main() {
if err := mainWithError(); err != nil {
log.Fatalf("silk-daemon-shutdown: %s", err)
}
}
func mainWithError() error {
repUrl := flag.String("repUrl", "", "path to rep url")
repTimeout := flag.Int("repTimeout", 5, "timeout (seconds) between calls to rep")
silkDaemonUrl := flag.String("silkDaemonUrl", "", "path to silk daemon url")
silkDaemonTimeout := flag.Int("silkDaemonTimeout", 2, "timeout (seconds) between calls to silk daemon")
silkDaemonPidPath := flag.String("silkDaemonPidPath", "", "pid file of silk daemon")
pingServerTimeout := flag.Int("pingServerTimeout", 300, "timeout (seconds) when pinging if server is up")
iptablesLockFile := flag.String("iptablesLockFile", "", "path to iptablesLockFile")
flag.Parse()
logger = lager.NewLogger(fmt.Sprintf("%s.silk-daemon-shutdown", "cfnetworking"))
logger.RegisterSink(lager.NewWriterSink(os.Stdout, lager.DEBUG))
var err error
repMaxAttempts := 40
isRepUp, err := waitForServer("rep", *repUrl, *repTimeout, repMaxAttempts, *pingServerTimeout)
if err != nil {
return err
}
if isRepUp {
logger.Debug(fmt.Sprintf("rep did not exit after %d ping attempts. Continuing", repMaxAttempts))
}
pidFileConents, err := ioutil.ReadFile(*silkDaemonPidPath)
if err != nil {
return err
}
pid, err := strconv.Atoi(strings.Trim(string(pidFileConents), "\n"))
if err != nil {
return err
}
logger.Debug(fmt.Sprintf("sending TERM signal to silk-daemon"))
_ = syscall.Kill(pid, syscall.SIGTERM)
silkDaemonMaxAttempts := 5
silkDaemonIsUp, err := waitForServer("silk daemon", *silkDaemonUrl, *silkDaemonTimeout, silkDaemonMaxAttempts, *pingServerTimeout)
if err != nil {
return err
}
if silkDaemonIsUp {
return errors.New(fmt.Sprintf("Silk Daemon Server did not exit after %d ping attempts", silkDaemonMaxAttempts))
}
ipt, err := iptables.New()
if err != nil {
return err
}
iptLocker := &filelock.Locker{
FileLocker: filelock.NewLocker(*iptablesLockFile),
Mutex: &sync.Mutex{},
}
restorer := &rules.Restorer{}
lockedIPTables := &rules.LockedIPTables{
IPTables: ipt,
Locker: iptLocker,
Restorer: restorer,
}
overlayAccessMarkRule := rules.NewOverlayAccessMarkRule()
exists, err := lockedIPTables.Exists("filter", "OUTPUT", overlayAccessMarkRule)
if err == nil && exists {
return lockedIPTables.Delete("filter", "OUTPUT", overlayAccessMarkRule)
}
return err
}
func waitForServer(serverName string, serverUrl string, pollingTimeInSeconds int, maxAttempts int, pingTimeout int) (isServerUp bool, err error) {
_, err = neturl.ParseRequestURI(serverUrl)
if err != nil {
return true, err
}
currentAttempt := 0
for currentAttempt < maxAttempts {
logger.Debug(fmt.Sprintf("waiting for the %s to exit", serverName))
select {
case <-time.After(time.Duration(pollingTimeInSeconds) * time.Second):
if !checkIfServerUp(serverName, serverUrl) {
return false, nil
}
currentAttempt++
case <-time.After(time.Duration(pingTimeout) * time.Second):
return true, nil
}
}
return true, nil
}
func checkIfServerUp(serverName string, url string) bool {
httpClient := &http.Client{
Transport: &http.Transport{},
Timeout: 5 * time.Second,
}
logger.Debug(fmt.Sprintf("pinging %s", url))
response, err := httpClient.Get(url)
if err != nil {
if netErr, ok := err.(net.Error); ok {
if netErr.Timeout() {
logger.Debug("pinging server timed out. trying again.")
return true
}
if netErr.Temporary() {
logger.Debug("pinging server returned temporary error. trying again.")
return true
}
}
} else {
defer response.Body.Close()
if response.StatusCode >= http.StatusOK && response.StatusCode <= http.StatusPartialContent {
return true
}
}
logger.Debug(fmt.Sprintf("could not ping %s server. Server is down", serverName))
return false
}