logsafe 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +24 -0
- data/CODE_OF_CONDUCT.md +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +654 -0
- data/exe/logsafe +50 -0
- data/lib/logsafe/cipher.rb +170 -0
- data/lib/logsafe/copies.rb +176 -0
- data/lib/logsafe/day.rb +139 -0
- data/lib/logsafe/merge_job.rb +13 -0
- data/lib/logsafe/private_folder.rb +29 -0
- data/lib/logsafe/reencrypt.rb +107 -0
- data/lib/logsafe/reencrypt_job.rb +12 -0
- data/lib/logsafe/room.rb +35 -0
- data/lib/logsafe/search.rb +121 -0
- data/lib/logsafe/source.rb +79 -0
- data/lib/logsafe/storage.rb +335 -0
- data/lib/logsafe/version.rb +5 -0
- data/lib/logsafe/web.html.erb +105 -0
- data/lib/logsafe/web.rb +150 -0
- data/lib/logsafe.rb +159 -0
- data/shipper/start +18 -0
- data/shipper/to-folder.yaml +20 -0
- data/shipper/to-gcs.yaml +21 -0
- data/shipper/to-s3.yaml +23 -0
- data/shipper/vector.yaml +113 -0
- metadata +90 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_job"
|
|
4
|
+
|
|
5
|
+
module Logsafe
|
|
6
|
+
# Run once after the key was changed, with the old key in Logsafe.old_keys. See Logsafe.reencrypt.
|
|
7
|
+
class ReencryptJob < ActiveJob::Base
|
|
8
|
+
def perform
|
|
9
|
+
Logsafe.reencrypt
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
data/lib/logsafe/room.rb
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "open3"
|
|
4
|
+
|
|
5
|
+
module Logsafe
|
|
6
|
+
# There is too little room on the disk for what was asked. Never a reason to leave a part out.
|
|
7
|
+
class Full < Error; end
|
|
8
|
+
|
|
9
|
+
# How much of a day may be unpacked in a folder: a quarter of what is free there, because sorting
|
|
10
|
+
# needs the lines up to three times, and what shares the disk must go on working.
|
|
11
|
+
class Room
|
|
12
|
+
SHARE = 4
|
|
13
|
+
|
|
14
|
+
# The bytes free where this folder is. A great many when df does not say.
|
|
15
|
+
def self.free(dir)
|
|
16
|
+
report, = Open3.capture2("df", "-Pk", dir)
|
|
17
|
+
report[/\s(-?\d+)\s+\d+%/, 1]&.to_i&.*(1024) || (1 << 62)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def initialize(dir)
|
|
21
|
+
@left = Room.free(dir) / SHARE
|
|
22
|
+
@lock = Mutex.new
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Called from several threads.
|
|
26
|
+
def take(bytes)
|
|
27
|
+
left = @lock.synchronize { @left -= bytes }
|
|
28
|
+
raise Full, "there is too little room on the disk for this day's logs" if left.negative?
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def left
|
|
32
|
+
[@left, 1].max
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "open3"
|
|
4
|
+
require "tmpdir"
|
|
5
|
+
|
|
6
|
+
module Logsafe
|
|
7
|
+
# Finds lines in the days asked for, in the copies of those days on this machine: see Copies.
|
|
8
|
+
module Search
|
|
9
|
+
LIMIT = 500
|
|
10
|
+
WORDS = 8 # each word is one more pass over the lines
|
|
11
|
+
SAME = 2000 # lines of one time that can be paged through
|
|
12
|
+
STREAMS = 50 # at once: each has its files fetched, and processes of its own
|
|
13
|
+
LIST_FOR = 60 # seconds a listing of the storage is believed: parts come once a minute
|
|
14
|
+
LISTING = Mutex.new
|
|
15
|
+
LEVELS = { "debug" => "debug", "info" => "info", "warn" => "warn|warning",
|
|
16
|
+
"error" => "error", "fatal" => "fatal" }.freeze
|
|
17
|
+
DAY_FILE = %r{\A\d{4}/\d{2}/(#{STREAM})\.(\d{4}-\d{2}-\d{2})\.log\.gz(\.enc)?\z}
|
|
18
|
+
|
|
19
|
+
class << self
|
|
20
|
+
# { [stream, day] => keys } for what the storage has of the days from..to (dates): each day's
|
|
21
|
+
# file, and the parts that are not in it yet.
|
|
22
|
+
def sources(from, to)
|
|
23
|
+
days = (from..to).map(&:to_s)
|
|
24
|
+
found = files(from, to).merge(Logsafe.parts(listed(PARTS))) { |_, file, parts| file + parts }
|
|
25
|
+
found.select { |(_, day), _| days.include?(day) }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# The last LIMIT lines that match, oldest first, as [stream, line], and then what could not be
|
|
29
|
+
# read, as texts: one stream that fails does not hide the others. days is what sources gave.
|
|
30
|
+
# A line matches when it holds every word, in any case, and the level as a word of its own.
|
|
31
|
+
# before is the time a line starts with: only lines from before it are given. With skip, the lines
|
|
32
|
+
# of that time too, but for this many that came last: those were on the page after.
|
|
33
|
+
def call(days, words: [], level: nil, before: nil, skip: nil)
|
|
34
|
+
Copies.tidy
|
|
35
|
+
days = days.reject { |(_, day), _| day > before[0, 10] } if before
|
|
36
|
+
problems = []
|
|
37
|
+
lines = Dir.mktmpdir("logsafe-search") do |dir|
|
|
38
|
+
newest(days, filters(written(words, dir), level, [before, skip]), LIMIT + skip.to_i, problems)
|
|
39
|
+
end
|
|
40
|
+
[lines.first([lines.size - skip.to_i, 0].max).last(LIMIT), problems]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
# The days' files, as sources gives them.
|
|
46
|
+
def files(from, to)
|
|
47
|
+
months = (from..to).map { |date| date.strftime("%Y/%m/") }.uniq
|
|
48
|
+
keys = months.flat_map { |month| listed(month) }.grep(DAY_FILE)
|
|
49
|
+
files = keys.group_by { |key| key.match(DAY_FILE).captures.first(2) }
|
|
50
|
+
files.select { |(stream, _), _| Logsafe.stream?(stream) }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# From the last day back, and no further than it takes: the days before only have older lines.
|
|
54
|
+
# In order, and no more of them at any time than are needed.
|
|
55
|
+
def newest(days, filters, limit, problems)
|
|
56
|
+
days.group_by { |(_, day), _| day }.sort.reverse_each.with_object([]) do |(_, streams), found|
|
|
57
|
+
streams.each do |(stream, day), keys|
|
|
58
|
+
found.concat(of_a_day(stream, day, keys, filters, problems))
|
|
59
|
+
found.replace(found.sort_by(&:reverse).last(limit))
|
|
60
|
+
end
|
|
61
|
+
break found if found.size >= limit
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# What fails here, such as a file that the storage will not give, must not hide the other streams.
|
|
66
|
+
def of_a_day(stream, day, keys, filters, problems)
|
|
67
|
+
log, left_out = Copies.path(stream, day, keys)
|
|
68
|
+
problems << "#{stream} #{day}: #{left_out.size} of its parts cannot be read. #{left_out.first}" if left_out.any?
|
|
69
|
+
matches(stream, log, filters)
|
|
70
|
+
rescue StandardError => e
|
|
71
|
+
LISTING.synchronize { @listed = nil } # it may be the listing that is no longer true
|
|
72
|
+
problems << "#{stream} #{day}: #{Day.reason(e)}"
|
|
73
|
+
[]
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Listing every part is the slow part of a search that has its copies, so a listing is used again
|
|
77
|
+
# for a while. A key that is gone by the time it is fetched is noticed there.
|
|
78
|
+
# One thread at a time: a server's threads share the listings, and a second search that comes
|
|
79
|
+
# while the storage is being listed waits for that listing and does not ask for its own.
|
|
80
|
+
def listed(prefix)
|
|
81
|
+
LISTING.synchronize do
|
|
82
|
+
at, keys = (@listed ||= {})[[Logsafe.storage, prefix]]
|
|
83
|
+
next keys if at && Time.now - at < LIST_FOR
|
|
84
|
+
|
|
85
|
+
@listed.delete_if { |_, (time, _)| Time.now - time >= LIST_FOR }
|
|
86
|
+
(@listed[[Logsafe.storage, prefix]] = [Time.now, Logsafe.storage.keys(prefix)]).last
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# The system's tools, so that the size of a day does not matter. Nothing goes through a shell.
|
|
91
|
+
# The lines are in time order, so awk can stop at the first one that is too late.
|
|
92
|
+
def filters(words, level, (before, skip))
|
|
93
|
+
late = "substr($0, 1, 30) #{skip ? ">" : ">="} (b \"\") { exit } 1"
|
|
94
|
+
[
|
|
95
|
+
(["awk", "-v", "b=#{before}", late] if before),
|
|
96
|
+
(["grep", "-a", "-i", "-w", "-E", LEVELS.fetch(level)] if level),
|
|
97
|
+
*words.map { |word| ["grep", "-a", "-i", "-F", "-f", word] },
|
|
98
|
+
["tail", "-n", (LIMIT + skip.to_i).to_s]
|
|
99
|
+
].compact.map { |filter| [Copies::C, *filter] }
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Each word in a file that only this user can read: the arguments of a command are for everyone
|
|
103
|
+
# on the machine to see, and what is searched for can be an address or a token.
|
|
104
|
+
def written(words, dir)
|
|
105
|
+
words.uniq.first(WORDS).each_with_index.map do |word, index|
|
|
106
|
+
File.join(dir, index.to_s).tap { |path| File.write(path, "#{word}\n") }
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# grep ends with 1 when it finds nothing. Anything else, and some of the lines may be missing.
|
|
111
|
+
def matches(stream, log, filters)
|
|
112
|
+
lines, ends = Open3.pipeline_r(*filters, in: log) do |out, waits|
|
|
113
|
+
[out.binmode.readlines(chomp: true), waits.map { |wait| wait.value.exitstatus }]
|
|
114
|
+
end
|
|
115
|
+
raise Error, "the search failed" unless (ends - [0, 1]).empty?
|
|
116
|
+
|
|
117
|
+
lines.map { |line| [stream, line.force_encoding("UTF-8").scrub] }
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Logsafe
|
|
4
|
+
# Puts what was fetched for one stream and day into plain text, in its place and in the thread that
|
|
5
|
+
# fetched it: a day of parts is a thousand runs of gzip.
|
|
6
|
+
# A part is not trusted. Whoever may write parts, such as a server that was broken into, chooses
|
|
7
|
+
# every byte of them. So what a part unpacks to and the length of a line have a limit, and every
|
|
8
|
+
# line comes out with a time of its own day in front: order, paging and what is a line rest on that.
|
|
9
|
+
class Source
|
|
10
|
+
PART_LIMIT = 256 << 20 # bytes a compressed part may unpack to; Vector sends 10 MB at a time
|
|
11
|
+
LINE_LIMIT = 2 << 20 # bytes of a line as it was sent; Vector reads lines of 1 MB at most
|
|
12
|
+
TIME = /T\d{2}:\d{2}:\d{2}\.\d{9}Z /n
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
def gzip?(path)
|
|
16
|
+
File.binread(path, 2) == GZIP
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def gunzip(path, limit)
|
|
20
|
+
system("gzip", "-dc", path, out: "#{path}.plain", err: File::NULL, rlimit_fsize: limit) ||
|
|
21
|
+
raise(Error, "cannot be read: it is damaged, or unpacks to more than #{limit >> 20} MB")
|
|
22
|
+
File.rename("#{path}.plain", path)
|
|
23
|
+
ensure
|
|
24
|
+
FileUtils.rm_f("#{path}.plain")
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Each line, with its line break.
|
|
28
|
+
def each_line(path)
|
|
29
|
+
File.foreach(path, "\n", LINE_LIMIT + 1, mode: "rb") do |line|
|
|
30
|
+
raise Error, "a line is longer than #{LINE_LIMIT >> 20} MB" if line.bytesize > LINE_LIMIT
|
|
31
|
+
|
|
32
|
+
yield line
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def initialize(stream, day, keys)
|
|
38
|
+
@name = "#{stream}/#{day}"
|
|
39
|
+
@day = day
|
|
40
|
+
@keys = keys
|
|
41
|
+
@line_keys = Cipher.day_keys(keys, stream, day)
|
|
42
|
+
@time = /\A#{day}#{TIME}/n
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# limit is for a day's file, which can be of any size.
|
|
46
|
+
def plain(key, path, limit)
|
|
47
|
+
key.start_with?(PARTS) ? part(path) : day_file(key, path, limit)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Encrypted or not is said by each line, compressed or not by the file: storages change names.
|
|
51
|
+
def part(path)
|
|
52
|
+
Source.gunzip(path, PART_LIMIT) if Source.gzip?(path)
|
|
53
|
+
File.open("#{path}.plain", "wb") do |plain|
|
|
54
|
+
Source.each_line(path) { |line| plain << text(line) }
|
|
55
|
+
end
|
|
56
|
+
File.rename("#{path}.plain", path)
|
|
57
|
+
ensure
|
|
58
|
+
FileUtils.rm_f("#{path}.plain")
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
private
|
|
62
|
+
|
|
63
|
+
# One that is named as encrypted has to be, and for this stream and day. Otherwise whoever may
|
|
64
|
+
# write to the storage could put a file of their own in its place.
|
|
65
|
+
def day_file(key, path, limit)
|
|
66
|
+
File.rename(Cipher.unseal(path, "#{path}.open", @keys, @name), path) if key.end_with?(".enc")
|
|
67
|
+
Source.gunzip(path, limit) if Source.gzip?(path)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# A line whose time is not of this day keeps what it says, behind the start of the day.
|
|
71
|
+
def text(line)
|
|
72
|
+
text = Cipher.plain(line, @line_keys).delete_suffix("\n")
|
|
73
|
+
raise Error, "an encrypted line holds a line break" if text.include?("\n")
|
|
74
|
+
return "" if text.empty?
|
|
75
|
+
|
|
76
|
+
text.match?(@time) ? "#{text}\n" : "#{@day}T00:00:00.000000000Z #{text}\n"
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
|
|
5
|
+
module Logsafe
|
|
6
|
+
# The places logs can live. Each one answers five messages, and so can an object of your own:
|
|
7
|
+
# keys(prefix) the keys that start with prefix, which ends in "/", or is empty for all
|
|
8
|
+
# fetch(key, path) copy the key's content to a local file; called from several threads at once
|
|
9
|
+
# version(key) what tells the content the key has now from any other; nil when it has none
|
|
10
|
+
# store(path, key, was) copy a local file to the key, if the key still has the version it was, and
|
|
11
|
+
# otherwise raise Changed: what was read before that is not the latest
|
|
12
|
+
# delete(keys)
|
|
13
|
+
# and says in to_s which storage it is, when one machine reads from more than one. One that cannot
|
|
14
|
+
# tell versions apart may give the same for every content: two runs at the same time are then not
|
|
15
|
+
# kept from each other.
|
|
16
|
+
module Storage
|
|
17
|
+
# Someone else wrote first.
|
|
18
|
+
class Changed < Error
|
|
19
|
+
def initialize(key)
|
|
20
|
+
super("#{key} was changed meanwhile")
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Copies the keys to files in dir, several at a time: a day can be more than a thousand parts.
|
|
25
|
+
# Gives each key and its file to the block, in the thread that fetched it, for work that can be
|
|
26
|
+
# done side by side. Returns the paths, in the order of the keys. A key that fails, here or in the
|
|
27
|
+
# block, is left out and given to failed with its error; when that raises, the rest is not fetched.
|
|
28
|
+
def self.fetch(storage, keys, dir, failed: ->(_, error) { raise error })
|
|
29
|
+
paths = keys.each_index.map { |index| File.join(dir, "source#{index}") }
|
|
30
|
+
side_by_side(keys.zip(paths)) do |key, path|
|
|
31
|
+
storage.fetch(key, path)
|
|
32
|
+
yield(key, path)
|
|
33
|
+
rescue StandardError => e
|
|
34
|
+
FileUtils.rm_f(path)
|
|
35
|
+
failed.call(key, e)
|
|
36
|
+
end
|
|
37
|
+
paths.select { |path| File.exist?(path) }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Gives each item to the block, several at a time. Stops at the first that fails, and raises what it raised.
|
|
41
|
+
def self.side_by_side(items)
|
|
42
|
+
jobs = Queue.new(items).close
|
|
43
|
+
failure = nil
|
|
44
|
+
Array.new([8, items.size].min) do
|
|
45
|
+
Thread.new do
|
|
46
|
+
while (job = jobs.pop)
|
|
47
|
+
yield(job)
|
|
48
|
+
end
|
|
49
|
+
rescue StandardError => e
|
|
50
|
+
jobs.clear
|
|
51
|
+
failure ||= e
|
|
52
|
+
end
|
|
53
|
+
end.each(&:join)
|
|
54
|
+
raise failure if failure
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def self.build(setting)
|
|
58
|
+
case setting
|
|
59
|
+
in String | Pathname then Folder.new(setting.to_s)
|
|
60
|
+
in { folder: } then Folder.new(folder.to_s)
|
|
61
|
+
in { s3: String => bucket, **options } then S3.new(S3.bucket(bucket, options))
|
|
62
|
+
in { gcs: String => bucket, **options } then GCS.new(GCS.bucket(bucket, options))
|
|
63
|
+
else setting
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# A folder: a local disk, a mounted volume or a network share.
|
|
68
|
+
# What is below the root is opened one folder at a time, each from the handle of the one above, and
|
|
69
|
+
# never through a symbolic link. Whoever may write to the folder, such as a server that was broken
|
|
70
|
+
# into, could otherwise have this job read, replace or delete files anywhere on its machine.
|
|
71
|
+
class Folder
|
|
72
|
+
SETTLE = 120 # seconds; a shipper appends to its files, so a part is left alone while it is fresh
|
|
73
|
+
OPEN = File::RDONLY | File::NOFOLLOW | File::NONBLOCK
|
|
74
|
+
NEW = File::WRONLY | File::CREAT | File::EXCL | File::NOFOLLOW | File::BINARY
|
|
75
|
+
HANDLES = "/proc/self/fd" # on Linux. Elsewhere by path, where a folder could be swapped in between.
|
|
76
|
+
TEMPORARY = /\A\.logsafe-\h{16}\z/
|
|
77
|
+
LOCK = ".logsafe-lock"
|
|
78
|
+
WAIT = 30 # seconds for another store, which holds the lock for a moment
|
|
79
|
+
|
|
80
|
+
def initialize(root)
|
|
81
|
+
@root = root
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Which storage this is, for what is kept from it on this machine. The same below.
|
|
85
|
+
def to_s
|
|
86
|
+
"folder:#{File.expand_path(@root)}"
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def keys(prefix)
|
|
90
|
+
within(prefix) do |folder|
|
|
91
|
+
Dir.glob("**/*", base: folder).filter_map do |name|
|
|
92
|
+
key = prefix + name
|
|
93
|
+
key if key.valid_encoding? && settled?(key, File.lstat(File.join(folder, name)))
|
|
94
|
+
rescue Errno::ENOENT
|
|
95
|
+
next # removed since it was listed
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
rescue Errno::ENOENT
|
|
99
|
+
[]
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def fetch(key, path)
|
|
103
|
+
within(File.dirname(key)) do |folder|
|
|
104
|
+
File.open(File.join(folder, File.basename(key)), OPEN) do |file|
|
|
105
|
+
file.stat.file? ? IO.copy_stream(file, path) : raise(Error, "#{key} is not a file")
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def version(key)
|
|
111
|
+
within(File.dirname(key)) { |folder| stamp(File.join(folder, File.basename(key))) }
|
|
112
|
+
rescue Errno::ENOENT
|
|
113
|
+
nil
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Written beside its place and then renamed, so nobody sees half a file. Under a name of its own:
|
|
117
|
+
# two jobs in one process, or in two containers, would share one made from the pid. Not for
|
|
118
|
+
# everyone to read: Docker's own log files are not either.
|
|
119
|
+
def store(path, key, was)
|
|
120
|
+
FileUtils.mkdir_p(@root, mode: 0o750)
|
|
121
|
+
within(File.dirname(key), create: true) do |folder|
|
|
122
|
+
tidy(folder)
|
|
123
|
+
temporary = File.join(folder, ".logsafe-#{SecureRandom.hex(8)}")
|
|
124
|
+
File.open(temporary, NEW, 0o640) { |file| IO.copy_stream(path, file) }
|
|
125
|
+
place = File.join(folder, File.basename(key))
|
|
126
|
+
alone { stamp(place) == was ? File.rename(temporary, place) : raise(Changed, key) }
|
|
127
|
+
ensure
|
|
128
|
+
FileUtils.rm_f(temporary) if temporary
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# What is gone already, another run removed: two runs at the same time are no failure.
|
|
133
|
+
def delete(keys)
|
|
134
|
+
keys.each { |key| in_place(key) { |path| File.delete(path) } }
|
|
135
|
+
keys.map { |key| File.dirname(key) }.uniq.each { |dir| in_place(dir) { |path| Dir.rmdir(path) } }
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
private
|
|
139
|
+
|
|
140
|
+
# Opened, and not only looked at: a network share then asks its server.
|
|
141
|
+
def stamp(path)
|
|
142
|
+
File.open(path, OPEN) { |file| [file.stat.ino, file.stat.size, file.stat.mtime.to_r].join("-") }
|
|
143
|
+
rescue Errno::ENOENT
|
|
144
|
+
nil
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# One store at a time, of all who share the folder.
|
|
148
|
+
def alone
|
|
149
|
+
File.open(File.join(@root, LOCK), File::RDWR | File::CREAT | File::NOFOLLOW, 0o640) do |lock|
|
|
150
|
+
give_up = Time.now + WAIT
|
|
151
|
+
until locked?(lock)
|
|
152
|
+
raise Error, "#{LOCK} in the log folder is taken" if Time.now > give_up
|
|
153
|
+
|
|
154
|
+
sleep(0.05)
|
|
155
|
+
end
|
|
156
|
+
yield
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# A share that has no locks leaves only the moment between the look and the rename.
|
|
161
|
+
def locked?(lock)
|
|
162
|
+
lock.flock(File::LOCK_EX | File::LOCK_NB)
|
|
163
|
+
rescue Errno::ENOLCK, Errno::ENOTSUP, Errno::EOPNOTSUPP, Errno::ENOSYS
|
|
164
|
+
true
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def in_place(key)
|
|
168
|
+
within(File.dirname(key)) { |folder| yield File.join(folder, File.basename(key)) }
|
|
169
|
+
rescue Errno::ENOENT, Errno::ENOTEMPTY, Errno::EEXIST
|
|
170
|
+
nil
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# Gives the block a path to this folder that stays this folder, whatever is swapped meanwhile.
|
|
174
|
+
def within(name, create: false)
|
|
175
|
+
handles = [File.open(@root)]
|
|
176
|
+
(name.split("/") - ["."]).each { |step| handles << enter(handles.last, step, create) }
|
|
177
|
+
yield path_to(handles.last)
|
|
178
|
+
rescue Errno::ELOOP, Errno::ENOTDIR
|
|
179
|
+
raise Error, "#{name} in the log folder has a link in it, or is not a folder"
|
|
180
|
+
ensure
|
|
181
|
+
handles&.each(&:close)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def enter(handle, step, create)
|
|
185
|
+
raise Errno::ENOTDIR if step == ".."
|
|
186
|
+
|
|
187
|
+
path = File.join(path_to(handle), step)
|
|
188
|
+
make(path) if create
|
|
189
|
+
File.open(path, OPEN).tap do |folder|
|
|
190
|
+
next if folder.stat.directory?
|
|
191
|
+
|
|
192
|
+
folder.close
|
|
193
|
+
raise Errno::ENOTDIR
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def make(path)
|
|
198
|
+
Dir.mkdir(path, 0o750)
|
|
199
|
+
rescue Errno::EEXIST
|
|
200
|
+
nil
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def path_to(handle)
|
|
204
|
+
File.directory?(HANDLES) ? File.join(HANDLES, handle.fileno.to_s) : handle.path
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# What a store that was killed left behind.
|
|
208
|
+
def tidy(folder)
|
|
209
|
+
Dir.children(folder).grep(TEMPORARY).each do |name|
|
|
210
|
+
path = File.join(folder, name)
|
|
211
|
+
File.delete(path) if File.lstat(path).mtime < Time.now - 86_400
|
|
212
|
+
rescue Errno::ENOENT
|
|
213
|
+
next
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def settled?(key, stat)
|
|
218
|
+
stat.file? && !(key.start_with?(PARTS) && stat.mtime > Time.now - SETTLE)
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# Amazon S3 and anything that speaks its protocol: DigitalOcean Spaces, MinIO, Garage, ...
|
|
223
|
+
class S3
|
|
224
|
+
def self.bucket(name, options)
|
|
225
|
+
require "aws-sdk-s3"
|
|
226
|
+
Aws::S3::Resource.new(**options).bucket(name)
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def initialize(bucket)
|
|
230
|
+
@bucket = bucket
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def keys(prefix)
|
|
234
|
+
@bucket.objects(prefix:).map(&:key)
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def to_s
|
|
238
|
+
"s3:#{@bucket.client.config.endpoint}/#{@bucket.name}"
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# In one request: left to itself the library asks for the size first, twice the requests for a
|
|
242
|
+
# day's thousand parts. A file of several GB comes in one piece too, which is slower but works.
|
|
243
|
+
def fetch(key, path)
|
|
244
|
+
transfer.download_file(path, bucket: @bucket.name, key:, mode: "single_request")
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def version(key)
|
|
248
|
+
@bucket.object(key).etag
|
|
249
|
+
rescue Aws::S3::Errors::NotFound
|
|
250
|
+
nil
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
# The storage itself refuses to replace another version. Some that speak the protocol do not know
|
|
254
|
+
# how, and say so or do as if they had: for those it is looked at just before.
|
|
255
|
+
def store(path, key, was)
|
|
256
|
+
raise Changed, key unless version(key) == was
|
|
257
|
+
|
|
258
|
+
upload(path, key, was ? { if_match: was } : { if_none_match: "*" })
|
|
259
|
+
rescue Aws::S3::Errors::ServiceError, Aws::S3::MultipartUploadError => e
|
|
260
|
+
raise Changed, key unless version(key) == was
|
|
261
|
+
raise unless codes(e).include?("NotImplemented")
|
|
262
|
+
|
|
263
|
+
upload(path, key, {})
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
def delete(keys)
|
|
267
|
+
keys.each_slice(1000) do |slice|
|
|
268
|
+
failed = @bucket.delete_objects(delete: { objects: slice.map { |key| { key: } }, quiet: true }).errors
|
|
269
|
+
raise Error, "could not delete #{failed.first.key}: #{failed.first.message}" if failed.any?
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
private
|
|
274
|
+
|
|
275
|
+
def upload(path, key, only_if)
|
|
276
|
+
transfer.upload_file(path, bucket: @bucket.name, key:, **only_if)
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
def codes(error)
|
|
280
|
+
errors = error.respond_to?(:errors) ? error.errors : [error]
|
|
281
|
+
errors.filter_map { |one| one.code if one.respond_to?(:code) }
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
# Files of any size, in several pieces when they are large. Needs aws-sdk-s3 1.197 or newer.
|
|
285
|
+
def transfer
|
|
286
|
+
@transfer ||= Aws::S3::TransferManager.new(client: @bucket.client)
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
# Google Cloud Storage.
|
|
291
|
+
class GCS
|
|
292
|
+
def self.bucket(name, options)
|
|
293
|
+
require "google/cloud/storage"
|
|
294
|
+
Google::Cloud::Storage.new(**options).bucket(name, skip_lookup: true)
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
def initialize(bucket)
|
|
298
|
+
@bucket = bucket
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def keys(prefix)
|
|
302
|
+
@bucket.files(prefix:).all.map(&:name)
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
def to_s
|
|
306
|
+
"gcs:#{@bucket.name}"
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# As it is stored: left to itself the library unpacks gzip in memory, however much that comes to.
|
|
310
|
+
def fetch(key, path)
|
|
311
|
+
@bucket.file(key, skip_lookup: true).download(path, skip_decompress: true)
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
def version(key)
|
|
315
|
+
@bucket.file(key)&.generation
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
# The storage itself refuses to replace another version; 0 is one that is not there.
|
|
319
|
+
def store(path, key, was)
|
|
320
|
+
@bucket.create_file(path, key, if_generation_match: was || 0)
|
|
321
|
+
rescue Google::Cloud::FailedPreconditionError
|
|
322
|
+
raise Changed, key
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
# One request per file, so several at a time. A file that is gone already, another run removed.
|
|
326
|
+
def delete(keys)
|
|
327
|
+
Storage.side_by_side(keys) do |key|
|
|
328
|
+
@bucket.file(key, skip_lookup: true).delete
|
|
329
|
+
rescue Google::Cloud::NotFoundError
|
|
330
|
+
next
|
|
331
|
+
end
|
|
332
|
+
end
|
|
333
|
+
end
|
|
334
|
+
end
|
|
335
|
+
end
|