From e160a73d21353451060a0ab7506a1dbc44f2b9b2 Mon Sep 17 00:00:00 2001 From: Robert Prehn <3952444+prehnRA@users.noreply.github.com> Date: Sat, 24 Apr 2021 11:44:45 -0500 Subject: [PATCH 1/2] fix: Use restore-timestamps inside docker image --- .gitlab-ci.yml | 2 -- Dockerfile | 3 +++ script/cibuild | 4 ++-- script/{ci => }/restore-timestamps | 0 4 files changed, 5 insertions(+), 4 deletions(-) rename script/{ci => }/restore-timestamps (100%) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3eb2925c..97540d9b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -38,7 +38,6 @@ build_image_for_commit: services: - name: docker:20.10-dind script: - - script/ci/restore-timestamps - docker login "https://${CI_REGISTRY}" -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD - docker pull $CI_REGISTRY_IMAGE:latest || true # This enables fast parallel builds @@ -49,7 +48,6 @@ build_image_for_commit: - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA # Pull out the built _build/prod directory so we can cache it! - docker cp `docker create $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA`:/root/app/_build/prod _build - - script/ci/restore-timestamps # If tests pass, tag the commit and update package versions deploy_to_tags: diff --git a/Dockerfile b/Dockerfile index d85377b8..b9b7066e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,6 +28,7 @@ ADD ./apps/content/mix.exs /root/app/apps/content/ ADD ./apps/core/mix.exs /root/app/apps/core/ ADD ./_build/ /root/app/_build/ ADD ./deps/ /root/app/deps/ +RUN script/restore-timestamps RUN mix deps.get RUN mix deps.compile @@ -55,4 +56,6 @@ RUN MAKE=cmake mix compile COPY --from=1 /root/app/apps/app/priv/static/ /root/app/apps/app/priv/static RUN mix phx.digest +RUN script/restore-timestamps + CMD ["mix", "phx.server"] diff --git a/script/cibuild b/script/cibuild index 40e8bc26..a44bc161 100755 --- a/script/cibuild +++ b/script/cibuild @@ -6,7 +6,7 @@ set -e mix local.hex --force mix local.rebar --force -script/ci/restore-timestamps +script/restore-timestamps mix deps.get mix ecto.create @@ -14,4 +14,4 @@ mix ecto.migrate mix test -script/ci/restore-timestamps +script/restore-timestamps diff --git a/script/ci/restore-timestamps b/script/restore-timestamps similarity index 100% rename from script/ci/restore-timestamps rename to script/restore-timestamps From 27aab5aa6396e6a909a4c44aed5548dcb9d9dcf2 Mon Sep 17 00:00:00 2001 From: Robert Prehn <3952444+prehnRA@users.noreply.github.com> Date: Sat, 24 Apr 2021 12:01:33 -0500 Subject: [PATCH 2/2] chore: Fix verbosity of restore-timestamps --- script/restore-timestamps | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/script/restore-timestamps b/script/restore-timestamps index a1938c0a..5bb8cdd9 100755 --- a/script/restore-timestamps +++ b/script/restore-timestamps @@ -4,13 +4,13 @@ defmodule TimestampRestorer do @environment System.get_env("MIX_ENV", "dev") @db_path "_build/#{@environment}/timestamp-database" - def sha_all do + def sha_all(opts \\ []) do timestamp_database = load_timestamp_database() "**/*.{ex,exs,beam}" |> Path.wildcard() |> Enum.reduce(%{}, fn filename, acc -> - {sha, timestamp} = process(filename, timestamp_database) + {sha, timestamp} = process(filename, timestamp_database, opts) Map.put(acc, sha, timestamp) end) |> write_timestamp_database() @@ -40,23 +40,27 @@ defmodule TimestampRestorer do |> (& File.write!(@db_path, &1)).() end - defp process(filename, timestamp_database) do + defp process(filename, timestamp_database, opts) do + {verbose, _opts} = Keyword.pop(opts, :verbose, false) sha = sha(filename) {:ok, %{mtime: new_timestamp}} = File.lstat(filename, time: :posix) case Map.get(timestamp_database, sha) do nil -> - :logger.debug("[NEW SHA ] #{filename}: #{new_timestamp}") + log("[NEW SHA ] #{filename}: #{new_timestamp}", verbose) timestamp when timestamp < new_timestamp -> - :logger.debug("[RESTORED ] #{filename}: #{timestamp}") + log("[RESTORED ] #{filename}: #{timestamp}", verbose) File.touch(filename, timestamp) timestamp when timestamp >= new_timestamp -> - :logger.debug("[UNCHANGED] #{filename}: #{timestamp}") + log("[UNCHANGED] #{filename}: #{timestamp}", verbose) end {sha, new_timestamp} end + defp log(_message, false), do: :ok + defp log(message, true), do: :logger.debug(message) + defp sha(filename) do hash_ref = :crypto.hash_init(:sha) @@ -71,6 +75,8 @@ defmodule TimestampRestorer do end end -{time, _result} = :timer.tc(TimestampRestorer, :sha_all, []) +{opts, _args, _errors} = OptionParser.parse(System.argv(), switches: [verbose: :boolean]) + +{time, _result} = :timer.tc(TimestampRestorer, :sha_all, [opts]) :logger.info("Restored timestamps in #{time / 1_000_000}s")