Class: Discourse::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/discourse.rb

Defined Under Namespace

Classes: CommandError, CommandRunner

Class Method Summary collapse

Class Method Details

.atomic_ln_s(source, destination) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/discourse.rb', line 103

def self.atomic_ln_s(source, destination)
  begin
    return if File.readlink(destination) == source
  rescue Errno::ENOENT, Errno::EINVAL
  end

  FileUtils.mkdir_p(File.join(Rails.root, "tmp"))
  temp_destination = File.join(Rails.root, "tmp", SecureRandom.hex)
  execute_command("ln", "-s", source, temp_destination)
  FileUtils.mv(temp_destination, destination)

  nil
end

.atomic_write_file(destination, contents) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/discourse.rb', line 84

def self.atomic_write_file(destination, contents)
  begin
    return if File.read(destination) == contents
  rescue Errno::ENOENT
  end

  FileUtils.mkdir_p(File.join(Rails.root, "tmp"))
  temp_destination = File.join(Rails.root, "tmp", SecureRandom.hex)

  File.open(temp_destination, "w") do |fd|
    fd.write(contents)
    fd.fsync()
  end

  FileUtils.mv(temp_destination, destination)

  nil
end

.execute_command(*command, **args) ⇒ Object

Usage:

Discourse::Utils.execute_command("pwd", chdir: 'mydirectory')

or with a block

Discourse::Utils.execute_command(chdir: 'mydirectory') do |runner|
  runner.exec("pwd")
end


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/discourse.rb', line 25

def self.execute_command(*command, **args)
  runner = CommandRunner.new(**args)

  if block_given?
    if command.present?
      raise RuntimeError.new("Cannot pass command and block to execute_command")
    end
    yield runner
  else
    runner.exec(*command)
  end
end

.logs_markdown(logs, user:, filename: "log.txt") ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/discourse.rb', line 42

def self.logs_markdown(logs, user:, filename: "log.txt")
  # Reserve 250 characters for the rest of the text
  max_logs_length = SiteSetting.max_post_length - 250
  pretty_logs = Discourse::Utils.pretty_logs(logs)

  # If logs are short, try to inline them
  return <<~TEXT if pretty_logs.size < max_logs_length
    ```text
    #{pretty_logs}
    ```
    TEXT

  # Try to create an upload for the logs
  upload =
    Dir.mktmpdir do |dir|
      File.write(File.join(dir, filename), pretty_logs)
      zipfile = Compression::Zip.new.compress(dir, filename)
      File.open(zipfile) do |file|
        UploadCreator.new(
          file,
          File.basename(zipfile),
          type: "backup_logs",
          for_export: "true",
        ).create_for(user.id)
      end
    end

  if upload.persisted?
    return UploadMarkdown.new(upload).attachment_markdown
  else
    Rails.logger.warn("Failed to upload the backup logs file: #{upload.errors.full_messages}")
  end

  # If logs are long and upload cannot be created, show trimmed logs
  <<~TEXT
  ```text
  ...
  #{pretty_logs.last(max_logs_length)}
  ```
  TEXT
end

.pretty_logs(logs) ⇒ Object



38
39
40
# File 'lib/discourse.rb', line 38

def self.pretty_logs(logs)
  logs.join("\n")
end