Class: OsCtld::Commands::Container::Create

Inherits:
Logged
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, Utils::Container
Defined in:
lib/osctld/commands/container/create.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

#custom_dataset(builder) ⇒ Object (protected)



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

def custom_dataset(builder)
  builder.create_root_dataset(mapping: false, parents: true)

  if opts[:no_template]
    # the rootfs is already there
    builder.shift_dataset
    builder.configure(opts[:distribution], opts[:version], opts[:arch])
    return
  end

  case opts[:template][:type].to_sym
  when :remote
    from_remote_template(builder, opts[:template])

  when :archive
    builder.setup_rootfs
    builder.from_local_archive(
      opts[:template][:path],
      distribution: opts[:distribution],
      version: opts[:version]
    )

  when :stream
    from_stream(builder)

  else
    fail "unsupported template type #{opts[:template][:type]}"
  end
end

#execute(builder) ⇒ Object



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

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

    begin
      if opts[:dataset]
        custom_dataset(builder)

      elsif opts[:template]
        from_local_template(builder)

      else
        fail 'should not be possible'
      end

      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
      progress('Error occurred, cleaning up')
      builder.cleanup(dataset: !opts[:dataset])
      raise
    end
  end
end

#findObject



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
54
55
# File 'lib/osctld/commands/container/create.rb', line 11

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

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

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

  else
    group = DB::Groups.default(pool)
  end

  error!('group not found') unless group

  if opts[:no_template] \
      || (opts[:template] && opts[:template][:type] == :stream && opts[:template][:path].nil?)
    if !opts[:distribution]
      error!('provide distribution')

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

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

  elsif !opts[:dataset] && !opts[:template]
    error!('provide template archive, stream or existing dataset')
  end

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

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

  builder
end

#from_local_template(builder) ⇒ Object (protected)



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

def from_local_template(builder)
  if opts[:no_template]
    builder.create_root_dataset(mapping: true)
    builder.setup_rootfs
    builder.configure(opts[:distribution], opts[:version], opts[:arch])
    return
  end

  case opts[:template][:type].to_sym
  when :remote
    builder.create_root_dataset(mapping: false)
    from_remote_template(builder, opts[:template])

  when :archive
    builder.create_root_dataset(mapping: false)
    builder.setup_rootfs
    builder.from_local_archive(opts[:template][:path])

  when :stream
    builder.create_root_dataset(mapping: false)
    from_stream(builder)

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