Class: OsCtld::Hook::Base
- Inherits:
-
Object
- Object
- OsCtld::Hook::Base
- Includes:
- OsCtl::Lib::Utils::Log
- Defined in:
- lib/osctld/hook/base.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.hook_name ⇒ Object
readonly
Returns the value of attribute hook_name.
-
.user_hook_name ⇒ Object
readonly
Returns the value of attribute user_hook_name.
Instance Attribute Summary collapse
- #event_instance ⇒ Class readonly
- #opts ⇒ Hash readonly protected
Class Method Summary collapse
-
.blocking(v) ⇒ Object
Mark the hook as blocking or async, defaults to async.
- .blocking? ⇒ Boolean
-
.hook(event_class, hook_name, hook_class) ⇒ Object
Register hook under a name.
Instance Method Summary collapse
- #blocking? ⇒ Boolean
-
#environment ⇒ Hash<String, String>
protected
Override this method to define environment variables that the script hook will have set.
-
#exec(hook_path) ⇒ Object
Execute the user script hook.
-
#executable(hook_path) ⇒ Array<String>
protected
Override this method to define the program and its arguments that will be execed to invoke the user script hook.
-
#initialize(event_instance, opts) ⇒ Base
constructor
A new instance of Base.
- #setup ⇒ Object
Constructor Details
#initialize(event_instance, opts) ⇒ Base
Returns a new instance of Base.
33 34 35 36 37 |
# File 'lib/osctld/hook/base.rb', line 33 def initialize(event_instance, opts) @event_instance = event_instance @opts = opts setup end |
Class Attribute Details
.hook_name ⇒ Object (readonly)
Returns the value of attribute hook_name.
6 7 8 |
# File 'lib/osctld/hook/base.rb', line 6 def hook_name @hook_name end |
.user_hook_name ⇒ Object (readonly)
Returns the value of attribute user_hook_name.
6 7 8 |
# File 'lib/osctld/hook/base.rb', line 6 def user_hook_name @user_hook_name end |
Instance Attribute Details
#event_instance ⇒ Class (readonly)
31 32 33 |
# File 'lib/osctld/hook/base.rb', line 31 def event_instance @event_instance end |
#opts ⇒ Hash (readonly, protected)
88 89 90 |
# File 'lib/osctld/hook/base.rb', line 88 def opts @opts end |
Class Method Details
.blocking(v) ⇒ Object
Mark the hook as blocking or async, defaults to async
19 20 21 |
# File 'lib/osctld/hook/base.rb', line 19 def blocking(v) @blocking = v end |
.blocking? ⇒ Boolean
23 24 25 |
# File 'lib/osctld/hook/base.rb', line 23 def blocking? @blocking || false end |
.hook(event_class, hook_name, hook_class) ⇒ Object
Register hook under a name
11 12 13 14 15 |
# File 'lib/osctld/hook/base.rb', line 11 def hook(event_class, hook_name, hook_class) @hook_name = hook_name @user_hook_name = hook_name.to_s.gsub('_', '-') Hook.register(event_class, hook_name, hook_class) end |
Instance Method Details
#blocking? ⇒ Boolean
81 82 83 |
# File 'lib/osctld/hook/base.rb', line 81 def blocking? self.class.blocking? end |
#environment ⇒ Hash<String, String> (protected)
Override this method to define environment variables that the script hook will have set.
93 94 95 96 97 |
# File 'lib/osctld/hook/base.rb', line 93 def environment { 'OSCTL_HOOK_NAME' => self.class.user_hook_name } end |
#exec(hook_path) ⇒ Object
Execute the user script hook.
For blocking hooks, this method waits for the script hook to exit. If it exits with non-zero exit status, exception OsCtld::HookFailed is raised. Async hooks return immediately and their exit status has no meaning.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/osctld/hook/base.rb', line 48 def exec(hook_path) log( :info, event_instance, "Executing hook #{self.class.hook_name} at #{hook_path}" ) env = environment pid = Process.fork do ENV.delete_if { |k, _| k != 'PATH' } env.each { |k, v| ENV[k] = v } Process.exec(*executable(hook_path)) end if blocking? Process.wait(pid) return true if $?.exitstatus == 0 log( :warn, event_instance, "Hook #{self.class.hook_name} at #{hook_path} exited with #{$?.exitstatus}" ) raise HookFailed.new(self, hook_path, $?.exitstatus) else Hook.watch(self, hook_path, pid) end end |
#executable(hook_path) ⇒ Array<String> (protected)
Override this method to define the program and its arguments that will be execed to invoke the user script hook.
103 104 105 |
# File 'lib/osctld/hook/base.rb', line 103 def executable(hook_path) [hook_path] end |
#setup ⇒ Object
39 |
# File 'lib/osctld/hook/base.rb', line 39 def setup; end |