asgard 0.6.0 → 0.6.1
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 +4 -4
- data/CHANGELOG.md +93 -0
- data/README.md +8 -2
- data/lib/asgard/base/dependency_graph.rb +19 -11
- data/lib/asgard/base/dispatch.rb +17 -3
- data/lib/asgard/base/task_dsl.rb +6 -0
- data/lib/asgard/base.rb +11 -4
- data/lib/asgard/doctor/report.rb +5 -1
- data/lib/asgard/doctor.rb +30 -24
- data/lib/asgard/jobs/commands.rb +38 -7
- data/lib/asgard/jobs/state.rb +12 -7
- data/lib/asgard/kernel_methods.rb +8 -18
- data/lib/asgard/leading_flag.rb +16 -0
- data/lib/asgard/log.rb +90 -0
- data/lib/asgard/pid.rb +4 -21
- data/lib/asgard/schedule/commands.rb +30 -8
- data/lib/asgard/schedule/declaration.rb +27 -22
- data/lib/asgard/schedule/launchd.rb +8 -9
- data/lib/asgard/schedule/systemd.rb +4 -2
- data/lib/asgard/schedule/table.rb +25 -21
- data/lib/asgard/schedule.rb +2 -2
- data/lib/asgard/shell.rb +6 -12
- data/lib/asgard/state_dir.rb +7 -1
- data/lib/asgard/version.rb +1 -1
- data/lib/asgard/watch/commands.rb +11 -1
- data/lib/asgard/watch/daemon.rb +55 -51
- data/lib/asgard/watch/reporter.rb +13 -7
- data/lib/asgard/watch/spawn.rb +3 -3
- data/lib/asgard.rb +23 -5
- metadata +4 -1
data/lib/asgard/log.rb
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Asgard
|
|
4
|
+
# Keeps the last M lines of a log, rewriting it in place. Every writer
|
|
5
|
+
# (launchd, systemd's append:, jobs, the watcher) opens its log with
|
|
6
|
+
# O_APPEND, so it keeps writing to the same file at the new end. As with
|
|
7
|
+
# logrotate's copytruncate, a write landing between the last read and the
|
|
8
|
+
# truncate can be lost.
|
|
9
|
+
module Log
|
|
10
|
+
CHUNK = 64 * 1024
|
|
11
|
+
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
def line_count(value)
|
|
15
|
+
raise ArgumentError, "--lines must be a nonnegative integer" unless value.to_s.match?(/\A[0-9]+\z/)
|
|
16
|
+
|
|
17
|
+
Integer(value.to_s, 10)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# The byte offset where the last +lines+ lines of +io+ begin. Scans
|
|
21
|
+
# backwards in bounded chunks; a final newline ends the last line
|
|
22
|
+
# rather than starting an empty one.
|
|
23
|
+
def tail_offset(io, lines)
|
|
24
|
+
position = io.size
|
|
25
|
+
return position if lines.zero? || position.zero?
|
|
26
|
+
|
|
27
|
+
io.seek(-1, IO::SEEK_END)
|
|
28
|
+
remaining = lines + (io.read(1) == "\n" ? 1 : 0)
|
|
29
|
+
while position.positive?
|
|
30
|
+
length = [position, CHUNK].min
|
|
31
|
+
position -= length
|
|
32
|
+
io.seek(position)
|
|
33
|
+
chunk = io.read(length)
|
|
34
|
+
index = chunk.bytesize
|
|
35
|
+
while index.positive? && (index = chunk.rindex("\n", index - 1))
|
|
36
|
+
remaining -= 1
|
|
37
|
+
return position + index + 1 if remaining.zero?
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
0
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Bytes removed (or that would be, with +dry_run+).
|
|
44
|
+
def truncate(path, lines:, dry_run: false)
|
|
45
|
+
File.open(path, dry_run ? "rb" : "r+b") do |file|
|
|
46
|
+
offset = tail_offset(file, lines)
|
|
47
|
+
rewrite(file, offset) unless dry_run || offset.zero?
|
|
48
|
+
offset
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Slides the bytes from +offset+ on to the start of +file+, a chunk at a
|
|
53
|
+
# time, then cuts the file after them. Each write lands behind the next
|
|
54
|
+
# read, so no copy is needed, and lines appended meanwhile move too.
|
|
55
|
+
# Signals and Thread#raise wait until the file is cut. A failed read or
|
|
56
|
+
# write still cuts the file after the bytes already moved, so it never
|
|
57
|
+
# holds the tail written over its own head.
|
|
58
|
+
def rewrite(file, offset)
|
|
59
|
+
moved = 0
|
|
60
|
+
Thread.handle_interrupt(Object => :never) do
|
|
61
|
+
while (chunk = read_at(file, offset + moved))
|
|
62
|
+
file.seek(moved)
|
|
63
|
+
file.write(chunk)
|
|
64
|
+
moved += chunk.bytesize
|
|
65
|
+
end
|
|
66
|
+
file.truncate(moved)
|
|
67
|
+
rescue StandardError
|
|
68
|
+
file.truncate(moved) if moved.positive?
|
|
69
|
+
raise
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Up to CHUNK bytes of +file+ from +position+, or nil at the end.
|
|
74
|
+
def read_at(file, position)
|
|
75
|
+
file.seek(position)
|
|
76
|
+
file.read(CHUNK)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def truncate_logs(paths, lines:, dry_run: false, out: $stdout)
|
|
80
|
+
count = line_count(lines)
|
|
81
|
+
found = paths.select { |path| File.file?(path) }.uniq.sort
|
|
82
|
+
return out.puts("No logs found for this project.") if found.empty?
|
|
83
|
+
|
|
84
|
+
found.each do |path|
|
|
85
|
+
bytes = truncate(path, lines: count, dry_run:)
|
|
86
|
+
out.puts "#{dry_run ? 'would truncate' : 'truncated'} #{path}: #{bytes} bytes #{dry_run ? 'would be removed' : 'removed'}"
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
data/lib/asgard/pid.rb
CHANGED
|
@@ -48,27 +48,10 @@ module Asgard
|
|
|
48
48
|
nil
|
|
49
49
|
end
|
|
50
50
|
|
|
51
|
-
#
|
|
52
|
-
#
|
|
53
|
-
#
|
|
54
|
-
|
|
55
|
-
out, status = Open3.capture2(PS, "-ww", "-A", "-o", "pgid=,command=")
|
|
56
|
-
status.success? ? group_commands(out, pgid) : []
|
|
57
|
-
rescue SystemCallError
|
|
58
|
-
[]
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
# The commands in `ps -o pgid=,command=` output that belong to +pgid+.
|
|
62
|
-
def group_commands(ps_output, pgid)
|
|
63
|
-
ps_output.each_line.filter_map do |line|
|
|
64
|
-
group, command = line.strip.split(/\s+/, 2)
|
|
65
|
-
command.to_s if group.to_i == pgid
|
|
66
|
-
end
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
# Every process in group +pgid+ as Members. Like group_command_lines,
|
|
70
|
-
# but with each one's age, so a caller can tell a group that has kept
|
|
71
|
-
# its members from a later group that reused the id.
|
|
51
|
+
# Every process in group +pgid+ as Members, each with its age, so a
|
|
52
|
+
# caller can tell a group that has kept its members from a later
|
|
53
|
+
# group that reused the id. ps by group id is not portable, so this
|
|
54
|
+
# lists every process with its group and keeps the matching ones.
|
|
72
55
|
def group_processes(pgid, now: Time.now)
|
|
73
56
|
out, status = Open3.capture2(PS, "-ww", "-A", "-o", "pid=,pgid=,etime=,command=")
|
|
74
57
|
status.success? ? parse_group(out, pgid, now:) : []
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "shellwords"
|
|
4
|
+
require_relative "../log"
|
|
4
5
|
|
|
5
6
|
# warn is silent when $VERBOSE is nil and bypasses $stderr in Ruby 4.0 (see
|
|
6
7
|
# base/task_dsl.rb); these messages are for the user, so they always print.
|
|
@@ -22,7 +23,7 @@ module Asgard
|
|
|
22
23
|
no_commands do
|
|
23
24
|
def schedules = Schedule.declarations
|
|
24
25
|
|
|
25
|
-
def schedule_root = @schedule_root ||=
|
|
26
|
+
def schedule_root = @schedule_root ||= Asgard.project_root
|
|
26
27
|
|
|
27
28
|
def schedule_project = @schedule_project ||= Schedule.project_slug(schedule_root)
|
|
28
29
|
|
|
@@ -118,6 +119,17 @@ module Asgard
|
|
|
118
119
|
find_installed(name) || abort("#{name} is neither declared nor installed (see `asgard schedule list --all`).")
|
|
119
120
|
end
|
|
120
121
|
|
|
122
|
+
# Whether +path+ is NAME's log to truncate. A declared entry that has
|
|
123
|
+
# not run yet says so and is skipped; any other name aborts.
|
|
124
|
+
def log_to_truncate?(name, path)
|
|
125
|
+
found = File.file?(path)
|
|
126
|
+
abort "schedule: no such schedule log: #{name}" if name.include?("/") || !(found || schedules.key?(name))
|
|
127
|
+
return true if found
|
|
128
|
+
|
|
129
|
+
$stderr.puts "#{name} has no log yet (#{path}); nothing to truncate"
|
|
130
|
+
false
|
|
131
|
+
end
|
|
132
|
+
|
|
121
133
|
# True when list should cover every project: asked for with --all, or
|
|
122
134
|
# there is no .loki here, so no single project to scope to.
|
|
123
135
|
def list_all? = options[:all] || Asgard.find_task_file.nil?
|
|
@@ -159,16 +171,12 @@ module Asgard
|
|
|
159
171
|
|
|
160
172
|
def entry_rows(entries) = entries.map { |slug, name| entry_row(slug, name) }
|
|
161
173
|
|
|
162
|
-
# This project's entries keep their declaration columns
|
|
174
|
+
# This project's entries keep their declaration columns. Another
|
|
175
|
+
# project's .loki isn't loaded, so its command and schedule are read
|
|
176
|
+
# back from the installed job files.
|
|
163
177
|
def entry_row(slug, name)
|
|
164
178
|
return schedule_row(name) if slug == current_slug
|
|
165
179
|
|
|
166
|
-
other_project_row(slug, name)
|
|
167
|
-
end
|
|
168
|
-
|
|
169
|
-
# A row for an entry of another project: its .loki isn't loaded, so the
|
|
170
|
-
# command and schedule are read back from the installed job files.
|
|
171
|
-
def other_project_row(slug, name)
|
|
172
180
|
backend = backend_for(slug)
|
|
173
181
|
shown = backend.installed_columns(name) || Schedule.declared_columns(nil, "(unreadable)")
|
|
174
182
|
{ name:, **shown, **Schedule.live_columns(backend, name) }
|
|
@@ -230,6 +238,20 @@ module Asgard
|
|
|
230
238
|
IO.copy_stream(path, $stdout)
|
|
231
239
|
end
|
|
232
240
|
|
|
241
|
+
desc "truncate_log [NAME]", "Keep the last M lines of an entry's log; no NAME truncates every schedule log of this project"
|
|
242
|
+
method_option :lines, type: :string, default: "1000", banner: "M", desc: "Lines to keep per log; 0 empties it"
|
|
243
|
+
method_option :dry_run, aliases: "-n", type: :boolean, desc: "Report what would be removed without changing logs"
|
|
244
|
+
def truncate_log(name = nil)
|
|
245
|
+
Log.line_count(options[:lines]) # reject a bad --lines even when there is no log to truncate
|
|
246
|
+
pattern = scheduler.log_path("*")
|
|
247
|
+
paths = name ? [scheduler.log_path(name)] : Dir.glob(File.join(Asgard.glob_escape(File.dirname(pattern)), File.basename(pattern)))
|
|
248
|
+
return if name && !log_to_truncate?(name, paths.first)
|
|
249
|
+
|
|
250
|
+
Log.truncate_logs(paths, lines: options[:lines], dry_run: options[:dry_run])
|
|
251
|
+
rescue ArgumentError, SystemCallError, IOError => e
|
|
252
|
+
abort "schedule: #{e.message}"
|
|
253
|
+
end
|
|
254
|
+
|
|
233
255
|
desc "remove [NAME]", "Remove one installed entry (NAME or project/NAME), or all of this project's"
|
|
234
256
|
def remove(name = nil)
|
|
235
257
|
return remove_one(name) if name
|
|
@@ -63,12 +63,13 @@ module Asgard
|
|
|
63
63
|
end
|
|
64
64
|
|
|
65
65
|
class << self
|
|
66
|
-
# The runner new backends get
|
|
67
|
-
# instead of running them; nil restores RUNNER.
|
|
68
|
-
attr_writer :runner
|
|
69
|
-
|
|
66
|
+
# The runner new backends get: RUNNER, unless use_runner swapped it.
|
|
70
67
|
def runner = @runner || RUNNER
|
|
71
68
|
|
|
69
|
+
# Tests swap in a runner that records commands instead of running
|
|
70
|
+
# them; nil restores RUNNER.
|
|
71
|
+
def use_runner(runner) = @runner = runner
|
|
72
|
+
|
|
72
73
|
# The backend class for +platform+ (a RUBY_PLATFORM string).
|
|
73
74
|
def backend_class(platform = RUBY_PLATFORM)
|
|
74
75
|
case platform
|
|
@@ -159,7 +160,7 @@ module Asgard
|
|
|
159
160
|
# The task alone, or a slug of the whole command when it has arguments,
|
|
160
161
|
# so one task can be scheduled more than once with different flags.
|
|
161
162
|
def entry_name(task, args, as = nil)
|
|
162
|
-
name = (as || (args.empty? ? task : command_slug(task, args))).to_s
|
|
163
|
+
name = (as || (args.empty? ? task : command_slug([task, *args]))).to_s
|
|
163
164
|
raise ArgumentError, "schedule as: #{name.inspect} may only contain letters, digits, _ . -" unless name.match?(/\A[\w.-]+\z/)
|
|
164
165
|
|
|
165
166
|
name
|
|
@@ -172,18 +173,19 @@ module Asgard
|
|
|
172
173
|
# `period week` and `report_period` with `week`. Schedule.declare
|
|
173
174
|
# gives each of two such rivals its hashed_slug, so different commands
|
|
174
175
|
# never share a name.
|
|
175
|
-
def command_slug(
|
|
176
|
-
words =
|
|
176
|
+
def command_slug(words)
|
|
177
|
+
words = words.map(&:to_s)
|
|
177
178
|
name = slug(words.join(" "))
|
|
178
179
|
return name if name.length <= NAME_MAX && words.drop(1).all? { |arg| arg.match?(PLAIN_ARG) }
|
|
179
180
|
|
|
180
|
-
hashed_slug(
|
|
181
|
+
hashed_slug(words)
|
|
181
182
|
end
|
|
182
183
|
|
|
183
|
-
# The slug
|
|
184
|
-
# NAME_MAX:
|
|
185
|
-
|
|
186
|
-
|
|
184
|
+
# The slug of a command's +words+ (the task, then its arguments) with a
|
|
185
|
+
# short hash of the exact words appended, capped at NAME_MAX:
|
|
186
|
+
# "report-period-week-1a2b3c4d".
|
|
187
|
+
def hashed_slug(words)
|
|
188
|
+
words = words.map(&:to_s)
|
|
187
189
|
digest = Digest::SHA256.hexdigest(words.join("\0"))[0, 8]
|
|
188
190
|
"#{slug(words.join(' '))[0, NAME_MAX - 9].delete_suffix('-')}-#{digest}"
|
|
189
191
|
end
|
|
@@ -207,22 +209,24 @@ module Asgard
|
|
|
207
209
|
# For display: the command line the job runs.
|
|
208
210
|
def command_line(task, args = []) = Shellwords.join(["asgard", task.to_s, *args])
|
|
209
211
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
+
# A declaration's schedule in words: "every 60s", or its times and days
|
|
213
|
+
# ("17:30 weekdays"; daily when +spec+ has no :on).
|
|
214
|
+
def describe(spec)
|
|
215
|
+
return "every #{spec[:every]}s" if spec[:every]
|
|
212
216
|
|
|
213
|
-
"#{Array(at).join(', ')} #{Array(on).join(', ')}"
|
|
217
|
+
"#{Array(spec[:at]).join(', ')} #{Array(spec[:on] || :daily).join(', ')}"
|
|
214
218
|
end
|
|
215
219
|
|
|
216
220
|
# For the `list` table: like describe, but a run of hourly times
|
|
217
221
|
# collapses ("10:00, 11:00 ... 17:00" => "10:00-17:00 hourly").
|
|
218
|
-
def describe_compact(
|
|
219
|
-
return "every #{every}s" if every
|
|
222
|
+
def describe_compact(spec)
|
|
223
|
+
return "every #{spec[:every]}s" if spec[:every]
|
|
220
224
|
|
|
221
|
-
"#{compact_times(Array(at))} #{Array(on).join(', ')}"
|
|
225
|
+
"#{compact_times(Array(spec[:at]))} #{Array(spec[:on] || :daily).join(', ')}"
|
|
222
226
|
end
|
|
223
227
|
|
|
224
228
|
# The command and schedule columns of a `list` row for a declared entry.
|
|
225
|
-
def list_columns(spec) = { command: command_line(spec[:task], spec[:args]), schedule: describe_compact(
|
|
229
|
+
def list_columns(spec) = { command: command_line(spec[:task], spec[:args]), schedule: describe_compact(spec) }
|
|
226
230
|
|
|
227
231
|
# The command and schedule columns of a `list` row: the declaration's, or
|
|
228
232
|
# `missing` in the schedule column when there is none.
|
|
@@ -355,9 +359,10 @@ module Asgard
|
|
|
355
359
|
# The job's argv. Schedulers need an absolute program. With direnv, the
|
|
356
360
|
# repo's .envrc (API keys, ...) is loaded at run time instead of being
|
|
357
361
|
# copied into the job definition, and direnv finds asgard on the job's
|
|
358
|
-
# PATH.
|
|
359
|
-
|
|
360
|
-
|
|
362
|
+
# PATH. +words+ is the task, then its arguments; each is its own
|
|
363
|
+
# element, so no shell re-splits them.
|
|
364
|
+
def program_arguments(words, root:, asgard:, direnv: nil)
|
|
365
|
+
argv = words.map(&:to_s)
|
|
361
366
|
direnv ? [direnv, "exec", root, "asgard", *argv] : [asgard, *argv]
|
|
362
367
|
end
|
|
363
368
|
end
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "fileutils"
|
|
4
|
+
require_relative "../state_dir"
|
|
4
5
|
require_relative "job_files"
|
|
5
6
|
|
|
6
7
|
module Asgard
|
|
@@ -128,7 +129,7 @@ module Asgard
|
|
|
128
129
|
klass = self.class
|
|
129
130
|
plist = klass.plist(
|
|
130
131
|
label: label(name),
|
|
131
|
-
arguments: Schedule.program_arguments(spec
|
|
132
|
+
arguments: Schedule.program_arguments(spec.values_at(:task, :args).flatten, root: @root, asgard:, direnv:),
|
|
132
133
|
working_directory: @root,
|
|
133
134
|
environment: Schedule.environment(spec),
|
|
134
135
|
log_path: log_path(name),
|
|
@@ -147,10 +148,11 @@ module Asgard
|
|
|
147
148
|
log = log_path(name)
|
|
148
149
|
content = files(spec, asgard:, direnv:).fetch(plist)
|
|
149
150
|
Schedule.check_lengths!([plist, "#{plist}.new", log])
|
|
151
|
+
Schedule.writing { StateDir.private_dir File.dirname(log) } # the logs hold the task's output
|
|
150
152
|
return :active if self.class.unchanged?(plist, content) && loaded?(name)
|
|
151
153
|
|
|
152
154
|
Schedule.writing do
|
|
153
|
-
FileUtils.mkdir_p
|
|
155
|
+
FileUtils.mkdir_p File.dirname(plist)
|
|
154
156
|
write_linted(plist, content)
|
|
155
157
|
end
|
|
156
158
|
unload(name)
|
|
@@ -246,20 +248,17 @@ module Asgard
|
|
|
246
248
|
return unless loaded?(name)
|
|
247
249
|
|
|
248
250
|
out, ok = run("launchctl", "bootout", target(name))
|
|
249
|
-
return if ok && unloaded?(name)
|
|
250
|
-
return unless loaded?(name)
|
|
251
|
+
return if (ok && unloaded?(name)) || !loaded?(name)
|
|
251
252
|
|
|
252
253
|
raise Error, "launchctl bootout #{target(name)} left #{label(name)} still loaded: #{out.strip}"
|
|
253
254
|
end
|
|
254
255
|
|
|
255
256
|
# Polls for up to two seconds; true once the job is gone.
|
|
256
257
|
def unloaded?(name)
|
|
257
|
-
20.times do
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
sleep 0.1
|
|
258
|
+
20.times.any? do |attempt|
|
|
259
|
+
sleep 0.1 if attempt.positive?
|
|
260
|
+
!loaded?(name)
|
|
261
261
|
end
|
|
262
|
-
false
|
|
263
262
|
end
|
|
264
263
|
|
|
265
264
|
def run(*argv) = @runner.call(*argv)
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "fileutils"
|
|
4
4
|
require "etc"
|
|
5
|
+
require_relative "../state_dir"
|
|
5
6
|
require_relative "job_files"
|
|
6
7
|
|
|
7
8
|
module Asgard
|
|
@@ -159,7 +160,7 @@ module Asgard
|
|
|
159
160
|
description = "#{Schedule.command_line(task, args)} (#{@project})"
|
|
160
161
|
service = klass.service_unit(
|
|
161
162
|
description:,
|
|
162
|
-
arguments: Schedule.program_arguments(task, args, root: @root, asgard:, direnv:),
|
|
163
|
+
arguments: Schedule.program_arguments([task, *args], root: @root, asgard:, direnv:),
|
|
163
164
|
working_directory: @root,
|
|
164
165
|
environment: Schedule.environment(spec),
|
|
165
166
|
log_path: log_path(name)
|
|
@@ -266,7 +267,8 @@ module Asgard
|
|
|
266
267
|
def write_changed(job, name)
|
|
267
268
|
changed = self.class.changed_files(job)
|
|
268
269
|
Schedule.writing do
|
|
269
|
-
FileUtils.mkdir_p
|
|
270
|
+
FileUtils.mkdir_p File.dirname(timer_path(name))
|
|
271
|
+
StateDir.private_dir File.dirname(log_path(name)) # the logs hold the task's output
|
|
270
272
|
changed.each { |path, content| Schedule.write_private(path, content) }
|
|
271
273
|
end
|
|
272
274
|
end
|
|
@@ -14,6 +14,25 @@ module Asgard
|
|
|
14
14
|
PADDING = 1 # spaces either side of a cell
|
|
15
15
|
EXTRA = PADDING * 2 # what a column adds to its content
|
|
16
16
|
|
|
17
|
+
# The color of a styled cell, by its column.
|
|
18
|
+
COLORS = {
|
|
19
|
+
state: ->(state) { { "active" => :green, "running" => :green, "stopped" => :yellow }.fetch(state.split.first, :red) },
|
|
20
|
+
last_exit: ->(last_exit) { last_exit.match?(/\A(?:[1-9]\d*\b|signal )/) ? :red : :dim }
|
|
21
|
+
}.freeze
|
|
22
|
+
|
|
23
|
+
# +widths+ (padding included) made to fit +limit+ columns of screen:
|
|
24
|
+
# the widest of the +flexible+ columns gives up a character at a time
|
|
25
|
+
# until the table fits or that column is down to MIN_WIDTH.
|
|
26
|
+
def self.fit(widths, flexible:, limit:)
|
|
27
|
+
widths = widths.dup
|
|
28
|
+
loop do
|
|
29
|
+
widest = flexible.max_by { |index| widths[index] }
|
|
30
|
+
return widths unless widths.sum + widths.size + 1 > limit && widths[widest] > MIN_WIDTH
|
|
31
|
+
|
|
32
|
+
widths[widest] -= 1
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
17
36
|
# Columns available for output: the terminal's when io is one, else
|
|
18
37
|
# $COLUMNS, else a roomy default (piped output shouldn't wrap at 80).
|
|
19
38
|
def self.terminal_width(io = $stdout, env: ENV, default: 120)
|
|
@@ -36,14 +55,11 @@ module Asgard
|
|
|
36
55
|
def initialize(rows, color:, width:, headers: HEADERS, keys: KEYS, flexible: FLEXIBLE)
|
|
37
56
|
require "pastel"
|
|
38
57
|
|
|
39
|
-
@headers
|
|
40
|
-
@
|
|
41
|
-
@
|
|
42
|
-
@
|
|
43
|
-
@
|
|
44
|
-
@width = width
|
|
45
|
-
@column_widths = natural_widths
|
|
46
|
-
shrink_widest while overflowing? && shrinkable?
|
|
58
|
+
@headers = headers
|
|
59
|
+
@styled = COLORS.keys.to_h { |column| [column, keys.index(column)] }.compact
|
|
60
|
+
@cells = rows.map { |row| row.values_at(*keys).map(&:to_s) }
|
|
61
|
+
@pastel = Pastel.new(enabled: color)
|
|
62
|
+
@column_widths = Table.fit(natural_widths, flexible:, limit: width)
|
|
47
63
|
end
|
|
48
64
|
|
|
49
65
|
def render
|
|
@@ -74,24 +90,12 @@ module Asgard
|
|
|
74
90
|
|
|
75
91
|
def natural_widths = [@headers, *@cells].transpose.map { |column| column.max_by(&:length).length + EXTRA }
|
|
76
92
|
|
|
77
|
-
def overflowing? = total_width > @width
|
|
78
|
-
|
|
79
|
-
def widest_flexible = @flexible.max_by { |index| @column_widths[index] }
|
|
80
|
-
|
|
81
|
-
def shrinkable? = @column_widths[widest_flexible] > MIN_WIDTH
|
|
82
|
-
|
|
83
|
-
def shrink_widest = @column_widths[widest_flexible] -= 1
|
|
84
|
-
|
|
85
93
|
# The state and last-exit cells colored, wherever keys: put them.
|
|
86
94
|
def styled(cells)
|
|
87
95
|
cells = cells.dup
|
|
88
|
-
@styled.each { |column, index| cells[index] = @pastel.decorate(cells[index],
|
|
96
|
+
@styled.each { |column, index| cells[index] = @pastel.decorate(cells[index], COLORS.fetch(column).call(cells[index])) }
|
|
89
97
|
cells
|
|
90
98
|
end
|
|
91
|
-
|
|
92
|
-
def state_color(state) = { "active" => :green, "running" => :green, "stopped" => :yellow }.fetch(state.split.first, :red)
|
|
93
|
-
|
|
94
|
-
def last_exit_color(last_exit) = last_exit.match?(/\A(?:[1-9]\d*\b|signal )/) ? :red : :dim
|
|
95
99
|
end
|
|
96
100
|
end
|
|
97
101
|
end
|
data/lib/asgard/schedule.rb
CHANGED
|
@@ -31,7 +31,7 @@ module Asgard
|
|
|
31
31
|
def declare(task, **) = Declarations.record(declarations, place(normalize(task, **)), "schedule")
|
|
32
32
|
|
|
33
33
|
# The name a command derives (a normalize result's task and args).
|
|
34
|
-
def plain_name(spec) = command_slug(spec
|
|
34
|
+
def plain_name(spec) = command_slug(spec.values_at(:task, :args).flatten)
|
|
35
35
|
|
|
36
36
|
# Whether +spec+ has the plain name its command derives, rather than
|
|
37
37
|
# an as: name or a hashed one.
|
|
@@ -43,7 +43,7 @@ module Asgard
|
|
|
43
43
|
derived_name?(one) && derived_name?(other) && one.values_at(:task, :args) != other.values_at(:task, :args)
|
|
44
44
|
end
|
|
45
45
|
|
|
46
|
-
def with_hashed_name(spec) = spec.merge(name: hashed_slug(spec
|
|
46
|
+
def with_hashed_name(spec) = spec.merge(name: hashed_slug(spec.values_at(:task, :args).flatten))
|
|
47
47
|
|
|
48
48
|
# Whether +spec+'s plain name was already given up by a rival in
|
|
49
49
|
# +declarations+, one now under its hashed name, so +spec+ takes its
|
data/lib/asgard/shell.rb
CHANGED
|
@@ -161,9 +161,12 @@ module Asgard
|
|
|
161
161
|
# The executable must exist: an executable file at that path, or a
|
|
162
162
|
# bare name found on PATH. Otherwise ArgumentError, as the .loki loads.
|
|
163
163
|
def shell(name_or_path, path = nil, args: nil, ext: nil)
|
|
164
|
-
name, path
|
|
165
|
-
|
|
166
|
-
|
|
164
|
+
# A Symbol first is the name, and its path defaults to the name
|
|
165
|
+
# itself; a String or Pathname first is the path, and the name
|
|
166
|
+
# comes from the entry.
|
|
167
|
+
name = name_or_path if name_or_path.is_a?(Symbol)
|
|
168
|
+
entry = Shell.entry(name ? path || name.to_s : name_or_path, args:, ext:)
|
|
169
|
+
name ||= Shell.name_for(entry)
|
|
167
170
|
Shell.shells[name] = Shell.validate!(name, entry)
|
|
168
171
|
name
|
|
169
172
|
end
|
|
@@ -178,15 +181,6 @@ module Asgard
|
|
|
178
181
|
Shell.validate!(name, Shell.shell_for(name))
|
|
179
182
|
name
|
|
180
183
|
end
|
|
181
|
-
|
|
182
|
-
# The [name, path] of a `shell` call: a Symbol first is the name, and
|
|
183
|
-
# its path defaults to the name itself; a String or Pathname first is
|
|
184
|
-
# the path, and the name comes from the entry.
|
|
185
|
-
def split_shell_args(name_or_path, path)
|
|
186
|
-
return [nil, name_or_path] unless name_or_path.is_a?(Symbol)
|
|
187
|
-
|
|
188
|
-
[name_or_path, path || name_or_path.to_s]
|
|
189
|
-
end
|
|
190
184
|
end
|
|
191
185
|
|
|
192
186
|
def self.included(base) = base.extend(ClassMethods)
|
data/lib/asgard/state_dir.rb
CHANGED
|
@@ -76,9 +76,15 @@ module Asgard
|
|
|
76
76
|
# Creates +dir+, readable by its owner only (logs and job scripts can
|
|
77
77
|
# hold secrets), and records the project root in it.
|
|
78
78
|
def record_root(dir, root)
|
|
79
|
+
private_dir(dir)
|
|
80
|
+
File.write(File.join(dir, ROOT_FILE), "#{root}\n")
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Creates +dir+ (and any missing parents) readable by its owner only,
|
|
84
|
+
# tightening an existing one: mkdir_p's mode: applies only to what it creates.
|
|
85
|
+
def private_dir(dir)
|
|
79
86
|
FileUtils.mkdir_p(dir, mode: 0o700)
|
|
80
87
|
File.chmod(0o700, dir)
|
|
81
|
-
File.write(File.join(dir, ROOT_FILE), "#{root}\n")
|
|
82
88
|
end
|
|
83
89
|
end
|
|
84
90
|
end
|
data/lib/asgard/version.rb
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "shellwords"
|
|
4
4
|
require "time"
|
|
5
|
+
require_relative "../log"
|
|
5
6
|
|
|
6
7
|
# warn is silent when $VERBOSE is nil and bypasses $stderr in Ruby 4.0 (see
|
|
7
8
|
# base/task_dsl.rb); these messages are for the user, so they always print.
|
|
@@ -57,7 +58,7 @@ module Asgard
|
|
|
57
58
|
no_commands do
|
|
58
59
|
def watches = Watch.declarations
|
|
59
60
|
|
|
60
|
-
def watch_root = @watch_root ||=
|
|
61
|
+
def watch_root = @watch_root ||= Asgard.project_root
|
|
61
62
|
|
|
62
63
|
def watch_project = @watch_project ||= Schedule.project_slug(watch_root)
|
|
63
64
|
|
|
@@ -245,6 +246,15 @@ module Asgard
|
|
|
245
246
|
|
|
246
247
|
IO.copy_stream(path, $stdout)
|
|
247
248
|
end
|
|
249
|
+
|
|
250
|
+
desc "truncate_log", "Keep the last M lines of this project's watcher logs; --lines 0 empties them"
|
|
251
|
+
method_option :lines, type: :string, default: "1000", banner: "M", desc: "Lines to keep per log; 0 empties it"
|
|
252
|
+
method_option :dry_run, aliases: "-n", type: :boolean, desc: "Report what would be removed without changing logs"
|
|
253
|
+
def truncate_log
|
|
254
|
+
Log.truncate_logs([state.log_path, "#{state.log_path}.1"], lines: options[:lines], dry_run: options[:dry_run])
|
|
255
|
+
rescue ArgumentError, SystemCallError, IOError => e
|
|
256
|
+
abort "watcher: #{e.message}"
|
|
257
|
+
end
|
|
248
258
|
end
|
|
249
259
|
end
|
|
250
260
|
end
|