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

#from_remote_template, #from_stream, #recv_stream

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

#apply_template(builder, tpl) ⇒ Object (protected)



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

def apply_template(builder, tpl)
  case tpl[:type].to_sym
  when :remote
    remove_rootfs(builder)

    tpl[:template][:distribution] ||= builder.ct.distribution
    tpl[:template][:version] ||= builder.ct.version
    tpl[:template][:arch] ||= builder.ct.arch
    tpl[:template][:vendor] ||= 'default'
    tpl[:template][:variant] ||= 'default'

    from_remote_template(builder, tpl)

  when :archive
    remove_rootfs(builder)
    builder.setup_rootfs
    builder.from_local_archive(tpl[:path])

  when :stream
    from_stream(builder)

  else
    fail "unknown template type '#{tpl[:type]}'"
  end
end

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

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

    builder = Container::Builder.new(ct, cmd: self, reinstall: true)

    # 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 template
    apply_template(builder, opts[:template])

    # 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
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)



72
73
74
75
76
77
78
# File 'lib/osctld/commands/container/reinstall.rb', line 72

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)



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

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