Skip to content

Commit

Permalink
Merge pull request #8 from arillso/refactor/galaxy-api-support
Browse files Browse the repository at this point in the history
Refactor/galaxy api support
  • Loading branch information
sbaerlocher authored Nov 11, 2023
2 parents 87af4c1 + bc48bdb commit f51c378
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 62 deletions.
41 changes: 31 additions & 10 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,53 @@
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
and [human-readable changelog](https://keepachangelog.com/en/1.0.0/).

## master
## [Unreleased]

## 0.0.4
## [0.1.0] - 11 Nov 2023

### Fix
### Added

- **GalaxyAPIKey**: Key for Galaxy API authentication.
- **GalaxyAPIServerURL**: URL of the Galaxy API server.
- **GalaxyCollectionsPath**: Path to Galaxy collections.
- **GalaxyDisableGPGVerify**: Disables GPG verification for Galaxy.
- **GalaxyForceWithDeps**: Forces Galaxy installation with dependencies.
- **GalaxyIgnoreCerts**: Ignores certificate checks for Galaxy.
- **GalaxyIgnoreSignatureStatusCodes**: List of status codes to ignore in Galaxy signature checks.
- **GalaxyKeyring**: Path to the Galaxy keyring.
- **GalaxyOffline**: Enables offline mode for Galaxy.
- **GalaxyPre**: Enables installation of pre-release versions in Galaxy.
- **GalaxyRequiredValidSignatureCount**: Number of required valid signatures for Galaxy.
- **GalaxyRequirementsFile**: Path to the file with Galaxy requirements.
- **GalaxySignature**: Specific signature for Galaxy.
- **GalaxyTimeout**: Timeout for Galaxy operations.
- **GalaxyUpgrade**: Enables the upgrade of Galaxy collections.
- **GalaxyNoDeps**: Disables dependency resolution in Galaxy.

## [0.0.4] - 09 Nov 2023

### Fixed

- fix(ansiblePlaybook): correct 'Config' field case sensitivity.
- Correct 'Config' field case sensitivity in ansiblePlaybook.

## 0.0.3
## [0.0.3] - 09 Nov 2023

### Added

- feat(variable): Add 'GalaxyForce' boolean.
- Add 'GalaxyForce' boolean variable.

## 0.0.2
## [0.0.2] - 28 Jul 2022

### Added

- Added Environment variable `ANSIBLE_GALAXY_DISPLAY_PROGRESS` with value `False`.
- Environment variable `ANSIBLE_GALAXY_DISPLAY_PROGRESS` with value `False`.

### Changed

- Bump github.com/urfave/cli/v2 from 2.2.0 to 2.3.0.
- fix(deps): bump github.com/joho/godotenv from 1.3.0 to 1.4.0
- Bump github.com/joho/godotenv from 1.3.0 to 1.4.0.

## 0.0.1
## [0.0.1] - 05 Apr 2021

### Added

Expand Down
176 changes: 124 additions & 52 deletions ansiblePlaybook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ansible

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -12,50 +11,64 @@ import (
"github.com/pkg/errors"
)

type (
Config struct {
Requirements string
GalaxyFile string
GalaxyForce bool
Inventories []string
Playbooks []string
Limit string
SkipTags string
StartAtTask string
Tags string
ExtraVars []string
ModulePath []string
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
}

AnsiblePlaybook struct {
Config Config
}
)
type Config struct {
Become bool
BecomeMethod string
BecomeUser string
Check bool
Connection string
Diff bool
ExtraVars []string
FlushCache bool
ForceHandlers bool
Forks int
GalaxyAPIKey string
GalaxyAPIServerURL string
GalaxyCollectionsPath string
GalaxyDisableGPGVerify bool
GalaxyFile string
GalaxyForce bool
GalaxyForceWithDeps bool
GalaxyIgnoreCerts bool
GalaxyIgnoreSignatureStatusCodes []string
GalaxyKeyring string
GalaxyOffline bool
GalaxyPre bool
GalaxyRequiredValidSignatureCount int
GalaxyRequirementsFile string
GalaxySignature string
GalaxyTimeout int
GalaxyUpgrade bool
GalaxyNoDeps bool
Inventories []string
Limit string
ListHosts bool
ListTags bool
ListTasks bool
ModulePath []string
Playbooks []string
PrivateKey string
PrivateKeyFile string
Requirements string
SCPExtraArgs string
SFTPExtraArgs string
SkipTags string
SSHCommonArgs string
SSHExtraArgs string
StartAtTask string
SyntaxCheck bool
Tags string
Timeout int
User string
VaultID string
VaultPassword string
VaultPasswordFile string
Verbose int
}

type AnsiblePlaybook struct {
Config Config
}

func (p *AnsiblePlaybook) Exec() error {
if err := p.playbooks(); err != nil {
Expand Down Expand Up @@ -110,8 +123,7 @@ func (p *AnsiblePlaybook) Exec() error {
}

func (p *AnsiblePlaybook) privateKey() error {
tmpfile, err := ioutil.TempFile("", "privateKey")

tmpfile, err := os.CreateTemp("", "privateKey")
if err != nil {
return errors.Wrap(err, "failed to create private key file")
}
Expand All @@ -129,8 +141,7 @@ func (p *AnsiblePlaybook) privateKey() error {
}

func (p *AnsiblePlaybook) vaultPass() error {
tmpfile, err := ioutil.TempFile("", "vaultPass")

tmpfile, err := os.CreateTemp("", "vaultPass")
if err != nil {
return errors.Wrap(err, "failed to create vault password file")
}
Expand Down Expand Up @@ -190,10 +201,34 @@ func (p *AnsiblePlaybook) galaxyRoleCommand() *exec.Cmd {
p.Config.GalaxyFile,
}

if p.Config.GalaxyAPIServerURL != "" {
args = append(args, "--server", p.Config.GalaxyAPIServerURL)
}

if p.Config.GalaxyAPIKey != "" {
args = append(args, "--api-key", p.Config.GalaxyAPIKey)
}

if p.Config.GalaxyIgnoreCerts {
args = append(args, "--ignore-certs")
}

if p.Config.GalaxyTimeout != 0 {
args = append(args, "--timeout", strconv.Itoa(p.Config.GalaxyTimeout))
}

if p.Config.GalaxyForce {
args = append(args, "--force")
}

if p.Config.GalaxyNoDeps {
args = append(args, "--no-deps")
}

if p.Config.GalaxyForceWithDeps {
args = append(args, "--force-with-deps")
}

if p.Config.Verbose > 0 {
args = append(args, fmt.Sprintf("-%s", strings.Repeat("v", p.Config.Verbose)))
}
Expand All @@ -212,13 +247,49 @@ func (p *AnsiblePlaybook) galaxyCollectionCommand() *exec.Cmd {
p.Config.GalaxyFile,
}

if p.Config.GalaxyAPIServerURL != "" {
args = append(args, "--server", p.Config.GalaxyAPIServerURL)
}

if p.Config.GalaxyAPIKey != "" {
args = append(args, "--api-key", p.Config.GalaxyAPIKey)
}

if p.Config.GalaxyIgnoreCerts {
args = append(args, "--ignore-certs")
}

if p.Config.GalaxyTimeout != 0 {
args = append(args, "--timeout", strconv.Itoa(p.Config.GalaxyTimeout))
}

if p.Config.GalaxyForceWithDeps {
args = append(args, "--force-with-deps")
}

if p.Config.GalaxyCollectionsPath != "" {
args = append(args, "--collections-path", p.Config.GalaxyCollectionsPath)
}

if p.Config.GalaxyRequirementsFile != "" {
args = append(args, "--requirements-file", p.Config.GalaxyRequirementsFile)
}

if p.Config.GalaxyPre {
args = append(args, "--pre")
}

if p.Config.GalaxyUpgrade {
args = append(args, "--upgrade")
}

if p.Config.GalaxyForce {
args = append(args, "--force")
}


if p.Config.Verbose > 0 {
args = append(args, fmt.Sprintf("-%s", strings.Repeat("v", p.Config.Verbose)))
verboseFlag := fmt.Sprintf("-%s", strings.Repeat("v", p.Config.Verbose))
args = append(args, verboseFlag)
}

return exec.Command(
Expand Down Expand Up @@ -358,7 +429,8 @@ func (p *AnsiblePlaybook) ansibleCommand(inventory string) *exec.Cmd {
}

if p.Config.Verbose > 0 {
args = append(args, fmt.Sprintf("-%s", strings.Repeat("v", p.Config.Verbose)))
verboseFlag := fmt.Sprintf("-%s", strings.Repeat("v", p.Config.Verbose))
args = append(args, verboseFlag)
}

args = append(args, p.Config.Playbooks...)
Expand Down
90 changes: 90 additions & 0 deletions ansiblePlaybook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package ansible

import (
"os"
"testing"
)

// TestPrivateKey tests the privateKey method of AnsiblePlaybook.
func TestPrivateKey(t *testing.T) {
// Initialize an AnsiblePlaybook instance with a test private key.
ap := AnsiblePlaybook{
Config: Config{
PrivateKey: "test-key",
},
}

// Execute the privateKey method and check for errors.
err := ap.privateKey()
if err != nil {
t.Errorf("privateKey() failed: %s", err)
}

// Read the content of the generated private key file.
content, err := os.ReadFile(ap.Config.PrivateKeyFile)
if err != nil {
t.Errorf("Read private key file failed: %s", err)
}

// Assert that the content of the file matches the expected private key.
if string(content) != "test-key" {
t.Errorf("Expected private key content to be 'test-key', got '%s'", string(content))
}
}

// TestVersionCommand tests the versionCommand method of AnsiblePlaybook.
func TestVersionCommand(t *testing.T) {
// Initialize an AnsiblePlaybook instance.
ap := AnsiblePlaybook{}

// Execute the versionCommand method.
cmd := ap.versionCommand()
if cmd == nil {
t.Errorf("versionCommand() returned nil")
}

// Assert the correctness of the command and arguments.
// Additional checks for command arguments can be added here.
}

// TestExecSuccess tests the Exec method of AnsiblePlaybook for successful execution.
func TestExecSuccess(t *testing.T) {
// Initialize an AnsiblePlaybook instance with a mock configuration.
playbook := &AnsiblePlaybook{
Config: Config{
Playbooks: []string{"tests/test.yml"},
},
}

// Note: Mock external dependencies here if necessary.

// Execute the Exec method and expect no errors.
if err := playbook.Exec(); err != nil {
t.Errorf("Exec should execute without error, but received: %v", err)
}

// Additional assertions to verify expected behavior can be added here.
}

// TestVaultPass tests the vaultPass method of AnsiblePlaybook.
func TestVaultPass(t *testing.T) {
// Initialize an AnsiblePlaybook instance with a test vault password.
playbook := &AnsiblePlaybook{
Config: Config{
VaultPassword: "test-password",
},
}

// Execute the vaultPass method and check for errors.
err := playbook.vaultPass()
if err != nil {
t.Errorf("vaultPass should not return an error, but received: %v", err)
}

// Assert that the VaultPasswordFile property is set correctly.
if playbook.Config.VaultPasswordFile == "" {
t.Error("VaultPasswordFile should not be empty")
}

// Cleanup (delete file) if necessary.
}
8 changes: 8 additions & 0 deletions tests/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: "Check Ansible Connection via SSH"
hosts: localhost
connection: local
gather_facts: true
tasks:
- name: "Test Connection to Hosts"
ansible.builtin.ping:

0 comments on commit f51c378

Please sign in to comment.