-
Notifications
You must be signed in to change notification settings - Fork 3
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
0 parents
commit 35c46ee
Showing
9 changed files
with
304 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
] |
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,21 @@ | ||
# LearnElixirDeploys | ||
|
||
**TODO: Add description** | ||
|
||
## Installation | ||
|
||
If [available in Hex](https://hex.pm/docs/publish), the package can be installed | ||
by adding `learn_elixir_deploys` to your list of dependencies in `mix.exs`: | ||
|
||
```elixir | ||
def deps do | ||
[ | ||
{:learn_elixir_deploys, "~> 0.1.0"} | ||
] | ||
end | ||
``` | ||
|
||
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) | ||
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can | ||
be found at <https://hexdocs.pm/learn_elixir_deploys>. | ||
|
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,2 @@ | ||
defmodule LearnElixirDeploys do | ||
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,73 @@ | ||
defmodule Mix.Tasks.Ansible.Build do | ||
use Mix.Task | ||
|
||
@shortdoc "Deploys to terraform resources using ansible" | ||
@moduledoc """ | ||
Deploys to ansible | ||
""" | ||
|
||
def run(args) do | ||
with :ok <- check_in_umbrella(), | ||
:ok <- create_ansible_hosts_file(parse_args(args)) do | ||
:ok | ||
else | ||
{:error, e} -> Mix.shell().error(to_string(e)) | ||
end | ||
end | ||
|
||
defp check_in_umbrella do | ||
if Mix.Project.umbrella?() do | ||
:ok | ||
else | ||
{:error, ErrorMessage.bad_request("must be in umbrella root")} | ||
end | ||
end | ||
|
||
defp parse_args(args) do | ||
{opts, _} = OptionParser.parse!(args, | ||
aliases: [f: :force, q: :quit], | ||
switches: [ | ||
force: :boolean, | ||
quiet: :boolean | ||
] | ||
) | ||
|
||
opts | ||
end | ||
|
||
defp create_ansible_hosts_file(opts) do | ||
with {:ok, hostname_ips} <- terraform_instance_ips() do | ||
ansible_host_file = EEx.eval_file("./deploys/ansible/hosts.eex", [ | ||
assigns: %{ | ||
host_name_ips: hostname_ips | ||
} | ||
]) | ||
|
||
Mix.Generator.create_file("./deploys/ansible/hosts", ansible_host_file, opts) | ||
|
||
:ok | ||
end | ||
end | ||
|
||
defp terraform_instance_ips do | ||
case System.shell("terraform output --json", cd: Path.expand("./deploys/terraform")) do | ||
{output, 0} -> | ||
{:ok, parse_terraform_output_to_ips(output)} | ||
|
||
{message, _} -> | ||
{:error, ErrorMessage.failed_dependency("terraform output failed", %{message: message})} | ||
end | ||
end | ||
|
||
defp parse_terraform_output_to_ips(output) do | ||
case Jason.decode!(output) do | ||
%{"public_ip" => %{"value" => values}} -> values | ||
_ -> [] | ||
end | ||
end | ||
|
||
def host_name(host_name, index) do | ||
"#{host_name}_#{:io_lib.format("~3..0B", [index])}" | ||
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,72 @@ | ||
defmodule Mix.Tasks.Terraform.Build do | ||
use Mix.Task | ||
|
||
@shortdoc "Deploys to terraform resources using ansible" | ||
@moduledoc """ | ||
Deploys to ansible | ||
""" | ||
|
||
@terraform_file Path.expand("../../deploys/terraform/variables.tf") | ||
|
||
if not File.exists?(@terraform_file) do | ||
raise "Terraform file doesn't exist at #{@terraform_file}" | ||
end | ||
|
||
def run(args) do | ||
terraform_output = LearnElixir.MixProject.releases() | ||
|> Keyword.keys | ||
|> Enum.map_join(",\n\n", &(&1 |> to_string |> generate_terraform_output)) | ||
|
||
opts = parse_args(args) | ||
|
||
write_to_terraform(terraform_output, opts) | ||
end | ||
|
||
defp parse_args(args) do | ||
{opts, _} = OptionParser.parse!(args, | ||
aliases: [f: :force, q: :quit], | ||
switches: [ | ||
force: :boolean, | ||
quiet: :boolean | ||
] | ||
) | ||
|
||
opts | ||
end | ||
|
||
defp generate_terraform_output(release_name) do | ||
""" | ||
#{release_name} = { | ||
environment = "prod" | ||
name = "#{title_case(release_name)}" | ||
} | ||
""" | ||
end | ||
|
||
defp title_case(string) do | ||
string |> String.split("_") |> Enum.map_join(" ", &String.capitalize/1) | ||
end | ||
|
||
defp write_to_terraform(terraform_output, opts) do | ||
new_terraform_file = @terraform_file |> File.read! |> inject_terraform_contents(terraform_output) | ||
|
||
if opts[:force] || Mix.Generator.overwrite?(@terraform_file, new_terraform_file) do | ||
File.write!(@terraform_file, new_terraform_file) | ||
|
||
if !opts[:quiet] do | ||
Mix.shell().info([:green, "* injecting ", :reset, @terraform_file]) | ||
end | ||
end | ||
end | ||
|
||
defp inject_terraform_contents(current_file, terraform_output) do | ||
current_file = String.split(current_file, "\n") | ||
project_variable_idx = Enum.find_index( | ||
current_file, | ||
&(&1 =~ "variable \"learn_elixir_project\"") | ||
) + 4 # 4 is the number of newlines till the default key | ||
{start_of_file, project_variable} = Enum.split(current_file, project_variable_idx + 1) | ||
|
||
Enum.join(start_of_file ++ String.split(terraform_output, "\n") ++ Enum.take(project_variable, -3), "\n") | ||
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,32 @@ | ||
defmodule LearnElixirDeploys.MixProject do | ||
use Mix.Project | ||
|
||
def project do | ||
[ | ||
app: :learn_elixir_deploys, | ||
version: "0.1.0", | ||
build_path: "../../_build", | ||
config_path: "../../config/config.exs", | ||
deps_path: "../../deps", | ||
lockfile: "../../mix.lock", | ||
elixir: "~> 1.13", | ||
start_permanent: Mix.env() == :prod, | ||
deps: deps() | ||
] | ||
end | ||
|
||
# Run "mix help compile.app" to learn about applications. | ||
def application do | ||
[ | ||
extra_applications: [:logger] | ||
] | ||
end | ||
|
||
# Run "mix help deps" to learn about dependencies. | ||
defp deps do | ||
[ | ||
{:jason, "~> 1.3"}, | ||
{:error_message, "~> 0.2"} | ||
] | ||
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,4 @@ | ||
defmodule LearnElixirDeploysTest do | ||
use ExUnit.Case | ||
doctest LearnElixirDeploys | ||
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 @@ | ||
ExUnit.start() |