Class: OsCtld::Commands::Pool::Export

Inherits:
Base
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System
Defined in:
lib/osctld/commands/pool/export.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!(pool) ⇒ Object (protected)



175
176
177
# File 'lib/osctld/commands/pool/export.rb', line 175

def check_abort!(pool)
  error!('pool export aborted') if check_abort?(pool)
end

#check_abort?(pool) ⇒ Boolean (protected)

Returns:

  • (Boolean)


179
180
181
# File 'lib/osctld/commands/pool/export.rb', line 179

def check_abort?(pool)
  pool.abort_export? || (indirect? && Daemon.get.abort_shutdown?)
end

#executeObject



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
93
94
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
124
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/pool/export.rb', line 10

def execute
  pool = DB::Pools.find(opts[:name])

  unless pool
    if opts[:if_imported]
      progress('pool not imported')
      return ok
    else
      error!('pool not imported')
    end
  end

  # Check for running containers
  if !opts[:force] && DB::Containers.get.detect { |ct| ct.pool == pool && ct.running? }
    error!('the pool has running containers')
  end

  begin
    Hook.run(pool, :pre_export)
  rescue HookFailed => e
    error!("pre-export hook failed: #{e.message}")
  end

  manipulate(pool) do
    pool.begin_export

    # Do not autostart any more containers
    pool.begin_stop
    check_abort!(pool)

    # Disable the pool
    pool.exclusively { pool.disable }
    check_abort!(pool)

    # Grab manipulation locks of all containers
    grab_cts(pool) unless opts[:grab_containers] === false
    check_abort!(pool)

    # Stop all containers
    if opts[:stop_containers]
      progress('Stopping all containers')
      pool.autostop_and_wait(client_handler:, message: opts[:message])
      check_abort!(pool)

      # Verify that all containers are stopped
      loop do
        all_stopped = true

        DB::Containers.get.each do |ct|
          next if ct.pool != pool

          next if %i[staged stopped].include?(ct.state)

          msg = "Container #{ct.ident} is still #{ct.state}, waiting until stopped"
          progress(msg)
          log(:warn, msg)
          all_stopped = false
        end

        check_abort!(pool)
        break if all_stopped

        check_abort!(pool)
        sleep(1)
      end
    end

    # Stop also autostop
    pool.all_stop

    # Unregister all entities
    progress('Unregistering users, groups and containers')

    # Preserve root group
    root_group = DB::Groups.root(pool)

    [
      DB::Containers,
      DB::Users,
      DB::Groups,
      DB::Repositories,
      DB::IdRanges
    ].each do |klass|
      klass.get.each do |obj|
        next if obj.pool != pool

        if obj.is_a?(User)
          obj.exclusively { UserControl::Supervisor.stop_server(obj) }

          if opts[:unregister_users] && opts[:stop_containers]
            call_cmd!(
              Commands::User::Unregister,
              pool: pool.name,
              name: obj.name
            )

            # When a user with the same name is going to be imported again,
            # he may have a different ugid than before. All files in his
            # directory would then have an incorrect owner.
            syscmd("rm -rf \"#{obj.userdir}\"")
          end

        elsif obj.is_a?(Container)
          obj.unregister
          Monitor::Master.demonitor(obj)
          Console.remove(obj)
        end

        klass.remove(obj)
        obj.release_manipulation_lock if obj.is_a?(Container)
      end
    end

    # Remove all cgroups & BPF
    if opts[:stop_containers]
      progress('Removing cgroups')
      begin
        CGroup.rmpath_all(root_group.cgroup_path)
      rescue SystemCallError
        # If some of the cgroups are busy, just leave them be
      end

      # Clear-out BPF FS
      BpfFs.remove_pool(pool.name)
    end

    # Regenerate /etc/sub{u,g}ids and lxc-usernet
    call_cmd!(Commands::User::SubUGIds)
    call_cmd!(Commands::User::LxcUsernet)

    # Close history
    History.close(pool)

    # Remove pool from the database
    DB::Pools.remove(pool)

    # Remove outdated send/receive keys
    SendReceive.deploy
  end

  Hook.run(pool, :post_export)
  ok
end

#grab_cts(pool) ⇒ Object (protected)



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/osctld/commands/pool/export.rb', line 156

def grab_cts(pool)
  progress('Grabbing all containers')
  cts = DB::Containers.get.select { |ct| ct.pool == pool }

  loop do
    cts.delete_if do |ct|
      ct.acquire_manipulation_lock(self)
      true
    rescue ResourceLocked => e
      progress(e.message)
      false
    end

    break if cts.empty?

    sleep(1)
  end
end