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.

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