Class: OsCtld::Commands::Self::Shutdown

Inherits:
Base
  • Object
show all
Defined in:
lib/osctld/commands/self/shutdown.rb

Instance Attribute Summary

Attributes inherited from Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

Methods inherited from Base

#base_execute, #call_cmd, #call_cmd!, #error, #error!, handle, #handled, #indirect?, #initialize, #manipulate, #manipulation_holder, #ok, #progress, #request_stop, run, run!

Constructor Details

This class inherits a constructor from OsCtld::Commands::Base

Instance Method Details

#check_abort! ⇒ Object (protected)



165
166
167
# File 'lib/osctld/commands/self/shutdown.rb', line 165

def check_abort!
  error!('shutdown aborted') if Daemon.get.abort_shutdown?
end

#check_abort? ⇒ Boolean (protected)

Returns:

  • (Boolean)


169
170
171
# File 'lib/osctld/commands/self/shutdown.rb', line 169

def check_abort?
  Daemon.get.abort_shutdown?
end

#execute ⇒ Object



7
8
9
# File 'lib/osctld/commands/self/shutdown.rb', line 7

def execute
  Daemon.get.run_shutdown { execute_shutdown }
end

#execute_shutdown ⇒ Object (protected)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/osctld/commands/self/shutdown.rb', line 13

def execute_shutdown
  grabbed_pools = []
  grabbed_cts = []
  wall_msg =
    if opts[:wall]
      opts[:message] || 'System is shutting down'
    end

  # Grab manipulation locks of all pools
  grab_pools(grabbed_pools)

  check_abort!

  # Disable all pools
  DB::Pools.get.each do |pool|
    progress("Disabling pool #{pool.name}")
    pool.exclusively { pool.disable }
  end
  check_abort!

  # Grab manipulation locks of all containers
  grab_all_cts(grabbed_cts)

  check_abort!

  # Stop containers from all pools
  stop_pools = DB::Pools.get

  if stop_pools.length > 1
    progress('Stopping all containers')
    progress_tracker = ProgressTracker.new

    stop_pools.each do |pool|
      break if check_abort?

      pool.begin_stop
      pool.autostop_no_wait(message: wall_msg, client_handler:, progress_tracker:)
    end

    stop_pools.each do |pool|
      break if check_abort?

      pool.wait_for_autostop
    end

    check_abort!
  end

  # Export pools one by one
  DB::Pools.get.each do |pool|
    check_abort!

    progress("Exporting pool #{pool.name}")

    call_cmd!(
      Commands::Pool::Export,
      name: pool.name,
      force: true,
      grab_containers: false,
      stop_containers: true,
      unregister_users: false,
      message: wall_msg
    )
  end

  ok
ensure
  original_error = $!
  cleanup_error = nil

  # The operation, not the lifetime of its setup/client thread, owns these
  # holds. Nested export may already have released some container locks.
  Thread.handle_interrupt(Exception => :never) do
    [grabbed_cts, grabbed_pools].each do |grabbed|
      release_grabbed(grabbed)
    rescue Exception => e # rubocop:disable Lint/RescueException -- Preserve interruption and cleanup outcomes.
      cleanup_error ||= e
    end
  end
  raise cleanup_error if cleanup_error && original_error.nil?
end

#grab_all_cts(grabbed) ⇒ Object (protected)



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/osctld/commands/self/shutdown.rb', line 125

def grab_all_cts(grabbed)
  progress('Grabbing all containers')
  cts = DB::Containers.get

  loop do
    break if check_abort?

    cts.delete_if do |ct|
      break if check_abort?

      begin
        Thread.handle_interrupt(Exception => :never) do
          ct.acquire_manipulation_lock(self)
          grabbed << ct
        end
        true
      rescue ResourceLocked => e
        progress(e.message)
        false
      end
    end

    break if cts.empty?

    sleep(1)
  end

  grabbed
end

#grab_pools(grabbed) ⇒ Object (protected)



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/osctld/commands/self/shutdown.rb', line 95

def grab_pools(grabbed)
  progress('Grabbing pools')
  pools = DB::Pools.get

  loop do
    break if check_abort?

    pools.delete_if do |pool|
      break if check_abort?

      begin
        Thread.handle_interrupt(Exception => :never) do
          pool.acquire_manipulation_lock(self)
          grabbed << pool
        end
        true
      rescue ResourceLocked => e
        progress(e.message)
        false
      end
    end

    break if pools.empty?

    sleep(1)
  end

  grabbed
end

#release_grabbed(grabbed) ⇒ Object (protected)



155
156
157
158
159
160
161
162
163
# File 'lib/osctld/commands/self/shutdown.rb', line 155

def release_grabbed(grabbed)
  error = nil
  grabbed.reverse_each do |v|
    v.release_manipulation_lock if v.manipulated_by.equal?(self)
  rescue Exception => e # rubocop:disable Lint/RescueException -- Preserve interruption and cleanup outcomes.
    error ||= e
  end
  raise error if error
end