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

Constant Summary collapse

BLOCK_SIZE =
128*1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Importer.



17
18
19
20
21
# File 'lib/osctld/container/importer.rb', line 17

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.



250
251
252
# File 'lib/osctld/container/importer.rb', line 250

def 
  @metadata
end

#poolObject (readonly, protected)

Returns the value of attribute pool.



250
251
252
# File 'lib/osctld/container/importer.rb', line 250

def pool
  @pool
end

#tarObject (readonly, protected)

Returns the value of attribute tar.



250
251
252
# File 'lib/osctld/container/importer.rb', line 250

def tar
  @tar
end

Instance Method Details

#closeObject



245
246
247
# File 'lib/osctld/container/importer.rb', line 245

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)


395
396
397
398
399
# File 'lib/osctld/container/importer.rb', line 395

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

#create_datasets(builder, accept_existing: false) ⇒ Object

Create the root and all descendants datasets

Parameters:



194
195
196
197
198
199
200
# File 'lib/osctld/container/importer.rb', line 194

def create_datasets(builder, accept_existing: false)
  datasets(builder).each do |ds|
    next if accept_existing && ds.exist?

    builder.create_dataset(ds, mapping: true, parents: ds.root?)
  end
end

#create_new_userUser (protected)

Returns:



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/osctld/container/importer.rb', line 288

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,
    standalone: false,
  )

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

#ct_idObject



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

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

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

Parameters:

Returns:

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


234
235
236
237
238
239
240
241
242
243
# File 'lib/osctld/container/importer.rb', line 234

def datasets(builder)
  return @datasets if @datasets

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

#get_container_configHash

Returns:

  • (Hash)


125
126
127
# File 'lib/osctld/container/importer.rb', line 125

def get_container_config
  YAML.load(tar.seek('config/container.yml') { |entry| entry.read })
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:



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

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:



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

def get_or_create_user
  if has_user?
    load_or_create_user
  else
    create_new_user
  end
end

#group_nameObject



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

def group_name
  ['group']
end

#has_ct_id?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/osctld/container/importer.rb', line 56

def has_ct_id?
  !ct_id.nil?
end

#has_group?Boolean

Returns:

  • (Boolean)


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

def has_group?
  !group_name.nil?
end

#has_user?Boolean

Returns:

  • (Boolean)


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

def has_user?
  !user_name.nil?
end

#import_all_datasets(builder) ⇒ Object

Import all datasets

Parameters:



204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/osctld/container/importer.rb', line 204

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:



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/osctld/container/importer.rb', line 219

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)



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/osctld/container/importer.rb', line 344

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:



309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/osctld/container/importer.rb', line 309

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:



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/osctld/container/importer.rb', line 173

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

    name = entry.full_name[ ('hooks/'.length-1) .. -1 ]

    if entry.directory?
      FileUtils.mkdir_p(
        File.join(ct.user_hook_script_dir, name),
        mode: entry.header.mode & 07777,
      )
    elsif entry.file?
      copy_file_to_disk(entry, File.join(ct.user_hook_script_dir, name))
    end
  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:



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

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:



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/osctld/container/importer.rb', line 82

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.



27
28
29
30
31
32
33
34
# File 'lib/osctld/container/importer.rb', line 27

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:



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/osctld/container/importer.rb', line 253

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:



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/osctld/container/importer.rb', line 65

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)



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/osctld/container/importer.rb', line 370

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.read(BLOCK_SIZE)) until gz.eof?
      gz.close

    when :off
      recv.write(tf.read(BLOCK_SIZE)) until tf.eof?

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

#stream_names(name) ⇒ Object (protected)



387
388
389
390
# File 'lib/osctld/container/importer.rb', line 387

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

#unpack_rootfs(builder) ⇒ Object (protected)

Parameters:



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/osctld/container/importer.rb', line 325

def unpack_rootfs(builder)
  # Ensure the dataset is mounted
  builder.ctrc.dataset.mount(recursive: true)

  # Create private/
  builder.setup_rootfs

  ret = tar.seek('rootfs/base.tar.gz') do |tf|
    IO.popen("exec tar -xz -C #{builder.ctrc.rootfs}", 'r+') do |io|
      io.write(tf.read(BLOCK_SIZE)) 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



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

def user_name
  ['user']
end