Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
9.1KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Mix.Tasks.Pleroma.Instance do
  5. use Mix.Task
  6. import Mix.Pleroma
  7. alias Pleroma.Config
  8. @shortdoc "Manages Pleroma instance"
  9. @moduledoc File.read!("docs/administration/CLI_tasks/instance.md")
  10. def run(["gen" | rest]) do
  11. {options, [], []} =
  12. OptionParser.parse(
  13. rest,
  14. strict: [
  15. force: :boolean,
  16. output: :string,
  17. output_psql: :string,
  18. domain: :string,
  19. instance_name: :string,
  20. admin_email: :string,
  21. notify_email: :string,
  22. dbhost: :string,
  23. dbname: :string,
  24. dbuser: :string,
  25. dbpass: :string,
  26. rum: :string,
  27. indexable: :string,
  28. db_configurable: :string,
  29. uploads_dir: :string,
  30. static_dir: :string,
  31. listen_ip: :string,
  32. listen_port: :string,
  33. strip_uploads: :string,
  34. anonymize_uploads: :string,
  35. dedupe_uploads: :string
  36. ],
  37. aliases: [
  38. o: :output,
  39. f: :force
  40. ]
  41. )
  42. paths =
  43. [config_path, psql_path] = [
  44. Keyword.get(options, :output, "config/generated_config.exs"),
  45. Keyword.get(options, :output_psql, "config/setup_db.psql")
  46. ]
  47. will_overwrite = Enum.filter(paths, &File.exists?/1)
  48. proceed? = Enum.empty?(will_overwrite) or Keyword.get(options, :force, false)
  49. if proceed? do
  50. [domain, port | _] =
  51. String.split(
  52. get_option(
  53. options,
  54. :domain,
  55. "What domain will your instance use? (e.g pleroma.soykaf.com)"
  56. ),
  57. ":"
  58. ) ++ [443]
  59. name =
  60. get_option(
  61. options,
  62. :instance_name,
  63. "What is the name of your instance? (e.g. The Corndog Emporium)",
  64. domain
  65. )
  66. email = get_option(options, :admin_email, "What is your admin email address?")
  67. notify_email =
  68. get_option(
  69. options,
  70. :notify_email,
  71. "What email address do you want to use for sending email notifications?",
  72. email
  73. )
  74. indexable =
  75. get_option(
  76. options,
  77. :indexable,
  78. "Do you want search engines to index your site? (y/n)",
  79. "y"
  80. ) === "y"
  81. db_configurable? =
  82. get_option(
  83. options,
  84. :db_configurable,
  85. "Do you want to store the configuration in the database (allows controlling it from admin-fe)? (y/n)",
  86. "n"
  87. ) === "y"
  88. dbhost = get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
  89. dbname = get_option(options, :dbname, "What is the name of your database?", "pleroma")
  90. dbuser =
  91. get_option(
  92. options,
  93. :dbuser,
  94. "What is the user used to connect to your database?",
  95. "pleroma"
  96. )
  97. dbpass =
  98. get_option(
  99. options,
  100. :dbpass,
  101. "What is the password used to connect to your database?",
  102. :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64),
  103. "autogenerated"
  104. )
  105. rum_enabled =
  106. get_option(
  107. options,
  108. :rum,
  109. "Would you like to use RUM indices?",
  110. "n"
  111. ) === "y"
  112. listen_port =
  113. get_option(
  114. options,
  115. :listen_port,
  116. "What port will the app listen to (leave it if you are using the default setup with nginx)?",
  117. 4000
  118. )
  119. listen_ip =
  120. get_option(
  121. options,
  122. :listen_ip,
  123. "What ip will the app listen to (leave it if you are using the default setup with nginx)?",
  124. "127.0.0.1"
  125. )
  126. uploads_dir =
  127. get_option(
  128. options,
  129. :uploads_dir,
  130. "What directory should media uploads go in (when using the local uploader)?",
  131. Config.get([Pleroma.Uploaders.Local, :uploads])
  132. )
  133. |> Path.expand()
  134. static_dir =
  135. get_option(
  136. options,
  137. :static_dir,
  138. "What directory should custom public files be read from (custom emojis, frontend bundle overrides, robots.txt, etc.)?",
  139. Config.get([:instance, :static_dir])
  140. )
  141. |> Path.expand()
  142. {strip_uploads_message, strip_uploads_default} =
  143. if Pleroma.Utils.command_available?("exiftool") do
  144. {"Do you want to strip location (GPS) data from uploaded images? This requires exiftool, it was detected as installed. (y/n)",
  145. "y"}
  146. else
  147. {"Do you want to strip location (GPS) data from uploaded images? This requires exiftool, it was detected as not installed, please install it if you answer yes. (y/n)",
  148. "n"}
  149. end
  150. strip_uploads =
  151. get_option(
  152. options,
  153. :strip_uploads,
  154. strip_uploads_message,
  155. strip_uploads_default
  156. ) === "y"
  157. anonymize_uploads =
  158. get_option(
  159. options,
  160. :anonymize_uploads,
  161. "Do you want to anonymize the filenames of uploads? (y/n)",
  162. "n"
  163. ) === "y"
  164. dedupe_uploads =
  165. get_option(
  166. options,
  167. :dedupe_uploads,
  168. "Do you want to deduplicate uploaded files? (y/n)",
  169. "n"
  170. ) === "y"
  171. Config.put([:instance, :static_dir], static_dir)
  172. secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
  173. jwt_secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
  174. signing_salt = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
  175. {web_push_public_key, web_push_private_key} = :crypto.generate_key(:ecdh, :prime256v1)
  176. template_dir = Application.app_dir(:pleroma, "priv") <> "/templates"
  177. result_config =
  178. EEx.eval_file(
  179. template_dir <> "/sample_config.eex",
  180. domain: domain,
  181. port: port,
  182. email: email,
  183. notify_email: notify_email,
  184. name: name,
  185. dbhost: dbhost,
  186. dbname: dbname,
  187. dbuser: dbuser,
  188. dbpass: dbpass,
  189. secret: secret,
  190. jwt_secret: jwt_secret,
  191. signing_salt: signing_salt,
  192. web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
  193. web_push_private_key: Base.url_encode64(web_push_private_key, padding: false),
  194. db_configurable?: db_configurable?,
  195. static_dir: static_dir,
  196. uploads_dir: uploads_dir,
  197. rum_enabled: rum_enabled,
  198. listen_ip: listen_ip,
  199. listen_port: listen_port,
  200. upload_filters:
  201. upload_filters(%{
  202. strip: strip_uploads,
  203. anonymize: anonymize_uploads,
  204. dedupe: dedupe_uploads
  205. })
  206. )
  207. result_psql =
  208. EEx.eval_file(
  209. template_dir <> "/sample_psql.eex",
  210. dbname: dbname,
  211. dbuser: dbuser,
  212. dbpass: dbpass,
  213. rum_enabled: rum_enabled
  214. )
  215. shell_info("Writing config to #{config_path}.")
  216. File.write(config_path, result_config)
  217. shell_info("Writing the postgres script to #{psql_path}.")
  218. File.write(psql_path, result_psql)
  219. write_robots_txt(static_dir, indexable, template_dir)
  220. shell_info(
  221. "\n All files successfully written! Refer to the installation instructions for your platform for next steps."
  222. )
  223. if db_configurable? do
  224. shell_info(
  225. " 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."
  226. )
  227. end
  228. else
  229. shell_error(
  230. "The task would have overwritten the following files:\n" <>
  231. (Enum.map(will_overwrite, &"- #{&1}\n") |> Enum.join("")) <>
  232. "Rerun with `--force` to overwrite them."
  233. )
  234. end
  235. end
  236. defp write_robots_txt(static_dir, indexable, template_dir) do
  237. robots_txt =
  238. EEx.eval_file(
  239. template_dir <> "/robots_txt.eex",
  240. indexable: indexable
  241. )
  242. unless File.exists?(static_dir) do
  243. File.mkdir_p!(static_dir)
  244. end
  245. robots_txt_path = Path.join(static_dir, "robots.txt")
  246. if File.exists?(robots_txt_path) do
  247. File.cp!(robots_txt_path, "#{robots_txt_path}.bak")
  248. shell_info("Backing up existing robots.txt to #{robots_txt_path}.bak")
  249. end
  250. File.write(robots_txt_path, robots_txt)
  251. shell_info("Writing #{robots_txt_path}.")
  252. end
  253. defp upload_filters(filters) when is_map(filters) do
  254. enabled_filters =
  255. if filters.strip do
  256. [Pleroma.Upload.Filter.Exiftool]
  257. else
  258. []
  259. end
  260. enabled_filters =
  261. if filters.anonymize do
  262. enabled_filters ++ [Pleroma.Upload.Filter.AnonymizeFilename]
  263. else
  264. enabled_filters
  265. end
  266. enabled_filters =
  267. if filters.dedupe do
  268. enabled_filters ++ [Pleroma.Upload.Filter.Dedupe]
  269. else
  270. enabled_filters
  271. end
  272. enabled_filters
  273. end
  274. defp upload_filters(_), do: []
  275. end