diff --git a/Dockerfile b/Dockerfile index 14e1b3ed0..6d9369f24 100644 --- a/Dockerfile +++ b/Dockerfile @@ -56,7 +56,7 @@ RUN ls -al COPY --from=node /app/build ./priv/static -RUN mix release +RUN mix do agent.chart, release FROM ${TOOLS_IMAGE} as tools @@ -74,6 +74,8 @@ ENV CLI_VERSION=v0.12.1 # renovate: datasource=github-tags depName=kubernetes/kubernetes # ENV KUBECTL_VERSION=v1.31.3 +COPY AGENT_VERSION AGENT_VERSION + RUN apk update && apk add curl wget unzip RUN curl -L https://github.com/pluralsh/plural-cli/releases/download/${CLI_VERSION}/plural-cli_${CLI_VERSION#v}_Linux_${TARGETARCH}.tar.gz | tar xvz plural && \ mv plural /usr/local/bin/plural && \ diff --git a/config/test.exs b/config/test.exs index 2bb98d581..0734b7cb1 100644 --- a/config/test.exs +++ b/config/test.exs @@ -16,14 +16,13 @@ config :console, ConsoleWeb.Endpoint, http: [port: 4002], server: false -config :logger, level: :warning +config :logger, level: :error config :goth, disabled: true config :piazza_core, aes_key: "1HdFP1DuK7xkkcEBne41yAwUY8NSfJnYfGVylYYCS2U=" - secrets_path = __ENV__.file |> Path.dirname() |> Path.join("secrets") binfile = fn p -> __ENV__.file diff --git a/lib/console/deployments/settings.ex b/lib/console/deployments/settings.ex index 846908e38..197bdcb65 100644 --- a/lib/console/deployments/settings.ex +++ b/lib/console/deployments/settings.ex @@ -10,6 +10,7 @@ defmodule Console.Deployments.Settings do @agent_vsn File.read!("AGENT_VERSION") |> String.trim() @kube_vsn File.read!("KUBE_VERSION") |> String.trim() + @agent_chart Path.join(:code.priv_dir(:console), "agent-chart.tgz") @cache_adapter Console.conf(:cache_adapter) @ttl :timer.minutes(45) @@ -64,6 +65,12 @@ defmodule Console.Deployments.Settings do @spec agent_vsn() :: binary def agent_vsn(), do: @agent_vsn + @doc """ + Local file containing the valid, working agent chart tarball + """ + @spec agent_chart() :: binary + def agent_chart(), do: @agent_chart + @doc "same as fetch/0 but always reads from db" def fetch_consistent() do Console.Repo.get_by(DeploymentSettings, name: "global") diff --git a/lib/console_web/controllers/git_controller.ex b/lib/console_web/controllers/git_controller.ex index 29ad93eb7..7347ce1a6 100644 --- a/lib/console_web/controllers/git_controller.ex +++ b/lib/console_web/controllers/git_controller.ex @@ -23,6 +23,11 @@ defmodule ConsoleWeb.GitController do end end + def agent_chart(conn, _) do + path = Console.Deployments.Settings.agent_chart() + chunk_send_tar(conn, File.open!(path, [:raw])) + end + def stack_tarball(conn, %{"id" => run_id}) do with %Cluster{} = cluster <- ConsoleWeb.Plugs.Token.get_cluster(conn), {:ok, run} <- Stacks.authorized(run_id, cluster), diff --git a/lib/console_web/router.ex b/lib/console_web/router.ex index 12337423d..c9fad54a5 100644 --- a/lib/console_web/router.ex +++ b/lib/console_web/router.ex @@ -25,6 +25,7 @@ defmodule ConsoleWeb.Router do pipe_through [:api] scope "/v1", ConsoleWeb do + get "/agent/chart", GitController, :agent_chart post "/webhooks/observability/:type/:id", WebhookController, :observability post "/webhooks/:type/:id", WebhookController, :scm diff --git a/lib/mix/tasks/agent.chart.ex b/lib/mix/tasks/agent.chart.ex new file mode 100644 index 000000000..2d14d7e04 --- /dev/null +++ b/lib/mix/tasks/agent.chart.ex @@ -0,0 +1,21 @@ +defmodule Mix.Tasks.Agent.Chart do + use Mix.Task + alias Console.Deployments.Settings + + @deps ~w(logger req)a + + def run(_) do + Enum.each(@deps, &Application.ensure_all_started/1) + Logger.configure(level: :error) + + file = Settings.agent_chart() + + Settings.agent_vsn() + |> String.trim_leading("v") + |> agent_chart_url() + |> Req.get!(into: File.stream!(file)) + end + + defp agent_chart_url(vsn), + do: "https://github.com/pluralsh/deployment-operator/releases/download/agent-v#{vsn}/deployment-operator-#{vsn}.tgz" +end diff --git a/mix.exs b/mix.exs index caecef53b..eca7c04f6 100644 --- a/mix.exs +++ b/mix.exs @@ -184,7 +184,7 @@ defmodule Console.MixProject do [ "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"], "ecto.reset": ["ecto.drop", "ecto.setup"], - test: ["ecto.create --quiet", "ecto.migrate", "elasticsearch.down", "elasticsearch.up", "test"] + test: ["ecto.create --quiet", "agent.chart", "ecto.migrate", "elasticsearch.down", "elasticsearch.up", "test"] ] end end diff --git a/priv/agent-chart.tgz b/priv/agent-chart.tgz new file mode 100644 index 000000000..1abfc3337 Binary files /dev/null and b/priv/agent-chart.tgz differ diff --git a/test/console_web/controllers/git_controller_test.exs b/test/console_web/controllers/git_controller_test.exs index 150735344..1310e3850 100644 --- a/test/console_web/controllers/git_controller_test.exs +++ b/test/console_web/controllers/git_controller_test.exs @@ -1,6 +1,14 @@ defmodule ConsoleWeb.GitControllerTest do use ConsoleWeb.ConnCase, async: false + describe "agent_chart/2" do + test "it can download the current valid agent chart", %{conn: conn} do + conn + |> get("/ext/v1/agent/chart") + |> response(200) + end + end + describe "#tarball/2" do test "it will download git content for valid deploy tokens", %{conn: conn} do git = insert(:git_repository, url: "https://github.com/pluralsh/console.git")