Class: OsCtld::Hook::Base

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log
Defined in:
lib/osctld/hook/base.rb

Direct Known Subclasses

Container::Hooks::Base, Pool::Hooks::Base

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_nameObject (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_nameObject (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_instanceClass (readonly)

Returns:

  • (Class)


31
32
33
# File 'lib/osctld/hook/base.rb', line 31

def event_instance
  @event_instance
end

#optsHash (readonly, protected)

Returns:

  • (Hash)


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

Parameters:

  • v (Boolean)

    ‘true` for blocking, `false` for async



19
20
21
# File 'lib/osctld/hook/base.rb', line 19

def blocking(v)
  @blocking = v
end

.blocking?Boolean

Returns:

  • (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

Parameters:

  • event_class (Class)
  • hook_name (Symbol)


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

Returns:

  • (Boolean)


81
82
83
# File 'lib/osctld/hook/base.rb', line 81

def blocking?
  self.class.blocking?
end

#environmentHash<String, String> (protected)

Override this method to define environment variables that the script hook will have set.

Returns:

  • (Hash<String, String>)


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.

Parameters:

  • hook_path (String)


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.

Parameters:

  • hook_path (String)

Returns:

  • (Array<String>)


103
104
105
# File 'lib/osctld/hook/base.rb', line 103

def executable(hook_path)
  [hook_path]
end

#setupObject



39
# File 'lib/osctld/hook/base.rb', line 39

def setup; end