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 Attribute Summary

Attributes inherited from Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

Methods included from Utils::Container

#get_image_path, #get_repositories

Methods inherited from Logged

#base_execute

Methods inherited from Base

#base_execute, #call_cmd, #call_cmd!, cmd, #error, #error!, handle, #handled, #indirect?, #initialize, #manipulate, #manipulation_holder, #ok, #progress, #request_stop, run, run!

Constructor Details

This class inherits a constructor from OsCtld::Commands::Base

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

def execute(ct)
  fh = nil

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

    if opts[:type] == 'image'
      tpl_path = opts[:path]
    elsif opts[:type] == 'remote'
      progress('Fetching image')

      tpl = opts[:image]
      tpl[:distribution] ||= ct.distribution
      tpl[:version] ||= ct.version
      tpl[:arch] ||= ct.arch
      tpl[:vendor] ||= 'default'
      tpl[:variant] ||= 'default'

      tpl_path = get_image_path(get_repositories(ct.pool), tpl)
      error!('image not found in searched repositories') if tpl_path.nil?
    else
      error!('invalid type')
    end

    builder = Container::Builder.new(ct, 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) }

    # Ensure the container is mounted
    ct.mount(force: true)

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

    remove_rootfs(builder)
    importer.import_root_dataset(builder)

    dist, ver, arch = importer.get_distribution_info
    ct.set(distribution: {name: dist, version: ver, arch: arch})

    # Remount all subdatasets (subdatasets are unmounted because the builder
    # unmounted them to configure uid/gid mapping again)
    ct.dataset.descendants.each { |ds| zfs(:mount, nil, ds) }

    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

#remove_rootfs(builder) ⇒ Object (protected)



77
78
79
80
81
82
83
# File 'lib/osctld/commands/container/reinstall.rb', line 77

def remove_rootfs(builder)
  progress('Removing rootfs')
  snap = "#{builder.ct.dataset}@osctl-reinstall"
  zfs(:snapshot, nil, snap)
  syscmd("rm -rf \"#{builder.ct.rootfs}\"")
  zfs(:destroy, nil, snap)
end

#snapshots(ct) ⇒ Object (protected)



85
86
87
# File 'lib/osctld/commands/container/reinstall.rb', line 85

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