Skip to content

Commit

Permalink
Upgrading tools and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkaspa committed Aug 10, 2020
1 parent 0f3fc28 commit 7dc5896
Show file tree
Hide file tree
Showing 40 changed files with 382 additions and 280 deletions.
2 changes: 2 additions & 0 deletions .elixir-1.8.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
elixir 1.8.1
erlang 21.2.4
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
elixir 1.8.1
erlang 21.2.4
elixir 1.10.4-otp-23
erlang 23.0.1
1 change: 1 addition & 0 deletions lib/akd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,6 @@ defmodule Akd do
defp resolve_config({:system, var_name}, default) do
System.get_env(var_name) || default
end

defp resolve_config(value, _default), do: value
end
20 changes: 14 additions & 6 deletions lib/akd/base/build/distillery.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ defmodule Akd.Build.Distillery do
user: :current}}], rollback: [], run_ensure: true}]
"""
@spec get_hooks(Akd.Deployment.t, Keyword.t) :: list(Akd.Hook.t)
@spec get_hooks(Akd.Deployment.t(), Keyword.t()) :: list(Akd.Hook.t())
def get_hooks(deployment, opts) do
[build_hook(deployment, uniq_merge(opts, @default_opts))]
end
Expand All @@ -109,14 +109,22 @@ defmodule Akd.Build.Distillery do

form_hook opts do
if release_name do
main "mix deps.get \n mix compile \n mix release --name=#{release_name} --env=#{distillery_env}",
destination, cmd_envs: cmd_envs
main(
"mix deps.get \n mix compile \n mix release --name=#{release_name} --env=#{
distillery_env
}",
destination,
cmd_envs: cmd_envs
)
else
main "mix deps.get \n mix compile \n mix release --env=#{distillery_env}",
destination, cmd_envs: cmd_envs
main(
"mix deps.get \n mix compile \n mix release --env=#{distillery_env}",
destination,
cmd_envs: cmd_envs
)
end

ensure "rm -rf ./_build/prod/rel", destination
ensure("rm -rf ./_build/prod/rel", destination)
end
end

Expand Down
9 changes: 4 additions & 5 deletions lib/akd/base/build/docker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ defmodule Akd.Build.Docker do

use Akd.Hook

@default_opts [run_ensure: false, ignore_failure: false, file: "Dockerfile",
path: "."]
@default_opts [run_ensure: false, ignore_failure: false, file: "Dockerfile", path: "."]

@doc """
Callback implementation for `get_hooks/2`.
Expand All @@ -62,7 +61,7 @@ defmodule Akd.Build.Docker do
user: :current}}], rollback: [], run_ensure: false}]
"""
@spec get_hooks(Akd.Deployment.t, Keyword.t) :: list(Akd.Hook.t)
@spec get_hooks(Akd.Deployment.t(), Keyword.t()) :: list(Akd.Hook.t())
def get_hooks(deployment, opts) do
[build_hook(deployment, uniq_merge(opts, @default_opts))]
end
Expand All @@ -76,9 +75,9 @@ defmodule Akd.Build.Docker do
tag = Keyword.get(opts, :tag, deployment.name <> ":" <> deployment.vsn)

form_hook opts do
main "docker build -f #{file} -t #{tag} #{path}", destination
main("docker build -f #{file} -t #{tag} #{path}", destination)

ensure "docker rm $(docker ps -a -q)", destination
ensure("docker rm $(docker ps -a -q)", destination)
end
end

Expand Down
29 changes: 16 additions & 13 deletions lib/akd/base/fetch/git.ex
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,14 @@ defmodule Akd.Fetch.Git do
destination: %Akd.Destination{host: :local, path: ".",
user: :current}}], rollback: [], run_ensure: false}]
"""
@spec get_hooks(Akd.Deployment.t, Keyword.t) :: list(Akd.Hook.t)
@spec get_hooks(Akd.Deployment.t(), Keyword.t()) :: list(Akd.Hook.t())
def get_hooks(deployment, opts \\ []) do
opts = uniq_merge(opts, @default_opts)
src = Keyword.get(opts, :src) || Map.get(deployment.data, :git_src)
branch = Keyword.get(opts, :branch) ||
Map.get(deployment.data, :git_branch) || "master"

branch =
Keyword.get(opts, :branch) ||
Map.get(deployment.data, :git_branch) || "master"

destination = Akd.DestinationResolver.resolve(:build, deployment)

Expand All @@ -201,18 +203,19 @@ defmodule Akd.Fetch.Git do

# This function takes a source, branch, destination and options and
# returns an Akd.Hook.t struct using the form_hook DSL.
defp fetch_hook(nil, _, _, _), do: raise @errmsg[:no_src]
defp fetch_hook(nil, _, _, _), do: raise(@errmsg[:no_src])

defp fetch_hook(src, branch, destination, opts) do
form_hook opts do
main "git status; if [[ $? != 0 ]]; then git clone #{src} .; fi", destination
main "git fetch", destination
main "git reset --hard", destination
main "git clean -fd", destination
main "git checkout #{branch}", destination
main "git pull", destination

ensure "rm -rf ./*", destination
ensure "rm -rf ./.*", destination
main("git status; if [[ $? != 0 ]]; then git clone #{src} .; fi", destination)
main("git fetch", destination)
main("git reset --hard", destination)
main("git clean -fd", destination)
main("git checkout #{branch}", destination)
main("git pull", destination)

ensure("rm -rf ./*", destination)
ensure("rm -rf ./.*", destination)
end
end

Expand Down
9 changes: 5 additions & 4 deletions lib/akd/base/fetch/scp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ defmodule Akd.Fetch.Scp do
user: :current}}], rollback: [], run_ensure: true}]
"""
@spec get_hooks(Akd.Deployment.t, Keyword.t) :: list(Akd.Hook.t)
@spec get_hooks(Akd.Deployment.t(), Keyword.t()) :: list(Akd.Hook.t())
def get_hooks(deployment, opts) do
opts = uniq_merge(opts, @default_opts)
src = Keyword.get(opts, :src)
Expand All @@ -78,11 +78,12 @@ defmodule Akd.Fetch.Scp do
excludes = Keyword.get(opts, :exclude, ~w(_build .git deps))

form_hook opts do
main rsync_cmd(src, dest, excludes), Akd.Destination.local()
main(rsync_cmd(src, dest, excludes), Akd.Destination.local())

ensure "rm -rf ./*", destination
ensure("rm -rf ./*", destination)
end
end

defp fetch_hook(%Akd.Destination{} = src, deployment, opts) do
src = Akd.Destination.to_string(src)
fetch_hook(src, deployment, opts)
Expand All @@ -91,7 +92,7 @@ defmodule Akd.Fetch.Scp do
# This function returns an rsync command with all the
# `exclude` switches added to it.
defp rsync_cmd(src, dest, excludes) do
Enum.reduce(excludes, "rsync -krav -e ssh", fn(ex, cmd) ->
Enum.reduce(excludes, "rsync -krav -e ssh", fn ex, cmd ->
cmd <> " --exclude=\"#{ex}\""
end) <> " #{src} #{dest}"
end
Expand Down
18 changes: 10 additions & 8 deletions lib/akd/base/init/distillery.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@ defmodule Akd.Init.Distillery do
user: :current}}], rollback: [], run_ensure: true}]
"""
@spec get_hooks(Akd.Deployment.t, Keyword.t) :: list(Akd.Hook.t)
@spec get_hooks(Akd.Deployment.t(), Keyword.t()) :: list(Akd.Hook.t())
def get_hooks(deployment, opts \\ []) do
opts = uniq_merge(opts, @default_opts)

destination = Akd.DestinationResolver.resolve(:build, deployment)
template_cmd = opts

template_cmd =
opts
|> Keyword.get(:template)
|> template_cmd()

name_cmd = name_cmd(deployment.name)

[init_hook(destination, deployment.mix_env, [name_cmd, template_cmd], opts)]
Expand All @@ -79,20 +82,19 @@ defmodule Akd.Init.Distillery do
cmd_envs = [{"MIX_ENV", mix_env} | cmd_envs]

form_hook opts do
main setup(), destination, cmd_envs: cmd_envs
main rel_init(switches), destination, cmd_envs: cmd_envs
main(setup(), destination, cmd_envs: cmd_envs)
main(rel_init(switches), destination, cmd_envs: cmd_envs)

ensure "rm -rf ./rel", destination
ensure "rm -rf _build/prod", destination
ensure("rm -rf ./rel", destination)
ensure("rm -rf _build/prod", destination)
end
end

# This function accumulates all the switches of release.init command
# and forms a new command.
# This currently supports only template
defp rel_init(switches) when is_list(switches) do
Enum.reduce(switches, "mix release.init",
fn(cmd, acc) -> acc <> " " <> cmd end)
Enum.reduce(switches, "mix release.init", fn cmd, acc -> acc <> " " <> cmd end)
end

# These commands are to be ran before calling release init
Expand Down
51 changes: 35 additions & 16 deletions lib/akd/base/publish/distillery.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ defmodule Akd.Publish.Distillery do
user: :current}}], rollback: [], run_ensure: true}]
"""
@spec get_hooks(Akd.Deployment.t, Keyword.t) :: list(Akd.Hook.t)
@spec get_hooks(Akd.Deployment.t(), Keyword.t()) :: list(Akd.Hook.t())
def get_hooks(deployment, opts \\ []) do
opts = uniq_merge(opts, @default_opts)

[
copy_release_hook(deployment, opts),
publish_hook(deployment, opts),
publish_hook(deployment, opts)
]
end

Expand All @@ -84,17 +85,20 @@ defmodule Akd.Publish.Distillery do
scp_options = Keyword.get(opts, :scp_options, false)

# Transfer between two remote servers by running the SCP command locally
scp_destination = if build.host != publish.host do
Akd.Destination.local()
else
build
end
scp_destination =
if build.host != publish.host do
Akd.Destination.local()
else
build
end

form_hook opts do
main copy_rel(deployment, scp_options), scp_destination
main(copy_rel(deployment, scp_options), scp_destination)

ensure "rm #{publish.path}/#{deployment.name}.tar.gz",
ensure(
"rm #{publish.path}/#{deployment.name}.tar.gz",
publish
)
end
end

Expand All @@ -104,31 +108,44 @@ defmodule Akd.Publish.Distillery do
publish = DestinationResolver.resolve(:publish, deployment)

form_hook opts do
main publish_rel(deployment), publish
main(publish_rel(deployment), publish)
end
end

# This function returns the command to be used to copy the release from
# build to production.
# This assumes that you're running this command from the same server
defp copy_rel(%Deployment{build_at: %Destination{host: s}, publish_to: %Destination{host: s}} = deployment, _scp_options) do
defp copy_rel(
%Deployment{build_at: %Destination{host: s}, publish_to: %Destination{host: s}} =
deployment,
_scp_options
) do
"""
cp #{path_to_release(deployment.build_at.path, deployment)} #{deployment.publish_to.path}
"""
end

defp copy_rel(%Deployment{build_at: src, publish_to: dest} = deployment, %{local_intermediate: true} = scp_options) do
defp copy_rel(
%Deployment{build_at: src, publish_to: dest} = deployment,
%{local_intermediate: true} = scp_options
) do
"""
scp #{src |> Destination.to_string() |> path_to_release(deployment)} #{Akd.Destination.local() |> Destination.to_string()}
scp #{Akd.Destination.local() |> Destination.to_string() |> path_to_local_release(deployment)} #{dest |> Destination.to_string()}
scp #{src |> Destination.to_string() |> path_to_release(deployment)} #{
Akd.Destination.local() |> Destination.to_string()
}
scp #{Akd.Destination.local() |> Destination.to_string() |> path_to_local_release(deployment)} #{
dest |> Destination.to_string()
}
rm #{Akd.Destination.local() |> Destination.to_string() |> path_to_local_release(deployment)}
"""
end

# This assumes that the publish server has ssh credentials to build server
defp copy_rel(%Deployment{build_at: src, publish_to: dest} = deployment, scp_options) do
"""
scp #{scp_options} #{src |> Destination.to_string() |> path_to_release(deployment)} #{Destination.to_string(dest)}
scp #{scp_options} #{src |> Destination.to_string() |> path_to_release(deployment)} #{
Destination.to_string(dest)
}
"""
end

Expand All @@ -144,7 +161,9 @@ defmodule Akd.Publish.Distillery do
# This function returns the path to the release based on deployment name
# and mix environment.
defp path_to_release(base, deployment) do
"#{base}/_build/#{deployment.mix_env}/rel/#{deployment.name}/releases/#{deployment.vsn}/#{deployment.name}.tar.gz"
"#{base}/_build/#{deployment.mix_env}/rel/#{deployment.name}/releases/#{deployment.vsn}/#{
deployment.name
}.tar.gz"
end

defp path_to_local_release(base, deployment) do
Expand Down
8 changes: 3 additions & 5 deletions lib/akd/base/start/distillery.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule Akd.Start.Distillery do
user: :current}}], run_ensure: true}]
"""
@spec get_hooks(Akd.Deployment.t, Keyword.t) :: list(Akd.Hook.t)
@spec get_hooks(Akd.Deployment.t(), Keyword.t()) :: list(Akd.Hook.t())
def get_hooks(deployment, opts \\ []) do
opts = uniq_merge(opts, @default_opts)
[start_hook(deployment, opts)]
Expand All @@ -61,11 +61,9 @@ defmodule Akd.Start.Distillery do
cmd_envs = Keyword.get(opts, :cmd_envs, [])

form_hook opts do
main "bin/#{deployment.name} start", destination,
cmd_envs: cmd_envs
main("bin/#{deployment.name} start", destination, cmd_envs: cmd_envs)

rollback "bin/#{deployment.name} stop", destination,
cmd_envs: cmd_envs
rollback("bin/#{deployment.name} stop", destination, cmd_envs: cmd_envs)
end
end

Expand Down
8 changes: 3 additions & 5 deletions lib/akd/base/stop/distillery.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule Akd.Stop.Distillery do
user: :current}}], run_ensure: true}]
"""
@spec get_hooks(Akd.Deployment.t, Keyword.t) :: list(Akd.Hook.t)
@spec get_hooks(Akd.Deployment.t(), Keyword.t()) :: list(Akd.Hook.t())
def get_hooks(deployment, opts \\ []) do
opts = uniq_merge(opts, @default_opts)
[stop_hook(deployment, opts)]
Expand All @@ -60,11 +60,9 @@ defmodule Akd.Stop.Distillery do
cmd_envs = Keyword.get(opts, :cmd_envs, [])

form_hook opts do
main "bin/#{deployment.name} stop", destination,
cmd_envs: cmd_envs
main("bin/#{deployment.name} stop", destination, cmd_envs: cmd_envs)

rollback "bin/#{deployment.name} start", destination,
cmd_envs: cmd_envs
rollback("bin/#{deployment.name} start", destination, cmd_envs: cmd_envs)
end
end

Expand Down
16 changes: 8 additions & 8 deletions lib/akd/deployment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ defmodule Akd.Deployment do

@typedoc ~s(Generic type for a Deployment struct)
@type t :: %__MODULE__{
mix_env: String.t,
build_at: Destination.t,
publish_to: Destination.t,
name: String.t,
vsn: String.t,
hooks: [Hook.t],
data: Map.t
}
mix_env: String.t(),
build_at: Destination.t(),
publish_to: Destination.t(),
name: String.t(),
vsn: String.t(),
hooks: [Hook.t()],
data: Map.t()
}
end
Loading

0 comments on commit 7dc5896

Please sign in to comment.