Class: OsCtld::ContainerControl::Commands::Stop::Runner

Inherits:
Runner
  • Object
show all
Includes:
Utils::Wall::Runner
Defined in:
lib/osctld/container_control/commands/stop.rb

Instance Attribute Summary

Attributes inherited from Runner

#ctid, #log_file, #lxc_home, #pool, #stderr, #stdin, #stdout, #user_home

Instance Method Summary collapse

Methods included from Utils::Wall::Runner

#ct_wall, #write_to_tty

Methods inherited from Runner

#error, #initialize, #lxc_ct, #ok, #setup_exec_env, #setup_exec_run_env, #system_path

Constructor Details

This class inherits a constructor from OsCtld::ContainerControl::Runner

Instance Method Details

#do_kill(_opts) ⇒ Object (protected)



98
99
100
101
102
103
# File 'lib/osctld/container_control/commands/stop.rb', line 98

def do_kill(_opts)
  lxc_ct.stop
  ok
rescue LXC::Error
  error('unable to kill container')
end

#do_shutdown(opts) ⇒ Object (protected)



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/osctld/container_control/commands/stop.rb', line 83

def do_shutdown(opts)
  timeout = opts[:timeout]

  if opts[:halt_from_inside]
    halt_seconds = run_halt(timeout)
    timeout -= halt_seconds
    timeout = 60 if timeout < 60
  end

  lxc_ct.shutdown(timeout)
  ok
rescue LXC::Error
  error('unable to shutdown container')
end

#do_stop(opts) ⇒ Object (protected)



75
76
77
78
79
80
81
# File 'lib/osctld/container_control/commands/stop.rb', line 75

def do_stop(opts)
  if do_shutdown(opts)[:status]
    ok
  else
    error('kill required')
  end
end

#execute(mode, opts) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/osctld/container_control/commands/stop.rb', line 61

def execute(mode, opts)
  if opts[:message]
    begin
      ct_wall(opts[:message])
    rescue LXC::Error
      # ignore
    end
  end

  send(:"do_#{mode}", opts)
end

#run_halt(timeout) ⇒ Integer (protected)

Returns halt duration in seconds.

Returns:

  • (Integer)

    halt duration in seconds



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/osctld/container_control/commands/stop.rb', line 106

def run_halt(timeout)
  queue = OsCtl::Lib::Queue.new
  t1 = Time.now

  pid = lxc_ct.attach do
    setup_exec_env

    %w[halt poweroff shutdown].each do |cmd|
      LXC.run_command(cmd)
    rescue LXC::Error
      next
    end
  end

  timeout_thread = Thread.new do
    next if queue.pop(timeout:) == :done

    Process.kill('KILL', pid) if pid && pid > 1
  rescue Errno::ESRCH
    next
  end

  Process.wait(pid) if pid && pid > 1
  queue << :done
  timeout_thread.join

  Time.now - t1
end