Class: OsCtld::Commands::Container::Reinstall

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

Instance Method Summary collapse

Methods included from Utils::Container

#format_unavailable_repositories, #get_image_path, #get_image_path!, #get_repositories, #image_not_found_message, #image_spec, #image_unavailable_message, #remove_accounting_cgroups, #with_image_path, #with_repository_image_path!

Methods inherited from Logged

#base_execute

Instance Method Details

#execute(ct) ⇒ Object



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
# File 'lib/osctld/commands/container/reinstall.rb', line 16

def execute(ct)
  fh = nil

  manipulate(ct) do
    error!('container is running') if ct.running?

    tpl = opts[:image]
    if opts[:type] == 'remote'
      tpl[:distribution] ||= ct.distribution
      tpl[:version] ||= ct.version
      tpl[:arch] ||= ct.arch
      tpl[:vendor] ||= ct.vendor
      tpl[:variant] ||= ct.variant
    end

    with_image_path(ct.pool, type: opts[:type], path: opts[:path], image: tpl) do |tpl_path|
      builder = Container::Builder.new(ct.new_run_conf, cmd: self)

      # Remove all snapshots
      snaps = snapshots(ct)

      if snaps.any? && !opts[:remove_snapshots]
        error!("the dataset has snapshots:\n  #{snaps.join("\n  ")}")
      end

      snaps.each { |snap| zfs(:destroy, nil, snap) }

      # Unmount all datasets
      ct.dataset.unmount(recursive: true)

      # Create a new rootfs dataset with temporary name
      props = OsCtl::Lib::Zfs::PropertyState.new
      props.read_from(ct.dataset)

      new_ds = OsCtl::Lib::Zfs::Dataset.new("#{ct.dataset}.reinstall")
      new_ds.create!(properties: props.options)

      # Move subdatasets to the new dataset
      ct.dataset.children.each do |ds|
        new_subds = File.join(new_ds.name, ds.relative_name)
        zfs(:rename, nil, "#{ds} #{new_subds}")
      end

      # Destroy the original rootfs dataset
      TrashBin.add_dataset(ct.pool, ct.dataset)

      # Replace the original dataset with the new one
      zfs(:rename, nil, "#{new_ds} #{ct.dataset}")

      # Apply new image
      fh = File.open(tpl_path, 'r')
      importer = Container::Importer.new(ct.pool, fh, ct_id: ct.id, image_file: tpl_path)
      importer.
      importer.import_root_dataset(builder)

      # Update image-specific config
      ct.patch_config(importer.get_container_config)

      # Remount all datasets
      ct.dataset.mount(recursive: true)

      builder.setup_ct_dir
      builder.setup_rootfs
    end

    ok
  end
ensure
  fh && fh.close
end

#findObject



11
12
13
14
# File 'lib/osctld/commands/container/reinstall.rb', line 11

def find
  ct = DB::Containers.find(opts[:id], opts[:pool])
  ct || error!('container not found')
end

#snapshots(ct) ⇒ Object (protected)



89
90
91
# File 'lib/osctld/commands/container/reinstall.rb', line 89

def snapshots(ct)
  zfs(:list, '-H -r -d 1 -o name -t snapshot', ct.dataset).output.split("\n")
end