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)



161
162
163
# File 'lib/osctld/commands/self/shutdown.rb', line 161

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

#check_abort?Boolean (protected)

Returns:

  • (Boolean)


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

def check_abort?
  Daemon.get.abort_shutdown?
end

#executeObject



7
8
9
10
11
12
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
# File 'lib/osctld/commands/self/shutdown.rb', line 7

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

  # Make sure that osctld crash/restart will not stop the shutdown
  Daemon.get.begin_shutdown

  # Grab manipulation locks of all pools
  grabbed_pools = grab_pools

  if check_abort?
    release_grabbed(grabbed_pools)
    error!('shutdown aborted')
  end

  # 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
  grabbed_cts = grab_all_cts

  if check_abort?
    release_grabbed(grabbed_cts)
    release_grabbed(grabbed_pools)
    error!('shutdown aborted')
  end

  # 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

    if check_abort?
      release_grabbed(grabbed_cts)
      release_grabbed(grabbed_pools)
      error!('shutdown aborted')
    end
  end

  # Export pools one by one
  DB::Pools.get.each do |pool|
    if check_abort?
      release_grabbed(grabbed_cts)
      release_grabbed(grabbed_pools)
      error!('shutdown aborted')
    end

    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

  # Confirm the shutdown for anyone waiting for it, i.e. osctl shutdown
  Daemon.get.confirm_shutdown

  ok
end

#grab_all_ctsObject (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
# File 'lib/osctld/commands/self/shutdown.rb', line 125

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

  loop do
    break if check_abort?

    cts.delete_if do |ct|
      break if check_abort?

      begin
        ct.acquire_manipulation_lock(self)
        grabbed << ct
        true
      rescue ResourceLocked => e
        progress(e.message)
        false
      end
    end

    break if cts.empty?

    sleep(1)
  end

  grabbed
end

#grab_poolsObject (protected)



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 96

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

  loop do
    break if check_abort?

    pools.delete_if do |pool|
      break if check_abort?

      begin
        pool.acquire_manipulation_lock(self)
        grabbed << pool
        true
      rescue ResourceLocked => e
        progress(e.message)
        false
      end
    end

    break if pools.empty?

    sleep(1)
  end

  grabbed
end

#release_grabbed(grabbed) ⇒ Object (protected)



154
155
156
157
158
159
# File 'lib/osctld/commands/self/shutdown.rb', line 154

def release_grabbed(grabbed)
  grabbed.each do |v|
    # The locks may have already been released by Commands::Pool::Export
    v.release_manipulation_lock if v.manipulated_by == self
  end
end