2021-02-10 10:34:04 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2023-12-19 08:19:23 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-02-10 10:34:04 +00:00
|
|
|
"os"
|
|
|
|
|
2024-01-03 21:35:23 +00:00
|
|
|
"github.com/thegeeklab/wp-plugin-go/trace"
|
2023-02-13 14:26:20 +00:00
|
|
|
"golang.org/x/sys/execabs"
|
2021-02-10 10:34:04 +00:00
|
|
|
)
|
|
|
|
|
2024-05-04 12:35:41 +00:00
|
|
|
func (p *Plugin) run(_ context.Context) error {
|
2023-12-19 08:19:23 +00:00
|
|
|
if err := p.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("validation failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.Execute(); err != nil {
|
|
|
|
return fmt.Errorf("execution failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 10:34:04 +00:00
|
|
|
// Validate handles the settings validation of the plugin.
|
|
|
|
func (p *Plugin) Validate() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute provides the implementation of the plugin.
|
|
|
|
func (p *Plugin) Execute() error {
|
|
|
|
if err := p.playbooks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.ansibleConfig(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-19 08:19:23 +00:00
|
|
|
if p.Settings.PrivateKey != "" {
|
2021-02-10 10:34:04 +00:00
|
|
|
if err := p.privateKey(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-19 08:19:23 +00:00
|
|
|
defer os.Remove(p.Settings.PrivateKeyFile)
|
2021-02-10 10:34:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-19 08:19:23 +00:00
|
|
|
if p.Settings.VaultPassword != "" {
|
2021-02-10 10:34:04 +00:00
|
|
|
if err := p.vaultPass(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-19 08:19:23 +00:00
|
|
|
defer os.Remove(p.Settings.VaultPasswordFile)
|
2021-02-10 10:34:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 14:26:20 +00:00
|
|
|
commands := []*execabs.Cmd{
|
2021-02-10 10:34:04 +00:00
|
|
|
p.versionCommand(),
|
|
|
|
}
|
|
|
|
|
2023-12-22 09:10:39 +00:00
|
|
|
if p.Settings.PythonRequirements != "" {
|
|
|
|
commands = append(commands, p.pythonRequirementsCommand())
|
2021-02-10 10:34:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-22 09:10:39 +00:00
|
|
|
if p.Settings.GalaxyRequirements != "" {
|
|
|
|
commands = append(commands, p.galaxyRequirementsCommand())
|
2021-02-10 10:34:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-19 08:19:23 +00:00
|
|
|
for _, inventory := range p.Settings.Inventories.Value() {
|
2021-02-10 10:34:04 +00:00
|
|
|
commands = append(commands, p.ansibleCommand(inventory))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cmd := range commands {
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
cmd.Env = os.Environ()
|
|
|
|
cmd.Env = append(cmd.Env, "ANSIBLE_FORCE_COLOR=1")
|
|
|
|
|
2024-01-03 21:35:23 +00:00
|
|
|
trace.Cmd(cmd)
|
2021-02-10 10:34:04 +00:00
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|