Class: OsCtld::Commands::Container::Import

Inherits:
Logged
  • Object
show all
Defined in:
lib/osctld/commands/container/import.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

#execute(pool) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/osctld/commands/container/import.rb', line 11

def execute(pool)
  error!('the pool is disabled') unless pool.active?

  if opts[:map_mode] && !Container::MAP_MODES.include?(opts[:map_mode])
    error!('invalid map mode')
  end

  File.open(opts[:file], 'r') do |f|
    import(pool, f, opts[:file])
  end

  ok
end

#findObject



7
8
9
# File 'lib/osctld/commands/container/import.rb', line 7

def find
  DB::Pools.get_or_default(opts[:pool]) || error!('pool not found')
end

#import(pool, io, image_file) ⇒ Object (protected)



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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/osctld/commands/container/import.rb', line 27

def import(pool, io, image_file)
  importer = Container::Importer.new(
    pool,
    io,
    ct_id: opts[:as_id],
    image_file:
  )
  importer.

  unless importer.has_ct_id?
    error!('the image does not include container id, specify it')
  end

  ctid = importer.ct_id

  if DB::Containers.find(ctid, pool)
    error!("container #{pool.name}:#{ctid} already exists")
  end

  if opts[:as_user]
    user = DB::Users.find(opts[:as_user], pool)
    error!('user not found') unless user

  else
    user = importer.get_or_create_user
  end

  if opts[:as_group]
    group = DB::Groups.find(opts[:as_group], pool)
    error!('group not found') unless group

  else
    group = importer.get_or_create_group
  end

  ct = importer.load_ct(
    id: ctid,
    user:,
    group:,
    dataset: opts[:dataset] && OsCtl::Lib::Zfs::Dataset.new(
      opts[:dataset],
      base: opts[:dataset]
    ),
    ct_opts: {
      map_mode: opts[:map_mode] || 'native',
      devices: false, # skip device initialization, see below
      staged: true
    }
  )

  manipulate(ct) do
    builder = Container::Builder.new(ct.new_run_conf, cmd: self)

    # TODO: check for conflicting configuration
    #   - ip addresses, mac addresses

    if !builder.valid?
      error!(builder.errors.join('; '))

    elsif !builder.register
      error!("container #{pool.name}:#{ctid} already exists")
    end

    begin
      case opts[:missing_devices]
      when 'provide'
        ct.devices.ensure_all

      when 'remove'
        ct.devices.remove_missing

      else
        begin
          ct.devices.check_all_available!
        rescue DeviceNotAvailable, DeviceModeInsufficient => e
          error!(e.message)
        end
      end

      progress('Creating datasets')
      importer.create_datasets(
        builder,
        accept_existing: !opts[:dataset].nil?,
        properties: opts[:zfs_properties] || {}
      )

      builder.setup_lxc_home

      progress('Importing rootfs')
      importer.import_all_datasets(builder)

      builder.setup_ct_dir
      builder.setup_rootfs

      # Delayed initialization, when we have ensured all required devices
      # are present, or that missing devices were removed and rootfs is present,
      # so we can create device nodes
      ct.devices.init

      ct.save_config
      builder.setup_lxc_configs
      builder.setup_log_file
      builder.setup_user_hook_script_dir
      importer.install_user_hook_scripts(ct)
      builder.monitor

      if ct.netifs.any?
        progress('Reconfiguring LXC usernet')
        call_cmd(Commands::User::LxcUsernet)
      end

      ct.state = :complete

      ok
    rescue StandardError
      progress('Error occurred, cleaning up')
      builder.cleanup(dataset: !opts[:dataset])
      raise
    end
  end
end