-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jussi Nummelin <jnummelin@mirantis.com>
- Loading branch information
Showing
7 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package cluster | ||
|
||
// Hooks define a list of hooks such as hooks["apply"]["before"] = ["ls -al", "rm foo.txt"] | ||
type Hooks map[string]map[string][]string | ||
|
||
// ForActionAndStage return hooks for given action and stage | ||
func (h Hooks) ForActionAndStage(action, stage string) []string { | ||
if len(h[action]) > 0 { | ||
return h[action][stage] | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package phase | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/k0sproject/k0sctl/config" | ||
"github.com/k0sproject/k0sctl/config/cluster" | ||
) | ||
|
||
var _ phase = &RunHooks{} | ||
|
||
// RunHooks phase runs a set of hooks configured for the host | ||
type RunHooks struct { | ||
Action string | ||
Stage string | ||
|
||
steps map[*cluster.Host][]string | ||
} | ||
|
||
// Title for the phase | ||
func (p *RunHooks) Title() string { | ||
return fmt.Sprintf("Run %s %s Hooks", strings.Title(p.Stage), strings.Title(p.Action)) | ||
} | ||
|
||
// Prepare digs out the hosts with steps from the config | ||
func (p *RunHooks) Prepare(config *config.Cluster) error { | ||
p.steps = make(map[*cluster.Host][]string) | ||
for _, h := range config.Spec.Hosts { | ||
if len(h.Hooks) > 0 { | ||
p.steps[h] = h.Hooks.ForActionAndStage(p.Action, p.Stage) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ShouldRun is true when there are hosts that need to be connected | ||
func (p *RunHooks) ShouldRun() bool { | ||
return len(p.steps) > 0 | ||
} | ||
|
||
// Run does all the prep work on the hosts in parallel | ||
func (p *RunHooks) Run() error { | ||
var wg sync.WaitGroup | ||
var errors []string | ||
type erritem struct { | ||
host string | ||
err error | ||
} | ||
ec := make(chan erritem, 1) | ||
|
||
wg.Add(len(p.steps)) | ||
|
||
for h, steps := range p.steps { | ||
go func(h *cluster.Host, steps []string) { | ||
for _, s := range steps { | ||
err := h.Exec(s) | ||
if err != nil { | ||
ec <- erritem{h.String(), err} | ||
return // do not exec remaining steps if one fails | ||
} | ||
} | ||
|
||
ec <- erritem{h.String(), nil} | ||
}(h, steps) | ||
} | ||
|
||
go func() { | ||
for e := range ec { | ||
if e.err != nil { | ||
errors = append(errors, fmt.Sprintf("%s: %s", e.host, e.err.Error())) | ||
} | ||
wg.Done() | ||
} | ||
}() | ||
|
||
wg.Wait() | ||
|
||
if len(errors) > 0 { | ||
return fmt.Errorf("failed on %d hosts:\n - %s", len(errors), strings.Join(errors, "\n - ")) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters