2021-02-10 10:34:04 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2023-09-21 11:01:35 +00:00
|
|
|
"errors"
|
2021-02-10 10:34:04 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/urfave/cli/v2"
|
2023-02-13 14:26:20 +00:00
|
|
|
"golang.org/x/sys/execabs"
|
2021-02-10 10:34:04 +00:00
|
|
|
)
|
|
|
|
|
2023-02-13 14:26:20 +00:00
|
|
|
const (
|
|
|
|
AnsibleForksDefault = 5
|
|
|
|
|
2022-04-25 13:54:18 +00:00
|
|
|
ansibleFolder = "/etc/ansible"
|
|
|
|
ansibleConfig = "/etc/ansible/ansible.cfg"
|
2023-02-13 14:26:20 +00:00
|
|
|
|
|
|
|
pipBin = "/usr/bin/pip"
|
|
|
|
ansibleBin = "/usr/bin/ansible"
|
|
|
|
ansibleGalaxyBin = "/usr/bin/ansible-galaxy"
|
|
|
|
ansiblePlaybookBin = "/usr/bin/ansible-playbook"
|
|
|
|
|
|
|
|
strictFilePerm = 0o600
|
2022-04-25 13:54:18 +00:00
|
|
|
)
|
2021-02-10 10:34:04 +00:00
|
|
|
|
2023-02-13 14:26:20 +00:00
|
|
|
const ansibleContent = `
|
2021-02-10 10:34:04 +00:00
|
|
|
[defaults]
|
|
|
|
host_key_checking = False
|
|
|
|
`
|
|
|
|
|
2023-09-21 11:01:35 +00:00
|
|
|
var ErrAnsiblePlaybookNotFound = errors.New("playbook not found")
|
|
|
|
|
2021-02-10 10:34:04 +00:00
|
|
|
func (p *Plugin) ansibleConfig() error {
|
|
|
|
if err := os.MkdirAll(ansibleFolder, os.ModePerm); err != nil {
|
2023-09-21 11:01:35 +00:00
|
|
|
return fmt.Errorf("failed to create ansible directory: %w", err)
|
2021-02-10 10:34:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 14:26:20 +00:00
|
|
|
if err := os.WriteFile(ansibleConfig, []byte(ansibleContent), strictFilePerm); err != nil {
|
2023-09-21 11:01:35 +00:00
|
|
|
return fmt.Errorf("failed to create ansible config: %w", err)
|
2021-02-10 10:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) privateKey() error {
|
2022-08-22 09:05:35 +00:00
|
|
|
tmpfile, err := os.CreateTemp("", "privateKey")
|
2021-02-10 10:34:04 +00:00
|
|
|
if err != nil {
|
2023-09-21 11:01:35 +00:00
|
|
|
return fmt.Errorf("failed to create private key file: %w", err)
|
2021-02-10 10:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := tmpfile.Write([]byte(p.settings.PrivateKey)); err != nil {
|
2023-09-21 11:01:35 +00:00
|
|
|
return fmt.Errorf("failed to write private key file: %w", err)
|
2021-02-10 10:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpfile.Close(); err != nil {
|
2023-09-21 11:01:35 +00:00
|
|
|
return fmt.Errorf("failed to close private key file: %w", err)
|
2021-02-10 10:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.settings.PrivateKeyFile = tmpfile.Name()
|
2023-02-13 14:26:20 +00:00
|
|
|
|
2021-02-10 10:34:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) vaultPass() error {
|
2022-08-22 09:05:35 +00:00
|
|
|
tmpfile, err := os.CreateTemp("", "vaultPass")
|
2021-02-10 10:34:04 +00:00
|
|
|
if err != nil {
|
2023-09-21 11:01:35 +00:00
|
|
|
return fmt.Errorf("failed to create vault password file: %w", err)
|
2021-02-10 10:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := tmpfile.Write([]byte(p.settings.VaultPassword)); err != nil {
|
2023-09-21 11:01:35 +00:00
|
|
|
return fmt.Errorf("failed to write vault password file: %w", err)
|
2021-02-10 10:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpfile.Close(); err != nil {
|
2023-09-21 11:01:35 +00:00
|
|
|
return fmt.Errorf("failed to close vault password file: %w", err)
|
2021-02-10 10:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.settings.VaultPasswordFile = tmpfile.Name()
|
2023-02-13 14:26:20 +00:00
|
|
|
|
2021-02-10 10:34:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) playbooks() error {
|
2022-04-25 13:54:18 +00:00
|
|
|
var playbooks []string
|
2021-02-10 10:34:04 +00:00
|
|
|
|
|
|
|
for _, p := range p.settings.Playbooks.Value() {
|
|
|
|
files, err := filepath.Glob(p)
|
|
|
|
if err != nil {
|
|
|
|
playbooks = append(playbooks, p)
|
2023-02-13 14:26:20 +00:00
|
|
|
|
2021-02-10 10:34:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
playbooks = append(playbooks, files...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(playbooks) == 0 {
|
2023-09-21 11:01:35 +00:00
|
|
|
return ErrAnsiblePlaybookNotFound
|
2021-02-10 10:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.settings.Playbooks = *cli.NewStringSlice(playbooks...)
|
2023-02-13 14:26:20 +00:00
|
|
|
|
2021-02-10 10:34:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:26:20 +00:00
|
|
|
func (p *Plugin) versionCommand() *execabs.Cmd {
|
2021-02-10 10:34:04 +00:00
|
|
|
args := []string{
|
|
|
|
"--version",
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:26:20 +00:00
|
|
|
return execabs.Command(
|
|
|
|
ansibleBin,
|
2021-02-10 10:34:04 +00:00
|
|
|
args...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:26:20 +00:00
|
|
|
func (p *Plugin) requirementsCommand() *execabs.Cmd {
|
2021-02-10 10:34:04 +00:00
|
|
|
args := []string{
|
|
|
|
"install",
|
|
|
|
"--upgrade",
|
|
|
|
"--requirement",
|
|
|
|
p.settings.Requirements,
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:26:20 +00:00
|
|
|
return execabs.Command(
|
|
|
|
pipBin,
|
2021-02-10 10:34:04 +00:00
|
|
|
args...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:26:20 +00:00
|
|
|
func (p *Plugin) galaxyCommand() *execabs.Cmd {
|
2021-02-10 10:34:04 +00:00
|
|
|
args := []string{
|
|
|
|
"install",
|
|
|
|
"--force",
|
|
|
|
"--role-file",
|
|
|
|
p.settings.Galaxy,
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.Verbose > 0 {
|
|
|
|
args = append(args, fmt.Sprintf("-%s", strings.Repeat("v", p.settings.Verbose)))
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:26:20 +00:00
|
|
|
return execabs.Command(
|
|
|
|
ansibleGalaxyBin,
|
2021-02-10 10:34:04 +00:00
|
|
|
args...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:26:20 +00:00
|
|
|
func (p *Plugin) ansibleCommand(inventory string) *execabs.Cmd {
|
2021-02-10 10:34:04 +00:00
|
|
|
args := []string{
|
|
|
|
"--inventory",
|
|
|
|
inventory,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.settings.ModulePath.Value()) > 0 {
|
|
|
|
args = append(args, "--module-path", strings.Join(p.settings.ModulePath.Value(), ":"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.VaultID != "" {
|
|
|
|
args = append(args, "--vault-id", p.settings.VaultID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.VaultPasswordFile != "" {
|
|
|
|
args = append(args, "--vault-password-file", p.settings.VaultPasswordFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range p.settings.ExtraVars.Value() {
|
|
|
|
args = append(args, "--extra-vars", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.ListHosts {
|
|
|
|
args = append(args, "--list-hosts")
|
|
|
|
args = append(args, p.settings.Playbooks.Value()...)
|
|
|
|
|
2023-02-13 14:26:20 +00:00
|
|
|
return execabs.Command(
|
|
|
|
ansiblePlaybookBin,
|
2021-02-10 10:34:04 +00:00
|
|
|
args...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.SyntaxCheck {
|
|
|
|
args = append(args, "--syntax-check")
|
|
|
|
args = append(args, p.settings.Playbooks.Value()...)
|
|
|
|
|
2023-02-13 14:26:20 +00:00
|
|
|
return execabs.Command(
|
|
|
|
ansiblePlaybookBin,
|
2021-02-10 10:34:04 +00:00
|
|
|
args...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.Check {
|
|
|
|
args = append(args, "--check")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.Diff {
|
|
|
|
args = append(args, "--diff")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.FlushCache {
|
|
|
|
args = append(args, "--flush-cache")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.ForceHandlers {
|
|
|
|
args = append(args, "--force-handlers")
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:26:20 +00:00
|
|
|
if p.settings.Forks != AnsibleForksDefault {
|
2021-02-10 10:34:04 +00:00
|
|
|
args = append(args, "--forks", strconv.Itoa(p.settings.Forks))
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.Limit != "" {
|
|
|
|
args = append(args, "--limit", p.settings.Limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.ListTags {
|
|
|
|
args = append(args, "--list-tags")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.ListTasks {
|
|
|
|
args = append(args, "--list-tasks")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.SkipTags != "" {
|
|
|
|
args = append(args, "--skip-tags", p.settings.SkipTags)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.StartAtTask != "" {
|
|
|
|
args = append(args, "--start-at-task", p.settings.StartAtTask)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.Tags != "" {
|
|
|
|
args = append(args, "--tags", p.settings.Tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.PrivateKeyFile != "" {
|
|
|
|
args = append(args, "--private-key", p.settings.PrivateKeyFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.User != "" {
|
|
|
|
args = append(args, "--user", p.settings.User)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.Connection != "" {
|
|
|
|
args = append(args, "--connection", p.settings.Connection)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.Timeout != 0 {
|
|
|
|
args = append(args, "--timeout", strconv.Itoa(p.settings.Timeout))
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.SSHCommonArgs != "" {
|
|
|
|
args = append(args, "--ssh-common-args", p.settings.SSHCommonArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.SFTPExtraArgs != "" {
|
|
|
|
args = append(args, "--sftp-extra-args", p.settings.SFTPExtraArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.SCPExtraArgs != "" {
|
|
|
|
args = append(args, "--scp-extra-args", p.settings.SCPExtraArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.SSHExtraArgs != "" {
|
|
|
|
args = append(args, "--ssh-extra-args", p.settings.SSHExtraArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.Become {
|
|
|
|
args = append(args, "--become")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.BecomeMethod != "" {
|
|
|
|
args = append(args, "--become-method", p.settings.BecomeMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.BecomeUser != "" {
|
|
|
|
args = append(args, "--become-user", p.settings.BecomeUser)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.Verbose > 0 {
|
|
|
|
args = append(args, fmt.Sprintf("-%s", strings.Repeat("v", p.settings.Verbose)))
|
|
|
|
}
|
|
|
|
|
|
|
|
args = append(args, p.settings.Playbooks.Value()...)
|
|
|
|
|
2023-02-13 14:26:20 +00:00
|
|
|
return execabs.Command(
|
|
|
|
ansiblePlaybookBin,
|
2021-02-10 10:34:04 +00:00
|
|
|
args...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:26:20 +00:00
|
|
|
func trace(cmd *execabs.Cmd) {
|
2021-02-10 10:34:04 +00:00
|
|
|
fmt.Println("$", strings.Join(cmd.Args, " "))
|
|
|
|
}
|