Class: OsCtld::Commands::Container::CreateEmpty

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

#create_user(pool) ⇒ Object (protected)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/osctld/commands/container/create_empty.rb', line 80

def create_user(pool)
  name = opts[:id]

  user = DB::Users.find(name, pool)
  return user if user

  call_cmd!(
    Commands::User::Create,
    pool: pool.name,
    name:,
    standalone: false
  )

  DB::Users.find(name, pool) || (raise 'expected user')
end

#execute(builder) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/osctld/commands/container/create_empty.rb', line 55

def execute(builder)
  manipulate(builder.ctrc.ct) do
    error!('container already exists') unless builder.register

    begin
      builder.create_root_dataset(mapping: true, parents: true)
      builder.shift_dataset if opts[:dataset]
      builder.configure(opts[:distribution], opts[:version], opts[:arch])
      builder.setup_ct_dir
      builder.setup_lxc_home
      builder.setup_lxc_configs
      builder.setup_log_file
      builder.setup_user_hook_script_dir
      builder.monitor
      ok
    rescue StandardError
      progress('Error occurred, cleaning up')
      builder.cleanup(dataset: !opts[:dataset])
      raise
    end
  end
end

#findObject



7
8
9
10
11
12
13
14
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
# File 'lib/osctld/commands/container/create_empty.rb', line 7

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

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

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

  group = if opts[:group]
            DB::Groups.find(opts[:group], pool)
          else
            DB::Groups.default(pool)
          end

  if !opts[:distribution]
    error!('provide distribution')

  elsif !opts[:version]
    error!('provide distribution version')

  elsif !opts[:arch]
    error!('provide architecture')
  end

  builder = Container::Builder.create(
    pool,
    opts[:id],
    user,
    group,
    opts[:dataset] && OsCtl::Lib::Zfs::Dataset.new(
      opts[:dataset],
      base: opts[:dataset]
    ),
    cmd: self
  )

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

  builder
end