-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
87 changed files
with
2,155 additions
and
68 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule Polar.Encrypted.Map do | ||
use Cloak.Ecto.Map, vault: Polar.Vault | ||
end |
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,27 @@ | ||
defmodule Polar.Machines do | ||
alias __MODULE__.Cluster | ||
|
||
defdelegate list_clusters(scope), | ||
to: Cluster.Manager, | ||
as: :list | ||
|
||
defdelegate create_cluster(params), | ||
to: Cluster.Manager, | ||
as: :create | ||
|
||
alias __MODULE__.Check | ||
|
||
defdelegate list_checks(), | ||
to: Check.Manager, | ||
as: :list | ||
|
||
defdelegate create_check(params), | ||
to: Check.Manager, | ||
as: :create | ||
|
||
alias __MODULE__.Assessment | ||
|
||
defdelegate get_or_create_assessment(version, params), | ||
to: Assessment.Manager, | ||
as: :get_or_create | ||
end |
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,49 @@ | ||
defmodule Polar.Machines.Assessment do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
alias Polar.Streams | ||
alias Polar.Machines.Check | ||
alias Polar.Machines.Cluster | ||
|
||
alias __MODULE__.Event | ||
alias __MODULE__.Transitions | ||
|
||
use Eventful.Transitable | ||
|
||
Transitions | ||
|> governs(:current_state, on: Event) | ||
|
||
@valid_attrs ~w( | ||
check_id | ||
cluster_id | ||
instance_type | ||
)a | ||
|
||
@required_attrs ~w( | ||
check_id | ||
cluster_id | ||
instance_type | ||
)a | ||
|
||
schema "assessments" do | ||
field :current_state, :string, default: "created" | ||
|
||
field :instance_type, :string | ||
|
||
belongs_to :check, Check | ||
belongs_to :cluster, Cluster | ||
|
||
belongs_to :version, Streams.Version | ||
|
||
timestamps(type: :utc_datetime_usec) | ||
end | ||
|
||
@doc false | ||
def changeset(assessment, attrs) do | ||
assessment | ||
|> cast(attrs, @valid_attrs) | ||
|> validate_required(@required_attrs) | ||
|> validate_inclusion(:instance_type, ["container", "vm"]) | ||
end | ||
end |
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,12 @@ | ||
defmodule Polar.Machines.Assessment.Event do | ||
alias Polar.Machines.Assessment | ||
alias Polar.Accounts.User | ||
|
||
use Eventful, | ||
parent: {:assessment, Assessment}, | ||
actor: {:user, User} | ||
|
||
alias Assessment.Transitions | ||
|
||
handle(:transitions, using: Transitions) | ||
end |
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,29 @@ | ||
defmodule Polar.Machines.Assessment.Manager do | ||
alias Polar.Repo | ||
alias Polar.Machines.Assessment | ||
|
||
def get_or_create(version, params) do | ||
check_id = Map.get(params, "check_id") || params.check_id | ||
instance_type = Map.get(params, "instance_type") || params.instance_type | ||
|
||
Assessment | ||
|> Repo.get_by( | ||
version_id: version.id, | ||
check_id: check_id, | ||
instance_type: instance_type | ||
) | ||
|> case do | ||
%Assessment{} = assessment -> | ||
{:ok, assessment} | ||
|
||
nil -> | ||
create(version, params) | ||
end | ||
end | ||
|
||
def create(version, params) do | ||
%Assessment{version_id: version.id} | ||
|> Assessment.changeset(params) | ||
|> Repo.insert() | ||
end | ||
end |
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,17 @@ | ||
defimpl Eventful.Transit, for: Polar.Machines.Assessment do | ||
alias Polar.Machines.Assessment.Event | ||
|
||
def perform(assessment, user, event_name, options \\ []) do | ||
comment = Keyword.get(options, :comment) | ||
domain = Keyword.get(options, :domain, "transitions") | ||
parameters = Keyword.get(options, :parameters, %{}) | ||
|
||
assessment | ||
|> Event.handle(user, %{ | ||
domain: domain, | ||
name: event_name, | ||
comment: comment, | ||
parameters: parameters | ||
}) | ||
end | ||
end |
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,36 @@ | ||
defmodule Polar.Machines.Assessment.Transitions do | ||
@behaviour Eventful.Handler | ||
use Eventful.Transition, repo: Polar.Repo | ||
|
||
alias Polar.Machines.Assessment | ||
|
||
Assessment | ||
|> transition( | ||
[from: "created", to: "running", via: "run"], | ||
fn changes -> transit(changes) end | ||
) | ||
|
||
Assessment | ||
|> transition( | ||
[from: "failed", to: "running", via: "run"], | ||
fn changes -> transit(changes) end | ||
) | ||
|
||
Assessment | ||
|> transition( | ||
[from: "running", to: "running", via: "run"], | ||
fn changes -> transit(changes) end | ||
) | ||
|
||
Assessment | ||
|> transition( | ||
[from: "running", to: "passed", via: "pass"], | ||
fn changes -> transit(changes) end | ||
) | ||
|
||
Assessment | ||
|> transition( | ||
[from: "running", to: "failed", via: "fail"], | ||
fn changes -> transit(changes) end | ||
) | ||
end |
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,29 @@ | ||
defmodule Polar.Machines.Check do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "checks" do | ||
field :name, :string, virtual: true | ||
|
||
field :slug, :string | ||
field :description, :string | ||
|
||
timestamps(type: :utc_datetime_usec) | ||
end | ||
|
||
@doc false | ||
def changeset(check, attrs) do | ||
check | ||
|> cast(attrs, [:name, :description]) | ||
|> validate_required([:name, :description]) | ||
|> generate_slug() | ||
end | ||
|
||
defp generate_slug(changeset) do | ||
if name = get_change(changeset, :name) do | ||
put_change(changeset, :slug, Slug.slugify(name)) | ||
else | ||
changeset | ||
end | ||
end | ||
end |
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,14 @@ | ||
defmodule Polar.Machines.Check.Manager do | ||
alias Polar.Repo | ||
alias Polar.Machines.Check | ||
|
||
def list() do | ||
Repo.all(Check) | ||
end | ||
|
||
def create(params) do | ||
%Check{} | ||
|> Check.changeset(params) | ||
|> Repo.insert() | ||
end | ||
end |
Oops, something went wrong.