Class: OsCtld::Container::Hooks::Base
- Inherits:
-
Object
- Object
- OsCtld::Container::Hooks::Base
- Includes:
- OsCtl::Lib::Utils::Log
- Defined in:
- lib/osctld/container/hooks/base.rb
Direct Known Subclasses
OnStart, OnStop, PostMount, PostStart, PostStop, PreMount, PreStart, PreStop, VethDown, VethUp
Class Attribute Summary collapse
-
.hook_name ⇒ Object
readonly
Returns the value of attribute hook_name.
Instance Attribute Summary collapse
- #ct ⇒ Container readonly
- #opts ⇒ Hash readonly protected
Class Method Summary collapse
-
.blocking(v) ⇒ Object
Mark the hook as blocking or async, defaults to async.
- .blocking? ⇒ Boolean
- .exist?(ct) ⇒ Boolean
-
.hook(name) ⇒ Object
Register hook unde a name.
- .hook_path(ct) ⇒ Object
-
.run(ct, opts) ⇒ Object
Run hook.
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 ⇒ Object
Execute the user script hook.
-
#executable ⇒ Array<String>
protected
Override this method to define the program and its arguments that will be execed to invoke the user script hook.
- #hook_path ⇒ Object
-
#initialize(ct, opts) ⇒ Base
constructor
A new instance of Base.
Constructor Details
#initialize(ct, opts) ⇒ Base
Returns a new instance of Base.
46 47 48 49 |
# File 'lib/osctld/container/hooks/base.rb', line 46 def initialize(ct, opts) @ct = ct @opts = opts end |
Class Attribute Details
.hook_name ⇒ Object (readonly)
Returns the value of attribute hook_name.
6 7 8 |
# File 'lib/osctld/container/hooks/base.rb', line 6 def hook_name @hook_name end |
Instance Attribute Details
#ct ⇒ Container (readonly)
44 45 46 |
# File 'lib/osctld/container/hooks/base.rb', line 44 def ct @ct end |
#opts ⇒ Hash (readonly, protected)
99 100 101 |
# File 'lib/osctld/container/hooks/base.rb', line 99 def opts @opts end |
Class Method Details
.blocking(v) ⇒ Object
Mark the hook as blocking or async, defaults to async
17 18 19 |
# File 'lib/osctld/container/hooks/base.rb', line 17 def blocking(v) @blocking = v end |
.blocking? ⇒ Boolean
21 22 23 |
# File 'lib/osctld/container/hooks/base.rb', line 21 def blocking? @blocking || false end |
.exist?(ct) ⇒ Boolean
32 33 34 |
# File 'lib/osctld/container/hooks/base.rb', line 32 def exist?(ct) File.executable?(hook_path(ct)) end |
.hook(name) ⇒ Object
Register hook unde a name
10 11 12 13 |
# File 'lib/osctld/container/hooks/base.rb', line 10 def hook(name) @hook_name = name Container::Hook.register(name, self) end |
.hook_path(ct) ⇒ Object
36 37 38 |
# File 'lib/osctld/container/hooks/base.rb', line 36 def hook_path(ct) File.join(ct.user_hook_script_dir, hook_name.to_s.gsub(/_/, '-')) end |
.run(ct, opts) ⇒ Object
Run hook
26 27 28 29 30 |
# File 'lib/osctld/container/hooks/base.rb', line 26 def run(ct, opts) return unless exist?(ct) hook = new(ct, opts) hook.exec end |
Instance Method Details
#blocking? ⇒ Boolean
89 90 91 |
# File 'lib/osctld/container/hooks/base.rb', line 89 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.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/osctld/container/hooks/base.rb', line 104 def environment { 'OSCTL_HOOK_NAME' => self.class.hook_name.to_s, 'OSCTL_POOL_NAME' => ct.pool.name, 'OSCTL_CT_ID' => ct.id, 'OSCTL_CT_USER' => ct.user.name, 'OSCTL_CT_GROUP' => ct.group.name, 'OSCTL_CT_DATASET' => ct.dataset.to_s, 'OSCTL_CT_ROOTFS' => ct.rootfs, 'OSCTL_CT_LXC_PATH' => ct.lxc_home, 'OSCTL_CT_LXC_DIR' => ct.lxc_dir, 'OSCTL_CT_CGROUP_PATH' => ct.cgroup_path, 'OSCTL_CT_DISTRIBUTION' => ct.distribution, 'OSCTL_CT_VERSION' => ct.version, 'OSCTL_CT_HOSTNAME' => ct.hostname.to_s, 'OSCTL_CT_LOG_FILE' => ct.log_path, } end |
#exec ⇒ 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 HookFailed is raised. Async hooks return immediately and their exit status has no meaning.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/osctld/container/hooks/base.rb', line 56 def exec log( :info, ct, "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) end if blocking? Process.wait(pid) return true if $?.exitstatus == 0 log( :warn, ct, "Hook #{self.class.hook_name} at #{hook_path} exited with #{$?.exitstatus}" ) raise HookFailed.new(self, $?.exitstatus) else Container::Hook.watch(self, pid) end end |
#executable ⇒ Array<String> (protected)
Override this method to define the program and its arguments that will be execed to invoke the user script hook.
126 127 128 |
# File 'lib/osctld/container/hooks/base.rb', line 126 def executable [hook_path] end |
#hook_path ⇒ Object
93 94 95 |
# File 'lib/osctld/container/hooks/base.rb', line 93 def hook_path self.class.hook_path(ct) end |