Skip to content

Commit ce6ecc6

Browse files
better error handling on server startup
- detect if stale corectld.store or corectld.nameserver somehow were left running from previous sessions and if found kill them and continue initialization instead of just popping an error and exiting - exit with a much more conscise error message if corectld.store or corectld.nameserver ports are found binded to some foreign app Signed-off-by: António Meireles <antonio.meireles@reformi.st>
1 parent 836a2f4 commit ce6ecc6

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

components/server/server.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os/exec"
2323
"os/signal"
2424
"path"
25+
"strings"
2526
"sync"
2627
"syscall"
2728
"time"
@@ -35,6 +36,7 @@ import (
3536
"github.com/braintree/manners"
3637
"github.com/coreos/etcd/client"
3738
"github.com/helm/helm/log"
39+
"github.com/keybase/go-ps"
3840
)
3941

4042
type (
@@ -103,9 +105,12 @@ func Start() (err error) {
103105
}
104106
Daemon.DataStore = client.NewKeysAPI(etcdc)
105107

108+
if err = killIfRunning("corectld.store"); err != nil {
109+
return
110+
}
106111
if isPortOpen("tcp", "127.0.0.1:2379") {
107-
return fmt.Errorf("Unable to start embedded etcd, as another one " +
108-
"seems to be already running")
112+
return fmt.Errorf("Unable to start embedded etcd (corectld.store) " +
113+
"as something else seems to be already binding port :2379")
109114
}
110115
log.Info("starting embedded etcd")
111116
if err = etcd.Start(); err != nil {
@@ -140,6 +145,14 @@ func Start() (err error) {
140145
Join(session.ExecutableFolder(), "corectld.nameserver"),
141146
dnsArgs...,
142147
)
148+
if err = killIfRunning("corectld.nameserver"); err != nil {
149+
return
150+
}
151+
if isPortOpen("tcp", "127.0.0.1:53") {
152+
return fmt.Errorf("Unable to start embedded skydns " +
153+
"(corectld.nameserver) as something else seems to be already " +
154+
"binding port :53")
155+
}
143156
log.Info("starting embedded name server")
144157
if err = skydns.Start(); err != nil {
145158
return
@@ -238,3 +251,23 @@ func Start() (err error) {
238251
log.Info("gone!")
239252
return
240253
}
254+
255+
func killIfRunning(blob string) (err error) {
256+
var p *os.Process
257+
all, _ := ps.Processes()
258+
for _, r := range all {
259+
if strings.HasSuffix(r.Executable(), blob) {
260+
if p, err = os.FindProcess(r.Pid()); err == nil {
261+
log.Warn("A stalled copy of '%v' was "+
262+
"found running. Killing it.", blob)
263+
if err = p.Kill(); err != nil {
264+
return
265+
}
266+
// so that binded port(s) gets actually freed
267+
// XXX find a more idiomatic way
268+
time.Sleep(150 * time.Millisecond)
269+
}
270+
}
271+
}
272+
return
273+
}

0 commit comments

Comments
 (0)