1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.Instance do
9 @shortdoc "Manages Pleroma instance"
11 def run(["gen" | rest]) do
20 instance_name: :string,
22 notify_email: :string,
29 db_configurable: :string,
42 [config_path, psql_path] = [
43 Keyword.get(options, :output, "config/generated_config.exs"),
44 Keyword.get(options, :output_psql, "config/setup_db.psql")
47 will_overwrite = Enum.filter(paths, &File.exists?/1)
48 proceed? = Enum.empty?(will_overwrite) or Keyword.get(options, :force, false)
56 "What domain will your instance use? (e.g pleroma.soykaf.com)"
65 "What is the name of your instance? (e.g. Pleroma/Soykaf)"
68 email = get_option(options, :admin_email, "What is your admin email address?")
74 "What email address do you want to use for sending email notifications?",
82 "Do you want search engines to index your site? (y/n)",
90 "Do you want to store the configuration in the database (allows controlling it from admin-fe)? (y/n)",
94 dbhost = get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
96 dbname = get_option(options, :dbname, "What is the name of your database?", "pleroma")
102 "What is the user used to connect to your database?",
110 "What is the password used to connect to your database?",
111 :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64),
119 "Would you like to use RUM indices?",
127 "What port will the app listen to (leave it if you are using the default setup with nginx)?",
135 "What ip will the app listen to (leave it if you are using the default setup with nginx)?",
143 "What directory should media uploads go in (when using the local uploader)?",
144 Pleroma.Config.get([Pleroma.Uploaders.Local, :uploads])
151 "What directory should custom public files be read from (custom emojis, frontend bundle overrides, robots.txt, etc.)?",
152 Pleroma.Config.get([:instance, :static_dir])
155 secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
156 jwt_secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
157 signing_salt = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
158 {web_push_public_key, web_push_private_key} = :crypto.generate_key(:ecdh, :prime256v1)
159 template_dir = Application.app_dir(:pleroma, "priv") <> "/templates"
163 template_dir <> "/sample_config.eex",
167 notify_email: notify_email,
174 jwt_secret: jwt_secret,
175 signing_salt: signing_salt,
176 web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
177 web_push_private_key: Base.url_encode64(web_push_private_key, padding: false),
178 db_configurable?: db_configurable?,
179 static_dir: static_dir,
180 uploads_dir: uploads_dir,
181 rum_enabled: rum_enabled,
182 listen_ip: listen_ip,
183 listen_port: listen_port
188 template_dir <> "/sample_psql.eex",
192 rum_enabled: rum_enabled
195 shell_info("Writing config to #{config_path}.")
197 File.write(config_path, result_config)
198 shell_info("Writing the postgres script to #{psql_path}.")
199 File.write(psql_path, result_psql)
201 write_robots_txt(indexable, template_dir)
204 "\n All files successfully written! Refer to the installation instructions for your platform for next steps"
208 "The task would have overwritten the following files:\n" <>
209 (Enum.map(paths, &"- #{&1}\n") |> Enum.join("")) <>
210 "Rerun with `--force` to overwrite them."
215 defp write_robots_txt(indexable, template_dir) do
218 template_dir <> "/robots_txt.eex",
222 static_dir = Pleroma.Config.get([:instance, :static_dir], "instance/static/")
224 unless File.exists?(static_dir) do
225 File.mkdir_p!(static_dir)
228 robots_txt_path = Path.join(static_dir, "robots.txt")
230 if File.exists?(robots_txt_path) do
231 File.cp!(robots_txt_path, "#{robots_txt_path}.bak")
232 shell_info("Backing up existing robots.txt to #{robots_txt_path}.bak")
235 File.write(robots_txt_path, robots_txt)
236 shell_info("Writing #{robots_txt_path}.")