Module: Litestream

Defined in:
lib/litestream.rb,
lib/litestream/engine.rb,
lib/litestream/version.rb,
lib/litestream/commands.rb,
lib/litestream/upstream.rb,
app/jobs/litestream/verification_job.rb,
app/controllers/litestream/processes_controller.rb,
app/controllers/litestream/application_controller.rb,
app/controllers/litestream/restorations_controller.rb,
lib/litestream/generators/litestream/install_generator.rb

Defined Under Namespace

Modules: Commands, Generators, Upstream Classes: ApplicationController, Configuration, Engine, ProcessesController, RestorationsController, VerificationJob

Constant Summary collapse

VerificationFailure =
Class.new(StandardError)
VERSION =
"0.12.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject



11
12
13
# File 'lib/litestream.rb', line 11

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



20
21
22
23
24
25
26
27
# File 'lib/litestream.rb', line 20

def self.configure
  deprecator.warn(
    "Configuring Litestream via Litestream.configure is deprecated. Use Rails.application.configure { config.litestream.* = ... } instead.",
    caller
  )
  self.configuration ||= Configuration.new
  yield(configuration)
end

.databasesObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/litestream.rb', line 143

def databases
  databases = Commands.databases

  databases.each do |db|
    generations = Commands.generations(db["path"])
    snapshots = Commands.snapshots(db["path"])
    db["path"] = db["path"].gsub(Rails.root.to_s, "[ROOT]")

    db["generations"] = generations.map do |generation|
      id = generation["generation"]
      replica = generation["name"]
      generation["snapshots"] = snapshots.select { |snapshot| snapshot["generation"] == id && snapshot["replica"] == replica }
        .map { |s| s.slice("index", "size", "created") }
      generation.slice("generation", "name", "lag", "start", "end", "snapshots")
    end
  end
end

.deprecatorObject



15
16
17
# File 'lib/litestream.rb', line 15

def deprecator
  @deprecator ||= ActiveSupport::Deprecation.new("0.12.0", "Litestream")
end

.passwordObject



68
69
70
# File 'lib/litestream.rb', line 68

def password
  ENV["LITESTREAM_PASSWORD"] || @@password
end

.queueObject



72
73
74
# File 'lib/litestream.rb', line 72

def queue
  ENV["LITESTREAM_QUEUE"] || @@queue || "default"
end

.replica_access_keyObject



84
85
86
# File 'lib/litestream.rb', line 84

def replica_access_key
  @@replica_access_key || configuration.replica_access_key
end

.replica_bucketObject



76
77
78
# File 'lib/litestream.rb', line 76

def replica_bucket
  @@replica_bucket || configuration.replica_bucket
end

.replica_key_idObject



80
81
82
# File 'lib/litestream.rb', line 80

def replica_key_id
  @@replica_key_id || configuration.replica_key_id
end

.replicate_processObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/litestream.rb', line 92

def replicate_process
  info = {}
  if !`which systemctl`.empty?
    systemctl_status = `#{Litestream.systemctl_command}`.chomp
    # ["● litestream.service - Litestream",
    #  "     Loaded: loaded (/lib/systemd/system/litestream.service; enabled; vendor preset: enabled)",
    #  "     Active: active (running) since Tue 2023-07-25 13:49:43 UTC; 8 months 24 days ago",
    #  "   Main PID: 1179656 (litestream)",
    #  "      Tasks: 9 (limit: 1115)",
    #  "     Memory: 22.9M",
    #  "        CPU: 10h 49.843s",
    #  "     CGroup: /system.slice/litestream.service",
    #  "             └─1179656 /usr/bin/litestream replicate",
    #  "",
    #  "Warning: some journal files were not opened due to insufficient permissions."]
    systemctl_status.split("\n").each do |line|
      line.strip!
      if line.start_with?("Main PID:")
        _key, value = line.split(":")
        pid, _name = value.strip.split(" ")
        info[:pid] = pid
      elsif line.start_with?("Active:")
        value, _ago = line.split(";")
        status, timestamp = value.split(" since ")
        info[:started] = DateTime.strptime(timestamp.strip, "%a %Y-%m-%d %H:%M:%S %Z")
        status_match = status.match(%r{\((?<status>.*)\)})
        info[:status] = status_match ? status_match[:status] : nil
      end
    end
  else
    litestream_replicate_ps = `ps -ax | grep litestream | grep replicate`.chomp
    litestream_replicate_ps.split("\n").each do |line|
      next unless line.include?("litestream replicate")
      pid, * = line.split(" ")
      info[:pid] = pid
      state, _, lstart = `ps -o "state,lstart" #{pid}`.chomp.split("\n").last.partition(/\s+/)

      info[:status] = case state[0]
      when "I" then "idle"
      when "R" then "running"
      when "S" then "sleeping"
      when "T" then "stopped"
      when "U" then "uninterruptible"
      when "Z" then "zombie"
      end
      info[:started] = DateTime.strptime(lstart.strip, "%a %b %d %H:%M:%S %Y")
    end
  end
  info
end

.systemctl_commandObject



88
89
90
# File 'lib/litestream.rb', line 88

def systemctl_command
  @@systemctl_command || "systemctl status litestream"
end

.usernameObject

use method instead of attr_accessor to ensure this works if variable set after Litestream is loaded



64
65
66
# File 'lib/litestream.rb', line 64

def username
  ENV["LITESTREAM_USERNAME"] || @@username || "litestream"
end

.verify!(database_path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/litestream.rb', line 39

def verify!(database_path)
  database = SQLite3::Database.new(database_path)
  database.execute("CREATE TABLE IF NOT EXISTS _litestream_verification (id INTEGER PRIMARY KEY, uuid BLOB)")
  sentinel = SecureRandom.uuid
  database.execute("INSERT INTO _litestream_verification (uuid) VALUES (?)", [sentinel])
  # give the Litestream replication process time to replicate the sentinel value
  sleep 10

  backup_path = "tmp/#{Time.now.utc.strftime("%Y%m%d%H%M%S")}_#{sentinel}.sqlite3"
  Litestream::Commands.restore(database_path, **{"-o" => backup_path})

  backup = SQLite3::Database.new(backup_path)
  result = backup.execute("SELECT 1 FROM _litestream_verification WHERE uuid = ? LIMIT 1", sentinel) # => [[1]] || []

  raise VerificationFailure, "Verification failed for `#{database_path}`" if result.empty?

  true
ensure
  database.execute("DELETE FROM _litestream_verification WHERE uuid = ?", sentinel)
  database.close
  Dir.glob(backup_path + "*").each { |file| File.delete(file) }
end