Class: OsCtld::Container::Builder

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

Constant Summary collapse

ID_RX =
/^[a-z0-9_-]{1,100}$/i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::SwitchUser

#ct_attach, #ct_syscmd

Constructor Details

#initialize(ctrc, opts = {}) ⇒ Builder

Returns a new instance of Builder.

Parameters:

Options Hash (opts):

  • :cmd (Command::Base)


33
34
35
36
37
38
# File 'lib/osctld/container/builder.rb', line 33

def initialize(ctrc, opts = {})
  @ctrc = ctrc
  @opts = opts
  @errors = []
  @ds_builder = Container::DatasetBuilder.new(cmd: opts[:cmd])
end

Instance Attribute Details

#ctrcObject (readonly)

Returns the value of attribute ctrc.



28
29
30
# File 'lib/osctld/container/builder.rb', line 28

def ctrc
  @ctrc
end

#ds_builderObject (readonly, protected)

Returns the value of attribute ds_builder.



243
244
245
# File 'lib/osctld/container/builder.rb', line 243

def ds_builder
  @ds_builder
end

#errorsObject (readonly)

Returns the value of attribute errors.



28
29
30
# File 'lib/osctld/container/builder.rb', line 28

def errors
  @errors
end

Class Method Details

.create(pool, id, user, group, dataset = nil, opts = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/osctld/container/builder.rb', line 13

def self.create(pool, id, user, group, dataset = nil, opts = {})
  ct = Container.new(
    pool,
    id,
    user,
    group,
    dataset || Container.default_dataset(pool, id),
    load: false,
  )

  ctrc = Container::RunConfiguration.new(ct)

  new(ctrc, opts)
end

Instance Method Details

#cleanup(opts = {}) ⇒ Object

Remove a partially created container when the building process failed

Parameters:

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :dataset (Boolean)

    destroy dataset or not



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/osctld/container/builder.rb', line 206

def cleanup(opts = {})
  Console.remove(ct)
  zfs(:destroy, '-r', ctrc.dataset, valid_rcs: [1]) if opts[:dataset]

  syscmd("rm -rf #{ctrc.lxc_dir} #{ctrc.ct.user_hook_script_dir}")
  File.unlink(ctrc.log_path) if File.exist?(ctrc.log_path)
  File.unlink(ctrc.config_path) if File.exist?(ctrc.config_path)

  DB::Containers.remove(ct)

  begin
    if ctrc.group.has_containers?(ctrc.user)
      CGroup.rmpath_all(ctrc.ct.base_cgroup_path)

    else
      CGroup.rmpath_all(ctrc.ct.group.full_cgroup_path(ctrc.user))
    end
  rescue SystemCallError
    # If some of the cgroups are busy, just leave them be
  end

  bashrc = File.join(ctrc.lxc_dir, '.bashrc')
  File.unlink(bashrc) if File.exist?(bashrc)

  grp_dir = ctrc.group.userdir(ctrc.user)

  if !ctrc.group.has_containers?(ctrc.user) && Dir.exist?(grp_dir)
    Dir.rmdir(grp_dir)
  end
end

#clear_snapshots(snaps) ⇒ Object



142
143
144
145
146
# File 'lib/osctld/container/builder.rb', line 142

def clear_snapshots(snaps)
  snaps.each do |snap|
    zfs(:destroy, nil, "#{ctrc.dataset}@#{snap}")
  end
end

#configure(distribution, version, arch) ⇒ Object



138
139
140
# File 'lib/osctld/container/builder.rb', line 138

def configure(distribution, version, arch)
  ctrc.ct.configure(distribution, version, arch)
end

#copy_datasets(src, dst, from: nil) ⇒ String

Returns snapshot name.

Parameters:

  • src (Array<OsCtl::Lib::Zfs::Dataset>)
  • dst (Array<OsCtl::Lib::Zfs::Dataset>)
  • from (String, nil) (defaults to: nil)

    base snapshot

Returns:

  • (String)

    snapshot name



90
91
92
# File 'lib/osctld/container/builder.rb', line 90

def copy_datasets(src, dst, from: nil)
  ds_builder.copy_datasets(src, dst, from: from)
end

#create_dataset(ds, opts = {}) ⇒ Object

Parameters:

  • ds (OsCtl::Lib::Zfs::Dataset)
  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :mapping (Boolean)
  • :parents (Boolean)


77
78
79
80
81
82
83
84
# File 'lib/osctld/container/builder.rb', line 77

def create_dataset(ds, opts = {})
  ds_builder.create_dataset(
    ds,
    parents: opts[:parents],
    uid_map: opts[:mapping] ? ctrc.uid_map : nil,
    gid_map: opts[:mapping] ? ctrc.gid_map : nil,
  )
end

#create_root_dataset(opts = {}) ⇒ Object



68
69
70
71
# File 'lib/osctld/container/builder.rb', line 68

def create_root_dataset(opts = {})
  progress('Creating root dataset')
  create_dataset(ctrc.dataset, opts)
end

#exist?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/osctld/container/builder.rb', line 64

def exist?
  DB::Containers.contains?(ctrc.id, ctrc.pool)
end

#from_local_archive(image, opts = {}) ⇒ Object

Parameters:

  • image (String)

    path

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :distribution (String)
  • :version (String)


98
99
100
101
102
103
104
105
106
107
108
# File 'lib/osctld/container/builder.rb', line 98

def from_local_archive(image, opts = {})
  ds_builder.from_local_archive(image, ctrc.rootfs, opts)

  distribution, version, arch = get_distribution_info(image)

  configure(
    opts[:distribution] || distribution,
    opts[:version] || version,
    opts[:arch] || arch
  )
end

#from_stream(ds = nil, &block) ⇒ Object



110
111
112
# File 'lib/osctld/container/builder.rb', line 110

def from_stream(ds = nil, &block)
  ds_builder.from_stream(ds || ctrc.dataset, &block)
end

#get_distribution_info(image) ⇒ Object



237
238
239
240
# File 'lib/osctld/container/builder.rb', line 237

def get_distribution_info(image)
  distribution, version, arch, *_ = File.basename(image).split('-')
  [distribution, version, arch]
end

#groupObject



48
49
50
# File 'lib/osctld/container/builder.rb', line 48

def group
  ctrc.group
end

#monitorObject



198
199
200
# File 'lib/osctld/container/builder.rb', line 198

def monitor
  Monitor::Master.monitor(ctrc.ct)
end

#poolObject



40
41
42
# File 'lib/osctld/container/builder.rb', line 40

def pool
  ctrc.pool
end

#progress(msg) ⇒ Object (protected)



245
246
247
248
# File 'lib/osctld/container/builder.rb', line 245

def progress(msg)
  return unless @opts[:cmd]
  @opts[:cmd].send(:progress, msg)
end

#registerObject



187
188
189
190
191
192
193
194
195
196
# File 'lib/osctld/container/builder.rb', line 187

def register
  DB::Containers.sync do
    if DB::Containers.contains?(ctrc.id, ctrc.pool)
      false
    else
      DB::Containers.add(ctrc.ct)
      true
    end
  end
end

#setup_ct_dirObject



122
123
124
125
126
# File 'lib/osctld/container/builder.rb', line 122

def setup_ct_dir
  # Chown to 0:0, zfs will shift it using the mapping
  File.chown(0, 0, ctrc.dir)
  File.chmod(0770, ctrc.dir)
end

#setup_log_fileObject



173
174
175
176
177
178
# File 'lib/osctld/container/builder.rb', line 173

def setup_log_file
  progress('Preparing log file')
  File.open(ctrc.log_path, 'w').close
  File.chmod(0660, ctrc.log_path)
  File.chown(0, ctrc.user.ugid, ctrc.log_path)
end

#setup_lxc_configsObject



168
169
170
171
# File 'lib/osctld/container/builder.rb', line 168

def setup_lxc_configs
  progress('Generating LXC configuration')
  ctrc.ct.lxc_config.configure
end

#setup_lxc_homeObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/osctld/container/builder.rb', line 148

def setup_lxc_home
  progress('Configuring LXC home')

  unless ctrc.group.setup_for?(ctrc.user)
    dir = ctrc.group.userdir(ctrc.user)

    FileUtils.mkdir_p(dir, mode: 0751)
    File.chown(0, ctrc.user.ugid, dir)
  end

  if Dir.exist?(ctrc.lxc_dir)
    File.chmod(0750, ctrc.lxc_dir)
  else
    Dir.mkdir(ctrc.lxc_dir, 0750)
  end
  File.chown(0, ctrc.user.ugid, ctrc.lxc_dir)

  ctrc.ct.configure_bashrc
end

#setup_rootfsObject



128
129
130
131
132
133
134
135
136
# File 'lib/osctld/container/builder.rb', line 128

def setup_rootfs
  if Dir.exist?(ctrc.rootfs)
    File.chmod(0755, ctrc.rootfs)
  else
    Dir.mkdir(ctrc.rootfs, 0755)
  end

  File.chown(0, 0, ctrc.rootfs)
end

#setup_user_hook_script_dirObject



180
181
182
183
184
185
# File 'lib/osctld/container/builder.rb', line 180

def setup_user_hook_script_dir
  return if Dir.exist?(ctrc.ct.user_hook_script_dir)

  progress('Preparing user script hook dir')
  Dir.mkdir(ctrc.ct.user_hook_script_dir, 0700)
end

#shift_datasetObject



114
115
116
117
118
119
120
# File 'lib/osctld/container/builder.rb', line 114

def shift_dataset
  ds_builder.shift_dataset(
    ctrc.dataset,
    uid_map: ctrc.uid_map,
    gid_map: ctrc.gid_map,
  )
end

#userObject



44
45
46
# File 'lib/osctld/container/builder.rb', line 44

def user
  ctrc.user
end

#valid?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/osctld/container/builder.rb', line 52

def valid?
  if ID_RX !~ ctrc.id
    errors << "invalid ID, allowed characters: #{ID_RX.source}"
  end

  if !ctrc.dataset.on_pool?(ctrc.pool.name)
    errors << "dataset #{ctrc.dataset} does not belong to pool #{ctrc.pool.name}"
  end

  errors.empty?
end