mirror of
https://github.com/thegeeklab/wp-ansible.git
synced 2024-06-02 08:19:40 +02:00
move ansible commands to own package
This commit is contained in:
parent
9a1c89a71a
commit
a8ec6f2d6b
258
ansible/ansible.go
Normal file
258
ansible/ansible.go
Normal file
|
@ -0,0 +1,258 @@
|
|||
package ansible
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/thegeeklab/wp-plugin-go/v2/types"
|
||||
"github.com/urfave/cli/v2"
|
||||
"golang.org/x/sys/execabs"
|
||||
)
|
||||
|
||||
const (
|
||||
AnsibleForksDefault = 5
|
||||
|
||||
ansibleBin = "/usr/local/bin/ansible"
|
||||
ansibleGalaxyBin = "/usr/local/bin/ansible-galaxy"
|
||||
ansiblePlaybookBin = "/usr/local/bin/ansible-playbook"
|
||||
)
|
||||
|
||||
var ErrAnsiblePlaybookNotFound = errors.New("no playbook found")
|
||||
|
||||
type Ansible struct {
|
||||
GalaxyRequirements string
|
||||
Inventories cli.StringSlice
|
||||
Playbooks cli.StringSlice
|
||||
Limit string
|
||||
SkipTags string
|
||||
StartAtTask string
|
||||
Tags string
|
||||
ExtraVars cli.StringSlice
|
||||
ModulePath cli.StringSlice
|
||||
Check bool
|
||||
Diff bool
|
||||
FlushCache bool
|
||||
ForceHandlers bool
|
||||
ListHosts bool
|
||||
ListTags bool
|
||||
ListTasks bool
|
||||
SyntaxCheck bool
|
||||
Forks int
|
||||
VaultID string
|
||||
VaultPasswordFile string
|
||||
Verbose int
|
||||
PrivateKeyFile string
|
||||
User string
|
||||
Connection string
|
||||
Timeout int
|
||||
SSHCommonArgs string
|
||||
SFTPExtraArgs string
|
||||
SCPExtraArgs string
|
||||
SSHExtraArgs string
|
||||
Become bool
|
||||
BecomeMethod string
|
||||
BecomeUser string
|
||||
}
|
||||
|
||||
// Version runs the Ansible binary with the --version flag to retrieve the current version.
|
||||
func (a *Ansible) Version() *types.Cmd {
|
||||
args := []string{
|
||||
"--version",
|
||||
}
|
||||
|
||||
return &types.Cmd{
|
||||
Cmd: execabs.Command(ansibleBin, args...),
|
||||
}
|
||||
}
|
||||
|
||||
// GetPlaybooks retrieves the list of Ansible playbook files based on the configured playbook patterns.
|
||||
func (a *Ansible) GetPlaybooks() error {
|
||||
var playbooks []string
|
||||
|
||||
for _, pb := range a.Playbooks.Value() {
|
||||
files, err := filepath.Glob(pb)
|
||||
if err != nil {
|
||||
playbooks = append(playbooks, pb)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
playbooks = append(playbooks, files...)
|
||||
}
|
||||
|
||||
if len(playbooks) == 0 {
|
||||
log.Debug().Strs("patterns", a.Playbooks.Value()).Msg("no playbooks found")
|
||||
|
||||
return ErrAnsiblePlaybookNotFound
|
||||
}
|
||||
|
||||
a.Playbooks = *cli.NewStringSlice(playbooks...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GalaxyInstall runs the ansible-galaxy install command with the configured options.
|
||||
func (a *Ansible) GalaxyInstall() *types.Cmd {
|
||||
args := []string{
|
||||
"install",
|
||||
"--force",
|
||||
"--role-file",
|
||||
a.GalaxyRequirements,
|
||||
}
|
||||
|
||||
if a.Verbose > 0 {
|
||||
args = append(args, fmt.Sprintf("-%s", strings.Repeat("v", a.Verbose)))
|
||||
}
|
||||
|
||||
return &types.Cmd{
|
||||
Cmd: execabs.Command(ansibleGalaxyBin, args...),
|
||||
}
|
||||
}
|
||||
|
||||
// Play runs the Ansible playbook with the configured options.
|
||||
//
|
||||
//nolint:gocyclo
|
||||
func (a *Ansible) Play() *types.Cmd {
|
||||
args := make([]string, 0)
|
||||
|
||||
for _, inventory := range a.Inventories.Value() {
|
||||
args = append(args, "--inventory", inventory)
|
||||
}
|
||||
|
||||
if len(a.ModulePath.Value()) > 0 {
|
||||
args = append(args, "--module-path", strings.Join(a.ModulePath.Value(), ":"))
|
||||
}
|
||||
|
||||
if a.VaultID != "" {
|
||||
args = append(args, "--vault-id", a.VaultID)
|
||||
}
|
||||
|
||||
if a.VaultPasswordFile != "" {
|
||||
args = append(args, "--vault-password-file", a.VaultPasswordFile)
|
||||
}
|
||||
|
||||
for _, v := range a.ExtraVars.Value() {
|
||||
args = append(args, "--extra-vars", v)
|
||||
}
|
||||
|
||||
if a.ListHosts {
|
||||
args = append(args, "--list-hosts")
|
||||
args = append(args, a.Playbooks.Value()...)
|
||||
|
||||
return &types.Cmd{
|
||||
Cmd: execabs.Command(ansiblePlaybookBin, args...),
|
||||
}
|
||||
}
|
||||
|
||||
if a.SyntaxCheck {
|
||||
args = append(args, "--syntax-check")
|
||||
args = append(args, a.Playbooks.Value()...)
|
||||
|
||||
return &types.Cmd{
|
||||
Cmd: execabs.Command(ansiblePlaybookBin, args...),
|
||||
}
|
||||
}
|
||||
|
||||
if a.Check {
|
||||
args = append(args, "--check")
|
||||
}
|
||||
|
||||
if a.Diff {
|
||||
args = append(args, "--diff")
|
||||
}
|
||||
|
||||
if a.FlushCache {
|
||||
args = append(args, "--flush-cache")
|
||||
}
|
||||
|
||||
if a.ForceHandlers {
|
||||
args = append(args, "--force-handlers")
|
||||
}
|
||||
|
||||
if a.Forks != AnsibleForksDefault {
|
||||
args = append(args, "--forks", strconv.Itoa(a.Forks))
|
||||
}
|
||||
|
||||
if a.Limit != "" {
|
||||
args = append(args, "--limit", a.Limit)
|
||||
}
|
||||
|
||||
if a.ListTags {
|
||||
args = append(args, "--list-tags")
|
||||
}
|
||||
|
||||
if a.ListTasks {
|
||||
args = append(args, "--list-tasks")
|
||||
}
|
||||
|
||||
if a.SkipTags != "" {
|
||||
args = append(args, "--skip-tags", a.SkipTags)
|
||||
}
|
||||
|
||||
if a.StartAtTask != "" {
|
||||
args = append(args, "--start-at-task", a.StartAtTask)
|
||||
}
|
||||
|
||||
if a.Tags != "" {
|
||||
args = append(args, "--tags", a.Tags)
|
||||
}
|
||||
|
||||
if a.PrivateKeyFile != "" {
|
||||
args = append(args, "--private-key", a.PrivateKeyFile)
|
||||
}
|
||||
|
||||
if a.User != "" {
|
||||
args = append(args, "--user", a.User)
|
||||
}
|
||||
|
||||
if a.Connection != "" {
|
||||
args = append(args, "--connection", a.Connection)
|
||||
}
|
||||
|
||||
if a.Timeout != 0 {
|
||||
args = append(args, "--timeout", strconv.Itoa(a.Timeout))
|
||||
}
|
||||
|
||||
if a.SSHCommonArgs != "" {
|
||||
args = append(args, "--ssh-common-args", a.SSHCommonArgs)
|
||||
}
|
||||
|
||||
if a.SFTPExtraArgs != "" {
|
||||
args = append(args, "--sftp-extra-args", a.SFTPExtraArgs)
|
||||
}
|
||||
|
||||
if a.SCPExtraArgs != "" {
|
||||
args = append(args, "--scp-extra-args", a.SCPExtraArgs)
|
||||
}
|
||||
|
||||
if a.SSHExtraArgs != "" {
|
||||
args = append(args, "--ssh-extra-args", a.SSHExtraArgs)
|
||||
}
|
||||
|
||||
if a.Become {
|
||||
args = append(args, "--become")
|
||||
}
|
||||
|
||||
if a.BecomeMethod != "" {
|
||||
args = append(args, "--become-method", a.BecomeMethod)
|
||||
}
|
||||
|
||||
if a.BecomeUser != "" {
|
||||
args = append(args, "--become-user", a.BecomeUser)
|
||||
}
|
||||
|
||||
if a.Verbose > 0 {
|
||||
args = append(args, fmt.Sprintf("-%s", strings.Repeat("v", a.Verbose)))
|
||||
}
|
||||
|
||||
args = append(args, a.Playbooks.Value()...)
|
||||
|
||||
return &types.Cmd{
|
||||
Cmd: execabs.Command(ansiblePlaybookBin, args...),
|
||||
Private: false,
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package plugin
|
||||
package ansible
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
@ -7,84 +7,53 @@ import (
|
|||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func TestVersionCommand(t *testing.T) {
|
||||
func TestVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
plugin *Plugin
|
||||
want []string
|
||||
name string
|
||||
ansible *Ansible
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "test version command",
|
||||
plugin: &Plugin{},
|
||||
want: []string{ansibleBin, "--version"},
|
||||
name: "test version command",
|
||||
ansible: &Ansible{},
|
||||
want: []string{ansibleBin, "--version"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cmd := tt.plugin.versionCommand()
|
||||
cmd := tt.ansible.Version()
|
||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPythonRequirementsCommand(t *testing.T) {
|
||||
func TestGalaxyInstall(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
plugin *Plugin
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "with valid requirements file",
|
||||
plugin: &Plugin{
|
||||
Settings: &Settings{
|
||||
PythonRequirements: "requirements.txt",
|
||||
},
|
||||
},
|
||||
want: []string{pipBin, "install", "--upgrade", "--requirement", "requirements.txt"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cmd := tt.plugin.pythonRequirementsCommand()
|
||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGalaxyRequirementsCommand(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
plugin *Plugin
|
||||
want []string
|
||||
name string
|
||||
ansible *Ansible
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "with valid requirements file and no verbosity",
|
||||
plugin: &Plugin{
|
||||
Settings: &Settings{
|
||||
GalaxyRequirements: "requirements.yml",
|
||||
},
|
||||
ansible: &Ansible{
|
||||
GalaxyRequirements: "requirements.yml",
|
||||
},
|
||||
want: []string{ansibleGalaxyBin, "install", "--force", "--role-file", "requirements.yml"},
|
||||
},
|
||||
{
|
||||
name: "with valid requirements file and verbosity level 1",
|
||||
plugin: &Plugin{
|
||||
Settings: &Settings{
|
||||
GalaxyRequirements: "requirements.yml",
|
||||
Verbose: 1,
|
||||
},
|
||||
ansible: &Ansible{
|
||||
GalaxyRequirements: "requirements.yml",
|
||||
Verbose: 1,
|
||||
},
|
||||
want: []string{ansibleGalaxyBin, "install", "--force", "--role-file", "requirements.yml", "-v"},
|
||||
},
|
||||
{
|
||||
name: "with valid requirements file and verbosity level 3",
|
||||
plugin: &Plugin{
|
||||
Settings: &Settings{
|
||||
GalaxyRequirements: "requirements.yml",
|
||||
Verbose: 3,
|
||||
},
|
||||
ansible: &Ansible{
|
||||
GalaxyRequirements: "requirements.yml",
|
||||
Verbose: 3,
|
||||
},
|
||||
want: []string{ansibleGalaxyBin, "install", "--force", "--role-file", "requirements.yml", "-vvv"},
|
||||
},
|
||||
|
@ -92,7 +61,7 @@ func TestGalaxyRequirementsCommand(t *testing.T) {
|
|||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cmd := tt.plugin.galaxyRequirementsCommand()
|
||||
cmd := tt.ansible.GalaxyInstall()
|
||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||
})
|
||||
}
|
||||
|
@ -100,23 +69,22 @@ func TestGalaxyRequirementsCommand(t *testing.T) {
|
|||
|
||||
func TestAnsibleCommand(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
plugin *Plugin
|
||||
want []string
|
||||
name string
|
||||
ansible *Ansible
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "with inventory and no other settings",
|
||||
plugin: &Plugin{
|
||||
Settings: &Settings{},
|
||||
ansible: &Ansible{
|
||||
Inventories: *cli.NewStringSlice("inventory.yml"),
|
||||
},
|
||||
want: []string{ansiblePlaybookBin, "--inventory", "inventory.yml", "--forks", "0"},
|
||||
},
|
||||
{
|
||||
name: "with inventory and module path",
|
||||
plugin: &Plugin{
|
||||
Settings: &Settings{
|
||||
ModulePath: *cli.NewStringSlice("/path/to/modules"),
|
||||
},
|
||||
ansible: &Ansible{
|
||||
Inventories: *cli.NewStringSlice("inventory.yml"),
|
||||
ModulePath: *cli.NewStringSlice("/path/to/modules"),
|
||||
},
|
||||
want: []string{
|
||||
ansiblePlaybookBin, "--inventory", "inventory.yml", "--module-path",
|
||||
|
@ -125,11 +93,10 @@ func TestAnsibleCommand(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "with inventory, module path, and vault ID",
|
||||
plugin: &Plugin{
|
||||
Settings: &Settings{
|
||||
ModulePath: *cli.NewStringSlice("/path/to/modules"),
|
||||
VaultID: "my_vault_id",
|
||||
},
|
||||
ansible: &Ansible{
|
||||
Inventories: *cli.NewStringSlice("inventory.yml"),
|
||||
ModulePath: *cli.NewStringSlice("/path/to/modules"),
|
||||
VaultID: "my_vault_id",
|
||||
},
|
||||
want: []string{
|
||||
ansiblePlaybookBin, "--inventory", "inventory.yml", "--module-path", "/path/to/modules",
|
||||
|
@ -138,12 +105,11 @@ func TestAnsibleCommand(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "with inventory, module path, vault ID, and vault password file",
|
||||
plugin: &Plugin{
|
||||
Settings: &Settings{
|
||||
ModulePath: *cli.NewStringSlice("/path/to/modules"),
|
||||
VaultID: "my_vault_id",
|
||||
VaultPasswordFile: "/path/to/vault/password/file",
|
||||
},
|
||||
ansible: &Ansible{
|
||||
Inventories: *cli.NewStringSlice("inventory.yml"),
|
||||
ModulePath: *cli.NewStringSlice("/path/to/modules"),
|
||||
VaultID: "my_vault_id",
|
||||
VaultPasswordFile: "/path/to/vault/password/file",
|
||||
},
|
||||
want: []string{
|
||||
ansiblePlaybookBin, "--inventory", "inventory.yml", "--module-path", "/path/to/modules",
|
||||
|
@ -153,13 +119,12 @@ func TestAnsibleCommand(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "with inventory, module path, vault ID, vault password file, and extra vars",
|
||||
plugin: &Plugin{
|
||||
Settings: &Settings{
|
||||
ModulePath: *cli.NewStringSlice("/path/to/modules"),
|
||||
VaultID: "my_vault_id",
|
||||
VaultPasswordFile: "/path/to/vault/password/file",
|
||||
ExtraVars: *cli.NewStringSlice("var1=value1", "var2=value2"),
|
||||
},
|
||||
ansible: &Ansible{
|
||||
Inventories: *cli.NewStringSlice("inventory.yml"),
|
||||
ModulePath: *cli.NewStringSlice("/path/to/modules"),
|
||||
VaultID: "my_vault_id",
|
||||
VaultPasswordFile: "/path/to/vault/password/file",
|
||||
ExtraVars: *cli.NewStringSlice("var1=value1", "var2=value2"),
|
||||
},
|
||||
want: []string{
|
||||
ansiblePlaybookBin, "--inventory", "inventory.yml", "--module-path", "/path/to/modules",
|
||||
|
@ -169,11 +134,10 @@ func TestAnsibleCommand(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "with inventory and list hosts",
|
||||
plugin: &Plugin{
|
||||
Settings: &Settings{
|
||||
ListHosts: true,
|
||||
Playbooks: *cli.NewStringSlice("playbook1.yml", "playbook2.yml"),
|
||||
},
|
||||
ansible: &Ansible{
|
||||
ListHosts: true,
|
||||
Inventories: *cli.NewStringSlice("inventory.yml"),
|
||||
Playbooks: *cli.NewStringSlice("playbook1.yml", "playbook2.yml"),
|
||||
},
|
||||
want: []string{
|
||||
ansiblePlaybookBin, "--inventory", "inventory.yml", "--list-hosts",
|
||||
|
@ -182,11 +146,10 @@ func TestAnsibleCommand(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "with inventory and syntax check",
|
||||
plugin: &Plugin{
|
||||
Settings: &Settings{
|
||||
SyntaxCheck: true,
|
||||
Playbooks: *cli.NewStringSlice("playbook1.yml", "playbook2.yml"),
|
||||
},
|
||||
ansible: &Ansible{
|
||||
SyntaxCheck: true,
|
||||
Inventories: *cli.NewStringSlice("inventory.yml"),
|
||||
Playbooks: *cli.NewStringSlice("playbook1.yml", "playbook2.yml"),
|
||||
},
|
||||
want: []string{
|
||||
ansiblePlaybookBin, "--inventory", "inventory.yml", "--syntax-check",
|
||||
|
@ -195,33 +158,32 @@ func TestAnsibleCommand(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "with all options",
|
||||
plugin: &Plugin{
|
||||
Settings: &Settings{
|
||||
Check: true,
|
||||
Diff: true,
|
||||
FlushCache: true,
|
||||
ForceHandlers: true,
|
||||
Forks: 10,
|
||||
Limit: "host1,host2",
|
||||
ListTags: true,
|
||||
ListTasks: true,
|
||||
SkipTags: "tag1,tag2",
|
||||
StartAtTask: "task_name",
|
||||
Tags: "tag3,tag4",
|
||||
PrivateKeyFile: "/path/to/private/key",
|
||||
User: "remote_user",
|
||||
Connection: "ssh",
|
||||
Timeout: 60,
|
||||
SSHCommonArgs: "-o StrictHostKeyChecking=no",
|
||||
SFTPExtraArgs: "-o IdentitiesOnly=yes",
|
||||
SCPExtraArgs: "-r",
|
||||
SSHExtraArgs: "-o ForwardAgent=yes",
|
||||
Become: true,
|
||||
BecomeMethod: "sudo",
|
||||
BecomeUser: "root",
|
||||
Verbose: 2,
|
||||
Playbooks: *cli.NewStringSlice("playbook1.yml", "playbook2.yml"),
|
||||
},
|
||||
ansible: &Ansible{
|
||||
Check: true,
|
||||
Diff: true,
|
||||
FlushCache: true,
|
||||
ForceHandlers: true,
|
||||
Forks: 10,
|
||||
Limit: "host1,host2",
|
||||
ListTags: true,
|
||||
ListTasks: true,
|
||||
SkipTags: "tag1,tag2",
|
||||
StartAtTask: "task_name",
|
||||
Tags: "tag3,tag4",
|
||||
PrivateKeyFile: "/path/to/private/key",
|
||||
User: "remote_user",
|
||||
Connection: "ssh",
|
||||
Timeout: 60,
|
||||
SSHCommonArgs: "-o StrictHostKeyChecking=no",
|
||||
SFTPExtraArgs: "-o IdentitiesOnly=yes",
|
||||
SCPExtraArgs: "-r",
|
||||
SSHExtraArgs: "-o ForwardAgent=yes",
|
||||
Become: true,
|
||||
BecomeMethod: "sudo",
|
||||
BecomeUser: "root",
|
||||
Verbose: 2,
|
||||
Inventories: *cli.NewStringSlice("inventory.yml"),
|
||||
Playbooks: *cli.NewStringSlice("playbook1.yml", "playbook2.yml"),
|
||||
},
|
||||
want: []string{
|
||||
ansiblePlaybookBin, "--inventory", "inventory.yml", "--check", "--diff", "--flush-cache",
|
||||
|
@ -237,7 +199,7 @@ func TestAnsibleCommand(t *testing.T) {
|
|||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cmd := tt.plugin.ansibleCommand("inventory.yml")
|
||||
cmd := tt.ansible.Play()
|
||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||
})
|
||||
}
|
|
@ -1,309 +0,0 @@
|
|||
package plugin
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/thegeeklab/wp-plugin-go/v2/types"
|
||||
"github.com/urfave/cli/v2"
|
||||
"golang.org/x/sys/execabs"
|
||||
)
|
||||
|
||||
const (
|
||||
AnsibleForksDefault = 5
|
||||
|
||||
ansibleFolder = "/etc/ansible"
|
||||
ansibleConfig = "/etc/ansible/ansible.cfg"
|
||||
|
||||
pipBin = "/usr/local/bin/pip"
|
||||
ansibleBin = "/usr/local/bin/ansible"
|
||||
ansibleGalaxyBin = "/usr/local/bin/ansible-galaxy"
|
||||
ansiblePlaybookBin = "/usr/local/bin/ansible-playbook"
|
||||
|
||||
strictFilePerm = 0o600
|
||||
)
|
||||
|
||||
const ansibleContent = `[defaults]
|
||||
host_key_checking = False
|
||||
`
|
||||
|
||||
var ErrAnsiblePlaybookNotFound = errors.New("no playbook found")
|
||||
|
||||
// ansibleConfig creates the Ansible configuration directory and file.
|
||||
// It ensures the directory exists and writes the Ansible configuration
|
||||
// content to the config file with strict file permissions.
|
||||
func (p *Plugin) ansibleConfig() error {
|
||||
if err := os.MkdirAll(ansibleFolder, os.ModePerm); err != nil {
|
||||
return fmt.Errorf("failed to create ansible directory: %w", err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(ansibleConfig, []byte(ansibleContent), strictFilePerm); err != nil {
|
||||
return fmt.Errorf("failed to create ansible config: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// privateKey creates a temporary file containing the private key specified in the plugin settings,
|
||||
// and sets the PrivateKeyFile field in the plugin settings to the name of the temporary file.
|
||||
// This is used to pass the private key to the Ansible command.
|
||||
func (p *Plugin) privateKey() error {
|
||||
tmpfile, err := os.CreateTemp("", "privateKey")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create private key file: %w", err)
|
||||
}
|
||||
|
||||
if _, err := tmpfile.Write([]byte(p.Settings.PrivateKey)); err != nil {
|
||||
return fmt.Errorf("failed to write private key file: %w", err)
|
||||
}
|
||||
|
||||
if err := tmpfile.Close(); err != nil {
|
||||
return fmt.Errorf("failed to close private key file: %w", err)
|
||||
}
|
||||
|
||||
p.Settings.PrivateKeyFile = tmpfile.Name()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// vaultPass creates a temporary file containing the vault password and sets the VaultPasswordFile
|
||||
// field in the Plugin's Settings. This allows the vault password to be used when running
|
||||
// Ansible commands that require it.
|
||||
func (p *Plugin) vaultPass() error {
|
||||
tmpfile, err := os.CreateTemp("", "vaultPass")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create vault password file: %w", err)
|
||||
}
|
||||
|
||||
if _, err := tmpfile.Write([]byte(p.Settings.VaultPassword)); err != nil {
|
||||
return fmt.Errorf("failed to write vault password file: %w", err)
|
||||
}
|
||||
|
||||
if err := tmpfile.Close(); err != nil {
|
||||
return fmt.Errorf("failed to close vault password file: %w", err)
|
||||
}
|
||||
|
||||
p.Settings.VaultPasswordFile = tmpfile.Name()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getPlaybooks retrieves a list of playbook files based on the configured playbook patterns.
|
||||
// If any of the patterns fail to match any files, the original pattern is included in the list.
|
||||
// If no playbooks are found, ErrAnsiblePlaybookNotFound is returned.
|
||||
func (p *Plugin) getPlaybooks() error {
|
||||
var playbooks []string
|
||||
|
||||
for _, pb := range p.Settings.Playbooks.Value() {
|
||||
files, err := filepath.Glob(pb)
|
||||
if err != nil {
|
||||
playbooks = append(playbooks, pb)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
playbooks = append(playbooks, files...)
|
||||
}
|
||||
|
||||
if len(playbooks) == 0 {
|
||||
log.Debug().Strs("patterns", p.Settings.Playbooks.Value()).Msg("no playbooks found")
|
||||
|
||||
return ErrAnsiblePlaybookNotFound
|
||||
}
|
||||
|
||||
p.Settings.Playbooks = *cli.NewStringSlice(playbooks...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Plugin) versionCommand() *types.Cmd {
|
||||
args := []string{
|
||||
"--version",
|
||||
}
|
||||
|
||||
return &types.Cmd{
|
||||
Cmd: execabs.Command(ansibleBin, args...),
|
||||
}
|
||||
}
|
||||
|
||||
// pythonRequirementsCommand returns an execabs.Cmd that runs the pip install
|
||||
// command with the specified Python requirements file and upgrades any existing
|
||||
// packages.
|
||||
func (p *Plugin) pythonRequirementsCommand() *types.Cmd {
|
||||
args := []string{
|
||||
"install",
|
||||
"--upgrade",
|
||||
"--requirement",
|
||||
p.Settings.PythonRequirements,
|
||||
}
|
||||
|
||||
return &types.Cmd{
|
||||
Cmd: execabs.Command(pipBin, args...),
|
||||
}
|
||||
}
|
||||
|
||||
// galaxyRequirementsCommand returns an execabs.Cmd that runs the Ansible Galaxy
|
||||
// install command with the specified role file and verbosity level.
|
||||
func (p *Plugin) galaxyRequirementsCommand() *types.Cmd {
|
||||
args := []string{
|
||||
"install",
|
||||
"--force",
|
||||
"--role-file",
|
||||
p.Settings.GalaxyRequirements,
|
||||
}
|
||||
|
||||
if p.Settings.Verbose > 0 {
|
||||
args = append(args, fmt.Sprintf("-%s", strings.Repeat("v", p.Settings.Verbose)))
|
||||
}
|
||||
|
||||
return &types.Cmd{
|
||||
Cmd: execabs.Command(ansibleGalaxyBin, args...),
|
||||
}
|
||||
}
|
||||
|
||||
// ansibleCommand returns an execabs.Cmd that runs the Ansible playbook with the
|
||||
// specified inventory file and various configuration options set on the Plugin struct.
|
||||
func (p *Plugin) ansibleCommand(inventory string) *types.Cmd {
|
||||
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()...)
|
||||
|
||||
return &types.Cmd{
|
||||
Cmd: execabs.Command(ansiblePlaybookBin, args...),
|
||||
}
|
||||
}
|
||||
|
||||
if p.Settings.SyntaxCheck {
|
||||
args = append(args, "--syntax-check")
|
||||
args = append(args, p.Settings.Playbooks.Value()...)
|
||||
|
||||
return &types.Cmd{
|
||||
Cmd: execabs.Command(ansiblePlaybookBin, 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")
|
||||
}
|
||||
|
||||
if p.Settings.Forks != AnsibleForksDefault {
|
||||
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()...)
|
||||
|
||||
return &types.Cmd{
|
||||
Cmd: execabs.Command(ansiblePlaybookBin, args...),
|
||||
Private: false,
|
||||
}
|
||||
}
|
|
@ -22,49 +22,46 @@ func (p *Plugin) run(_ context.Context) error {
|
|||
|
||||
// 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.versionCommand())
|
||||
|
||||
if err := p.getPlaybooks(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := p.ansibleConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
batchCmd = append(batchCmd, p.Settings.Ansible.Version())
|
||||
|
||||
if p.Settings.PrivateKey != "" {
|
||||
if err := p.privateKey(); err != nil {
|
||||
if p.Settings.Ansible.PrivateKeyFile, err = WriteFile("privateKey", p.Settings.PrivateKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer os.Remove(p.Settings.PrivateKeyFile)
|
||||
defer os.Remove(p.Settings.Ansible.PrivateKeyFile)
|
||||
}
|
||||
|
||||
if p.Settings.VaultPassword != "" {
|
||||
if err := p.vaultPass(); err != nil {
|
||||
if p.Settings.Ansible.VaultPasswordFile, err = WriteFile("vaultPass", p.Settings.VaultPassword); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer os.Remove(p.Settings.VaultPasswordFile)
|
||||
defer os.Remove(p.Settings.Ansible.VaultPasswordFile)
|
||||
}
|
||||
|
||||
if p.Settings.PythonRequirements != "" {
|
||||
batchCmd = append(batchCmd, p.pythonRequirementsCommand())
|
||||
batchCmd = append(batchCmd, PipInstall(p.Settings.PythonRequirements))
|
||||
}
|
||||
|
||||
if p.Settings.GalaxyRequirements != "" {
|
||||
batchCmd = append(batchCmd, p.galaxyRequirementsCommand())
|
||||
if p.Settings.Ansible.GalaxyRequirements != "" {
|
||||
batchCmd = append(batchCmd, p.Settings.Ansible.GalaxyInstall())
|
||||
}
|
||||
|
||||
for _, inventory := range p.Settings.Inventories.Value() {
|
||||
batchCmd = append(batchCmd, p.ansibleCommand(inventory))
|
||||
}
|
||||
batchCmd = append(batchCmd, p.Settings.Ansible.Play())
|
||||
|
||||
for _, cmd := range batchCmd {
|
||||
cmd.Env = append(cmd.Env, "ANSIBLE_FORCE_COLOR=1")
|
||||
|
|
|
@ -3,6 +3,7 @@ package plugin
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thegeeklab/wp-ansible/ansible"
|
||||
wp "github.com/thegeeklab/wp-plugin-go/v2/plugin"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
@ -18,40 +19,9 @@ type Plugin struct {
|
|||
// Settings for the Plugin.
|
||||
type Settings struct {
|
||||
PythonRequirements string
|
||||
GalaxyRequirements string
|
||||
Inventories cli.StringSlice
|
||||
Playbooks cli.StringSlice
|
||||
Limit string
|
||||
SkipTags string
|
||||
StartAtTask string
|
||||
Tags string
|
||||
ExtraVars cli.StringSlice
|
||||
ModulePath cli.StringSlice
|
||||
Check bool
|
||||
Diff bool
|
||||
FlushCache bool
|
||||
ForceHandlers bool
|
||||
ListHosts bool
|
||||
ListTags bool
|
||||
ListTasks bool
|
||||
SyntaxCheck bool
|
||||
Forks int
|
||||
VaultID string
|
||||
VaultPassword string
|
||||
VaultPasswordFile string
|
||||
Verbose int
|
||||
PrivateKey string
|
||||
PrivateKeyFile string
|
||||
User string
|
||||
Connection string
|
||||
Timeout int
|
||||
SSHCommonArgs string
|
||||
SFTPExtraArgs string
|
||||
SCPExtraArgs string
|
||||
SSHExtraArgs string
|
||||
Become bool
|
||||
BecomeMethod string
|
||||
BecomeUser string
|
||||
VaultPassword string
|
||||
Ansible ansible.Ansible
|
||||
}
|
||||
|
||||
func New(e wp.ExecuteFunc, build ...string) *Plugin {
|
||||
|
@ -98,7 +68,7 @@ func Flags(settings *Settings, category string) []cli.Flag {
|
|||
Name: "galaxy-requirements",
|
||||
Usage: "path to galaxy requirements file",
|
||||
EnvVars: []string{"PLUGIN_GALAXY_REQUIREMENTS"},
|
||||
Destination: &settings.GalaxyRequirements,
|
||||
Destination: &settings.Ansible.GalaxyRequirements,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringSliceFlag{
|
||||
|
@ -106,7 +76,7 @@ func Flags(settings *Settings, category string) []cli.Flag {
|
|||
Usage: "path to inventory file",
|
||||
EnvVars: []string{"PLUGIN_INVENTORY", "PLUGIN_INVENTORIES"},
|
||||
Required: true,
|
||||
Destination: &settings.Inventories,
|
||||
Destination: &settings.Ansible.Inventories,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringSliceFlag{
|
||||
|
@ -114,120 +84,120 @@ func Flags(settings *Settings, category string) []cli.Flag {
|
|||
Usage: "list of playbooks to apply",
|
||||
EnvVars: []string{"PLUGIN_PLAYBOOK", "PLUGIN_PLAYBOOKS"},
|
||||
Required: true,
|
||||
Destination: &settings.Playbooks,
|
||||
Destination: &settings.Ansible.Playbooks,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "limit",
|
||||
Usage: "limit selected hosts to an additional pattern",
|
||||
EnvVars: []string{"PLUGIN_LIMIT"},
|
||||
Destination: &settings.Limit,
|
||||
Destination: &settings.Ansible.Limit,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "skip-tags",
|
||||
Usage: "only run plays and tasks whose tags do not match",
|
||||
EnvVars: []string{"PLUGIN_SKIP_TAGS"},
|
||||
Destination: &settings.SkipTags,
|
||||
Destination: &settings.Ansible.SkipTags,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "start-at-task",
|
||||
Usage: "start the playbook at the task matching this name",
|
||||
EnvVars: []string{"PLUGIN_START_AT_TASK"},
|
||||
Destination: &settings.StartAtTask,
|
||||
Destination: &settings.Ansible.StartAtTask,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "tags",
|
||||
Usage: "only run plays and tasks tagged with these values",
|
||||
EnvVars: []string{"PLUGIN_TAGS"},
|
||||
Destination: &settings.Tags,
|
||||
Destination: &settings.Ansible.Tags,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringSliceFlag{
|
||||
Name: "extra-vars",
|
||||
Usage: "set additional variables as `key=value`",
|
||||
EnvVars: []string{"PLUGIN_EXTRA_VARS", "ANSIBLE_EXTRA_VARS"},
|
||||
Destination: &settings.ExtraVars,
|
||||
Destination: &settings.Ansible.ExtraVars,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringSliceFlag{
|
||||
Name: "module-path",
|
||||
Usage: "prepend paths to module library",
|
||||
EnvVars: []string{"PLUGIN_MODULE_PATH"},
|
||||
Destination: &settings.ModulePath,
|
||||
Destination: &settings.Ansible.ModulePath,
|
||||
Category: category,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "check",
|
||||
Usage: "run a check, do not apply any changes",
|
||||
EnvVars: []string{"PLUGIN_CHECK"},
|
||||
Destination: &settings.Check,
|
||||
Destination: &settings.Ansible.Check,
|
||||
Category: category,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "diff",
|
||||
Usage: "show the differences, may print secrets",
|
||||
EnvVars: []string{"PLUGIN_DIFF"},
|
||||
Destination: &settings.Diff,
|
||||
Destination: &settings.Ansible.Diff,
|
||||
Category: category,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "flush-cache",
|
||||
Usage: "clear the fact cache for every host in inventory",
|
||||
EnvVars: []string{"PLUGIN_FLUSH_CACHE"},
|
||||
Destination: &settings.FlushCache,
|
||||
Destination: &settings.Ansible.FlushCache,
|
||||
Category: category,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "force-handlers",
|
||||
Usage: "run handlers even if a task fails",
|
||||
EnvVars: []string{"PLUGIN_FORCE_HANDLERS"},
|
||||
Destination: &settings.ForceHandlers,
|
||||
Destination: &settings.Ansible.ForceHandlers,
|
||||
Category: category,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "list-hosts",
|
||||
Usage: "outputs a list of matching hosts",
|
||||
EnvVars: []string{"PLUGIN_LIST_HOSTS"},
|
||||
Destination: &settings.ListHosts,
|
||||
Destination: &settings.Ansible.ListHosts,
|
||||
Category: category,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "list-tags",
|
||||
Usage: "list all available tags",
|
||||
EnvVars: []string{"PLUGIN_LIST_TAGS"},
|
||||
Destination: &settings.ListTags,
|
||||
Destination: &settings.Ansible.ListTags,
|
||||
Category: category,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "list-tasks",
|
||||
Usage: "list all tasks that would be executed",
|
||||
EnvVars: []string{"PLUGIN_LIST_TASKS"},
|
||||
Destination: &settings.ListTasks,
|
||||
Destination: &settings.Ansible.ListTasks,
|
||||
Category: category,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "syntax-check",
|
||||
Usage: "perform a syntax check on the playbook",
|
||||
EnvVars: []string{"PLUGIN_SYNTAX_CHECK"},
|
||||
Destination: &settings.SyntaxCheck,
|
||||
Destination: &settings.Ansible.SyntaxCheck,
|
||||
Category: category,
|
||||
},
|
||||
&cli.IntFlag{
|
||||
Name: "forks",
|
||||
Usage: "specify number of parallel processes to use",
|
||||
EnvVars: []string{"PLUGIN_FORKS"},
|
||||
Value: AnsibleForksDefault,
|
||||
Destination: &settings.Forks,
|
||||
Value: ansible.AnsibleForksDefault,
|
||||
Destination: &settings.Ansible.Forks,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "vault-id",
|
||||
Usage: "the vault identity to use",
|
||||
EnvVars: []string{"PLUGIN_VAULT_ID", "ANSIBLE_VAULT_ID"},
|
||||
Destination: &settings.VaultID,
|
||||
Destination: &settings.Ansible.VaultID,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
|
@ -241,7 +211,7 @@ func Flags(settings *Settings, category string) []cli.Flag {
|
|||
Name: "verbose",
|
||||
Usage: "level of verbosity, 0 up to 4",
|
||||
EnvVars: []string{"PLUGIN_VERBOSE"},
|
||||
Destination: &settings.Verbose,
|
||||
Destination: &settings.Ansible.Verbose,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
|
@ -255,70 +225,70 @@ func Flags(settings *Settings, category string) []cli.Flag {
|
|||
Name: "user",
|
||||
Usage: "connect as this user",
|
||||
EnvVars: []string{"PLUGIN_USER", "ANSIBLE_USER"},
|
||||
Destination: &settings.User,
|
||||
Destination: &settings.Ansible.User,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "connection",
|
||||
Usage: "connection type to use",
|
||||
EnvVars: []string{"PLUGIN_CONNECTION"},
|
||||
Destination: &settings.Connection,
|
||||
Destination: &settings.Ansible.Connection,
|
||||
Category: category,
|
||||
},
|
||||
&cli.IntFlag{
|
||||
Name: "timeout",
|
||||
Usage: "override the connection timeout in seconds",
|
||||
EnvVars: []string{"PLUGIN_TIMEOUT"},
|
||||
Destination: &settings.Timeout,
|
||||
Destination: &settings.Ansible.Timeout,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "ssh-common-args",
|
||||
Usage: "specify common arguments to pass to SFTP, SCP and SSH connections",
|
||||
EnvVars: []string{"PLUGIN_SSH_COMMON_ARGS"},
|
||||
Destination: &settings.SSHCommonArgs,
|
||||
Destination: &settings.Ansible.SSHCommonArgs,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "sftp-extra-args",
|
||||
Usage: "specify extra arguments to pass to SFTP connections only",
|
||||
EnvVars: []string{"PLUGIN_SFTP_EXTRA_ARGS"},
|
||||
Destination: &settings.SFTPExtraArgs,
|
||||
Destination: &settings.Ansible.SFTPExtraArgs,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "scp-extra-args",
|
||||
Usage: "specify extra arguments to pass to SCP connections only",
|
||||
EnvVars: []string{"PLUGIN_SCP_EXTRA_ARGS"},
|
||||
Destination: &settings.SCPExtraArgs,
|
||||
Destination: &settings.Ansible.SCPExtraArgs,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "ssh-extra-args",
|
||||
Usage: "specify extra arguments to pass to SSH connections only",
|
||||
EnvVars: []string{"PLUGIN_SSH_EXTRA_ARGS"},
|
||||
Destination: &settings.SSHExtraArgs,
|
||||
Destination: &settings.Ansible.SSHExtraArgs,
|
||||
Category: category,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "become",
|
||||
Usage: "enable privilege escalation",
|
||||
EnvVars: []string{"PLUGIN_BECOME"},
|
||||
Destination: &settings.Become,
|
||||
Destination: &settings.Ansible.Become,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "become-method",
|
||||
Usage: "privilege escalation method to use",
|
||||
EnvVars: []string{"PLUGIN_BECOME_METHOD", "ANSIBLE_BECOME_METHOD"},
|
||||
Destination: &settings.BecomeMethod,
|
||||
Destination: &settings.Ansible.BecomeMethod,
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "become-user",
|
||||
Usage: "privilege escalation user to use",
|
||||
EnvVars: []string{"PLUGIN_BECOME_USER", "ANSIBLE_BECOME_USER"},
|
||||
Destination: &settings.BecomeUser,
|
||||
Destination: &settings.Ansible.BecomeUser,
|
||||
Category: category,
|
||||
},
|
||||
}
|
||||
|
|
44
plugin/util.go
Normal file
44
plugin/util.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package plugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/thegeeklab/wp-plugin-go/v2/types"
|
||||
"golang.org/x/sys/execabs"
|
||||
)
|
||||
|
||||
const pipBin = "/usr/local/bin/pip"
|
||||
|
||||
// PipInstall returns a command to install Python packages from a requirements file.
|
||||
// The command will upgrade any existing packages and install the packages specified in the given requirements file.
|
||||
func PipInstall(req string) *types.Cmd {
|
||||
args := []string{
|
||||
"install",
|
||||
"--upgrade",
|
||||
"--requirement",
|
||||
req,
|
||||
}
|
||||
|
||||
return &types.Cmd{
|
||||
Cmd: execabs.Command(pipBin, args...),
|
||||
}
|
||||
}
|
||||
|
||||
// WriteFile creates a temporary file with the given name and content, and returns the path to the created file.
|
||||
func WriteFile(name, content string) (string, error) {
|
||||
tmpfile, err := os.CreateTemp("", name)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create file: %w", err)
|
||||
}
|
||||
|
||||
if _, err := tmpfile.Write([]byte(content)); err != nil {
|
||||
return "", fmt.Errorf("failed to write file: %w", err)
|
||||
}
|
||||
|
||||
if err := tmpfile.Close(); err != nil {
|
||||
return "", fmt.Errorf("failed to close file: %w", err)
|
||||
}
|
||||
|
||||
return tmpfile.Name(), nil
|
||||
}
|
70
plugin/util_test.go
Normal file
70
plugin/util_test.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
package plugin
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPipInstall(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
requirements string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "with valid requirements file",
|
||||
requirements: "requirements.txt",
|
||||
want: []string{pipBin, "install", "--upgrade", "--requirement", "requirements.txt"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cmd := PipInstall(tt.requirements)
|
||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteFile(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
filename string
|
||||
content string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "successful write",
|
||||
filename: "test.txt",
|
||||
content: "test content",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "empty content",
|
||||
filename: "test.txt",
|
||||
content: "",
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
path, err := WriteFile(tt.filename, tt.content)
|
||||
if tt.wantErr {
|
||||
require.Error(t, err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
defer os.Remove(path)
|
||||
|
||||
content, err := os.ReadFile(path)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tt.content, string(content))
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user