Skip to content

Commit

Permalink
feat: add ansible-deploy action
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippHeuer committed Nov 25, 2024
1 parent 6743152 commit 29b689c
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 4 deletions.
57 changes: 57 additions & 0 deletions actions/ansible/ansibledeploy/ansible-deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ansibledeploy

import (
"fmt"
"path"

cidsdk "github.com/cidverse/cid-sdk-go"
)

type Action struct {
Sdk cidsdk.SDKClient
}

type Config struct {
PlaybookFile string `json:"ansible_playbook" env:"ANSIBLE_PLAYBOOK"`
InventoryFile string `json:"ansible_inventory" env:"ANSIBLE_INVENTORY"`
}

func (a Action) Execute() (err error) {
cfg := Config{}
ctx, err := a.Sdk.ModuleAction(&cfg)
if err != nil {
return err
}

// config
playbookFile := cfg.PlaybookFile
inventoryFile := cfg.InventoryFile
if playbookFile == "" {
playbookFile = ctx.Module.Discovery[0].File
}
if inventoryFile == "" {
inventoryFile = path.Join(path.Dir(playbookFile), "inventory")
}

// role and collection requirements
if a.Sdk.FileExists(path.Join(ctx.Module.ModuleDir, "requirements.yml")) {
_, err = a.Sdk.ExecuteCommand(cidsdk.ExecuteCommandRequest{
Command: `ansible-galaxy collection install -r requirements.yml`,
WorkDir: ctx.Module.ModuleDir,
})
if err != nil {
return err
}
}

// deploy
_, err = a.Sdk.ExecuteCommand(cidsdk.ExecuteCommandRequest{
Command: fmt.Sprintf(`ansible-playbook %q -i %q`, playbookFile, inventoryFile),
WorkDir: ctx.Module.ModuleDir,
})
if err != nil {
return err
}

return nil
}
25 changes: 25 additions & 0 deletions actions/ansible/ansibledeploy/ansible-deploy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ansibledeploy

import (
"testing"

"github.com/cidverse/cid-actions-go/actions/api"
"github.com/cidverse/cid-actions-go/pkg/core/test"
cidsdk "github.com/cidverse/cid-sdk-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestAnsibleDeploy(t *testing.T) {
sdk := test.Setup(t)
sdk.On("ModuleAction", mock.Anything).Return(api.GetAnsibleTestData(false), nil)
sdk.On("FileExists", "/my-project/playbook-a/requirements.yml").Return(false)
sdk.On("ExecuteCommand", cidsdk.ExecuteCommandRequest{
Command: `ansible-playbook "/my-project/playbook-a/playbook.yml" -i "/my-project/playbook-a/inventory"`,
WorkDir: "/my-project/playbook-a",
}).Return(nil, nil)

action := Action{Sdk: sdk}
err := action.Execute()
assert.NoError(t, err)
}
9 changes: 8 additions & 1 deletion actions/ansible/ansiblelint/ansible-lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Action struct {
}

type ScanConfig struct {
LintProfile string `json:"ansible_lint_profile" env:"ANSIBLE_LINT_PROFILE"`
}

func (a Action) Execute() (err error) {
Expand All @@ -22,6 +23,12 @@ func (a Action) Execute() (err error) {
return err
}

// config
lintProfile := cfg.LintProfile
if lintProfile == "" {
lintProfile = "production"
}

// files
reportFile := cidsdk.JoinPath(ctx.Config.TempDir, "ansiblelint.sarif.json")

Expand All @@ -39,7 +46,7 @@ func (a Action) Execute() (err error) {
// lint
// config lookup: https://ansible.readthedocs.io/projects/lint/configuring/#using-local-configuration-files
_, err = a.Sdk.ExecuteCommand(cidsdk.ExecuteCommandRequest{
Command: fmt.Sprintf(`ansible-lint --project . --sarif-file %s`, reportFile),
Command: fmt.Sprintf(`ansible-lint --project . --profile %q --sarif-file %q`, lintProfile, reportFile),
WorkDir: ctx.Module.ModuleDir,
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions actions/ansible/ansiblelint/ansible-lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAnsibleLint(t *testing.T) {
sdk.On("ModuleAction", mock.Anything).Return(api.GetAnsibleTestData(false), nil)
sdk.On("FileExists", "/my-project/playbook-a/requirements.yml").Return(false)
sdk.On("ExecuteCommand", cidsdk.ExecuteCommandRequest{
Command: "ansible-lint --project . --sarif-file /my-project/.tmp/ansiblelint.sarif.json",
Command: `ansible-lint --project . --profile "production" --sarif-file "/my-project/.tmp/ansiblelint.sarif.json"`,
WorkDir: "/my-project/playbook-a",
}).Return(nil, nil)
sdk.On("FileRead", "/my-project/.tmp/ansiblelint.sarif.json").Return(reportJson, nil)
Expand All @@ -44,7 +44,7 @@ func TestAnsibleLintWithDependencies(t *testing.T) {
WorkDir: "/my-project/playbook-a",
}).Return(nil, nil)
sdk.On("ExecuteCommand", cidsdk.ExecuteCommandRequest{
Command: "ansible-lint --project . --sarif-file /my-project/.tmp/ansiblelint.sarif.json",
Command: `ansible-lint --project . --profile "production" --sarif-file "/my-project/.tmp/ansiblelint.sarif.json"`,
WorkDir: "/my-project/playbook-a",
}).Return(nil, nil)
sdk.On("FileRead", "/my-project/.tmp/ansiblelint.sarif.json").Return(reportJson, nil)
Expand Down
4 changes: 3 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/cidverse/cid-actions-go/actions/ansible/ansibledeploy"
"github.com/cidverse/cid-actions-go/actions/ansible/ansiblelint"
"github.com/cidverse/cid-actions-go/actions/api"
"github.com/cidverse/cid-actions-go/actions/applicationinspector"
Expand Down Expand Up @@ -74,7 +75,8 @@ var runCmd = &cobra.Command{
// actions
actions := map[string]api.Action{
// ansible
"ansible-lint": ansiblelint.Action{Sdk: *sdk},
"ansible-lint": ansiblelint.Action{Sdk: *sdk},
"ansible-deploy": ansibledeploy.Action{Sdk: *sdk},
// changeloggenerate
"changelog-generate": changeloggenerate.Action{Sdk: *sdk},
// container
Expand Down

0 comments on commit 29b689c

Please sign in to comment.