Class: OsCtld::Container::Hooks::Base

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#ctContainer (readonly)

Returns:



44
45
46
# File 'lib/osctld/container/hooks/base.rb', line 44

def ct
  @ct
end

#optsHash (readonly, protected)

Returns:

  • (Hash)


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

Parameters:

  • v (Boolean)

    `true` for blocking, `false` for async



17
18
19
# File 'lib/osctld/container/hooks/base.rb', line 17

def blocking(v)
  @blocking = v
end

.blocking?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/osctld/container/hooks/base.rb', line 21

def blocking?
  @blocking || false
end

.exist?(ct) ⇒ Boolean

Returns:

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

Parameters:

  • name (Symbol)


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

Returns:

  • (Boolean)


89
90
91
# File 'lib/osctld/container/hooks/base.rb', line 89

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>)


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

#execObject

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

#executableArray<String> (protected)

Override this method to define the program and its arguments that will be execed to invoke the user script hook.

Returns:

  • (Array<String>)


126
127
128
# File 'lib/osctld/container/hooks/base.rb', line 126

def executable
  [hook_path]
end

#hook_pathObject



93
94
95
# File 'lib/osctld/container/hooks/base.rb', line 93

def hook_path
  self.class.hook_path(ct)
end