Class: OsCtl::Lib::Exporter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/libosctl/exporter/base.rb

Overview

Handles dumping containers into tar archives

This base class can dump only archive metadata and config files. To export the container's rootfs, use either Zfs or Tar.

Direct Known Subclasses

Tar, Zfs

Constant Summary collapse

DIR_MODE =

0755

16877
FILE_MODE =

0644

33188

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ct, io, opts = {}) ⇒ Base

Returns a new instance of Base

Parameters:

  • ct (Container)
  • io (IO)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • compression (Symbol)

    auto/off/gzip

  • compressed_send (Boolean)


21
22
23
24
25
26
# File 'lib/libosctl/exporter/base.rb', line 21

def initialize(ct, io, opts = {})
  @ct = ct
  @tar = Gem::Package::TarWriter.new(io)
  @opts = opts
  @datasets = []
end

Instance Attribute Details

#base_snapObject (readonly, protected)

Returns the value of attribute base_snap



86
87
88
# File 'lib/libosctl/exporter/base.rb', line 86

def base_snap
  @base_snap
end

#ctObject (readonly, protected)

Returns the value of attribute ct



86
87
88
# File 'lib/libosctl/exporter/base.rb', line 86

def ct
  @ct
end

#datasetsObject (readonly, protected)

Returns the value of attribute datasets



86
87
88
# File 'lib/libosctl/exporter/base.rb', line 86

def datasets
  @datasets
end

#optsObject (readonly, protected)

Returns the value of attribute opts



86
87
88
# File 'lib/libosctl/exporter/base.rb', line 86

def opts
  @opts
end

#tarObject (readonly, protected)

Returns the value of attribute tar



86
87
88
# File 'lib/libosctl/exporter/base.rb', line 86

def tar
  @tar
end

Instance Method Details

#add_file_from_disk(src, dst) ⇒ Object (protected)

Add file from disk to the created tar archive

Parameters:

  • src (String)

    path on disk

  • dst (String)

    path in tar



91
92
93
94
95
96
97
# File 'lib/libosctl/exporter/base.rb', line 91

def add_file_from_disk(src, dst)
  st = File.stat(src)

  tar.add_file(dst, st.mode) do |tf|
    File.open(src, 'r') { |df| IO.copy_stream(df, tf) }
  end
end

#closeObject



79
80
81
# File 'lib/libosctl/exporter/base.rb', line 79

def close
  tar.close
end

#dump_configsObject

Dump configuration of the container, its user and group



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/libosctl/exporter/base.rb', line 44

def dump_configs
  tar.mkdir('config', DIR_MODE)
  tar.add_file('config/user.yml', FILE_MODE) do |tf|
    tf.write(File.read(ct.user.config_path))
  end
  tar.add_file('config/group.yml', FILE_MODE) do |tf|
    tf.write(File.read(ct.group.config_path))
  end
  tar.add_file('config/container.yml', FILE_MODE) do |tf|
    tf.write(File.read(ct.config_path))
  end
end

#dump_metadata(type) ⇒ Object

Dump important metadata describing the archive



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/libosctl/exporter/base.rb', line 29

def (type)
  tar.add_file('metadata.yml', FILE_MODE) do |tf|
    tf.write(YAML.dump(
      'type' => type,
      'format' => format.to_s,
      'user' => ct.user.name,
      'group' => ct.group.name,
      'container' => ct.id,
      'datasets' => datasets.map { |ds| ds.relative_name },
      'exported_at' => Time.now.to_i,
    ))
  end
end

#dump_user_hook_scripts(supported_hooks) ⇒ Object

Dump user script hooks, if there is at least one present

Parameters:

  • supported_hooks (Array<Symbol>)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/libosctl/exporter/base.rb', line 59

def dump_user_hook_scripts(supported_hooks)
  dir = ct.user_hook_script_dir
  hooks = Dir.entries(dir).map do |f|
    path = File.join(dir, f)

    [path, f, File.lstat(path)]

  end.select do |path, name, st|
    supported_hooks.include?(name.gsub(/-/, '_').to_sym) && st.file?
  end

  return if hooks.empty?

  tar.mkdir('hooks', DIR_MODE)

  hooks.each do |path, name, _st|
    add_file_from_disk(path, File.join('hooks', name))
  end
end

#formatObject



83
# File 'lib/libosctl/exporter/base.rb', line 83

def format; nil; end