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!, 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

#executeObject



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
# File 'lib/osctld/commands/pool/export.rb', line 11

def execute
  pool = DB::Pools.find(opts[:name])
  error!('pool not imported') unless pool

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

  manipulate(pool) do
    # Do not autostart any more containers
    pool.stop

    # Disable the pool
    pool.exclusively { pool.disable }

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

    # Stop all containers
    if opts[:stop_containers]
      progress('Stopping all containers')
      stop_cts(pool)
    end

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

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

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

        elsif obj.is_a?(Container)
          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
    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
    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)

    ok
  end
end

#grab_cts(pool) ⇒ Object (protected)



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/osctld/commands/pool/export.rb', line 103

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

  loop do
    cts.delete_if do |ct|
      begin
        ct.acquire_manipulation_lock(self)
        true

      rescue ResourceLocked => e
        progress(e.message)
        false
      end
    end

    break if cts.empty?
    sleep(1)
  end
end

#stop_cts(pool) ⇒ Object (protected)



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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/osctld/commands/pool/export.rb', line 124

def stop_cts(pool)
  # Sort containers by reversed autostart priority -- containers with
  # the lowest priority are stopped first
  cts = DB::Containers.get.select { |ct| ct.pool == pool }
  cts.sort! do |a, b|
    if a.autostart && b.autostart
      a.autostart <=> b.autostart

    elsif a.autostart
      -1

    elsif b.autostart
      1

    else
      0
    end
  end
  cts.reverse!

  total = cts.count
  done = 0
  mutex = Mutex.new

  plan = ExecutionPlan.new
  cts.each { |ct| plan << ct }

  plan.run(pool.parallel_stop) do |ct|
    mutex.synchronize do
      done += 1
      progress(
        "[#{done}/#{total}] "+
        (ct.ephemeral? ? 'Deleting ephemeral container' : 'Stopping container')+
        " #{ct.ident}"
      )
    end

    if ct.ephemeral?
      call_cmd!(
        Commands::Container::Delete,
        pool: pool.name,
        id: ct.id,
        force: true,
        progress: false,
        manipulation_lock: 'ignore',
      )
    else
      call_cmd!(
        Commands::Container::Stop,
        pool: pool.name,
        id: ct.id,
        progress: false,
        manipulation_lock: 'ignore',
      )
    end
  end

  plan.wait
end