Class: WheneverSystemd::JobList
- Inherits:
-
Object
- Object
- WheneverSystemd::JobList
- Defined in:
- lib/whenever_systemd/job_list.rb
Instance Attribute Summary collapse
-
#jobs ⇒ Object
readonly
Returns the value of attribute jobs.
-
#roles ⇒ Object
readonly
Returns the value of attribute roles.
Class Method Summary collapse
Instance Method Summary collapse
- #at(time) ⇒ Object
- #backup_previous_units_from(path, **opts) ⇒ Object
- #copy_updated_units_to(path) ⇒ Object
- #daily(at: "00:00:00", **options, &block) ⇒ Object
- #dry_units(path) ⇒ Object
- #env(variable, value) ⇒ Object
- #every(frequency, at: nil, **timer) ⇒ Object
- #generate(name, path) ⇒ Object
- #generate_clear_script(path) ⇒ Object
- #generate_units_script(path) ⇒ Object
- #generate_update_script(path) ⇒ Object
-
#initialize(options) ⇒ JobList
constructor
A new instance of JobList.
- #job_type(name, template) ⇒ Object
- #make_backup_dir ⇒ Object
- #method_missing(name, *args, &block) ⇒ Object
- #set(variable, value) ⇒ Object
- #systemctl_timers(*args, **opts) ⇒ Object
- #systemd_units(path, include_target = true) ⇒ Object
- #timers ⇒ Object
- #timers_target(path) ⇒ Object
- #unit_files(path) ⇒ Object
- #units_expansion(ext = "{service,timer}", all: false, sub: false) ⇒ Object
- #weekly(*on_days, **options, &block) ⇒ Object
Constructor Details
#initialize(options) ⇒ JobList
Returns a new instance of JobList.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/whenever_systemd/job_list.rb', line 12 def initialize() @jobs, @env, @set_variables, @pre_set_variables = [], {}, {}, {} if .is_a? String = { :string => } end @temp_path = [:temp_path] pre_set([:set]) @roles = [:roles] || [] setup_file = File.('../setup.rb', __FILE__) setup = File.read(setup_file) schedule = if [:string] [:string] elsif [:file] File.read([:file]) end instance_eval(setup, setup_file) instance_eval(schedule, [:file] || '<eval>') end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
42 43 44 |
# File 'lib/whenever_systemd/job_list.rb', line 42 def method_missing(name, *args, &block) @set_variables.has_key?(name) ? @set_variables[name] : super end |
Instance Attribute Details
#jobs ⇒ Object (readonly)
Returns the value of attribute jobs.
89 90 91 |
# File 'lib/whenever_systemd/job_list.rb', line 89 def jobs @jobs end |
#roles ⇒ Object (readonly)
Returns the value of attribute roles.
10 11 12 |
# File 'lib/whenever_systemd/job_list.rb', line 10 def roles @roles end |
Class Method Details
.respond_to?(name, include_private = false) ⇒ Boolean
46 47 48 |
# File 'lib/whenever_systemd/job_list.rb', line 46 def self.respond_to?(name, include_private = false) @set_variables.has_key?(name) || super end |
Instance Method Details
#at(time) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/whenever_systemd/job_list.rb', line 82 def at(time) @options_was, @options = @options, @options.to_h.merge(at: time) yield ensure @options, @options_was = @options_was, nil end |
#backup_previous_units_from(path, **opts) ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/whenever_systemd/job_list.rb', line 138 def backup_previous_units_from(path, **opts) format(%(/usr/bin/cp -rvf %{source}/%{expansion} -t %{target}), source: Shellwords.escape(path), expansion: units_expansion(**opts), target: Shellwords.escape("#{@temp_path}/backup") ) end |
#copy_updated_units_to(path) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/whenever_systemd/job_list.rb', line 146 def copy_updated_units_to(path) format(%(/usr/bin/cp -rvf %{source}/*.{service,timer} -t %{target}), source: Shellwords.escape(@temp_path), target: Shellwords.escape(path) ) end |
#daily(at: "00:00:00", **options, &block) ⇒ Object
62 63 64 |
# File 'lib/whenever_systemd/job_list.rb', line 62 def daily(at: "00:00:00", **, &block) every("*-*-*", at: at, **, &block) end |
#dry_units(path) ⇒ Object
188 189 190 |
# File 'lib/whenever_systemd/job_list.rb', line 188 def dry_units(path) Formatters::DryUnits[systemd_units(path)] end |
#env(variable, value) ⇒ Object
50 51 52 |
# File 'lib/whenever_systemd/job_list.rb', line 50 def env(variable, value) @env[variable.to_s] = value end |
#every(frequency, at: nil, **timer) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/whenever_systemd/job_list.rb', line 54 def every(frequency, at: nil, **timer) @options_was = @options @options = @options.to_h.merge(interval: Formatters::Freq[frequency], timer: timer, at: at) yield ensure @options, @options_was = @options_was, nil end |
#generate(name, path) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/whenever_systemd/job_list.rb', line 104 def generate(name, path) case name.to_sym when :units; generate_units_script(path) when :update_units; generate_update_script(path) when :clear_units; generate_clear_script(path) else raise ArgumentError, %(available names: units, update_units, clear_units, given: #{name}) end end |
#generate_clear_script(path) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/whenever_systemd/job_list.rb', line 129 def generate_clear_script(path) [ make_backup_dir, backup_previous_units_from(path, all: true), systemctl_timers('disable', '--now', all: true), format(%(/usr/bin/rm -rfI %{target}/%{expansion}), target: Shellwords.escape(path), expansion: units_expansion(all: true)) ].join("\n\n") end |
#generate_units_script(path) ⇒ Object
113 114 115 |
# File 'lib/whenever_systemd/job_list.rb', line 113 def generate_units_script(path) Formatters::MaterializeUnits[systemd_units(path)] end |
#generate_update_script(path) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/whenever_systemd/job_list.rb', line 117 def generate_update_script(path) [ make_backup_dir, backup_previous_units_from(path, all: true), systemctl_timers('disable', '--now', all: true), generate_units_script(@temp_path), copy_updated_units_to(path), Shellwords.join(["/usr/bin/systemctl", "daemon-reload"]), systemctl_timers('enable', '--now') ].join("\n\n") end |
#job_type(name, template) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/whenever_systemd/job_list.rb', line 91 def job_type(name, template) singleton_class.class_eval do define_method(name) do |job_name, task, *args| = { :task => task, :template => template } .merge!(args[0]) if args[0].is_a? Hash [:description] ||= .delete("?") { "WheneverSystemd-generated Job" } @jobs ||= [] @jobs << Job.new("#{@prefix}-#{job_name}", @set_variables.deep_merge(@options).deep_merge().deep_dup) end end end |
#make_backup_dir ⇒ Object
153 154 155 |
# File 'lib/whenever_systemd/job_list.rb', line 153 def make_backup_dir Shellwords.join(["mkdir", "-p", "#{@temp_path}/backup"]) end |
#set(variable, value) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/whenever_systemd/job_list.rb', line 34 def set(variable, value) variable = variable.to_sym return if @pre_set_variables[variable] instance_variable_set("@#{variable}".to_sym, value) @set_variables[variable] = value end |
#systemctl_timers(*args, **opts) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/whenever_systemd/job_list.rb', line 157 def systemctl_timers(*args, **opts) case args[0] when "enable", "disable" format(%(for timer in %s; do %s $timer; done), units_expansion('timer', sub: true, **opts), Shellwords.join(["/usr/bin/systemctl", *args]) ) else Shellwords.join(["/usr/bin/systemctl", *args, units_expansion('timer', **opts)]) end end |
#systemd_units(path, include_target = true) ⇒ Object
192 193 194 195 196 197 198 199 |
# File 'lib/whenever_systemd/job_list.rb', line 192 def systemd_units(path, include_target = true) wanted_by = @set_variables.dig(:install, :wanted_by) if include_target && wanted_by != "timers.target" [timers_target(path), *systemd_units("#{path}/#{wanted_by}.wants", false)] else @jobs.flat_map { |job| job.systemd_units(path) } end end |
#timers ⇒ Object
169 170 171 |
# File 'lib/whenever_systemd/job_list.rb', line 169 def timers @jobs.map(&:timer_name) end |
#timers_target(path) ⇒ Object
201 202 203 204 205 206 207 |
# File 'lib/whenever_systemd/job_list.rb', line 201 def timers_target(path) { path: path, filename: @set_variables.dig(:install, :wanted_by), content: Formatters::Target[unit: { description: "Timers target" }] } end |
#unit_files(path) ⇒ Object
173 174 175 |
# File 'lib/whenever_systemd/job_list.rb', line 173 def unit_files(path) Dir.glob("#{path}/#{units_expansion}") end |
#units_expansion(ext = "{service,timer}", all: false, sub: false) ⇒ Object
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/whenever_systemd/job_list.rb', line 177 def units_expansion(ext = "{service,timer}", all: false, sub: false) suffixes = all ? "*" : format("{%s}", @jobs.map { |j| Shellwords.escape(j.unprefixed_name) }.join(?,)) pattern = "#{@prefix}-#{suffixes}.#{ext}" return pattern unless sub if all format(%($(/usr/bin/systemctl list-unit-files '%s' | /usr/bin/cut -d ' ' -f 1 | /usr/bin/head -n -2 | /usr/bin/tail -n +2)), pattern) else format(%($(/usr/bin/echo %s)), pattern) end end |
#weekly(*on_days, **options, &block) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/whenever_systemd/job_list.rb', line 74 def weekly(*on_days, **, &block) if on_days.size > 0 every("#{on_days * ?,} *-*-*", **, &block) else every(:weekly, **, &block) end end |