Skip to content

Commit

Permalink
Add ansible stack run commands
Browse files Browse the repository at this point in the history
Needed to be able to execute an ansible type stack (still needs harness changes to execute e2e though)
  • Loading branch information
michaeljguarino committed Jun 4, 2024
1 parent 9203858 commit 1d35931
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/console/deployments/stacks/commands.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
defmodule Console.Deployments.Stacks.Commands do
alias Console.Schema.{Stack}

def commands(%Stack{type: :terraform} = stack, dry \\ false) do
def commands(stack, dry \\ false)

def commands(%Stack{type: :terraform} = stack, dry) do
terraform_commands(stack, dry)
|> stitch_hooks(stack)
end

def commands(%Stack{type: :ansible} = stack, dry) do
ansible_commands(stack, dry)
|> stitch_hooks(stack)
end

defp stitch_hooks(commands, %Stack{configuration: %{hooks: [_ | _] = hooks}}) do
cmds = group_by_stage(commands)
hooks = group_by_stage(hooks)
Expand Down Expand Up @@ -33,6 +40,22 @@ defmodule Console.Deployments.Stacks.Commands do
end
end

defp ansible_commands(%Stack{}, true) do
indexed([
cmd("plan", "ansible-playbook", ["main.yaml", "--diff", "--check"], :plan)
])
end

defp ansible_commands(%Stack{deleted_at: d} = s, _) when not is_nil(d),
do: ansible_commands(s, true)

defp ansible_commands(%Stack{}, _) do
indexed([
cmd("plan", "ansible-playbook", ["main.yaml", "--diff", "--check"], :plan),
cmd("apply", "ansible-playbook", ["main.yaml"], :apply)
])
end

defp terraform_commands(%Stack{}, true) do
indexed([
cmd("init", "terraform", ["init", "-upgrade"], :init),
Expand Down
39 changes: 39 additions & 0 deletions test/console/deployments/stacks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,45 @@ defmodule Console.Deployments.StacksTest do
assert refetch(pr).ref == "new-sha"
end

test "it can create a new run an ansible stack" do
stack = insert(:stack,
type: :ansible,
environment: [%{name: "ENV", value: "1"}],
files: [%{path: "test.txt", content: "test"}],
git: %{ref: "main", folder: "ansible"},
sha: "old-sha"
)
expect(Discovery, :sha, fn _, _ -> {:ok, "new-sha"} end)
expect(Discovery, :changes, fn _, _, _, _ -> {:ok, ["ansible/main.yaml"], "a commit message"} end)

{:ok, run} = Stacks.poll(stack)

assert run.stack_id == stack.id
assert run.status == :queued
assert run.message == "a commit message"
assert run.cluster_id == stack.cluster_id
assert run.repository_id == stack.repository_id
assert run.git.ref == "new-sha"
assert run.git.folder == stack.git.folder
[first, second] = run.steps

assert first.cmd == "ansible-playbook"
assert first.args == ["main.yaml", "--diff", "--check"]
assert first.stage == :plan
assert first.index == 0

assert second.cmd == "ansible-playbook"
assert second.args == ["main.yaml"]
assert second.stage == :apply
assert second.index == 1

stack = refetch(stack)
assert stack.sha == "new-sha"
%{environment: [_], files: [_]} = Console.Repo.preload(stack, [:environment, :files])

assert_receive {:event, %PubSub.StackRunCreated{item: ^run}}
end

test "it will ignore if the shas are the same" do
stack = insert(:stack, sha: "old-sha")
expect(Discovery, :sha, fn _, _ -> {:ok, "old-sha"} end)
Expand Down

0 comments on commit 1d35931

Please sign in to comment.