-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PRODENG-2744 host override sudo test
- host flag allows explicit sudo assign, preventing rig discovery STILL POC, and requires rig k0sproject/rig#227 Signed-off-by: James Nesbitt <[email protected]>
- Loading branch information
1 parent
1111954
commit 6f1eba7
Showing
8 changed files
with
102 additions
and
9 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
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,76 @@ | ||
package phase | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/alessio/shellescape" | ||
"github.com/mattn/go-shellwords" | ||
|
||
"github.com/Mirantis/mcc/pkg/phase" | ||
"github.com/Mirantis/mcc/pkg/product/mke/api" | ||
) | ||
|
||
// OverrideHostSudo of the host if it has an override in the config. | ||
type OverrideHostSudo struct { | ||
phase.Analytics | ||
phase.HostSelectPhase | ||
|
||
overrideHosts api.Hosts | ||
} | ||
|
||
// Title for the phase. | ||
func (p *OverrideHostSudo) Title() string { | ||
return "Override the host sudo" | ||
} | ||
|
||
// ShouldRun should return true only when there is a host with an overridet. | ||
func (p *OverrideHostSudo) ShouldRun() bool { | ||
for _, h := range p.Hosts { | ||
if h.SudoOverride { | ||
p.overrideHosts = append(p.overrideHosts, h) | ||
} | ||
} | ||
return len(p.overrideHosts) > 1 | ||
} | ||
|
||
// Run the phase. | ||
func (p *OverrideHostSudo) Run() error { | ||
err := p.Hosts.ParallelEach(func(h *api.Host) error { | ||
if h.SudoOverride { | ||
h.SetSudofn(sudoSudo) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to override sudo on hosts: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
// @see https://github.com/k0sproject/rig/blob/release-0.x/connection.go#L253 | ||
func sudoSudo(cmd string) string { | ||
parts, err := shellwords.Parse(cmd) | ||
if err != nil { | ||
return "sudo -- " + cmd | ||
} | ||
|
||
var idx int | ||
for i, p := range parts { | ||
if strings.Contains(p, "=") { | ||
idx = i + 1 | ||
continue | ||
} | ||
break | ||
} | ||
|
||
if idx == 0 { | ||
return "sudo -- " + cmd | ||
} | ||
|
||
for i, p := range parts { | ||
parts[i] = shellescape.Quote(p) | ||
} | ||
|
||
return fmt.Sprintf("sudo %s -- %s", strings.Join(parts[0:idx], " "), strings.Join(parts[idx:], " ")) | ||
} |
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