-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from SmartColumbusOS/external_module_support
Support pre-defined module compose stacks
- Loading branch information
Showing
10 changed files
with
231 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
defmodule Divo.Stack do | ||
@moduledoc """ | ||
Implements a behaviour for importing | ||
predefined compose stacks from external | ||
library complementary to divo for quickly | ||
standing up well-defined services with | ||
little variation in configuration. | ||
""" | ||
|
||
@doc """ | ||
Defines the behaviour that must be implemented to | ||
supply configs from external modules to divo. The | ||
configuration values are expected as a keyword list | ||
of attributes specific to each module adopting the | ||
behaviour. | ||
""" | ||
@callback gen_stack(keyword()) :: {atom(), map()} | ||
|
||
@doc """ | ||
Iterates over modules supplied in the app :divo | ||
config, calling the `gen_stack` function on each | ||
module's implementation of the behavior, collecting | ||
the resulting maps into a single map and passing | ||
the accumulated result out for writing out the file. | ||
""" | ||
@spec concat_compose([tuple()]) :: map() | ||
def concat_compose(config) do | ||
compose_file = %{version: "3.4"} | ||
|
||
services = | ||
config | ||
|> Enum.map(fn {module, envars} -> | ||
apply(module, :gen_stack, [envars]) | ||
end) | ||
|> Enum.reduce(%{}, fn service, acc -> Map.merge(service, acc) end) | ||
|
||
Map.put(compose_file, :services, services) | ||
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
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,57 @@ | ||
defmodule Divo.StackTest do | ||
use ExUnit.Case | ||
require TemporaryEnv | ||
|
||
test "behaviour returns the service definition" do | ||
services = [ | ||
{DivoFoobar, [db_password: "we-are-divo", db_name: "foobar-db", something: "else"]} | ||
] | ||
|
||
expected = %{ | ||
version: "3.4", | ||
services: %{ | ||
foobar: %{ | ||
image: "foobar:latest", | ||
ports: ["8080:8080"], | ||
command: ["/bin/server", "foreground"] | ||
}, | ||
db: %{ | ||
image: "cooldb:5.0.9", | ||
environment: [ | ||
"PASSWORD=we-are-divo", | ||
"DB=foobar-db" | ||
] | ||
} | ||
} | ||
} | ||
|
||
actual = Divo.Stack.concat_compose(services) | ||
|
||
assert expected == actual | ||
end | ||
end | ||
|
||
defmodule DivoFoobar do | ||
@behaviour Divo.Stack | ||
|
||
@impl Divo.Stack | ||
def gen_stack(envars) do | ||
password = envars[:db_password] | ||
db_name = envars[:db_name] | ||
|
||
%{ | ||
foobar: %{ | ||
image: "foobar:latest", | ||
ports: ["8080:8080"], | ||
command: ["/bin/server", "foreground"] | ||
}, | ||
db: %{ | ||
image: "cooldb:5.0.9", | ||
environment: [ | ||
"PASSWORD=#{password}", | ||
"DB=#{db_name}" | ||
] | ||
} | ||
} | ||
end | ||
end |