diff options
Diffstat (limited to 'lib/mix')
-rw-r--r-- | lib/mix/tasks/pleroma/frontend.ex | 149 | ||||
-rw-r--r-- | lib/mix/tasks/pleroma/instance.ex | 93 | ||||
-rw-r--r-- | lib/mix/tasks/pleroma/user.ex | 2 |
3 files changed, 241 insertions, 3 deletions
diff --git a/lib/mix/tasks/pleroma/frontend.ex b/lib/mix/tasks/pleroma/frontend.ex new file mode 100644 index 000000000..c7de5d217 --- /dev/null +++ b/lib/mix/tasks/pleroma/frontend.ex @@ -0,0 +1,149 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Mix.Tasks.Pleroma.Frontend do + @doc """ + Scenario 1: + - clone repo to /frontends/fe/_src_tmp + - build fe + - move built files into /frontends/fe + - remove frontends/fe/_src_tmp + + Scenario 2: + - download bundle from CI to /frontends/fe/_src_tmp + - move build files + - remove tmp + + Scenario 3: + - move built files from _path to /frontends/fe + + Pleroma: + /dist + Kenoma: + /build + Fedi: + /dist + Admin: + /dist + Mastodon + /public + """ + use Mix.Task + + import Mix.Pleroma + + # alias Pleroma.Config + + @shortdoc "Manages bundled Pleroma frontends" + @moduledoc File.read!("docs/administration/CLI_tasks/frontend.md") + + @frontends %{ + "admin" => %{"project" => "pleroma/admin-fe"}, + "kenoma" => %{"project" => "lambadalambda/kenoma"}, + "mastodon" => %{"project" => "pleroma/mastofe"}, + "pleroma" => %{"project" => "pleroma/pleroma-fe"}, + "fedi" => %{"project" => "dockyard/fedi-fe"} + } + @known_frontends Map.keys(@frontends) + + @ref_local "__local__" + + def run(["install", "none" | _args]) do + shell_info("Skipping frontend installation because none was requested") + end + + def run(["install", unknown_fe | _args]) when unknown_fe not in @known_frontends do + shell_error( + "Frontend \"#{unknown_fe}\" is not known. Known frontends are: #{ + Enum.join(@known_frontends, ", ") + }" + ) + end + + def run(["install", frontend | args]) do + log_level = Logger.level() + Logger.configure(level: :warn) + {:ok, _} = Application.ensure_all_started(:pleroma) + + {options, [], []} = + OptionParser.parse( + args, + strict: [ + ref: :string, + path: :string, + develop: :boolean + ] + ) + + path = options[:path] + ref = local_path_frontend_ref(path) + + dest = + Path.join([ + Pleroma.Config.get!([:instance, :static_dir]), + "frontends", + frontend, + ref + ]) + + shell_info("Installing frontend #{frontend} (#{ref}) from local path") + + install_bundle(frontend, path, dest) + shell_info("Frontend #{frontend} (#{ref}) installed to #{dest}") + + Logger.configure(level: log_level) + end + + defp local_path_frontend_ref(path) do + path + |> Path.join("package.json") + |> File.read() + |> case do + {:ok, bin} -> + bin + |> Jason.decode!() + |> Map.get("version", @ref_local) + + _ -> + @ref_local + end + end + + defp post_install("mastodon", path) do + File.rename!("#{path}/assets/sw.js", "#{path}/sw.js") + + {:ok, files} = File.ls(path) + + Enum.each(files, fn file -> + with false <- file in ~w(packs sw.js) do + [path, file] + |> Path.join() + |> File.rm_rf!() + end + end) + end + + defp post_install(_frontend, _path) do + :ok + end + + defp install_bundle(frontend, source, dest) do + from = + case frontend do + "mastodon" -> + "public" + + "kenoma" -> + "build" + + _ -> + "dist" + end + + File.mkdir_p!(dest) + File.cp_r!(Path.join([source, from]), dest) + post_install(frontend, dest) + :ok + end +end diff --git a/lib/mix/tasks/pleroma/instance.ex b/lib/mix/tasks/pleroma/instance.ex index 91440b453..226ed399f 100644 --- a/lib/mix/tasks/pleroma/instance.ex +++ b/lib/mix/tasks/pleroma/instance.ex @@ -33,7 +33,14 @@ defmodule Mix.Tasks.Pleroma.Instance do uploads_dir: :string, static_dir: :string, listen_ip: :string, - listen_port: :string + listen_port: :string, + fe_primary: :string, + fe_primary_ref: :string, + fe_mastodon: :string, + fe_mastodon_ref: :string, + fe_admin: :string, + fe_admin_ref: :string, + fe_static: :string ], aliases: [ o: :output, @@ -160,6 +167,62 @@ defmodule Mix.Tasks.Pleroma.Instance do Config.put([:instance, :static_dir], static_dir) + install_fe = + case Mix.env() do + :test -> fn _, _ -> :ok end + _ -> &Mix.Tasks.Pleroma.Frontend.run(["install", &1, "--ref", &2]) + end + + fe_primary = + get_option( + options, + :fe_primary, + "Choose primary frontend for your instance (available: pleroma/kenoma/none)", + "pleroma" + ) + + fe_primary_ref = + get_frontend_ref(fe_primary !== "none", fe_primary, :fe_primary_ref, options) + + install_fe.(fe_primary, fe_primary_ref) + + enable_static_fe? = + get_option( + options, + :fe_static, + "Would you like to enable Static frontend (render profiles and posts using server-generated HTML that is viewable without using JavaScript)?", + "y" + ) === "y" + + install_mastodon_fe? = + get_option( + options, + :fe_mastodon, + "Would you like to install Mastodon frontend?", + "y" + ) === "y" + + fe_mastodon_ref = + get_frontend_ref(install_mastodon_fe?, "mastodon", :fe_mastodon_ref, options) + + if install_mastodon_fe? do + install_fe.("mastodon", fe_mastodon_ref) + end + + install_admin_fe? = + get_option( + options, + :fe_admin, + "Would you like to install Admin frontend?", + "y" + ) === "y" + + fe_admin_ref = get_frontend_ref(install_admin_fe?, "admin", :fe_admin_ref, options) + + if install_admin_fe? do + install_fe.("admin", fe_admin_ref) + end + secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64) jwt_secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64) signing_salt = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8) @@ -188,7 +251,11 @@ defmodule Mix.Tasks.Pleroma.Instance do uploads_dir: uploads_dir, rum_enabled: rum_enabled, listen_ip: listen_ip, - listen_port: listen_port + listen_port: listen_port, + fe_primary: %{"name" => fe_primary, "ref" => fe_primary_ref}, + fe_mastodon: %{"name" => "mastodon", "ref" => fe_mastodon_ref}, + fe_admin: %{"name" => "admin", "ref" => fe_admin_ref}, + enable_static_fe?: enable_static_fe? ) result_psql = @@ -247,4 +314,26 @@ defmodule Mix.Tasks.Pleroma.Instance do File.write(robots_txt_path, robots_txt) shell_info("Writing #{robots_txt_path}.") end + + defp get_frontend_ref(false, _frontend, _option_key, _options), do: "" + + defp get_frontend_ref(true, frontend, option_key, options) do + stable_pleroma? = Pleroma.Application.stable?() + + current_stable_out = + if stable_pleroma? do + "stable" + else + "develop" + end + + get_option( + options, + option_key, + "You are currently running #{current_stable_out} version of Pleroma. What version of #{ + frontend + } you want to install? (\"stable\", \"develop\" or specific ref)", + current_stable_out + ) + end end diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index 01824aa18..ce7ba9b80 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -277,7 +277,7 @@ defmodule Mix.Tasks.Pleroma.User do shell_info("Generated user invite token " <> String.replace(invite.invite_type, "_", " ")) url = - Pleroma.Web.Router.Helpers.redirect_url( + Pleroma.Web.Router.Helpers.frontend_url( Pleroma.Web.Endpoint, :registration_page, invite.token |