Skip to content

Commit

Permalink
Merge pull request #20 from appunite/release-builds
Browse files Browse the repository at this point in the history
Fix Docker builds
  • Loading branch information
hauleth committed Oct 11, 2018
2 parents 740adab + 29db294 commit fb1085e
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 27 deletions.
9 changes: 5 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
FROM ubuntu:latest AS source
RUN apt-get update && apt-get install -y --no-install-recommends \
libssl1.1 imagemagick ghostscript libcap \
libssl1.1 imagemagick ghostscript libcap2 sudo \
&& rm -rf /var/lib/apt/lists/*
ENV LANG C.UTF-8
ENV PORT 80
HEALTHCHECK --timeout=5s --interval=10s CMD imager ping
ENTRYPOINT ["/usr/local/bin/imager"]
CMD ["foreground"]

FROM appunite/elixir-ci:1.7.1 AS build
RUN apt-get update && apt-get install libcap-dev
ENV MIX_ENV prod
ENV OPTIMIZE true
COPY . /app
WORKDIR /app
RUN apt-get install libcap-dev
RUN mix local.hex --force && mix local.rebar --force
RUN mix deps.get
RUN mix compile && mix release --env=prod

FROM source
MAINTAINER Łukasz Jan Niemier <lukasz.niemier@appunite.com>
COPY --from=build /app/_build/prod/rel/imager /usr/local
ENTRYPOINT ["/usr/local/bin/imager"]
CMD ["foreground"]
4 changes: 3 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# is restricted to this project.
use Mix.Config

config :phoenix, :plug_init_mode, :runtime

# Configures the endpoint
config :imager, ImagerWeb.Endpoint,
url: [host: "localhost"],
Expand All @@ -22,7 +24,7 @@ config :ex_aws,
json_codec: Jason

config :sentry,
included_environments: [:prod],
included_environments: [],
release: Mix.Project.config()[:version],
environment_name: Mix.env()

Expand Down
16 changes: 9 additions & 7 deletions lib/imager/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ defmodule Imager.Application do
ImagerWeb.Endpoint
]

Application.put_env(
:sentry,
:dsn,
Application.get_env(:imager, :sentry_dsn)
)
if dsn = Application.get_env(:imager, :sentry_dsn) do
Application.put_env(:sentry, :dsn, dsn)
Application.put_env(:sentry, :included_environments, [:prod])
end

{:ok, _} = Logger.add_backend(Sentry.LoggerBackend)
_ = Application.ensure_all_started(:sentry)

prometheus()

Expand All @@ -31,9 +31,11 @@ defmodule Imager.Application do
end

defp exec_app do
default = if System.get_pid() == "1", do: "nobody"

opts =
with {:ok, name} when not is_nil(name) <-
Application.fetch_env(:imager, :user) do
with name when not is_nil(name) <-
Application.get_env(:imager, :user, default) do
[user: String.to_charlist(name)]
else
_ -> []
Expand Down
4 changes: 2 additions & 2 deletions lib/imager/config/store_transform.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ defmodule Imager.Config.StoreTransform do

require Logger

@reserved ~w[health]
@types ~w[S3 Local Blackhole]

def transform(:stores, entries) do
Expand All @@ -16,7 +15,8 @@ defmodule Imager.Config.StoreTransform do
{:ok, cache} <- get_cache(values) do
path = to_string(path)

if path in @reserved, do: raise("'#{path}' is reserved name")
if String.starts_with?(path, "_"),
do: raise("'#{path}' cannot start with underscore")

if String.contains?(path, "/"),
do: raise("'#{path}' cannot contain '/'")
Expand Down
5 changes: 1 addition & 4 deletions lib/imager_web/plug/metrics_exporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ defmodule ImagerWeb.Plug.MetricsExporter do
opts =
opts
|> Keyword.merge(Application.get_env(:imager, :prometheus, []))
|> Keyword.put_new(:endpoint, "/metrics")
|> Keyword.update(:format, :prometheus_text_format, &parse_format/1)

endpoint = Keyword.fetch!(opts, :endpoint)

case conn.request_path do
^endpoint -> send_stats(conn, opts)
"/__metrics" -> send_stats(conn, opts)
_ -> conn
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/imager_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule ImagerWeb.Router do
scope "/", ImagerWeb.Controllers do
pipe_through(:api)

get("/health", Health, :get)
get("/__health", Health, :get)

get("/:store/*path", Image, :get)
end
Expand Down
13 changes: 10 additions & 3 deletions test/imager/config/store_transform_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule Imager.Config.StoreTransformTest do

property "returns map with stringified atoms" do
check all path <- atom(:alphanumeric),
not String.starts_with?("#{path}", "_"),
config <- store_config() do
assert Map.has_key?(
Subject.transform(:stores, %{path => config}),
Expand Down Expand Up @@ -73,9 +74,15 @@ defmodule Imager.Config.StoreTransformTest do
end
end

test "raises when uses reserved name" do
assert_raise RuntimeError, "'health' is reserved name", fn ->
Subject.transform(:stores, %{health: %{type: "Blackhole"}})
property "raises when uses reserved name" do
check all name <- string(:alphanumeric) do
assert_raise RuntimeError,
"'_#{name}' cannot start with underscore",
fn ->
Subject.transform(:stores, %{
:"_#{name}" => %{type: "Blackhole"}
})
end
end
end

Expand Down
3 changes: 2 additions & 1 deletion test/imager_web/controllers/health_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule ImagerWeb.Controllers.HealthTest do
use ImagerWeb.ConnCase, async: true

test "returns 200 on existing file", %{conn: conn} do
assert %{"status" => "pass"} = conn |> get("/health") |> json_response(200)
assert %{"status" => "pass"} =
conn |> get("/__health") |> json_response(200)
end
end
8 changes: 4 additions & 4 deletions test/imager_web/plug/metrics_exporter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ defmodule ImagerWeb.Plug.MetricsExporterTest do
end

property "non-metric endpoints are passed through" do
opts = Subject.init(endpoint: "/metric")
opts = Subject.init([])

check all path <- path(),
method <- http_method(),
path != "/metric" do
path != "/__metrics" do
conn = conn(method, path)

assert conn == Subject.call(conn, opts)
end
end

test "metric endpoint return 200" do
opts = Subject.init(endpoint: "/metric")
conn = conn(:get, "/metric")
opts = Subject.init([])
conn = conn(:get, "/__metrics")

assert {200, _, _} = sent_resp(Subject.call(conn, opts))
end
Expand Down

0 comments on commit fb1085e

Please sign in to comment.