1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.Instance do
11 @shortdoc "Manages Pleroma instance"
12 @moduledoc File.read!("docs/administration/CLI_tasks/instance.md")
14 def run(["gen" | rest]) do
23 instance_name: :string,
25 notify_email: :string,
32 db_configurable: :string,
45 [config_path, psql_path] = [
46 Keyword.get(options, :output, "config/generated_config.exs"),
47 Keyword.get(options, :output_psql, "config/setup_db.psql")
50 will_overwrite = Enum.filter(paths, &File.exists?/1)
51 proceed? = Enum.empty?(will_overwrite) or Keyword.get(options, :force, false)
59 "What domain will your instance use? (e.g pleroma.soykaf.com)"
68 "What is the name of your instance? (e.g. The Corndog Emporium)",
72 email = get_option(options, :admin_email, "What is your admin email address?")
78 "What email address do you want to use for sending email notifications?",
86 "Do you want search engines to index your site? (y/n)",
94 "Do you want to store the configuration in the database (allows controlling it from admin-fe)? (y/n)",
98 dbhost = get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
100 dbname = get_option(options, :dbname, "What is the name of your database?", "pleroma")
106 "What is the user used to connect to your database?",
114 "What is the password used to connect to your database?",
115 :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64),
123 "Would you like to use RUM indices?",
131 "What port will the app listen to (leave it if you are using the default setup with nginx)?",
139 "What ip will the app listen to (leave it if you are using the default setup with nginx)?",
147 "What directory should media uploads go in (when using the local uploader)?",
148 Pleroma.Config.get([Pleroma.Uploaders.Local, :uploads])
156 "What directory should custom public files be read from (custom emojis, frontend bundle overrides, robots.txt, etc.)?",
157 Pleroma.Config.get([:instance, :static_dir])
161 Config.put([:instance, :static_dir], static_dir)
163 secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
164 jwt_secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
165 signing_salt = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
166 {web_push_public_key, web_push_private_key} = :crypto.generate_key(:ecdh, :prime256v1)
167 template_dir = Application.app_dir(:pleroma, "priv") <> "/templates"
171 template_dir <> "/sample_config.eex",
175 notify_email: notify_email,
182 jwt_secret: jwt_secret,
183 signing_salt: signing_salt,
184 web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
185 web_push_private_key: Base.url_encode64(web_push_private_key, padding: false),
186 db_configurable?: db_configurable?,
187 static_dir: static_dir,
188 uploads_dir: uploads_dir,
189 rum_enabled: rum_enabled,
190 listen_ip: listen_ip,
191 listen_port: listen_port
196 template_dir <> "/sample_psql.eex",
200 rum_enabled: rum_enabled
203 shell_info("Writing config to #{config_path}.")
205 File.write(config_path, result_config)
206 shell_info("Writing the postgres script to #{psql_path}.")
207 File.write(psql_path, result_psql)
209 write_robots_txt(static_dir, indexable, template_dir)
212 "\n All files successfully written! Refer to the installation instructions for your platform for next steps."
215 if db_configurable? do
217 " Please transfer your config to the database after running database migrations. Refer to \"Transfering the config to/from the database\" section of the docs for more information."
222 "The task would have overwritten the following files:\n" <>
223 (Enum.map(paths, &"- #{&1}\n") |> Enum.join("")) <>
224 "Rerun with `--force` to overwrite them."
229 defp write_robots_txt(static_dir, indexable, template_dir) do
232 template_dir <> "/robots_txt.eex",
236 unless File.exists?(static_dir) do
237 File.mkdir_p!(static_dir)
240 robots_txt_path = Path.join(static_dir, "robots.txt")
242 if File.exists?(robots_txt_path) do
243 File.cp!(robots_txt_path, "#{robots_txt_path}.bak")
244 shell_info("Backing up existing robots.txt to #{robots_txt_path}.bak")
247 File.write(robots_txt_path, robots_txt)
248 shell_info("Writing #{robots_txt_path}.")