~ihabunek/triglav

bb5ba4b48e74d6e285d45715dca5af59576acff1 — Ivan Habunek 1 year, 20 days ago 2743a4b
Release related improvements
4 files changed, 50 insertions(+), 25 deletions(-)

M Makefile
M lib/triglav/application.ex
M lib/triglav/import/osm.ex
M lib/triglav/release.ex
M Makefile => Makefile +3 -1
@@ 9,4 9,6 @@ release:
	MIX_ENV=prod mix release

deploy:
	rsync -av _build/prod bezdomni:projects/triglav
\ No newline at end of file
	rsync --archive --compress --delete --links --verbose _build/prod bezdomni:projects/triglav
	# TODO: run on remote server
	# ./triglav eval "Triglav.Release.migrate"

M lib/triglav/application.ex => lib/triglav/application.ex +13 -16
@@ 1,26 1,23 @@
defmodule Triglav.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  def start(_type, _args) do
    children = [
      # Start the Ecto repository
      Triglav.Repo,
      # Start the Telemetry supervisor
      TriglavWeb.Telemetry,
      # Start the PubSub system
      {Phoenix.PubSub, name: Triglav.PubSub},
      # Start the Endpoint (http/https)
      TriglavWeb.Endpoint
      # Start a worker by calling: Triglav.Worker.start_link(arg)
      # {Triglav.Worker, arg}
    ]
    minimal = Application.get_env(:triglav, :repo_only, false)

    children =
      if minimal do
        [Triglav.Repo]
      else
        [
          Triglav.Repo,
          TriglavWeb.Telemetry,
          {Phoenix.PubSub, name: Triglav.PubSub},
          TriglavWeb.Endpoint,
        ]
      end

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: Triglav.Supervisor]
    Supervisor.start_link(children, opts)
  end

M lib/triglav/import/osm.ex => lib/triglav/import/osm.ex +33 -7
@@ 24,16 24,29 @@ defmodule Triglav.Import.Osm do
    IO.puts("Remote data: seq ##{web_seq} from #{web_ts}\n")

    if is_nil(db_seq) or web_seq > db_seq or force do
      url = "https://download.geofabrik.de/europe/croatia-latest.osm.pbf"
      IO.puts("Downloading #{url}")
      download(url, "croatia-latest.osm.pbf")

      System.cmd("osm2pgsql", [
      download("https://download.geofabrik.de/europe/croatia-latest.osm.pbf")
      download("https://download.geofabrik.de/europe/croatia-latest.osm.pbf.md5")

      with {_, 0} <- System.cmd("md5sum", ["--check", "croatia-latest.osm.pbf.md5"]) do
        IO.puts("Checksum OK.")
      else
        _ -> raise "Checksum validation failed."
      end

      database =
        :triglav
        |> Application.fetch_env!(Triglav.Repo)
        |> Keyword.fetch!(:url)
        |> URI.parse()
        |> Map.fetch!(:path)
        |> String.trim_leading("/")

      {_, 0} = System.cmd("osm2pgsql", [
        "--hstore-all",
        "--create",
        "--slim",
        "--database",
        "triglav_dev",
        database,
        "croatia-latest.osm.pbf"
      ])



@@ 56,6 69,7 @@ defmodule Triglav.Import.Osm do

      IO.puts("Deleting PBF archive...")
      File.rm("croatia-latest.osm.pbf")
      File.rm("croatia-latest.osm.pbf.md5")

      IO.puts("Done.")
    else


@@ 90,7 104,19 @@ defmodule Triglav.Import.Osm do
    to_string(body)
  end

  defp download(url, target) do
  defp download(url) do
    IO.puts("Downloading #{url}")

    target =
      url
      |> URI.parse()
      |> Map.fetch!(:path)
      |> Path.basename()

    if File.exists?(target) do
      File.rm(target)
    end

    {:ok, :saved_to_file} =
      :httpc.request(:get, {to_charlist(url), []}, [], stream: to_charlist(target))
  end

M lib/triglav/release.ex => lib/triglav/release.ex +1 -1
@@ 29,7 29,7 @@ defmodule Triglav.Release do

  defp start_app do
    load_app()
    Application.put_env(@app, :minimal, true)
    Application.put_env(@app, :repo_only, true)
    Application.ensure_all_started(@app)
  end
end