Skip to content

Commit

Permalink
fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Implement boo…
Browse files Browse the repository at this point in the history
…tstrapper spec(#4096)
  • Loading branch information
Yevhenii Voievodin committed Jun 20, 2017
1 parent c2869d2 commit 95c726f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 33 deletions.
3 changes: 0 additions & 3 deletions agents/go-agents/bootstrapper/booter/booter.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ func installOne(installer Installer) error {
} else {
inst = &scriptInst{installer, installerTimeout}
}
if err := inst.preCheck(); err != nil {
return err
}
return inst.execute()
}

Expand Down
52 changes: 22 additions & 30 deletions agents/go-agents/bootstrapper/booter/installations.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ import (
// Process of getting certain installer software/servers installed.
type installation interface {

// Check installation preconditions e.g. ports are free.
// If error is returned installation is not executed.
preCheck() error

// Executes the installation in implementation specific way.
execute() error
}
Expand All @@ -38,8 +34,6 @@ type scriptInst struct {
timeout time.Duration
}

func (sci *scriptInst) preCheck() error { return nil }

func (sci *scriptInst) execute() error {
_, diedC, err := executeScript(sci.installer)
if err != nil {
Expand Down Expand Up @@ -68,31 +62,28 @@ type serverInst struct {
timeout time.Duration
}

func (svi *serverInst) preCheck() error {
for _, server := range svi.installer.Servers {
if tryConn(server) == nil {
return fmt.Errorf("server address 'localhost:%s' already in use", server.Port)
}
}
return nil
}

func (svi *serverInst) execute() error {
pid, diedC, err := executeScript(svi.installer)
if err != nil {
fmt.Errorf("Scrip execution failed Error: %s", err)
}
checker := &dialChecker{svi.period, make(chan bool, 1)}
select {
case <-checker.allAvailable(svi.installer.Servers):
case <-checker.availableC(svi.installer.Servers):
process.RemoveSubscriber(pid, svi.installer.ID)
return nil
case <-time.After(svi.timeout):
checker.stop()
return fmt.Errorf("Timeout reached before installation of '%s' finished", svi.installer.ID)
case <-diedC:
case died := <-diedC:
checker.stop()
return fmt.Errorf("Process of installation '%s' exited before server became available", svi.installer.ID)
if died.ExitCode != 0 {
return fmt.Errorf("Installation '%s' failed, script exit code is %d", svi.installer.ID, died.ExitCode)
}
if !checker.available(svi.installer.Servers) {
fmt.Errorf("Process of installation '%s' exit code is 0 but servers are not available", svi.installer.ID)
}
return nil
}
}

Expand All @@ -116,30 +107,23 @@ func executeScript(installer Installer) (uint64, chan *process.DiedEvent, error)
return p.Pid, diedC, nil
}

// dialChecker performs a servers availability check
// dialChecker performs a servers availability available
type dialChecker struct {
// period defines period between checks
period time.Duration
// stopped channel for interrupting servers checks
stopped chan bool
}

func (cc *dialChecker) allAvailable(servers map[string]Server) chan bool {
func (cc *dialChecker) availableC(servers map[string]Server) chan bool {
ticker := time.NewTicker(cc.period)
state := make(chan bool, 1)
go func() {
for {
ok := true
select {
case <-ticker.C:
for _, server := range servers {
if tryConn(server) != nil {
ok = false
break
}
}
if ok {
state <- ok
if cc.available(servers) {
state <- true
ticker.Stop()
return
}
Expand All @@ -153,7 +137,15 @@ func (cc *dialChecker) allAvailable(servers map[string]Server) chan bool {
return state
}

// stop stops server health checking
func (cc *dialChecker) available(servers map[string]Server) bool {
for _, server := range servers {
if tryConn(server) != nil {
return false
}
}
return true
}

func (cc *dialChecker) stop() {
close(cc.stopped)
}
Expand Down

0 comments on commit 95c726f

Please sign in to comment.