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.

142 lines
5.0 KiB

  1. # frozen_string_literal: true
  2. require 'thor'
  3. require_relative 'mastodon/media_cli'
  4. require_relative 'mastodon/emoji_cli'
  5. require_relative 'mastodon/accounts_cli'
  6. require_relative 'mastodon/feeds_cli'
  7. require_relative 'mastodon/search_cli'
  8. require_relative 'mastodon/settings_cli'
  9. require_relative 'mastodon/statuses_cli'
  10. require_relative 'mastodon/domains_cli'
  11. require_relative 'mastodon/preview_cards_cli'
  12. require_relative 'mastodon/cache_cli'
  13. require_relative 'mastodon/upgrade_cli'
  14. require_relative 'mastodon/email_domain_blocks_cli'
  15. require_relative 'mastodon/version'
  16. module Mastodon
  17. class CLI < Thor
  18. def self.exit_on_failure?
  19. true
  20. end
  21. desc 'media SUBCOMMAND ...ARGS', 'Manage media files'
  22. subcommand 'media', Mastodon::MediaCLI
  23. desc 'emoji SUBCOMMAND ...ARGS', 'Manage custom emoji'
  24. subcommand 'emoji', Mastodon::EmojiCLI
  25. desc 'accounts SUBCOMMAND ...ARGS', 'Manage accounts'
  26. subcommand 'accounts', Mastodon::AccountsCLI
  27. desc 'feeds SUBCOMMAND ...ARGS', 'Manage feeds'
  28. subcommand 'feeds', Mastodon::FeedsCLI
  29. desc 'search SUBCOMMAND ...ARGS', 'Manage the search engine'
  30. subcommand 'search', Mastodon::SearchCLI
  31. desc 'settings SUBCOMMAND ...ARGS', 'Manage dynamic settings'
  32. subcommand 'settings', Mastodon::SettingsCLI
  33. desc 'statuses SUBCOMMAND ...ARGS', 'Manage statuses'
  34. subcommand 'statuses', Mastodon::StatusesCLI
  35. desc 'domains SUBCOMMAND ...ARGS', 'Manage account domains'
  36. subcommand 'domains', Mastodon::DomainsCLI
  37. desc 'preview_cards SUBCOMMAND ...ARGS', 'Manage preview cards'
  38. subcommand 'preview_cards', Mastodon::PreviewCardsCLI
  39. desc 'cache SUBCOMMAND ...ARGS', 'Manage cache'
  40. subcommand 'cache', Mastodon::CacheCLI
  41. desc 'upgrade SUBCOMMAND ...ARGS', 'Various version upgrade utilities'
  42. subcommand 'upgrade', Mastodon::UpgradeCLI
  43. desc 'email_domain_blocks SUBCOMMAND ...ARGS', 'Manage e-mail domain blocks'
  44. subcommand 'email_domain_blocks', Mastodon::EmailDomainBlocksCLI
  45. option :dry_run, type: :boolean
  46. desc 'self-destruct', 'Erase the server from the federation'
  47. long_desc <<~LONG_DESC
  48. Erase the server from the federation by broadcasting account delete
  49. activities to all known other servers. This allows a "clean exit" from
  50. running a Mastodon server, as it leaves next to no cache behind on
  51. other servers.
  52. This command is always interactive and requires confirmation twice.
  53. No local data is actually deleted, because emptying the
  54. database or removing files is much faster through other, external
  55. means, such as e.g. deleting the entire VPS. However, because other
  56. servers will delete data about local users, but no local data will be
  57. updated (such as e.g. followers), there will be a state mismatch
  58. that will lead to glitches and issues if you then continue to run and use
  59. the server.
  60. So either you know exactly what you are doing, or you are starting
  61. from a blank slate afterwards by manually clearing out all the local
  62. data!
  63. LONG_DESC
  64. def self_destruct
  65. require 'tty-prompt'
  66. prompt = TTY::Prompt.new
  67. exit(1) unless prompt.ask('Type in the domain of the server to confirm:', required: true) == Rails.configuration.x.local_domain
  68. prompt.warn('This operation WILL NOT be reversible. It can also take a long time.')
  69. prompt.warn('While the data won\'t be erased locally, the server will be in a BROKEN STATE afterwards.')
  70. prompt.warn('A running Sidekiq process is required. Do not shut it down until queues clear.')
  71. exit(1) if prompt.no?('Are you sure you want to proceed?')
  72. inboxes = Account.inboxes
  73. processed = 0
  74. dry_run = options[:dry_run] ? ' (DRY RUN)' : ''
  75. if inboxes.empty?
  76. prompt.ok('It seems like your server has not federated with anything')
  77. prompt.ok('You can shut it down and delete it any time')
  78. return
  79. end
  80. prompt.warn('Do NOT interrupt this process...')
  81. Setting.registrations_mode = 'none'
  82. Account.local.without_suspended.find_each do |account|
  83. payload = ActiveModelSerializers::SerializableResource.new(
  84. account,
  85. serializer: ActivityPub::DeleteActorSerializer,
  86. adapter: ActivityPub::Adapter
  87. ).as_json
  88. json = Oj.dump(ActivityPub::LinkedDataSignature.new(payload).sign!(account))
  89. unless options[:dry_run]
  90. ActivityPub::DeliveryWorker.push_bulk(inboxes) do |inbox_url|
  91. [json, account.id, inbox_url]
  92. end
  93. account.suspend!
  94. end
  95. processed += 1
  96. end
  97. prompt.ok("Queued #{inboxes.size * processed} items into Sidekiq for #{processed} accounts#{dry_run}")
  98. prompt.ok('Wait until Sidekiq processes all items, then you can shut everything down and delete the data')
  99. rescue TTY::Reader::InputInterrupt
  100. exit(1)
  101. end
  102. map %w(--version -v) => :version
  103. desc 'version', 'Show version'
  104. def version
  105. say(Mastodon::Version.to_s)
  106. end
  107. end
  108. end