The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

434 lines
14 KiB

  1. # frozen_string_literal: true
  2. require 'optparse'
  3. require 'colorize'
  4. require 'tty-command'
  5. require 'tty-prompt'
  6. namespace :mastodon do
  7. desc 'Configure the instance for production use'
  8. task :setup do
  9. prompt = TTY::Prompt.new
  10. env = {}
  11. begin
  12. prompt.say('Your instance is identified by its domain name. Changing it afterward will break things.')
  13. env['LOCAL_DOMAIN'] = prompt.ask('Domain name:') do |q|
  14. q.required true
  15. q.modify :strip
  16. q.validate(/\A[a-z0-9\.\-]+\z/i)
  17. q.messages[:valid?] = 'Invalid domain. If you intend to use unicode characters, enter punycode here'
  18. end
  19. prompt.say "\n"
  20. prompt.say('Single user mode disables registrations and redirects the landing page to your public profile.')
  21. env['SINGLE_USER_MODE'] = prompt.yes?('Do you want to enable single user mode?', default: false)
  22. %w(SECRET_KEY_BASE OTP_SECRET).each do |key|
  23. env[key] = SecureRandom.hex(64)
  24. end
  25. vapid_key = Webpush.generate_key
  26. env['VAPID_PRIVATE_KEY'] = vapid_key.private_key
  27. env['VAPID_PUBLIC_KEY'] = vapid_key.public_key
  28. prompt.say "\n"
  29. using_docker = prompt.yes?('Are you using Docker to run Mastodon?')
  30. db_connection_works = false
  31. prompt.say "\n"
  32. loop do
  33. env['DB_HOST'] = prompt.ask('PostgreSQL host:') do |q|
  34. q.required true
  35. q.default using_docker ? 'db' : '/var/run/postgresql'
  36. q.modify :strip
  37. end
  38. env['DB_PORT'] = prompt.ask('PostgreSQL port:') do |q|
  39. q.required true
  40. q.default 5432
  41. q.convert :int
  42. end
  43. env['DB_NAME'] = prompt.ask('Name of PostgreSQL database:') do |q|
  44. q.required true
  45. q.default using_docker ? 'postgres' : 'mastodon_production'
  46. q.modify :strip
  47. end
  48. env['DB_USER'] = prompt.ask('Name of PostgreSQL user:') do |q|
  49. q.required true
  50. q.default using_docker ? 'postgres' : 'mastodon'
  51. q.modify :strip
  52. end
  53. env['DB_PASS'] = prompt.ask('Password of PostgreSQL user:') do |q|
  54. q.echo false
  55. end
  56. # The chosen database may not exist yet. Connect to default database
  57. # to avoid "database does not exist" error.
  58. db_options = {
  59. adapter: :postgresql,
  60. database: 'postgres',
  61. host: env['DB_HOST'],
  62. port: env['DB_PORT'],
  63. user: env['DB_USER'],
  64. password: env['DB_PASS'],
  65. }
  66. begin
  67. ActiveRecord::Base.establish_connection(db_options)
  68. ActiveRecord::Base.connection
  69. prompt.ok 'Database configuration works! 🎆'
  70. db_connection_works = true
  71. break
  72. rescue StandardError => e
  73. prompt.error 'Database connection could not be established with this configuration, try again.'
  74. prompt.error e.message
  75. break unless prompt.yes?('Try again?')
  76. end
  77. end
  78. prompt.say "\n"
  79. loop do
  80. env['REDIS_HOST'] = prompt.ask('Redis host:') do |q|
  81. q.required true
  82. q.default using_docker ? 'redis' : 'localhost'
  83. q.modify :strip
  84. end
  85. env['REDIS_PORT'] = prompt.ask('Redis port:') do |q|
  86. q.required true
  87. q.default 6379
  88. q.convert :int
  89. end
  90. env['REDIS_PASSWORD'] = prompt.ask('Redis password:') do |q|
  91. q.required false
  92. q.default nil
  93. q.modify :strip
  94. end
  95. redis_options = {
  96. host: env['REDIS_HOST'],
  97. port: env['REDIS_PORT'],
  98. password: env['REDIS_PASSWORD'],
  99. driver: :hiredis,
  100. }
  101. begin
  102. redis = Redis.new(redis_options)
  103. redis.ping
  104. prompt.ok 'Redis configuration works! 🎆'
  105. break
  106. rescue StandardError => e
  107. prompt.error 'Redis connection could not be established with this configuration, try again.'
  108. prompt.error e.message
  109. break unless prompt.yes?('Try again?')
  110. end
  111. end
  112. prompt.say "\n"
  113. if prompt.yes?('Do you want to store uploaded files on the cloud?', default: false)
  114. case prompt.select('Provider', ['Amazon S3', 'Wasabi', 'Minio'])
  115. when 'Amazon S3'
  116. env['S3_ENABLED'] = 'true'
  117. env['S3_PROTOCOL'] = 'https'
  118. env['S3_BUCKET'] = prompt.ask('S3 bucket name:') do |q|
  119. q.required true
  120. q.default "files.#{env['LOCAL_DOMAIN']}"
  121. q.modify :strip
  122. end
  123. env['S3_REGION'] = prompt.ask('S3 region:') do |q|
  124. q.required true
  125. q.default 'us-east-1'
  126. q.modify :strip
  127. end
  128. env['S3_HOSTNAME'] = prompt.ask('S3 hostname:') do |q|
  129. q.required true
  130. q.default 's3-us-east-1.amazonaws.com'
  131. q.modify :strip
  132. end
  133. env['AWS_ACCESS_KEY_ID'] = prompt.ask('S3 access key:') do |q|
  134. q.required true
  135. q.modify :strip
  136. end
  137. env['AWS_SECRET_ACCESS_KEY'] = prompt.ask('S3 secret key:') do |q|
  138. q.required true
  139. q.modify :strip
  140. end
  141. when 'Wasabi'
  142. env['S3_ENABLED'] = 'true'
  143. env['S3_PROTOCOL'] = 'https'
  144. env['S3_REGION'] = 'us-east-1'
  145. env['S3_HOSTNAME'] = 's3.wasabisys.com'
  146. env['S3_ENDPOINT'] = 'https://s3.wasabisys.com/'
  147. env['S3_BUCKET'] = prompt.ask('Wasabi bucket name:') do |q|
  148. q.required true
  149. q.default "files.#{env['LOCAL_DOMAIN']}"
  150. q.modify :strip
  151. end
  152. env['AWS_ACCESS_KEY_ID'] = prompt.ask('Wasabi access key:') do |q|
  153. q.required true
  154. q.modify :strip
  155. end
  156. env['AWS_SECRET_ACCESS_KEY'] = prompt.ask('Wasabi secret key:') do |q|
  157. q.required true
  158. q.modify :strip
  159. end
  160. when 'Minio'
  161. env['S3_ENABLED'] = 'true'
  162. env['S3_PROTOCOL'] = 'https'
  163. env['S3_REGION'] = 'us-east-1'
  164. env['S3_ENDPOINT'] = prompt.ask('Minio endpoint URL:') do |q|
  165. q.required true
  166. q.modify :strip
  167. end
  168. env['S3_PROTOCOL'] = env['S3_ENDPOINT'].start_with?('https') ? 'https' : 'http'
  169. env['S3_HOSTNAME'] = env['S3_ENDPOINT'].gsub(/\Ahttps?:\/\//, '')
  170. env['S3_BUCKET'] = prompt.ask('Minio bucket name:') do |q|
  171. q.required true
  172. q.default "files.#{env['LOCAL_DOMAIN']}"
  173. q.modify :strip
  174. end
  175. env['AWS_ACCESS_KEY_ID'] = prompt.ask('Minio access key:') do |q|
  176. q.required true
  177. q.modify :strip
  178. end
  179. env['AWS_SECRET_ACCESS_KEY'] = prompt.ask('Minio secret key:') do |q|
  180. q.required true
  181. q.modify :strip
  182. end
  183. end
  184. if prompt.yes?('Do you want to access the uploaded files from your own domain?')
  185. env['S3_ALIAS_HOST'] = prompt.ask('Domain for uploaded files:') do |q|
  186. q.required true
  187. q.default "files.#{env['LOCAL_DOMAIN']}"
  188. q.modify :strip
  189. end
  190. end
  191. end
  192. prompt.say "\n"
  193. loop do
  194. if prompt.yes?('Do you want to send e-mails from localhost?', default: false)
  195. env['SMTP_SERVER'] = 'localhost'
  196. env['SMTP_PORT'] = 25
  197. env['SMTP_AUTH_METHOD'] = 'none'
  198. env['SMTP_OPENSSL_VERIFY_MODE'] = 'none'
  199. else
  200. env['SMTP_SERVER'] = prompt.ask('SMTP server:') do |q|
  201. q.required true
  202. q.default 'smtp.mailgun.org'
  203. q.modify :strip
  204. end
  205. env['SMTP_PORT'] = prompt.ask('SMTP port:') do |q|
  206. q.required true
  207. q.default 587
  208. q.convert :int
  209. end
  210. env['SMTP_LOGIN'] = prompt.ask('SMTP username:') do |q|
  211. q.modify :strip
  212. end
  213. env['SMTP_PASSWORD'] = prompt.ask('SMTP password:') do |q|
  214. q.echo false
  215. end
  216. env['SMTP_AUTH_METHOD'] = prompt.ask('SMTP authentication:') do |q|
  217. q.required
  218. q.default 'plain'
  219. q.modify :strip
  220. end
  221. env['SMTP_OPENSSL_VERIFY_MODE'] = prompt.select('SMTP OpenSSL verify mode:', %w(none peer client_once fail_if_no_peer_cert))
  222. end
  223. env['SMTP_FROM_ADDRESS'] = prompt.ask('E-mail address to send e-mails "from":') do |q|
  224. q.required true
  225. q.default "Mastodon <notifications@#{env['LOCAL_DOMAIN']}>"
  226. q.modify :strip
  227. end
  228. break unless prompt.yes?('Send a test e-mail with this configuration right now?')
  229. send_to = prompt.ask('Send test e-mail to:', required: true)
  230. begin
  231. ActionMailer::Base.smtp_settings = {
  232. port: env['SMTP_PORT'],
  233. address: env['SMTP_SERVER'],
  234. user_name: env['SMTP_LOGIN'].presence,
  235. password: env['SMTP_PASSWORD'].presence,
  236. domain: env['LOCAL_DOMAIN'],
  237. authentication: env['SMTP_AUTH_METHOD'] == 'none' ? nil : env['SMTP_AUTH_METHOD'] || :plain,
  238. openssl_verify_mode: env['SMTP_OPENSSL_VERIFY_MODE'],
  239. enable_starttls_auto: true,
  240. }
  241. ActionMailer::Base.default_options = {
  242. from: env['SMTP_FROM_ADDRESS'],
  243. }
  244. mail = ActionMailer::Base.new.mail to: send_to, subject: 'Test', body: 'Mastodon SMTP configuration works!'
  245. mail.deliver
  246. break
  247. rescue StandardError => e
  248. prompt.error 'E-mail could not be sent with this configuration, try again.'
  249. prompt.error e.message
  250. break unless prompt.yes?('Try again?')
  251. end
  252. end
  253. prompt.say "\n"
  254. prompt.say 'This configuration will be written to .env.production'
  255. if prompt.yes?('Save configuration?')
  256. cmd = TTY::Command.new(printer: :quiet)
  257. File.write(Rails.root.join('.env.production'), "# Generated with mastodon:setup on #{Time.now.utc}\n\n" + env.each_pair.map { |key, value| "#{key}=#{value}" }.join("\n") + "\n")
  258. if using_docker
  259. prompt.ok 'Below is your configuration, save it to an .env.production file outside Docker:'
  260. prompt.say "\n"
  261. prompt.say File.read(Rails.root.join('.env.production'))
  262. prompt.say "\n"
  263. prompt.ok 'It is also saved within this container so you can proceed with this wizard.'
  264. end
  265. prompt.say "\n"
  266. prompt.say 'Now that configuration is saved, the database schema must be loaded.'
  267. prompt.warn 'If the database already exists, this will erase its contents.'
  268. if prompt.yes?('Prepare the database now?')
  269. prompt.say 'Running `RAILS_ENV=production rails db:setup` ...'
  270. prompt.say "\n\n"
  271. if cmd.run!({ RAILS_ENV: 'production', SAFETY_ASSURED: 1 }, :rails, 'db:setup').failure?
  272. prompt.error 'That failed! Perhaps your configuration is not right'
  273. else
  274. prompt.ok 'Done!'
  275. end
  276. end
  277. prompt.say "\n"
  278. prompt.say 'The final step is compiling CSS/JS assets.'
  279. prompt.say 'This may take a while and consume a lot of RAM.'
  280. if prompt.yes?('Compile the assets now?')
  281. prompt.say 'Running `RAILS_ENV=production rails assets:precompile` ...'
  282. prompt.say "\n\n"
  283. if cmd.run!({ RAILS_ENV: 'production' }, :rails, 'assets:precompile').failure?
  284. prompt.error 'That failed! Maybe you need swap space?'
  285. else
  286. prompt.say 'Done!'
  287. end
  288. end
  289. prompt.say "\n"
  290. prompt.ok 'All done! You can now power on the Mastodon server 🐘'
  291. prompt.say "\n"
  292. if db_connection_works && prompt.yes?('Do you want to create an admin user straight away?')
  293. env.each_pair do |key, value|
  294. ENV[key] = value.to_s
  295. end
  296. require_relative '../../config/environment'
  297. disable_log_stdout!
  298. username = prompt.ask('Username:') do |q|
  299. q.required true
  300. q.default 'admin'
  301. q.validate(/\A[a-z0-9_]+\z/i)
  302. q.modify :strip
  303. end
  304. email = prompt.ask('E-mail:') do |q|
  305. q.required true
  306. q.modify :strip
  307. end
  308. password = SecureRandom.hex(16)
  309. user = User.new(admin: true, email: email, password: password, confirmed_at: Time.now.utc, account_attributes: { username: username })
  310. user.save(validate: false)
  311. prompt.ok "You can login with the password: #{password}"
  312. prompt.warn 'You can change your password once you login.'
  313. end
  314. else
  315. prompt.warn 'Nothing saved. Bye!'
  316. end
  317. rescue TTY::Reader::InputInterrupt
  318. prompt.ok 'Aborting. Bye!'
  319. end
  320. end
  321. namespace :push do
  322. desc 'Unsubscribes from PuSH updates of feeds nobody follows locally'
  323. task clear: :environment do
  324. Pubsubhubbub::UnsubscribeWorker.push_bulk(Account.remote.without_followers.where.not(subscription_expires_at: nil).pluck(:id))
  325. end
  326. end
  327. namespace :settings do
  328. desc 'Open registrations on this instance'
  329. task open_registrations: :environment do
  330. Setting.open_registrations = true
  331. end
  332. desc 'Close registrations on this instance'
  333. task close_registrations: :environment do
  334. Setting.open_registrations = false
  335. end
  336. end
  337. namespace :webpush do
  338. desc 'Generate VAPID key'
  339. task generate_vapid_key: :environment do
  340. vapid_key = Webpush.generate_key
  341. puts "VAPID_PRIVATE_KEY=#{vapid_key.private_key}"
  342. puts "VAPID_PUBLIC_KEY=#{vapid_key.public_key}"
  343. end
  344. end
  345. end
  346. def disable_log_stdout!
  347. dev_null = Logger.new('/dev/null')
  348. Rails.logger = dev_null
  349. ActiveRecord::Base.logger = dev_null
  350. HttpLog.configuration.logger = dev_null
  351. Paperclip.options[:log] = false
  352. end
  353. def prepare_for_options!
  354. 2.times { ARGV.shift }
  355. end