Class: OsCtld::Container::Importer

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System
Defined in:
lib/osctld/container/importer.rb

Overview

An interface for reading tar archives generated by OsCtl::Lib::Container::Exporter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool, io, ct_id: nil) ⇒ Importer

Returns a new instance of Importer



14
15
16
17
18
# File 'lib/osctld/container/importer.rb', line 14

def initialize(pool, io, ct_id: nil)
  @pool = pool
  @tar = Gem::Package::TarReader.new(io)
  @ct_id = ct_id
end

Instance Attribute Details

#metadataObject (readonly, protected)

Returns the value of attribute metadata



240
241
242
# File 'lib/osctld/container/importer.rb', line 240

def 
  @metadata
end

#poolObject (readonly, protected)

Returns the value of attribute pool



240
241
242
# File 'lib/osctld/container/importer.rb', line 240

def pool
  @pool
end

#tarObject (readonly, protected)

Returns the value of attribute tar



240
241
242
# File 'lib/osctld/container/importer.rb', line 240

def tar
  @tar
end

Instance Method Details

#closeObject



235
236
237
# File 'lib/osctld/container/importer.rb', line 235

def close
  tar.close
end

#copy_file_to_disk(entry, dst) ⇒ Object (protected)

Copy file from the tar archive to disk

Parameters:

  • entry (Gem::Package::TarReader::Entry)
  • dst (String)


381
382
383
384
385
# File 'lib/osctld/container/importer.rb', line 381

def copy_file_to_disk(entry, dst)
  File.open(dst, 'w', entry.header.mode & 07777) do |df|
    df.write(entry.read(16*1024)) until entry.eof?
  end
end

#create_datasets(builder) ⇒ Object

Create the root and all descendants datasets

Parameters:



186
187
188
189
190
# File 'lib/osctld/container/importer.rb', line 186

def create_datasets(builder)
  datasets(builder).each do |ds|
    builder.create_dataset(ds, mapping: true, parents: ds.root?)
  end
end

#create_new_userUser (protected)

Returns:



278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/osctld/container/importer.rb', line 278

def create_new_user
  name = ct_id

  db = DB::Users.find(name, pool)
  return db if db

  # The user does not exist, create him
  Commands::User::Create.run!(
    pool: pool.name,
    name: name,
  )

  DB::Users.find(name, pool) || (fail 'expected user')
end

#ct_idObject



49
50
51
# File 'lib/osctld/container/importer.rb', line 49

def ct_id
  @ct_id || ['container']
end

#datasets(builder) ⇒ Array<OsCtl::Lib::Zfs::Dataset>

Parameters:

Returns:

  • (Array<OsCtl::Lib::Zfs::Dataset>)


224
225
226
227
228
229
230
231
232
233
# File 'lib/osctld/container/importer.rb', line 224

def datasets(builder)
  return @datasets if @datasets

  @datasets = [builder.ct.dataset] + ['datasets'].map do |name|
    OsCtl::Lib::Zfs::Dataset.new(
      File.join(builder.ct.dataset.name, name),
      base: builder.ct.dataset.name,
    )
  end
end

#get_distribution_infoArray(String, String, String)

Returns distribution, version, arch

Returns:

  • (Array(String, String, String))

    distribution, version, arch



122
123
124
125
# File 'lib/osctld/container/importer.rb', line 122

def get_distribution_info
  cfg = YAML.load(tar.seek('config/container.yml') { |entry| entry.read })
  [cfg['distribution'], cfg['version'], cfg['arch']]
end

#get_or_create_groupGroup

Load the group from the archive and register it, or create a new group

If a group with the same name already exists and all its parameters are the same, the existing group is returned. Otherwise an exception is raised.

Returns:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/osctld/container/importer.rb', line 145

def get_or_create_group
  if has_group?
    name = ['group']

    db = DB::Groups.find(name, pool)
    grp = load_group

    if db.nil?
      # The group does not exist, create it
      Commands::Group::Create.run!(
        pool: pool.name,
        name: grp.name
      )

      return DB::Groups.find(name, pool) || (fail 'expected group')
    end

    db

  else
    DB::Groups.default(pool)
  end
end

#get_or_create_userUser

Load the user from the archive and register him, or create a new user

If a user with the same name already exists and all his parameters are the same, the existing user is returned. Otherwise an exception is raised.

Returns:



132
133
134
135
136
137
138
# File 'lib/osctld/container/importer.rb', line 132

def get_or_create_user
  if has_user?
    load_or_create_user
  else
    create_new_user
  end
end

#group_nameObject



41
42
43
# File 'lib/osctld/container/importer.rb', line 41

def group_name
  ['group']
end

#has_ct_id?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/osctld/container/importer.rb', line 53

def has_ct_id?
  !ct_id.nil?
end

#has_group?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/osctld/container/importer.rb', line 45

def has_group?
  !group_name.nil?
end

#has_user?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/osctld/container/importer.rb', line 37

def has_user?
  !user_name.nil?
end

#import_all_datasets(builder) ⇒ Object

Import all datasets

Parameters:



194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/osctld/container/importer.rb', line 194

def import_all_datasets(builder)
  case ['format']
  when 'zfs'
    import_streams(builder, datasets(builder))

  when 'tar'
    unpack_rootfs(builder)

  else
    fail "unsupported archive format '#{['format']}'"
  end
end

#import_root_dataset(builder) ⇒ Object

Import just the root dataset

Parameters:



209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/osctld/container/importer.rb', line 209

def import_root_dataset(builder)
  case ['format']
  when 'zfs'
    import_streams(builder, [datasets(builder).first])

  when 'tar'
    unpack_rootfs(builder)

  else
    fail "unsupported archive format '#{['format']}'"
  end
end

#import_stream(builder, ds, name, required) ⇒ Object (protected)



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/osctld/container/importer.rb', line 330

def import_stream(builder, ds, name, required)
  found = nil

  stream_names(name).each do |file, compression|
    tf = tar.find { |entry| entry.full_name == file }

    if tf.nil?
      tar.rewind
      next
    end

    found = [tf, compression]
    break
  end

  if found.nil?
    tar.rewind
    fail "unable to import: #{name} not found" if required
    return
  end

  entry, compression = found
  process_stream(builder, ds, entry, compression)
  tar.rewind
end

#import_streams(builder, datasets) ⇒ Object (protected)

Load ZFS data streams from the archive and write them to appropriate datasets

Parameters:



298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/osctld/container/importer.rb', line 298

def import_streams(builder, datasets)
  datasets.each do |ds|
    import_stream(builder, ds, File.join(ds.relative_name, 'base'), true)
    import_stream(builder, ds, File.join(ds.relative_name, 'incremental'), false)
  end

  tar.seek('snapshots.yml') do |entry|
    snapshots = YAML.load(entry.read)

    datasets(builder).each do |ds|
      snapshots.each { |snap| zfs(:destroy, nil, "#{ds}@#{snap}") }
    end
  end
end

#install_user_hook_scripts(ct) ⇒ Object

Load user-defined script hooks from the archive and install them

Parameters:



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/osctld/container/importer.rb', line 171

def install_user_hook_scripts(ct)
  tar.each do |entry|
    next if !entry.full_name.start_with?('hooks/') || !entry.file? \
            || entry.full_name.count('/') > 1

    name = File.basename(entry.full_name)
    next unless Container::Hook.exist?(name.gsub(/-/, '_').to_sym)

    copy_file_to_disk(entry, File.join(ct.user_hook_script_dir, name))
  end
end

#load_ct(opts) ⇒ Container

Create a new instance of OsCtld::Container as described by the tar archive

The returned CT is not registered in the internal database, it may even conflict with a CT already registered in the database.

Parameters:

  • opts (Hash)

    options

Options Hash (opts):

Returns:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/osctld/container/importer.rb', line 104

def load_ct(opts)
  id = opts[:id] || ['container']
  user = opts[:user] || get_or_create_user
  group = opts[:group] || get_or_create_group
  ct_opts = opts[:ct_opts] || {}
  ct_opts[:load_from] = tar.seek('config/container.yml') { |entry| entry.read }

  Container.new(
    pool,
    id,
    user,
    group,
    opts[:dataset] || Container.default_dataset(pool, id),
    ct_opts
  )
end

#load_groupGroup?

Create a new instance of Group as described by the tar archive

The returned group is not registered in the internal database, it may even conflict with a group already registered in the database.

Returns:



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/osctld/container/importer.rb', line 79

def load_group
  if has_group?
    Group.new(
      pool,
      ['group'],
      config: tar.seek('config/group.yml') { |entry| entry.read },
      devices: false
    )
  else
    nil
  end
end

#load_metadataObject

Load metadata describing the archive

Loading the metadata is the first thing that should be done, because all other methods depend on its result.



24
25
26
27
28
29
30
31
# File 'lib/osctld/container/importer.rb', line 24

def 
  ret = tar.seek('metadata.yml') do |entry|
    YAML.load(entry.read)
  end
  fail 'metadata.yml not found' unless ret
  @metadata = ret
  ret
end

#load_or_create_userUser (protected)

Returns:



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/osctld/container/importer.rb', line 243

def load_or_create_user
  name = ['user']

  db = DB::Users.find(name, pool)
  u = load_user

  if db.nil?
    # The user does not exist, create him
    Commands::User::Create.run!(
      pool: pool.name,
      name: u.name,
      ugid: u.ugid,
      uid_map: u.uid_map.export,
      gid_map: u.gid_map.export,
    )

    return DB::Users.find(name, pool) || (fail 'expected user')
  end

  # Free the newly allocated ugid, use ugid from the existing user
  UGidRegistry.remove(u.ugid) if u.ugid != db.ugid

  %i(uid_map gid_map).each do |param|
    mine = db.send(param)
    other = u.send(param)
    next if mine == other

    fail "user #{pool.name}:#{name} already exists: #{param} mismatch: "+
         "existing #{mine}, trying to import #{other}"
  end

  db
end

#load_userUser?

Create a new instance of User as described by the tar archive

The returned user is not registered in the internal database, it may even conflict with a user already registered in the database.

Returns:



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/osctld/container/importer.rb', line 62

def load_user
  if has_user?
    User.new(
      pool,
      ['user'],
      config: tar.seek('config/user.yml') { |entry| entry.read }
    )
  else
    nil
  end
end

#process_stream(builder, ds, tf, compression) ⇒ Object (protected)



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/osctld/container/importer.rb', line 356

def process_stream(builder, ds, tf, compression)
  builder.from_stream(ds) do |recv|
    case compression
    when :gzip
      gz = Zlib::GzipReader.new(tf)
      recv.write(gz.readpartial(16*1024)) until gz.eof?
      gz.close

    when :off
      recv.write(tf.read(16*1024)) until tf.eof?

    else
      fail "unexpected compression type '#{compression}'"
    end
  end
end

#stream_names(name) ⇒ Object (protected)



373
374
375
376
# File 'lib/osctld/container/importer.rb', line 373

def stream_names(name)
  base = File.join('rootfs', "#{name}.dat")
  [[base, :off], ["#{base}.gz", :gzip]]
end

#unpack_rootfs(builder) ⇒ Object (protected)

Parameters:



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/osctld/container/importer.rb', line 314

def unpack_rootfs(builder)
  # Create private/
  builder.setup_rootfs

  ret = tar.seek('rootfs/base.tar.gz') do |tf|
    IO.popen("exec tar -xz -C #{builder.ct.rootfs}", 'r+') do |io|
      io.write(tf.read(16*1024)) until tf.eof?
    end

    fail "tar failed with exit status #{$?.exitstatus}" if $?.exitstatus != 0
    true
  end

  fail 'rootfs archive not found' unless ret === true
end

#user_nameObject



33
34
35
# File 'lib/osctld/container/importer.rb', line 33

def user_name
  ['user']
end