-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6743152
commit 29b689c
Showing
5 changed files
with
95 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters