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-05-06 18:29:44 +00:00
|
|
|
"github.com/thegeeklab/wp-plugin-go/v2/file"
|
|
|
|
"github.com/thegeeklab/wp-plugin-go/v2/types"
|
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 {
|
2024-05-06 18:29:44 +00:00
|
|
|
if err := p.Settings.Ansible.GetPlaybooks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-10 10:34:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute provides the implementation of the plugin.
|
|
|
|
func (p *Plugin) Execute() error {
|
2024-05-06 18:29:44 +00:00
|
|
|
var err error
|
2024-05-05 11:03:29 +00:00
|
|
|
|
2024-05-06 18:29:44 +00:00
|
|
|
batchCmd := make([]*types.Cmd, 0)
|
2021-02-10 10:34:04 +00:00
|
|
|
|
2024-05-06 18:29:44 +00:00
|
|
|
batchCmd = append(batchCmd, p.Settings.Ansible.Version())
|
2021-02-10 10:34:04 +00:00
|
|
|
|
2023-12-19 08:19:23 +00:00
|
|
|
if p.Settings.PrivateKey != "" {
|
2024-05-06 18:29:44 +00:00
|
|
|
if p.Settings.Ansible.PrivateKeyFile, err = file.WriteTmpFile("privateKey", p.Settings.PrivateKey); err != nil {
|
2021-02-10 10:34:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-06 18:29:44 +00:00
|
|
|
defer os.Remove(p.Settings.Ansible.PrivateKeyFile)
|
2021-02-10 10:34:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-19 08:19:23 +00:00
|
|
|
if p.Settings.VaultPassword != "" {
|
2024-05-06 18:29:44 +00:00
|
|
|
if p.Settings.Ansible.VaultPasswordFile, err = file.WriteTmpFile("vaultPass", p.Settings.VaultPassword); err != nil {
|
2021-02-10 10:34:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-06 18:29:44 +00:00
|
|
|
defer os.Remove(p.Settings.Ansible.VaultPasswordFile)
|
2021-02-10 10:34:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-22 09:10:39 +00:00
|
|
|
if p.Settings.PythonRequirements != "" {
|
2024-05-06 18:29:44 +00:00
|
|
|
batchCmd = append(batchCmd, PipInstall(p.Settings.PythonRequirements))
|
2021-02-10 10:34:04 +00:00
|
|
|
}
|
|
|
|
|
2024-05-06 18:29:44 +00:00
|
|
|
if p.Settings.Ansible.GalaxyRequirements != "" {
|
|
|
|
batchCmd = append(batchCmd, p.Settings.Ansible.GalaxyInstall())
|
2021-02-10 10:34:04 +00:00
|
|
|
}
|
|
|
|
|
2024-05-06 18:29:44 +00:00
|
|
|
batchCmd = append(batchCmd, p.Settings.Ansible.Play())
|
2021-02-10 10:34:04 +00:00
|
|
|
|
2024-05-06 18:29:44 +00:00
|
|
|
for _, cmd := range batchCmd {
|
2024-05-07 09:57:21 +00:00
|
|
|
if cmd == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-05-06 18:29:44 +00:00
|
|
|
cmd.Env = append(os.Environ(), "ANSIBLE_FORCE_COLOR=1")
|
2021-02-10 10:34:04 +00:00
|
|
|
|
2024-05-06 18:29:44 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
2021-02-10 10:34:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|