asgard 0.5.1 → 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 +272 -99
- data/README.md +147 -46
- data/bin/asgard +10 -0
- data/lib/asgard/base/dependency_graph.rb +19 -11
- data/lib/asgard/base/dispatch.rb +272 -32
- data/lib/asgard/base/registry.rb +7 -3
- data/lib/asgard/base/task_dsl.rb +6 -0
- data/lib/asgard/base.rb +147 -9
- data/lib/asgard/declarations.rb +21 -0
- data/lib/asgard/doctor/report.rb +5 -1
- data/lib/asgard/doctor/task_sections.rb +11 -3
- data/lib/asgard/doctor.rb +41 -31
- data/lib/asgard/jobs/commands.rb +183 -0
- data/lib/asgard/jobs/declaration.rb +149 -0
- data/lib/asgard/jobs/launcher.rb +109 -0
- data/lib/asgard/jobs/state.rb +195 -0
- data/lib/asgard/jobs/stopper.rb +141 -0
- data/lib/asgard/jobs.rb +25 -0
- data/lib/asgard/kernel_methods.rb +25 -30
- data/lib/asgard/leading_flag.rb +26 -0
- data/lib/asgard/log.rb +90 -0
- data/lib/asgard/pid.rb +113 -0
- data/lib/asgard/schedule/commands.rb +68 -18
- data/lib/asgard/schedule/declaration.rb +158 -26
- data/lib/asgard/schedule/job_files.rb +24 -0
- data/lib/asgard/schedule/launchd.rb +57 -21
- data/lib/asgard/schedule/systemd.rb +72 -28
- data/lib/asgard/schedule/table.rb +48 -26
- data/lib/asgard/schedule.rb +49 -9
- data/lib/asgard/shell.rb +366 -44
- data/lib/asgard/state_dir.rb +90 -0
- data/lib/asgard/tasks.rb +8 -0
- data/lib/asgard/version.rb +1 -1
- data/lib/asgard/watch/changes.rb +26 -1
- data/lib/asgard/watch/commands.rb +63 -13
- data/lib/asgard/watch/daemon.rb +347 -52
- data/lib/asgard/watch/declaration.rb +135 -40
- data/lib/asgard/watch/dispatcher.rb +61 -10
- data/lib/asgard/watch/mailbox.rb +50 -0
- data/lib/asgard/watch/reporter.rb +54 -32
- data/lib/asgard/watch/spawn.rb +43 -18
- data/lib/asgard/watch/state.rb +93 -27
- data/lib/asgard/watch/worker.rb +44 -9
- data/lib/asgard/watch.rb +4 -11
- data/lib/asgard.rb +100 -16
- metadata +170 -186
- data/.envrc +0 -6
- data/.github/workflows/deploy-github-pages.yml +0 -52
- data/.loki +0 -39
- data/.reek.yml +0 -166
- data/.rubocop.yml +0 -163
- data/Archspec.rb +0 -24
- data/CLAUDE.md +0 -139
- data/COMMITS.md +0 -196
- data/bin/console +0 -11
- data/bin/setup +0 -8
- data/doc_tasks.loki +0 -21
- data/docs/api.md +0 -368
- data/docs/assets/css/custom.css +0 -93
- data/docs/assets/images/asgard.jpg +0 -0
- data/docs/changelog.md +0 -1
- data/docs/dependencies.md +0 -313
- data/docs/environment.md +0 -129
- data/docs/examples.md +0 -194
- data/docs/getting-started.md +0 -181
- data/docs/helpers.md +0 -262
- data/docs/index.md +0 -87
- data/docs/options.md +0 -224
- data/docs/schedule.md +0 -106
- data/docs/shell.md +0 -237
- data/docs/subcommands.md +0 -181
- data/docs/task-files.md +0 -407
- data/docs/tasks.md +0 -326
- data/docs/variables.md +0 -338
- data/docs/watch.md +0 -123
- data/examples/.env +0 -4
- data/examples/.envrc +0 -3
- data/examples/.loki +0 -24
- data/examples/bad.loki +0 -63
- data/examples/concurrent.loki +0 -58
- data/examples/confirm.loki +0 -48
- data/examples/db_subcommands.loki +0 -74
- data/examples/depends_on_block/bad/.loki +0 -51
- data/examples/depends_on_block/good/.loki +0 -83
- data/examples/do.loki +0 -45
- data/examples/env_usage.loki +0 -27
- data/examples/kitchen_sink.loki +0 -210
- data/examples/server_subcommands.loki +0 -65
- data/examples/subdir/.loki +0 -12
- data/examples/subdir/import_demo.loki +0 -14
- data/examples/subdir/import_up_demo.loki +0 -18
- data/examples/watcher_examples/.envrc +0 -3
- data/examples/watcher_examples/.loki +0 -54
- data/examples/watcher_examples/README.md +0 -112
- data/examples/watcher_examples/bin/hello +0 -3
- data/examples/watcher_examples/data/records.json +0 -1
- data/examples/watcher_examples/docs/drafts/ideas.md +0 -5
- data/examples/watcher_examples/docs/index.md +0 -4
- data/examples/watcher_examples/lib/greeter.rb +0 -7
- data/gem_tasks.loki +0 -51
- data/git.loki +0 -13
- data/mkdocs.yml +0 -166
- data/quality.loki +0 -266
- data/quality_rails.loki +0 -47
- data/xyzzy.loki +0 -12
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "fileutils"
|
|
4
|
+
require_relative "../state_dir"
|
|
5
|
+
require_relative "job_files"
|
|
4
6
|
|
|
5
7
|
module Asgard
|
|
6
8
|
module Schedule
|
|
@@ -28,6 +30,9 @@ module Asgard
|
|
|
28
30
|
# ("label" => disabled, or "label" => true on older macOS).
|
|
29
31
|
def self.disabled_labels(output) = output.scan(/"([^"]+)"\s*=>\s*(disabled|true)\b/).map(&:first)
|
|
30
32
|
|
|
33
|
+
# Whether the installed file at +path+ already holds +content+.
|
|
34
|
+
def self.unchanged?(path, content) = File.exist?(path) && File.read(path) == content
|
|
35
|
+
|
|
31
36
|
def self.plist(label:, arguments:, working_directory:, environment:, log_path:, intervals: nil, every: nil)
|
|
32
37
|
dict = {
|
|
33
38
|
"Label" => label,
|
|
@@ -124,7 +129,7 @@ module Asgard
|
|
|
124
129
|
klass = self.class
|
|
125
130
|
plist = klass.plist(
|
|
126
131
|
label: label(name),
|
|
127
|
-
arguments: Schedule.program_arguments(spec
|
|
132
|
+
arguments: Schedule.program_arguments(spec.values_at(:task, :args).flatten, root: @root, asgard:, direnv:),
|
|
128
133
|
working_directory: @root,
|
|
129
134
|
environment: Schedule.environment(spec),
|
|
130
135
|
log_path: log_path(name),
|
|
@@ -134,12 +139,22 @@ module Asgard
|
|
|
134
139
|
{ plist_path(name) => plist }
|
|
135
140
|
end
|
|
136
141
|
|
|
142
|
+
# An unchanged plist whose job is loaded is left alone: a bootout
|
|
143
|
+
# would kill a run in progress for nothing. A changed one is linted
|
|
144
|
+
# before it replaces the installed file, then reloaded.
|
|
137
145
|
def install(spec, asgard:, direnv:)
|
|
138
|
-
name
|
|
139
|
-
plist
|
|
140
|
-
|
|
141
|
-
files(spec, asgard:, direnv:).
|
|
142
|
-
|
|
146
|
+
name = spec[:name]
|
|
147
|
+
plist = plist_path(name)
|
|
148
|
+
log = log_path(name)
|
|
149
|
+
content = files(spec, asgard:, direnv:).fetch(plist)
|
|
150
|
+
Schedule.check_lengths!([plist, "#{plist}.new", log])
|
|
151
|
+
Schedule.writing { StateDir.private_dir File.dirname(log) } # the logs hold the task's output
|
|
152
|
+
return :active if self.class.unchanged?(plist, content) && loaded?(name)
|
|
153
|
+
|
|
154
|
+
Schedule.writing do
|
|
155
|
+
FileUtils.mkdir_p File.dirname(plist)
|
|
156
|
+
write_linted(plist, content)
|
|
157
|
+
end
|
|
143
158
|
unload(name)
|
|
144
159
|
return :stopped if stopped?(name)
|
|
145
160
|
|
|
@@ -171,11 +186,7 @@ module Asgard
|
|
|
171
186
|
end
|
|
172
187
|
|
|
173
188
|
# Every asgard entry on this machine, whatever its project: [[project_slug, name], ...].
|
|
174
|
-
def installed_entries
|
|
175
|
-
paths = Dir.glob(File.join(@home, "Library", "LaunchAgents", "#{LABEL_PREFIX}.*.plist"))
|
|
176
|
-
paths.map { |path| File.basename(path, ".plist").delete_prefix("#{LABEL_PREFIX}.").split(".", 2) }
|
|
177
|
-
.select { |entry| entry.size == 2 }.sort
|
|
178
|
-
end
|
|
189
|
+
def installed_entries = JobFiles.entries_in(File.join(@home, "Library", "LaunchAgents"), prefix: LABEL_PREFIX, ext: ".plist")
|
|
179
190
|
|
|
180
191
|
# { command:, schedule: } as installed, read from the plist; nil when there isn't one.
|
|
181
192
|
def installed_columns(name)
|
|
@@ -186,17 +197,23 @@ module Asgard
|
|
|
186
197
|
end
|
|
187
198
|
|
|
188
199
|
# The project root the job runs in, read from the plist; nil when there isn't one.
|
|
189
|
-
def installed_directory(name)
|
|
190
|
-
path = plist_path(name)
|
|
191
|
-
self.class.parse_directory(File.read(path)) if File.exist?(path)
|
|
192
|
-
end
|
|
200
|
+
def installed_directory(name) = JobFiles.directory(plist_path(name), self.class.method(:parse_directory))
|
|
193
201
|
|
|
194
202
|
def status(name)
|
|
195
203
|
out, ok = run("launchctl", "print", target(name))
|
|
196
204
|
return { state: stopped?(name) ? :stopped : :not_loaded, last_exit: nil } unless ok
|
|
197
205
|
|
|
206
|
+
{ state: out.include?("state = running") ? :running : :active, last_exit: self.class.last_exit(out) }
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# The last exit from `launchctl print`: its code, or the signal that
|
|
210
|
+
# ended it ("signal 15 (Terminated)"); nil when it never exited.
|
|
211
|
+
def self.last_exit(out)
|
|
198
212
|
code = out[/last exit code = (\d+)/, 1]
|
|
199
|
-
|
|
213
|
+
return code if code
|
|
214
|
+
|
|
215
|
+
signal = out.match(/last terminating signal = ([^:\n]+): (\d+)/)
|
|
216
|
+
Schedule.signal_exit(signal[2], signal[1]) if signal
|
|
200
217
|
end
|
|
201
218
|
|
|
202
219
|
def notes = []
|
|
@@ -205,6 +222,17 @@ module Asgard
|
|
|
205
222
|
|
|
206
223
|
def target(name) = "#{@domain}/#{label(name)}"
|
|
207
224
|
|
|
225
|
+
# Written beside the plist (not *.plist, so never listed), linted,
|
|
226
|
+
# then renamed over it; a plist plutil rejects never lands.
|
|
227
|
+
def write_linted(plist, content)
|
|
228
|
+
draft = "#{plist}.new"
|
|
229
|
+
Schedule.write_private(draft, content)
|
|
230
|
+
run! "plutil", "-lint", "-s", draft
|
|
231
|
+
File.rename(draft, plist)
|
|
232
|
+
ensure
|
|
233
|
+
FileUtils.rm_f(draft)
|
|
234
|
+
end
|
|
235
|
+
|
|
208
236
|
def loaded?(name) = run("launchctl", "print", target(name)).last
|
|
209
237
|
|
|
210
238
|
def stopped?(name)
|
|
@@ -213,15 +241,23 @@ module Asgard
|
|
|
213
241
|
end
|
|
214
242
|
|
|
215
243
|
# bootout returns before the job is fully gone; bootstrapping the same
|
|
216
|
-
# label too soon fails with "Input/output error", so wait for it.
|
|
244
|
+
# label too soon fails with "Input/output error", so wait for it. A job
|
|
245
|
+
# still loaded afterwards raises, so callers never delete the plist of
|
|
246
|
+
# a job launchd keeps running.
|
|
217
247
|
def unload(name)
|
|
218
248
|
return unless loaded?(name)
|
|
219
249
|
|
|
220
|
-
run
|
|
221
|
-
|
|
222
|
-
|
|
250
|
+
out, ok = run("launchctl", "bootout", target(name))
|
|
251
|
+
return if (ok && unloaded?(name)) || !loaded?(name)
|
|
252
|
+
|
|
253
|
+
raise Error, "launchctl bootout #{target(name)} left #{label(name)} still loaded: #{out.strip}"
|
|
254
|
+
end
|
|
223
255
|
|
|
224
|
-
|
|
256
|
+
# Polls for up to two seconds; true once the job is gone.
|
|
257
|
+
def unloaded?(name)
|
|
258
|
+
20.times.any? do |attempt|
|
|
259
|
+
sleep 0.1 if attempt.positive?
|
|
260
|
+
!loaded?(name)
|
|
225
261
|
end
|
|
226
262
|
end
|
|
227
263
|
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
require "fileutils"
|
|
4
4
|
require "etc"
|
|
5
|
+
require_relative "../state_dir"
|
|
6
|
+
require_relative "job_files"
|
|
5
7
|
|
|
6
8
|
module Asgard
|
|
7
9
|
module Schedule
|
|
@@ -15,6 +17,8 @@ module Asgard
|
|
|
15
17
|
class Systemd
|
|
16
18
|
UNIT_PREFIX = "asgard"
|
|
17
19
|
DAY_ABBREVIATIONS = %w[Sun Mon Tue Wed Thu Fri Sat].freeze
|
|
20
|
+
# The service properties status asks `systemctl show` for.
|
|
21
|
+
SHOW_PROPERTIES = %w[ActiveState SubState ExecMainCode ExecMainStatus ExecMainExitTimestampMonotonic].freeze
|
|
18
22
|
|
|
19
23
|
# ---- pure helpers -----------------------------------------------------
|
|
20
24
|
|
|
@@ -31,14 +35,23 @@ module Asgard
|
|
|
31
35
|
# systemd expands %specifiers everywhere; % must be doubled to be literal.
|
|
32
36
|
def self.escape_specifiers(text) = text.to_s.gsub("%", "%%")
|
|
33
37
|
|
|
34
|
-
# A double-quoted word for ExecStart
|
|
35
|
-
#
|
|
36
|
-
def self.quote(text)
|
|
37
|
-
|
|
38
|
-
|
|
38
|
+
# A double-quoted word for ExecStart=. $ is doubled too so ExecStart=
|
|
39
|
+
# doesn't expand it as a variable.
|
|
40
|
+
def self.quote(text) = %("#{quote_body(text).gsub('$', '$$')}")
|
|
41
|
+
|
|
42
|
+
# A double-quoted Environment= assignment. Environment= does no
|
|
43
|
+
# variable expansion, so a $ there is already literal and doubling it
|
|
44
|
+
# would leave $$ in the value (systemd.exec(5)).
|
|
45
|
+
def self.quote_env(text) = %("#{quote_body(text)}")
|
|
39
46
|
|
|
47
|
+
# % doubled, \ and " backslash-escaped: what both kinds of quoting share.
|
|
48
|
+
def self.quote_body(text) = escape_specifiers(text).gsub('\\') { '\\\\' }.gsub('"', '\\"')
|
|
49
|
+
|
|
50
|
+
# KillMode=process: when the run ends, systemd kills only the task's own
|
|
51
|
+
# process, so background jobs it started (setsid alone can't leave the
|
|
52
|
+
# unit's cgroup) keep running, as they do under launchd.
|
|
40
53
|
def self.service_unit(description:, arguments:, working_directory:, environment:, log_path:)
|
|
41
|
-
env_lines = environment.map { |key, value| "Environment=#{
|
|
54
|
+
env_lines = environment.map { |key, value| "Environment=#{quote_env("#{key}=#{value}")}" }
|
|
42
55
|
log = escape_specifiers(log_path)
|
|
43
56
|
<<~UNIT
|
|
44
57
|
[Unit]
|
|
@@ -46,6 +59,7 @@ module Asgard
|
|
|
46
59
|
|
|
47
60
|
[Service]
|
|
48
61
|
Type=oneshot
|
|
62
|
+
KillMode=process
|
|
49
63
|
WorkingDirectory=#{escape_specifiers(working_directory)}
|
|
50
64
|
#{env_lines.join("\n")}
|
|
51
65
|
ExecStart=#{arguments.map { |arg| quote(arg) }.join(' ')}
|
|
@@ -103,6 +117,22 @@ module Asgard
|
|
|
103
117
|
# `systemctl show -p A -p B` output => { "A" => "...", "B" => "..." }
|
|
104
118
|
def self.parse_show(output) = output.lines.to_h { |line| line.chomp.split("=", 2) }.reject { |k, _| k.to_s.empty? }
|
|
105
119
|
|
|
120
|
+
# parse_show of SHOW_PROPERTIES => { running: Boolean, last_exit: String|nil }.
|
|
121
|
+
# A oneshot service is "activating" while its command runs. ExecMainCode
|
|
122
|
+
# 2 (killed) or 3 (dumped) means ExecMainStatus is a signal number.
|
|
123
|
+
def self.service_status(props)
|
|
124
|
+
stamp = props["ExecMainExitTimestampMonotonic"].to_s
|
|
125
|
+
status = props["ExecMainStatus"] unless stamp.empty? || stamp == "0"
|
|
126
|
+
signal = status if %w[2 3].include?(props["ExecMainCode"])
|
|
127
|
+
{
|
|
128
|
+
running: props["ActiveState"] == "activating" || props["SubState"] == "running",
|
|
129
|
+
last_exit: signal ? Schedule.signal_exit(signal, "SIG#{Signal.signame(signal.to_i)}") : status
|
|
130
|
+
}
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# The { path => content } entries whose file is missing or differs.
|
|
134
|
+
def self.changed_files(files) = files.reject { |path, content| File.exist?(path) && File.read(path) == content }
|
|
135
|
+
|
|
106
136
|
# ---- backend API ------------------------------------------------------
|
|
107
137
|
|
|
108
138
|
def initialize(project:, root:, home: Dir.home, runner: Schedule.runner, env: ENV, user: Etc.getpwuid(Process.uid).name)
|
|
@@ -130,7 +160,7 @@ module Asgard
|
|
|
130
160
|
description = "#{Schedule.command_line(task, args)} (#{@project})"
|
|
131
161
|
service = klass.service_unit(
|
|
132
162
|
description:,
|
|
133
|
-
arguments: Schedule.program_arguments(task, args, root: @root, asgard:, direnv:),
|
|
163
|
+
arguments: Schedule.program_arguments([task, *args], root: @root, asgard:, direnv:),
|
|
134
164
|
working_directory: @root,
|
|
135
165
|
environment: Schedule.environment(spec),
|
|
136
166
|
log_path: log_path(name)
|
|
@@ -144,25 +174,36 @@ module Asgard
|
|
|
144
174
|
{ service_path(name) => service, timer_path(name) => timer }
|
|
145
175
|
end
|
|
146
176
|
|
|
147
|
-
# A timer that exists but is disabled was stopped on purpose; keep it
|
|
177
|
+
# A timer that exists but is disabled was stopped on purpose; keep it
|
|
178
|
+
# so. That is only true once an install has finished, so a first
|
|
179
|
+
# install that fails takes its unit files back out. Unchanged units are
|
|
180
|
+
# left alone: restarting a timer would restart an every: countdown, and
|
|
181
|
+
# `start` is a no-op for a timer already running.
|
|
148
182
|
def install(spec, asgard:, direnv:)
|
|
149
183
|
name = spec[:name]
|
|
150
184
|
timer_file = timer_path(name)
|
|
151
185
|
timer_unit = timer(name)
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
186
|
+
job = files(spec, asgard:, direnv:)
|
|
187
|
+
Schedule.check_lengths!([*job.keys, log_path(name)])
|
|
188
|
+
first = !File.exist?(timer_file)
|
|
189
|
+
stopped = !first && !enabled?(name)
|
|
190
|
+
changed = write_changed(job, name).any?
|
|
191
|
+
systemctl! "daemon-reload" if changed
|
|
156
192
|
return :stopped if stopped
|
|
157
193
|
|
|
158
194
|
systemctl! "enable", timer_unit
|
|
159
|
-
systemctl! "restart", timer_unit # picks up a changed schedule
|
|
195
|
+
systemctl! changed ? "restart" : "start", timer_unit # restart picks up a changed schedule
|
|
160
196
|
:active
|
|
197
|
+
rescue Error
|
|
198
|
+
FileUtils.rm_f([service_path(name), timer_file]) if first
|
|
199
|
+
raise
|
|
161
200
|
end
|
|
162
201
|
|
|
202
|
+
# A timer that won't disable is left installed, files and all, rather
|
|
203
|
+
# than deleted while systemd keeps it running.
|
|
163
204
|
def uninstall(name)
|
|
164
205
|
service_unit = service(name)
|
|
165
|
-
systemctl "disable", "--now", timer(name)
|
|
206
|
+
systemctl! "disable", "--now", timer(name)
|
|
166
207
|
systemctl "stop", service_unit
|
|
167
208
|
FileUtils.rm_f [service_path(name), timer_path(name)]
|
|
168
209
|
systemctl "daemon-reload"
|
|
@@ -181,11 +222,7 @@ module Asgard
|
|
|
181
222
|
end
|
|
182
223
|
|
|
183
224
|
# Every asgard entry on this machine, whatever its project: [[project_slug, name], ...].
|
|
184
|
-
def installed_entries
|
|
185
|
-
paths = Dir.glob(File.join(@config, "systemd", "user", "#{UNIT_PREFIX}.*.timer"))
|
|
186
|
-
paths.map { |path| File.basename(path, ".timer").delete_prefix("#{UNIT_PREFIX}.").split(".", 2) }
|
|
187
|
-
.select { |entry| entry.size == 2 }.sort
|
|
188
|
-
end
|
|
225
|
+
def installed_entries = JobFiles.entries_in(File.join(@config, "systemd", "user"), prefix: UNIT_PREFIX, ext: ".timer")
|
|
189
226
|
|
|
190
227
|
# { command:, schedule: } as installed, read from the unit files; nil when there are none.
|
|
191
228
|
def installed_columns(name)
|
|
@@ -198,10 +235,7 @@ module Asgard
|
|
|
198
235
|
end
|
|
199
236
|
|
|
200
237
|
# The project root the job runs in, read from the service unit; nil when there isn't one.
|
|
201
|
-
def installed_directory(name)
|
|
202
|
-
path = service_path(name)
|
|
203
|
-
self.class.parse_directory(File.read(path)) if File.exist?(path)
|
|
204
|
-
end
|
|
238
|
+
def installed_directory(name) = JobFiles.directory(service_path(name), self.class.method(:parse_directory))
|
|
205
239
|
|
|
206
240
|
def status(name)
|
|
207
241
|
_, active = systemctl("is-active", "--quiet", timer(name))
|
|
@@ -209,11 +243,10 @@ module Asgard
|
|
|
209
243
|
elsif enabled?(name) then :not_loaded
|
|
210
244
|
else :stopped
|
|
211
245
|
end
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
{ state:, last_exit: ran ? props["ExecMainStatus"] : nil }
|
|
246
|
+
klass = self.class
|
|
247
|
+
out, = systemctl("show", service(name), *SHOW_PROPERTIES.flat_map { |prop| ["-p", prop] })
|
|
248
|
+
running, last_exit = klass.service_status(klass.parse_show(out)).values_at(:running, :last_exit)
|
|
249
|
+
{ state: running ? :running : state, last_exit: }
|
|
217
250
|
end
|
|
218
251
|
|
|
219
252
|
def notes
|
|
@@ -229,6 +262,17 @@ module Asgard
|
|
|
229
262
|
|
|
230
263
|
def service(name) = "#{unit(name)}.service"
|
|
231
264
|
|
|
265
|
+
# Writes the unit files whose content changed (and the log directory);
|
|
266
|
+
# returns them as { path => content }.
|
|
267
|
+
def write_changed(job, name)
|
|
268
|
+
changed = self.class.changed_files(job)
|
|
269
|
+
Schedule.writing do
|
|
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
|
|
272
|
+
changed.each { |path, content| Schedule.write_private(path, content) }
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
|
|
232
276
|
def enabled?(name) = systemctl("is-enabled", "--quiet", timer(name)).last
|
|
233
277
|
|
|
234
278
|
def systemctl(*) = run("systemctl", "--user", *)
|
|
@@ -8,10 +8,30 @@ module Asgard
|
|
|
8
8
|
# give up width, so the short columns always stay whole.
|
|
9
9
|
class Table
|
|
10
10
|
HEADERS = ["NAME", "COMMAND", "SCHEDULE", "STATE", "LAST EXIT"].freeze
|
|
11
|
+
KEYS = %i[name command schedule state last_exit].freeze
|
|
11
12
|
FLEXIBLE = [1, 2].freeze
|
|
12
13
|
MIN_WIDTH = 14
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
PADDING = 1 # spaces either side of a cell
|
|
15
|
+
EXTRA = PADDING * 2 # what a column adds to its content
|
|
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
|
|
15
35
|
|
|
16
36
|
# Columns available for output: the terminal's when io is one, else
|
|
17
37
|
# $COLUMNS, else a roomy default (piped output shouldn't wrap at 80).
|
|
@@ -23,29 +43,35 @@ module Asgard
|
|
|
23
43
|
end
|
|
24
44
|
|
|
25
45
|
# rows rendered for the terminal, under title when given: colored when io
|
|
26
|
-
# is a tty and NO_COLOR is unset. headers: relabels the
|
|
27
|
-
|
|
28
|
-
|
|
46
|
+
# is a tty and NO_COLOR is unset. headers: relabels the columns; keys:
|
|
47
|
+
# picks them from each row, and flexible: says which give up width
|
|
48
|
+
# first when the table is wider than the screen.
|
|
49
|
+
def self.draw(rows, title: nil, io: $stdout, headers: HEADERS, keys: KEYS, flexible: FLEXIBLE)
|
|
50
|
+
table = new(rows, color: io.tty? && !ENV.key?("NO_COLOR"), width: terminal_width(io), headers:, keys:, flexible:).render
|
|
29
51
|
[title, table].compact.join("\n")
|
|
30
52
|
end
|
|
31
53
|
|
|
32
54
|
# color: styles the header, state and last exit with ANSI codes.
|
|
33
|
-
def initialize(rows, color:, width:, headers: HEADERS)
|
|
55
|
+
def initialize(rows, color:, width:, headers: HEADERS, keys: KEYS, flexible: FLEXIBLE)
|
|
34
56
|
require "pastel"
|
|
35
57
|
|
|
36
58
|
@headers = headers
|
|
37
|
-
@
|
|
38
|
-
@
|
|
39
|
-
@
|
|
40
|
-
@column_widths = natural_widths
|
|
41
|
-
shrink_widest while overflowing? && shrinkable?
|
|
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)
|
|
42
63
|
end
|
|
43
64
|
|
|
44
65
|
def render
|
|
45
66
|
require "tty-table"
|
|
46
67
|
|
|
47
68
|
grid = TTY::Table.new(header: @headers.map { |text| @pastel.bold(text) }, rows: @cells.map { |cells| styled(cells) })
|
|
48
|
-
|
|
69
|
+
# tty-table gets the content widths and no resize: with resize it
|
|
70
|
+
# re-spreads the columns by its own arithmetic (and squeezes a
|
|
71
|
+
# one-row table), while this way the widths computed here are the
|
|
72
|
+
# widths it draws, wrapping any cell that is longer.
|
|
73
|
+
grid.render(:unicode, multiline: true, resize: false, width: total_width, column_widths: content_widths,
|
|
74
|
+
padding: [0, PADDING]).to_s
|
|
49
75
|
end
|
|
50
76
|
|
|
51
77
|
# Width of each column, padding included. Every column starts at its
|
|
@@ -53,26 +79,22 @@ module Asgard
|
|
|
53
79
|
# flexible columns give up a character at a time until it fits.
|
|
54
80
|
attr_reader :column_widths
|
|
55
81
|
|
|
56
|
-
# Rendered width: the columns plus their borders
|
|
57
|
-
|
|
82
|
+
# Rendered width: the columns plus their borders, one before each
|
|
83
|
+
# column and one after the last.
|
|
84
|
+
def total_width = column_widths.sum + @headers.size + 1
|
|
58
85
|
|
|
59
86
|
private
|
|
60
87
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
def overflowing? = total_width > @width
|
|
88
|
+
# The widths without their padding, as tty-table counts them.
|
|
89
|
+
def content_widths = column_widths.map { |width| width - EXTRA }
|
|
64
90
|
|
|
65
|
-
def
|
|
66
|
-
|
|
67
|
-
def shrinkable? = @column_widths[widest_flexible] > MIN_WIDTH
|
|
68
|
-
|
|
69
|
-
def shrink_widest = @column_widths[widest_flexible] -= 1
|
|
91
|
+
def natural_widths = [@headers, *@cells].transpose.map { |column| column.max_by(&:length).length + EXTRA }
|
|
70
92
|
|
|
93
|
+
# The state and last-exit cells colored, wherever keys: put them.
|
|
71
94
|
def styled(cells)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
[*lead, @pastel.decorate(state, state_color), @pastel.decorate(last_exit, exit_color)]
|
|
95
|
+
cells = cells.dup
|
|
96
|
+
@styled.each { |column, index| cells[index] = @pastel.decorate(cells[index], COLORS.fetch(column).call(cells[index])) }
|
|
97
|
+
cells
|
|
76
98
|
end
|
|
77
99
|
end
|
|
78
100
|
end
|
data/lib/asgard/schedule.rb
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "declarations"
|
|
3
4
|
require_relative "schedule/declaration"
|
|
4
5
|
require_relative "schedule/launchd"
|
|
5
6
|
require_relative "schedule/systemd"
|
|
@@ -24,16 +25,55 @@ module Asgard
|
|
|
24
25
|
@declarations ||= {}
|
|
25
26
|
end
|
|
26
27
|
|
|
27
|
-
# Records one declaration
|
|
28
|
-
#
|
|
29
|
-
#
|
|
30
|
-
def declare(task, **
|
|
31
|
-
spec = normalize(task, **settings)
|
|
32
|
-
name = spec[:name]
|
|
33
|
-
taken = declarations[name]
|
|
34
|
-
raise ArgumentError, "schedule: #{name.inspect} is already declared; give one a distinct as: name" if taken && taken != spec
|
|
28
|
+
# Records one declaration (see Declarations.record). Two different
|
|
29
|
+
# commands whose names derive alike both take hashed names first
|
|
30
|
+
# (see rival_commands?).
|
|
31
|
+
def declare(task, **) = Declarations.record(declarations, place(normalize(task, **)), "schedule")
|
|
35
32
|
|
|
36
|
-
|
|
33
|
+
# The name a command derives (a normalize result's task and args).
|
|
34
|
+
def plain_name(spec) = command_slug(spec.values_at(:task, :args).flatten)
|
|
35
|
+
|
|
36
|
+
# Whether +spec+ has the plain name its command derives, rather than
|
|
37
|
+
# an as: name or a hashed one.
|
|
38
|
+
def derived_name?(spec) = !spec[:args].empty? && spec[:name] == plain_name(spec)
|
|
39
|
+
|
|
40
|
+
# Two declarations of different commands that derive the same plain
|
|
41
|
+
# name: neither may keep it.
|
|
42
|
+
def rival_commands?(one, other)
|
|
43
|
+
derived_name?(one) && derived_name?(other) && one.values_at(:task, :args) != other.values_at(:task, :args)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def with_hashed_name(spec) = spec.merge(name: hashed_slug(spec.values_at(:task, :args).flatten))
|
|
47
|
+
|
|
48
|
+
# Whether +spec+'s plain name was already given up by a rival in
|
|
49
|
+
# +declarations+, one now under its hashed name, so +spec+ takes its
|
|
50
|
+
# hashed name too. Whatever order the .loki declares them in, every
|
|
51
|
+
# rival ends up hashed.
|
|
52
|
+
def contested?(spec, declarations)
|
|
53
|
+
derived_name?(spec) && declarations.each_value.any? do |other|
|
|
54
|
+
other == with_hashed_name(other) && rival_commands?(other.merge(name: plain_name(other)), spec)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
# +spec+ under the name it is declared by: hashed when a rival gave
|
|
61
|
+
# up its plain name earlier, or meets it now (the rival then moves to
|
|
62
|
+
# its hashed name too).
|
|
63
|
+
def place(spec)
|
|
64
|
+
return with_hashed_name(spec) if contested?(spec, declarations)
|
|
65
|
+
|
|
66
|
+
taken = declarations[spec[:name]]
|
|
67
|
+
return spec unless taken && rival_commands?(taken, spec)
|
|
68
|
+
|
|
69
|
+
rename_to_hashed(taken)
|
|
70
|
+
with_hashed_name(spec)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def rename_to_hashed(spec)
|
|
74
|
+
declarations.delete(spec[:name])
|
|
75
|
+
renamed = with_hashed_name(spec)
|
|
76
|
+
declarations[renamed[:name]] = renamed
|
|
37
77
|
end
|
|
38
78
|
end
|
|
39
79
|
|