0
0
mirror of https://github.com/thegeeklab/wp-ansible.git synced 2024-11-09 17:10:41 +00:00
wp-ansible/plugin/impl.go

77 lines
1.7 KiB
Go
Raw Normal View History

package plugin
import (
2023-12-19 08:19:23 +00:00
"context"
"fmt"
"os"
"github.com/thegeeklab/wp-plugin-go/v2/file"
"github.com/thegeeklab/wp-plugin-go/v2/types"
)
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
}
// Validate handles the settings validation of the plugin.
func (p *Plugin) Validate() error {
if err := p.Settings.Ansible.GetPlaybooks(); err != nil {
return err
}
return nil
}
// Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error {
var err error
batchCmd := make([]*types.Cmd, 0)
batchCmd = append(batchCmd, p.Settings.Ansible.Version())
2023-12-19 08:19:23 +00:00
if p.Settings.PrivateKey != "" {
if p.Settings.Ansible.PrivateKeyFile, err = file.WriteTmpFile("privateKey", p.Settings.PrivateKey); err != nil {
return err
}
defer os.Remove(p.Settings.Ansible.PrivateKeyFile)
}
2023-12-19 08:19:23 +00:00
if p.Settings.VaultPassword != "" {
if p.Settings.Ansible.VaultPasswordFile, err = file.WriteTmpFile("vaultPass", p.Settings.VaultPassword); err != nil {
return err
}
defer os.Remove(p.Settings.Ansible.VaultPasswordFile)
}
2023-12-22 09:10:39 +00:00
if p.Settings.PythonRequirements != "" {
batchCmd = append(batchCmd, PipInstall(p.Settings.PythonRequirements))
}
if p.Settings.Ansible.GalaxyRequirements != "" {
batchCmd = append(batchCmd, p.Settings.Ansible.GalaxyInstall())
}
batchCmd = append(batchCmd, p.Settings.Ansible.Play())
for _, cmd := range batchCmd {
cmd.Env = append(os.Environ(), "ANSIBLE_FORCE_COLOR=1")
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}