Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mirrors to download the agent #671

Merged
merged 1 commit into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changesets/add-mirrors-to-download-the-agent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
bump: "patch"
---

Add mirrors to download the agent
31 changes: 19 additions & 12 deletions agent.exs
Original file line number Diff line number Diff line change
@@ -1,55 +1,62 @@
defmodule Appsignal.Agent do
def version, do: "0318770"

def mirrors do
[
"https://appsignal-agent-releases.global.ssl.fastly.net",
"https://d135dj0rjqvssy.cloudfront.net",
]
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@luismiramirez have we updated the appsignal-agent's ship task to add these mirrors to the agent.exs template?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed with team in call. I'll pick up this change.


def triples do
%{
"x86_64-darwin" => %{
checksum: "7b17cb76edc58ea54381455f74934d08efbfb7807007e97ae01f751101da8b50",
download_url: "https://appsignal-agent-releases.global.ssl.fastly.net/0318770/appsignal-x86_64-darwin-all-static.tar.gz"
filename: "appsignal-x86_64-darwin-all-static.tar.gz"
},
"universal-darwin" => %{
checksum: "7b17cb76edc58ea54381455f74934d08efbfb7807007e97ae01f751101da8b50",
download_url: "https://appsignal-agent-releases.global.ssl.fastly.net/0318770/appsignal-x86_64-darwin-all-static.tar.gz"
filename: "appsignal-x86_64-darwin-all-static.tar.gz"
},
"aarch64-darwin" => %{
checksum: "d90172492ccf83527696fcd0353796d3d0d4e1704ff986ae90a774a7f11a85a2",
download_url: "https://appsignal-agent-releases.global.ssl.fastly.net/0318770/appsignal-aarch64-darwin-all-static.tar.gz"
filename: "appsignal-aarch64-darwin-all-static.tar.gz"
},
"arm64-darwin" => %{
checksum: "d90172492ccf83527696fcd0353796d3d0d4e1704ff986ae90a774a7f11a85a2",
download_url: "https://appsignal-agent-releases.global.ssl.fastly.net/0318770/appsignal-aarch64-darwin-all-static.tar.gz"
filename: "appsignal-aarch64-darwin-all-static.tar.gz"
},
"arm-darwin" => %{
checksum: "d90172492ccf83527696fcd0353796d3d0d4e1704ff986ae90a774a7f11a85a2",
download_url: "https://appsignal-agent-releases.global.ssl.fastly.net/0318770/appsignal-aarch64-darwin-all-static.tar.gz"
filename: "appsignal-aarch64-darwin-all-static.tar.gz"
},
"aarch64-linux" => %{
checksum: "bef06f27d98cc1afc30b2d8fa23af69bd0206407b0d8d2f052278de3b8c5f2b5",
download_url: "https://appsignal-agent-releases.global.ssl.fastly.net/0318770/appsignal-aarch64-linux-all-static.tar.gz"
filename: "appsignal-aarch64-linux-all-static.tar.gz"
},
"i686-linux" => %{
checksum: "7e0aa277c4e49ebe1b805e9db615544c5488a23d8b439867a2a6357d37c897bc",
download_url: "https://appsignal-agent-releases.global.ssl.fastly.net/0318770/appsignal-i686-linux-all-static.tar.gz"
filename: "appsignal-i686-linux-all-static.tar.gz"
},
"x86-linux" => %{
checksum: "7e0aa277c4e49ebe1b805e9db615544c5488a23d8b439867a2a6357d37c897bc",
download_url: "https://appsignal-agent-releases.global.ssl.fastly.net/0318770/appsignal-i686-linux-all-static.tar.gz"
filename: "appsignal-i686-linux-all-static.tar.gz"
},
"x86_64-linux" => %{
checksum: "e918e24ff1f86d939b8f571506b11f2890d81c741de56cb06ac81b5dcc3f70e1",
download_url: "https://appsignal-agent-releases.global.ssl.fastly.net/0318770/appsignal-x86_64-linux-all-static.tar.gz"
filename: "appsignal-x86_64-linux-all-static.tar.gz"
},
"x86_64-linux-musl" => %{
checksum: "1a90421519d7860bf41d606866252cc7f1cb828a7efb9622045ee4f04d757ebd",
download_url: "https://appsignal-agent-releases.global.ssl.fastly.net/0318770/appsignal-x86_64-linux-musl-all-static.tar.gz"
filename: "appsignal-x86_64-linux-musl-all-static.tar.gz"
},
"x86_64-freebsd" => %{
checksum: "22cdd8e44e60dd69003d28ea95c994c27d2223a3872c541c966f32dbea3b0462",
download_url: "https://appsignal-agent-releases.global.ssl.fastly.net/0318770/appsignal-x86_64-freebsd-all-static.tar.gz"
filename: "appsignal-x86_64-freebsd-all-static.tar.gz"
},
"amd64-freebsd" => %{
checksum: "22cdd8e44e60dd69003d28ea95c994c27d2223a3872c541c966f32dbea3b0462",
download_url: "https://appsignal-agent-releases.global.ssl.fastly.net/0318770/appsignal-x86_64-freebsd-all-static.tar.gz"
filename: "appsignal-x86_64-freebsd-all-static.tar.gz"
},
}
end
Expand Down
49 changes: 28 additions & 21 deletions mix_helpers.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ defmodule Mix.Appsignal.Helper do
@os Application.get_env(:appsignal, :os, :os)
@system Application.get_env(:appsignal, :mix_system, System)

@max_retries 5

@proxy_env_vars [
"APPSIGNAL_HTTP_PROXY",
"https_proxy",
Expand Down Expand Up @@ -130,27 +128,27 @@ defmodule Mix.Appsignal.Helper do

defp download_package(arch_config, report) do
version = Appsignal.Agent.version()
filename = arch_config[:filename]

File.mkdir_p!(priv_dir())
clean_up_extension_files()
url = arch_config[:download_url]
report = merge_report(report, %{download: %{download_url: url}})

filename = Path.join(tmp_dir(), "appsignal-agent-#{version}.tar.gz")
local_filename = Path.join(tmp_dir(), "appsignal-agent-#{version}.tar.gz")

case File.exists?(filename) do
case File.exists?(local_filename) do
true ->
{:ok, {filename, merge_report(report, %{build: %{source: "cached_in_tmp_dir"}})}}
{:ok, {local_filename, merge_report(report, %{build: %{source: "cached_in_tmp_dir"}})}}

false ->
Mix.shell().info("Downloading agent release from #{url}")
Mix.shell().info("Downloading agent release")
:application.ensure_all_started(:hackney)

case do_download_file!(url, filename, @max_retries) do
:ok ->
{:ok, {filename, report}}
case do_download_file!(filename, local_filename, Appsignal.Agent.mirrors()) do
{:ok, url} ->
{:ok, {local_filename, merge_report(report, %{download: %{download_url: url}})}}

error ->
{:error, {error, report}}
{error, url} ->
{:error, {error, merge_report(report, %{download: %{download_url: url}})}}
end
end
end
Expand All @@ -174,13 +172,25 @@ defmodule Mix.Appsignal.Helper do
end
end

defp do_download_file!(url, filename, retries \\ 0)
defp do_download_file!(filename, local_filename, mirrors) do
Enum.reduce_while(mirrors, 1, fn mirror, acc ->
version = Appsignal.Agent.version()
url = build_download_url(mirror, version, filename)
result = do_download_file!(url, local_filename)

if result == :ok or length(mirrors) == acc do
{:halt, {result, url}}
else
{:cont, acc + 1}
end
end)
end

defp do_download_file!(url, filename, 0) do
defp do_download_file!(url, local_filename) do
case :hackney.request(:get, url, [], "", download_options()) do
{:ok, 200, _, reference} ->
case :hackney.body(reference) do
{:ok, body} -> File.write(filename, body)
{:ok, body} -> File.write(local_filename, body)
{:error, reason} -> {:error, reason}
end

Expand All @@ -189,11 +199,8 @@ defmodule Mix.Appsignal.Helper do
end
end

defp do_download_file!(url, filename, retries) do
case do_download_file!(url, filename) do
:ok -> :ok
_ -> do_download_file!(url, filename, retries - 1)
end
defp build_download_url(mirror, version, filename) do
Enum.join([mirror, version, filename], "/")
end

defp download_options do
Expand Down