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



99
100
101
# File 'lib/libosctl/exporter/base.rb', line 99

def base_snap
  @base_snap
end

#ctObject (readonly, protected)

Returns the value of attribute ct



99
100
101
# File 'lib/libosctl/exporter/base.rb', line 99

def ct
  @ct
end

#datasetsObject (readonly, protected)

Returns the value of attribute datasets



99
100
101
# File 'lib/libosctl/exporter/base.rb', line 99

def datasets
  @datasets
end

#optsObject (readonly, protected)

Returns the value of attribute opts



99
100
101
# File 'lib/libosctl/exporter/base.rb', line 99

def opts
  @opts
end

#tarObject (readonly, protected)

Returns the value of attribute tar



99
100
101
# File 'lib/libosctl/exporter/base.rb', line 99

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



104
105
106
107
108
109
110
# File 'lib/libosctl/exporter/base.rb', line 104

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



92
93
94
# File 'lib/libosctl/exporter/base.rb', line 92

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
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/libosctl/exporter/base.rb', line 44

def dump_configs
  tar.mkdir('config', DIR_MODE)

  if ct.user
    tar.add_file('config/user.yml', FILE_MODE) do |tf|
      tf.write(File.read(ct.user.config_path))
    end
  end

  if ct.group
    tar.add_file('config/group.yml', FILE_MODE) do |tf|
      tf.write(File.read(ct.group.config_path))
    end
  end

  tar.add_file('config/container.yml', FILE_MODE) do |tf|
    if ct.respond_to?(:dump_config)
      tf.write(YAML.dump(ct.dump_config))
    elsif ct.respond_to?(:config_path)
      tf.write(File.read(ct.config_path))
    else
      fail "don't know how to dump container config"
    end
  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 && ct.user.name,
      'group' => ct.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>)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/libosctl/exporter/base.rb', line 72

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



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

def format; nil; end