mirror of
https://github.com/thegeeklab/wp-ansible.git
synced 2024-11-21 13:40:39 +00:00
chore: migrate to wp-plugin-go v3 (#63)
This commit is contained in:
parent
7b51a968a2
commit
ed42a97d1c
@ -3,14 +3,14 @@ package ansible
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/thegeeklab/wp-plugin-go/v2/types"
|
plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"golang.org/x/sys/execabs"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -59,14 +59,16 @@ type Ansible struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Version runs the Ansible binary with the --version flag to retrieve the current version.
|
// Version runs the Ansible binary with the --version flag to retrieve the current version.
|
||||||
func (a *Ansible) Version() *types.Cmd {
|
func (a *Ansible) Version() *plugin_exec.Cmd {
|
||||||
args := []string{
|
args := []string{
|
||||||
"--version",
|
"--version",
|
||||||
}
|
}
|
||||||
|
|
||||||
return &types.Cmd{
|
cmd := plugin_exec.Command(ansibleBin, args...)
|
||||||
Cmd: execabs.Command(ansibleBin, args...),
|
cmd.Stdout = os.Stdout
|
||||||
}
|
cmd.Stderr = os.Stderr
|
||||||
|
|
||||||
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPlaybooks retrieves the list of Ansible playbook files based on the configured playbook patterns.
|
// GetPlaybooks retrieves the list of Ansible playbook files based on the configured playbook patterns.
|
||||||
@ -96,7 +98,7 @@ func (a *Ansible) GetPlaybooks() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GalaxyInstall runs the ansible-galaxy install command with the configured options.
|
// GalaxyInstall runs the ansible-galaxy install command with the configured options.
|
||||||
func (a *Ansible) GalaxyInstall() *types.Cmd {
|
func (a *Ansible) GalaxyInstall() *plugin_exec.Cmd {
|
||||||
args := []string{
|
args := []string{
|
||||||
"install",
|
"install",
|
||||||
"--force",
|
"--force",
|
||||||
@ -108,15 +110,17 @@ func (a *Ansible) GalaxyInstall() *types.Cmd {
|
|||||||
args = append(args, fmt.Sprintf("-%s", strings.Repeat("v", a.Verbose)))
|
args = append(args, fmt.Sprintf("-%s", strings.Repeat("v", a.Verbose)))
|
||||||
}
|
}
|
||||||
|
|
||||||
return &types.Cmd{
|
cmd := plugin_exec.Command(ansibleGalaxyBin, args...)
|
||||||
Cmd: execabs.Command(ansibleGalaxyBin, args...),
|
cmd.Stdout = os.Stdout
|
||||||
}
|
cmd.Stderr = os.Stderr
|
||||||
|
|
||||||
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
// Play runs the Ansible playbook with the configured options.
|
// Play runs the Ansible playbook with the configured options.
|
||||||
//
|
//
|
||||||
//nolint:gocyclo
|
//nolint:gocyclo
|
||||||
func (a *Ansible) Play() *types.Cmd {
|
func (a *Ansible) Play() *plugin_exec.Cmd {
|
||||||
args := make([]string, 0)
|
args := make([]string, 0)
|
||||||
|
|
||||||
for _, inventory := range a.Inventories.Value() {
|
for _, inventory := range a.Inventories.Value() {
|
||||||
@ -143,18 +147,22 @@ func (a *Ansible) Play() *types.Cmd {
|
|||||||
args = append(args, "--list-hosts")
|
args = append(args, "--list-hosts")
|
||||||
args = append(args, a.Playbooks.Value()...)
|
args = append(args, a.Playbooks.Value()...)
|
||||||
|
|
||||||
return &types.Cmd{
|
cmd := plugin_exec.Command(ansiblePlaybookBin, args...)
|
||||||
Cmd: execabs.Command(ansiblePlaybookBin, args...),
|
cmd.Stdout = os.Stdout
|
||||||
}
|
cmd.Stderr = os.Stderr
|
||||||
|
|
||||||
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
if a.SyntaxCheck {
|
if a.SyntaxCheck {
|
||||||
args = append(args, "--syntax-check")
|
args = append(args, "--syntax-check")
|
||||||
args = append(args, a.Playbooks.Value()...)
|
args = append(args, a.Playbooks.Value()...)
|
||||||
|
|
||||||
return &types.Cmd{
|
cmd := plugin_exec.Command(ansiblePlaybookBin, args...)
|
||||||
Cmd: execabs.Command(ansiblePlaybookBin, args...),
|
cmd.Stdout = os.Stdout
|
||||||
}
|
cmd.Stderr = os.Stderr
|
||||||
|
|
||||||
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
if a.Check {
|
if a.Check {
|
||||||
@ -251,8 +259,9 @@ func (a *Ansible) Play() *types.Cmd {
|
|||||||
|
|
||||||
args = append(args, a.Playbooks.Value()...)
|
args = append(args, a.Playbooks.Value()...)
|
||||||
|
|
||||||
return &types.Cmd{
|
cmd := plugin_exec.Command(ansiblePlaybookBin, args...)
|
||||||
Cmd: execabs.Command(ansiblePlaybookBin, args...),
|
cmd.Stdout = os.Stdout
|
||||||
Private: false,
|
cmd.Stderr = os.Stderr
|
||||||
}
|
|
||||||
|
return cmd
|
||||||
}
|
}
|
||||||
|
4
go.mod
4
go.mod
@ -5,9 +5,8 @@ go 1.22
|
|||||||
require (
|
require (
|
||||||
github.com/rs/zerolog v1.32.0
|
github.com/rs/zerolog v1.32.0
|
||||||
github.com/stretchr/testify v1.9.0
|
github.com/stretchr/testify v1.9.0
|
||||||
github.com/thegeeklab/wp-plugin-go/v2 v2.3.1
|
github.com/thegeeklab/wp-plugin-go/v3 v3.0.2
|
||||||
github.com/urfave/cli/v2 v2.27.2
|
github.com/urfave/cli/v2 v2.27.2
|
||||||
golang.org/x/sys v0.20.0
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@ -31,5 +30,6 @@ require (
|
|||||||
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
|
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
|
||||||
golang.org/x/crypto v0.23.0 // indirect
|
golang.org/x/crypto v0.23.0 // indirect
|
||||||
golang.org/x/net v0.25.0 // indirect
|
golang.org/x/net v0.25.0 // indirect
|
||||||
|
golang.org/x/sys v0.20.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
4
go.sum
4
go.sum
@ -47,8 +47,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
|
|||||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/thegeeklab/wp-plugin-go/v2 v2.3.1 h1:ARwYgTPZ5iPsmOenmqcCf8TjiEe8wBOHKO7H/Xshe48=
|
github.com/thegeeklab/wp-plugin-go/v3 v3.0.2 h1:Mv5i8S1WY+BUNjTjX6lOnB3p8S9mvM+XwfY4R98gx0g=
|
||||||
github.com/thegeeklab/wp-plugin-go/v2 v2.3.1/go.mod h1:0t8M8txtEFiaB6RqLX8vLrxkqAo5FT5Hx7dztN592D4=
|
github.com/thegeeklab/wp-plugin-go/v3 v3.0.2/go.mod h1:ij1iJcAVgzerBTqXnmq0bu1VA+hhVVwzXKqiqfoGjjg=
|
||||||
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
|
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
|
||||||
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
|
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
|
||||||
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
|
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
|
||||||
|
@ -11,8 +11,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/thegeeklab/wp-ansible/plugin"
|
"github.com/thegeeklab/wp-ansible/plugin"
|
||||||
"github.com/thegeeklab/wp-plugin-go/v2/docs"
|
plugin_docs "github.com/thegeeklab/wp-plugin-go/v3/docs"
|
||||||
"github.com/thegeeklab/wp-plugin-go/v2/template"
|
plugin_template "github.com/thegeeklab/wp-plugin-go/v3/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -23,7 +23,7 @@ func main() {
|
|||||||
|
|
||||||
p := plugin.New(nil)
|
p := plugin.New(nil)
|
||||||
|
|
||||||
out, err := template.Render(context.Background(), client, tmpl, docs.GetTemplateData(p.App))
|
out, err := plugin_template.Render(context.Background(), client, tmpl, plugin_docs.GetTemplateData(p.App))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/thegeeklab/wp-plugin-go/v2/file"
|
plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
|
||||||
"github.com/thegeeklab/wp-plugin-go/v2/types"
|
plugin_file "github.com/thegeeklab/wp-plugin-go/v3/file"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (p *Plugin) run(_ context.Context) error {
|
func (p *Plugin) run(_ context.Context) error {
|
||||||
@ -34,12 +34,13 @@ func (p *Plugin) Validate() error {
|
|||||||
func (p *Plugin) Execute() error {
|
func (p *Plugin) Execute() error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
batchCmd := make([]*types.Cmd, 0)
|
batchCmd := make([]*plugin_exec.Cmd, 0)
|
||||||
|
|
||||||
batchCmd = append(batchCmd, p.Settings.Ansible.Version())
|
batchCmd = append(batchCmd, p.Settings.Ansible.Version())
|
||||||
|
|
||||||
if p.Settings.PrivateKey != "" {
|
if p.Settings.PrivateKey != "" {
|
||||||
if p.Settings.Ansible.PrivateKeyFile, err = file.WriteTmpFile("privateKey", p.Settings.PrivateKey); err != nil {
|
p.Settings.Ansible.PrivateKeyFile, err = plugin_file.WriteTmpFile("privateKey", p.Settings.PrivateKey)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,7 +48,8 @@ func (p *Plugin) Execute() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if p.Settings.VaultPassword != "" {
|
if p.Settings.VaultPassword != "" {
|
||||||
if p.Settings.Ansible.VaultPasswordFile, err = file.WriteTmpFile("vaultPass", p.Settings.VaultPassword); err != nil {
|
p.Settings.Ansible.VaultPasswordFile, err = plugin_file.WriteTmpFile("vaultPass", p.Settings.VaultPassword)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/thegeeklab/wp-ansible/ansible"
|
"github.com/thegeeklab/wp-ansible/ansible"
|
||||||
wp "github.com/thegeeklab/wp-plugin-go/v2/plugin"
|
plugin_base "github.com/thegeeklab/wp-plugin-go/v3/plugin"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
// Plugin implements provide the plugin.
|
// Plugin implements provide the plugin.
|
||||||
type Plugin struct {
|
type Plugin struct {
|
||||||
*wp.Plugin
|
*plugin_base.Plugin
|
||||||
Settings *Settings
|
Settings *Settings
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,15 +24,15 @@ type Settings struct {
|
|||||||
Ansible ansible.Ansible
|
Ansible ansible.Ansible
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(e wp.ExecuteFunc, build ...string) *Plugin {
|
func New(e plugin_base.ExecuteFunc, build ...string) *Plugin {
|
||||||
p := &Plugin{
|
p := &Plugin{
|
||||||
Settings: &Settings{},
|
Settings: &Settings{},
|
||||||
}
|
}
|
||||||
|
|
||||||
options := wp.Options{
|
options := plugin_base.Options{
|
||||||
Name: "wp-ansible",
|
Name: "wp-ansible",
|
||||||
Description: "Manage infrastructure with Ansible",
|
Description: "Manage infrastructure with Ansible",
|
||||||
Flags: Flags(p.Settings, wp.FlagsPluginCategory),
|
Flags: Flags(p.Settings, plugin_base.FlagsPluginCategory),
|
||||||
Execute: p.run,
|
Execute: p.run,
|
||||||
HideWoodpeckerFlags: true,
|
HideWoodpeckerFlags: true,
|
||||||
}
|
}
|
||||||
@ -49,7 +49,7 @@ func New(e wp.ExecuteFunc, build ...string) *Plugin {
|
|||||||
options.Execute = e
|
options.Execute = e
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Plugin = wp.New(options)
|
p.Plugin = plugin_base.New(options)
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,16 @@
|
|||||||
package plugin
|
package plugin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/thegeeklab/wp-plugin-go/v2/types"
|
"os"
|
||||||
"golang.org/x/sys/execabs"
|
|
||||||
|
plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
const pipBin = "/usr/local/bin/pip"
|
const pipBin = "/usr/local/bin/pip"
|
||||||
|
|
||||||
// PipInstall returns a command to install Python packages from a requirements file.
|
// 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.
|
// The command will upgrade any existing packages and install the packages specified in the given requirements file.
|
||||||
func PipInstall(req string) *types.Cmd {
|
func PipInstall(req string) *plugin_exec.Cmd {
|
||||||
args := []string{
|
args := []string{
|
||||||
"install",
|
"install",
|
||||||
"--upgrade",
|
"--upgrade",
|
||||||
@ -17,7 +18,9 @@ func PipInstall(req string) *types.Cmd {
|
|||||||
req,
|
req,
|
||||||
}
|
}
|
||||||
|
|
||||||
return &types.Cmd{
|
cmd := plugin_exec.Command(pipBin, args...)
|
||||||
Cmd: execabs.Command(pipBin, args...),
|
cmd.Stdout = os.Stdout
|
||||||
}
|
cmd.Stderr = os.Stderr
|
||||||
|
|
||||||
|
return cmd
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user