Skip to content
This repository was archived by the owner on May 9, 2021. It is now read-only.

Commit f789f20

Browse files
committed
Message-based -> RESTful
1 parent d12aa2b commit f789f20

File tree

8 files changed

+88
-38
lines changed

8 files changed

+88
-38
lines changed

config/dev.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ config :agma, AgmaWeb.Endpoint,
88
watchers: []
99

1010
config :logger, :console,
11-
format: "$time $metadata[$level] $message\n",
11+
format: "$metadata[$level] $message\n",
1212
metadata: [:request_id]
1313

1414
config :phoenix, :stacktrace_depth, 20

lib/agma/application.ex

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ defmodule Agma.Application do
1212
{Phoenix.PubSub, name: Agma.PubSub},
1313
AgmaWeb.Endpoint,
1414
]
15-
++ Mahou.Singyeong.child_specs(dsn, Agma.Consumer)
15+
++ Mahou.Singyeong.supervisor(dsn, Agma.Consumer)
1616
++ [Agma.Stats]
1717

1818
opts = [strategy: :one_for_one, name: Agma.Supervisor]
19-
Supervisor.start_link(children, opts)
19+
20+
Supervisor.start_link children, opts
2021
end
2122

2223
def config_change(changed, _new, removed) do
23-
AgmaWeb.Endpoint.config_change(changed, removed)
24+
AgmaWeb.Endpoint.config_change changed, removed
2425
:ok
2526
end
2627
end

lib/agma/consumer.ex

+2-31
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
defmodule Agma.Consumer do
22
use Singyeong.Consumer
3-
alias Agma.Docker
43
alias Mahou.Message
5-
alias Mahou.Message.{
6-
ChangeContainerStatus,
7-
CreateContainer,
8-
}
94
require Logger
105

116
def start_link do
@@ -22,7 +17,7 @@ defmodule Agma.Consumer do
2217

2318
defp process(event) do
2419
event
25-
|> Message.parse
20+
|> Message.decode
2621
|> inspect_ts
2722
|> Map.get(:payload)
2823
|> process_event
@@ -36,29 +31,5 @@ defmodule Agma.Consumer do
3631
m
3732
end
3833

39-
def process_event(%CreateContainer{apps: apps}) do
40-
Logger.info "deploy: apps:\n* #{apps |> Enum.map(&("#{&1.namespace}:#{&1.name} -> #{&1.image}")) |> Enum.join("\n* ")}"
41-
Logger.info "deploy: apps: #{Enum.count apps} total"
42-
for app <- apps do
43-
name = Docker.app_name app
44-
{:ok, res} = Docker.create app
45-
Logger.info "deploy: app: created #{name}"
46-
Logger.debug "deploy: app: #{name}: #{inspect res, pretty: true}"
47-
end
48-
for app <- apps do
49-
name = Docker.app_name app
50-
{:ok, _} = Docker.start name
51-
Logger.info "deploy: app: started #{name}"
52-
end
53-
end
54-
55-
# TODO: Use id somewhere S:
56-
def process_event(%ChangeContainerStatus{id: _id, name: name, namespace: ns, command: cmd}) do
57-
app = Docker.app_name name, ns
58-
Logger.info "status: app: #{app}: sending :#{cmd}"
59-
case cmd do
60-
:stop -> {:ok, _} = Docker.stop app
61-
:kill -> {:ok, _} = Docker.kill app
62-
end
63-
end
34+
defp process_event(_), do: :ok
6435
end

lib/agma/deployer.ex

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
defmodule Agma.Deployer do
2+
alias Agma.Docker
3+
alias Mahou.Message.ChangeContainerStatus
4+
require Logger
5+
6+
def deploy(apps) do
7+
Logger.info "deploy: apps:\n* #{apps |> Enum.map(&("#{&1.namespace}:#{&1.name} -> #{&1.image}")) |> Enum.join("\n* ")}"
8+
Logger.info "deploy: apps: #{Enum.count apps} total"
9+
10+
for app <- apps do
11+
name = Docker.app_name app
12+
{:ok, res} = Docker.create app
13+
14+
Logger.info "deploy: app: created #{name}"
15+
Logger.debug "deploy: app: #{name}: #{inspect res, pretty: true}"
16+
end
17+
18+
for app <- apps do
19+
name = Docker.app_name app
20+
{:ok, _} = Docker.start name
21+
22+
Logger.info "deploy: app: started #{name}"
23+
end
24+
end
25+
26+
def change_status(%ChangeContainerStatus{
27+
id: _id, # TODO: Use someday?
28+
name: name,
29+
namespace: ns,
30+
command: command,
31+
}) do
32+
app = Docker.app_name name, ns
33+
Logger.info "status: app: #{app}: sending :#{command}"
34+
case command do
35+
:stop -> {:ok, _} = Docker.stop app
36+
:kill -> {:ok, _} = Docker.kill app
37+
_ -> raise "wtf is #{command} (HINT: not implemented)"
38+
end
39+
end
40+
end

lib/agma/docker.ex

+4
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,16 @@ defmodule Agma.Docker do
4747
if not String.match?(name, ~r/^\/?[a-zA-Z0-9][a-zA-Z0-9_.-]+$/) do
4848
{:error, :invalid_name}
4949
else
50+
51+
# TODO: Iterate and find first available
5052
port = 6666
53+
5154
labels =
5255
%{
5356
Labels.namespace() => ns || "default",
5457
Labels.managed() => "true",
5558
}
59+
5660
host_config =
5761
%{
5862
"AutoRemove" => true,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
defmodule AgmaWeb.ContainerController do
2+
use AgmaWeb, :controller
3+
use Mahou.Docs
4+
alias Agma.Deployer
5+
alias Mahou.Message
6+
alias Mahou.Message.{ChangeContainerStatus, CreateContainer}
7+
8+
def create(conn, _) do
9+
{:ok, body, conn} = read_body conn
10+
# TODO: lol check ts
11+
%Message{payload: %CreateContainer{apps: apps}} = Message.decode body, json: true
12+
13+
# TODO: lol error checking
14+
Deployer.deploy apps
15+
16+
json conn, %{}
17+
end
18+
19+
def change_status(conn, _) do
20+
{:ok, body, conn} = read_body conn
21+
# TODO: lol check ts
22+
%Message{payload: %ChangeContainerStatus{} = msg} = Message.decode body, json: true
23+
24+
# TODO: lol real retval checking
25+
Deployer.change_status msg
26+
27+
json conn, %{}
28+
end
29+
end

lib/agma_web/router.ex

+5
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@ defmodule AgmaWeb.Router do
77

88
scope "/api", AgmaWeb do
99
pipe_through :api
10+
11+
scope "/v1" do
12+
post "/create", ContainerController, :create
13+
post "/status", ContainerController, :change_status
14+
end
1015
end
1116
end

mix.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
"cowlib": {:hex, :cowlib, "2.9.1", "61a6c7c50cf07fdd24b2f45b89500bb93b6686579b069a89f88cb211e1125c78", [:rebar3], [], "hexpm", "e4175dc240a70d996156160891e1c62238ede1729e45740bdd38064dad476170"},
88
"ex_json_schema": {:hex, :ex_json_schema, "0.7.4", "09eb5b0c8184e5702bc89625a9d0c05c7a0a845d382e9f6f406a0fc1c9a8cc3f", [:mix], [], "hexpm", "45c67fa840f0d719a2b5578126dc29bcdc1f92499c0f61bcb8a3bcb5935f9684"},
99
"gen_json_schema": {:hex, :gen_json_schema, "0.1.0", "d6fe453c26b11d5938593a0b3040e86fdc1ff37f443fea620a6c08f675f538c5", [:mix], [{:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "abd940a8c6f4af66efa3299f09ff36e831e1f122278cc7fcbba9a562e76daa83"},
10-
"gen_stage": {:hex, :gen_stage, "1.0.0", "51c8ae56ff54f9a2a604ca583798c210ad245f415115453b773b621c49776df5", [:mix], [], "hexpm", "1d9fc978db5305ac54e6f5fec7adf80cd893b1000cf78271564c516aa2af7706"},
10+
"gen_stage": {:hex, :gen_stage, "1.1.0", "dd0c0f8d2f3b993fdbd3d58e94abbe65380f4e78bdee3fa93d5618d7d14abe60", [:mix], [], "hexpm", "7f2b36a6d02f7ef2ba410733b540ec423af65ec9c99f3d1083da508aca3b9305"},
1111
"gun": {:hex, :gun, "1.3.3", "cf8b51beb36c22b9c8df1921e3f2bc4d2b1f68b49ad4fbc64e91875aa14e16b4", [:rebar3], [{:cowlib, "~> 2.7.0", [hex: :cowlib, repo: "hexpm", optional: false]}], "hexpm", "3106ce167f9c9723f849e4fb54ea4a4d814e3996ae243a1c828b256e749041e0"},
1212
"hackney": {:hex, :hackney, "1.17.0", "717ea195fd2f898d9fe9f1ce0afcc2621a41ecfe137fae57e7fe6e9484b9aa99", [:rebar3], [{:certifi, "~>2.5", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "64c22225f1ea8855f584720c0e5b3cd14095703af1c9fbc845ba042811dc671c"},
1313
"httpoison": {:hex, :httpoison, "1.8.0", "6b85dea15820b7804ef607ff78406ab449dd78bed923a49c7160e1886e987a3d", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "28089eaa98cf90c66265b6b5ad87c59a3729bea2e74e9d08f9b51eb9729b3c3a"},
1414
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
1515
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
1616
"lens": {:hex, :lens, "1.0.0", "bad01a2ff90ce0e13ce486ed1750c92c06d96d271368e56771fe68d0415d4877", [:mix], [], "hexpm", "581349c3e42181696f71ead8b016cc548715eb125c41d003297dd1a670e46f21"},
1717
"libmahou": {:git, "[email protected]:queer/libmahou.git", "a2d531b0015c869981fd2493b81b24d55adb580c", []},
18-
"mahou": {:git, "[email protected]:queer/libmahou.git", "dd7c2aa0882a8db26947ce9827d7f13df706f5eb", []},
18+
"mahou": {:git, "[email protected]:queer/libmahou.git", "b62e6decb1e2c38a7b5e0fcded427738d7a1d00f", []},
1919
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
2020
"mime": {:hex, :mime, "1.5.0", "203ef35ef3389aae6d361918bf3f952fa17a09e8e43b5aa592b93eba05d0fb8d", [:mix], [], "hexpm", "55a94c0f552249fc1a3dd9cd2d3ab9de9d3c89b559c2bd01121f824834f24746"},
2121
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
@@ -27,7 +27,7 @@
2727
"plug_cowboy": {:hex, :plug_cowboy, "2.4.1", "779ba386c0915027f22e14a48919a9545714f849505fa15af2631a0d298abf0f", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d72113b6dff7b37a7d9b2a5b68892808e3a9a752f2bf7e503240945385b70507"},
2828
"plug_crypto": {:hex, :plug_crypto, "1.2.0", "1cb20793aa63a6c619dd18bb33d7a3aa94818e5fd39ad357051a67f26dfa2df6", [:mix], [], "hexpm", "a48b538ae8bf381ffac344520755f3007cc10bd8e90b240af98ea29b69683fc2"},
2929
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"},
30-
"singyeong": {:hex, :singyeong, "0.4.4", "8e0a04fb724b95e23e6b562b89f246ad916ceaeb57953489a85f4ef0ce571c41", [:mix], [{:gen_stage, "~> 1.0", [hex: :gen_stage, repo: "hexpm", optional: false]}, {:gun, "~> 1.3", [hex: :gun, repo: "hexpm", optional: false]}, {:httpoison, "~> 1.7", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: false]}, {:typed_struct, "~> 0.2.1", [hex: :typed_struct, repo: "hexpm", optional: false]}], "hexpm", "b81ece7bc1e028d5f5b74be45897ca4abb783a506ecd64e8644f6f8cd944f19e"},
30+
"singyeong": {:hex, :singyeong, "0.4.7", "d01c0a82b4f2d3686aabcaec1b617f0b8cb4e1f38673db12b3ec2d08fd36c3e7", [:mix], [{:gen_stage, "~> 1.0", [hex: :gen_stage, repo: "hexpm", optional: false]}, {:gun, "~> 1.3", [hex: :gun, repo: "hexpm", optional: false]}, {:httpoison, "~> 1.7", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: false]}, {:typed_struct, "~> 0.2.1", [hex: :typed_struct, repo: "hexpm", optional: false]}], "hexpm", "db7934e5d17c6e0be3f016f969c9e9f4c121c96f1918b88e2dd7a8217386ea2e"},
3131
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
3232
"telemetry": {:hex, :telemetry, "0.4.2", "2808c992455e08d6177322f14d3bdb6b625fbcfd233a73505870d8738a2f4599", [:rebar3], [], "hexpm", "2d1419bd9dda6a206d7b5852179511722e2b18812310d304620c7bd92a13fcef"},
3333
"telemetry_metrics": {:hex, :telemetry_metrics, "0.6.0", "da9d49ee7e6bb1c259d36ce6539cd45ae14d81247a2b0c90edf55e2b50507f7b", [:mix], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "5cfe67ad464b243835512aa44321cee91faed6ea868d7fb761d7016e02915c3d"},

0 commit comments

Comments
 (0)