Class: OsCtld::Commands::Container::Copy

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

Instance Attribute Summary

Attributes inherited from Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

Methods inherited from Logged

#base_execute

Methods inherited from Base

#base_execute, #call_cmd, #call_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

#copy_datasets_from(builder, ct) ⇒ Object (protected)



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/osctld/commands/container/copy.rb', line 77

def copy_datasets_from(builder, ct)
  snaps = []
  src_datasets = ct.datasets
  dst_datasets = [builder.ctrc.dataset] + ct.dataset.descendants.map do |ds|
    OsCtl::Lib::Zfs::Dataset.new(
      File.join(builder.ctrc.dataset.name, ds.relative_name),
      base: builder.ctrc.dataset.name
    )
  end

  # Create datasets
  dst_datasets.each { |ds| builder.create_dataset(ds, mapping: true) }

  # Copy data
  snaps << builder.copy_datasets(src_datasets, dst_datasets)

  if ct.running? && opts[:consistent]
    call_cmd(Commands::Container::Stop, id: ct.id, pool: ct.pool.name)

    snaps << builder.copy_datasets(src_datasets, dst_datasets, from: snaps.last)

    if opts[:restart].nil? || opts[:restart]
      call_cmd(
        Commands::Container::Start,
        id: ct.id,
        pool: ct.pool.name,
        force: true,
        wait: false
      )
    end
  end

  # Cleanup snapshots
  (src_datasets + dst_datasets).each do |ds|
    snaps.each { |s| zfs(:destroy, nil, "#{ds}@#{s}") }
  end
end

#execute(ct) ⇒ Object



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

def execute(ct)
  target_pool = opts[:target_pool] ? DB::Pools.find(opts[:target_pool]) : ct.pool
  error!('pool not found') unless target_pool

  if target_pool == ct.pool
    target_user = nil
    target_group = nil

  else
    target_user = DB::Users.find(opts[:target_user] || ct.user.name, target_pool)
    error!('target user not found') unless target_user

    target_group = DB::Groups.find(opts[:target_group] || ct.group.name, target_pool)
    error!('target group not found') unless target_group
  end

  if DB::Containers.contains?(opts[:target_id], target_pool)
    error!("container #{target_pool.name}:#{opts[:target_id]} already exists")
  end

  new_ct = ct.exclusively do
    ct.dup(
      opts[:target_id],
      pool: target_pool,
      user: target_user,
      group: target_group,
      dataset: opts[:target_dataset],
      network_interfaces: opts[:network_interfaces]
    )
  end

  builder = Container::Builder.new(new_ct.new_run_conf, cmd: self)
  error!(builder.errors.join('; ')) unless builder.valid?

  manipulate([ct, new_ct]) do
    unless builder.register
      error!("container #{new_ct.pool.name}:#{new_ct.id} already exists")
    end

    begin
      copy_datasets_from(builder, ct)
      new_ct.save_config
      builder.setup_ct_dir
      builder.setup_lxc_home
      builder.setup_lxc_configs
      builder.setup_log_file
      builder.setup_user_hook_script_dir
      builder.monitor
      new_ct.state = :complete
    rescue StandardError
      progress('Error occurred, cleaning up')
      builder.cleanup(dataset: !opts[:target_dataset])
      raise
    end
  end

  call_cmd!(Commands::User::LxcUsernet)
  ok
end

#findObject



10
11
12
13
# File 'lib/osctld/commands/container/copy.rb', line 10

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