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.

311 lines
8.6KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 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 =
  143. get_option(
  144. options,
  145. :strip_uploads,
  146. "Do you want to strip location (GPS) data from uploaded images? (y/n)",
  147. "y"
  148. ) === "y"
  149. anonymize_uploads =
  150. get_option(
  151. options,
  152. :anonymize_uploads,
  153. "Do you want to anonymize the filenames of uploads? (y/n)",
  154. "n"
  155. ) === "y"
  156. dedupe_uploads =
  157. get_option(
  158. options,
  159. :dedupe_uploads,
  160. "Do you want to deduplicate uploaded files? (y/n)",
  161. "n"
  162. ) === "y"
  163. Config.put([:instance, :static_dir], static_dir)
  164. secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
  165. jwt_secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
  166. signing_salt = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
  167. {web_push_public_key, web_push_private_key} = :crypto.generate_key(:ecdh, :prime256v1)
  168. template_dir = Application.app_dir(:pleroma, "priv") <> "/templates"
  169. result_config =
  170. EEx.eval_file(
  171. template_dir <> "/sample_config.eex",
  172. domain: domain,
  173. port: port,
  174. email: email,
  175. notify_email: notify_email,
  176. name: name,
  177. dbhost: dbhost,
  178. dbname: dbname,
  179. dbuser: dbuser,
  180. dbpass: dbpass,
  181. secret: secret,
  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,
  192. upload_filters:
  193. upload_filters(%{
  194. strip: strip_uploads,
  195. anonymize: anonymize_uploads,
  196. dedupe: dedupe_uploads
  197. })
  198. )
  199. result_psql =
  200. EEx.eval_file(
  201. template_dir <> "/sample_psql.eex",
  202. dbname: dbname,
  203. dbuser: dbuser,
  204. dbpass: dbpass,
  205. rum_enabled: rum_enabled
  206. )
  207. shell_info("Writing config to #{config_path}.")
  208. File.write(config_path, result_config)
  209. shell_info("Writing the postgres script to #{psql_path}.")
  210. File.write(psql_path, result_psql)
  211. write_robots_txt(static_dir, indexable, template_dir)
  212. shell_info(
  213. "\n All files successfully written! Refer to the installation instructions for your platform for next steps."
  214. )
  215. if db_configurable? do
  216. shell_info(
  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."
  218. )
  219. end
  220. else
  221. shell_error(
  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."
  225. )
  226. end
  227. end
  228. defp write_robots_txt(static_dir, indexable, template_dir) do
  229. robots_txt =
  230. EEx.eval_file(
  231. template_dir <> "/robots_txt.eex",
  232. indexable: indexable
  233. )
  234. unless File.exists?(static_dir) do
  235. File.mkdir_p!(static_dir)
  236. end
  237. robots_txt_path = Path.join(static_dir, "robots.txt")
  238. if File.exists?(robots_txt_path) do
  239. File.cp!(robots_txt_path, "#{robots_txt_path}.bak")
  240. shell_info("Backing up existing robots.txt to #{robots_txt_path}.bak")
  241. end
  242. File.write(robots_txt_path, robots_txt)
  243. shell_info("Writing #{robots_txt_path}.")
  244. end
  245. defp upload_filters(filters) when is_map(filters) do
  246. enabled_filters =
  247. if filters.strip do
  248. [Pleroma.Upload.Filter.ExifTool]
  249. else
  250. []
  251. end
  252. enabled_filters =
  253. if filters.anonymize do
  254. enabled_filters ++ [Pleroma.Upload.Filter.AnonymizeFilename]
  255. else
  256. enabled_filters
  257. end
  258. enabled_filters =
  259. if filters.dedupe do
  260. enabled_filters ++ [Pleroma.Upload.Filter.Dedupe]
  261. else
  262. enabled_filters
  263. end
  264. enabled_filters
  265. end
  266. defp upload_filters(_), do: []
  267. end