Skip to content

Commit

Permalink
Add new demo env that uses faked execution server (#187)
Browse files Browse the repository at this point in the history
* Add new demo env that uses faked execution server

* Randomize results

* Build demo image on the CI

* Add demo dir to credo and formatter
  • Loading branch information
rtorrero authored Feb 17, 2023
1 parent 4c7985b commit 701f0f2
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"apps/*/lib/",
"apps/*/src/",
"apps/*/test/",
"apps/*/web/"
"apps/*/web/",
"demo"
],
excluded: [~r"/_build/", ~r"/deps/", ~r"/node_modules/"]
},
Expand Down
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/deps/
/doc/
/test/
!/test/support/factory.ex
/tmp/
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Used by "mix format"
[
import_deps: [:ecto, :phoenix, :open_api_spex],
inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs}"],
inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test,demo}/**/*.{ex,exs}"],
subdirectories: ["priv/*/migrations"]
]
39 changes: 39 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,45 @@ jobs:
cache-from: type=gha
cache-to: type=gha,mode=max

build-demo-img:
name: Build the docker image for the demo environment
runs-on: ubuntu-20.04
if: github.event_name == 'release' || (github.event_name == 'push' && github.ref_name == 'main') || github.event_name == 'workflow_dispatch'
needs: [static-code-analysis, test]
permissions:
contents: read
packages: write
env:
MIX_ENV: demo
REGISTRY: ghcr.io
IMAGE_REPOSITORY: ghcr.io/${{ github.repository_owner }}/trento-wanda
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: docker/setup-buildx-action@v2
- name: Log in to the Container registry
uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@507c2f2dc502c992ad446e3d7a5dfbe311567a96
with:
images: ${{ env.IMAGE_REPOSITORY }}
- name: Build and push container image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ env.IMAGE_REPOSITORY }}:${{ env.MIX_ENV }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: MIX_ENV=${{ env.MIX_ENV }}

generate-docs:
name: Generate project documentation
runs-on: ubuntu-20.04
Expand Down
10 changes: 10 additions & 0 deletions config/demo.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Config

config :wanda, Wanda.Policy, execution_server_impl: Wanda.Executions.FakeServer

config :wanda, WandaWeb.Endpoint,
check_origin: :conn,
cache_static_manifest: "priv/static/cache_manifest.json",
server: true

config :logger, level: :debug
2 changes: 1 addition & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Config
# and secrets from environment variables or elsewhere. Do not define
# any compile-time configuration in here, as it won't be applied.
# The block below contains prod specific runtime configuration.
if config_env() == :prod do
if config_env() in [:prod, :demo] do
database_url =
System.get_env("DATABASE_URL") ||
raise """
Expand Down
64 changes: 64 additions & 0 deletions demo/fake_server.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
defmodule Wanda.Executions.FakeServer do
@moduledoc """
Execution server implementation that does not actually execute anything and just
returns (fake) random results.
"""
@behaviour Wanda.Executions.ServerBehaviour

import Wanda.Factory

alias Wanda.{
Executions,
Messaging
}

require Logger

@impl true
def start_execution(execution_id, group_id, targets, _env, _config \\ []) do
create_fake_execution(execution_id, group_id, targets)
Process.sleep(2_000)
complete_fake_execution(execution_id, group_id, targets)

:ok
end

@impl true
def receive_facts(_execution_id, _group_id, _agent_id, _facts) do
:ok
end

defp create_fake_execution(execution_id, group_id, targets) do
insert(:execution,
status: :running,
execution_id: execution_id,
group_id: group_id,
targets: Enum.map(targets, &Map.from_struct/1)
)

Executions.create_execution!(execution_id, group_id, targets)
execution_started = Messaging.Mapper.to_execution_started(execution_id, group_id, targets)
:ok = Messaging.publish("results", execution_started)
end

defp complete_fake_execution(execution_id, group_id, targets) do
result = Enum.random([:passing, :warning, :critical])
check_results = build(:check_results_from_targets, targets: targets, result: result)

build_result =
build(:result,
check_results: check_results,
execution_id: execution_id,
group_id: group_id,
result: result
)

Executions.complete_execution!(
execution_id,
build_result
)

execution_completed = Messaging.Mapper.to_execution_completed(build_result)
:ok = Messaging.publish("results", execution_completed)
end
end
11 changes: 9 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ defmodule Wanda.MixProject do
"test/support"
]

defp elixirc_paths(:demo),
do: [
"test/support/factory.ex",
"demo/fake_server.ex",
"lib"
]

defp elixirc_paths(_), do: ["lib"]
# Run "mix help deps" to learn about dependencies.
defp deps do
Expand All @@ -120,8 +127,8 @@ defmodule Wanda.MixProject do
{:credo, "~> 1.6", only: [:dev, :test], runtime: false},
{:dialyxir, "~> 1.0", only: [:dev, :test], runtime: false},
{:mox, "~> 1.0", only: :test},
{:ex_machina, "~> 2.7.0", only: :test},
{:faker, "~> 0.17", only: :test},
{:ex_machina, "~> 2.7.0", only: [:demo, :test]},
{:faker, "~> 0.17", only: [:demo, :test]},
{:excoveralls, "~> 0.10", only: :test},
# phoenix deps
{:phoenix, "~> 1.6.12"},
Expand Down

0 comments on commit 701f0f2

Please sign in to comment.