Class: OsCtld::Hook::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/osctld/hook/manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_instance) ⇒ Manager

Returns a new instance of Manager.



21
22
23
# File 'lib/osctld/hook/manager.rb', line 21

def initialize(event_instance)
  @event_instance = event_instance
end

Instance Attribute Details

#event_instanceClass (readonly)

Returns:

  • (Class)


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

def event_instance
  @event_instance
end

Class Method Details

.list_all_scripts(event_instance) ⇒ Array<Hook::Script>

Parameters:

  • event_instance (Class)

Returns:



13
14
15
16
# File 'lib/osctld/hook/manager.rb', line 13

def self.list_all_scripts(event_instance)
  m = new(event_instance)
  m.list_all_scripts
end

.run(event_instance, hook_class, opts) ⇒ Object

Parameters:

  • event_instance (Class)
  • hook_class (Class)

    subclass of Base

  • opts (Hash)

    hook options



6
7
8
9
# File 'lib/osctld/hook/manager.rb', line 6

def self.run(event_instance, hook_class, opts)
  m = new(event_instance)
  m.run(hook_class, opts)
end

Instance Method Details

#get_script_dir(hook_class, basedir, file_name) ⇒ Object (protected)



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/osctld/hook/manager.rb', line 78

def get_script_dir(hook_class, basedir, file_name)
  dir_name = "#{file_name}.d"
  hookd = File.join(basedir, dir_name)
  scripts = []

  if Dir.exist?(hookd)
    Dir.entries(hookd).each do |v|
      next if %w[. ..].include?(v)

      abs_path = File.join(hookd, v)

      begin
        st = File.stat(abs_path)
      rescue Errno::ENOENT
        next
      end

      next unless st.file? && st.executable?

      scripts << Hook::Script.new(
        hook_class.hook_name,
        abs_path,
        File.join(dir_name, v)
      )
    end
  end

  scripts
end

#get_script_singleton(hook_class, basedir, file_name) ⇒ Object (protected)



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/osctld/hook/manager.rb', line 64

def get_script_singleton(hook_class, basedir, file_name)
  singleton = File.join(basedir, file_name)
  st = File.stat(singleton)
  return if !st.file? || !st.executable?

  Hook::Script.new(
    hook_class.hook_name,
    singleton,
    file_name
  )
rescue Errno::ENOENT
  nil
end

#list_all_scriptsArray<Hook::Script>

List of container hooks of all types

Returns:



43
44
45
46
47
48
49
50
51
# File 'lib/osctld/hook/manager.rb', line 43

def list_all_scripts
  scripts = []

  Hook.hooks(event_instance.class).each_value do |klass|
    scripts.concat(list_scripts(klass))
  end

  scripts
end

#list_scripts(hook_class) ⇒ Array<Hook::Script>

List of hooks of particular type

Parameters:

  • hook_class (Class)

    subclass of Base

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/osctld/hook/manager.rb', line 28

def list_scripts(hook_class)
  file_name = hook_class.user_hook_name
  basedir = event_instance.user_hook_script_dir
  scripts = []

  singleton = get_script_singleton(hook_class, basedir, file_name)
  scripts << singleton if singleton

  scripts.concat(get_script_dir(hook_class, basedir, file_name))

  scripts.sort! { |a, b| a.base_name <=> b.base_name }
end

#run(hook_class, opts) ⇒ Object

Parameters:

  • hook_class (Class)

    subclass of Base



54
55
56
57
58
59
60
# File 'lib/osctld/hook/manager.rb', line 54

def run(hook_class, opts)
  hook = hook_class.new(event_instance, opts)

  list_scripts(hook_class).each do |v|
    hook.exec(v.abs_path)
  end
end