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(ct, opts = {}) ⇒ Builder

Returns a new instance of Builder

Parameters:

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

Options Hash (opts):

  • :cmd (Command::Base)


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

def initialize(ct, opts = {})
  @ct = ct
  @opts = opts
  @errors = []
end

Instance Attribute Details

#ctObject (readonly)

Returns the value of attribute ct



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

def ct
  @ct
end

#errorsObject (readonly)

Returns the value of attribute errors



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

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
# File 'lib/osctld/container/builder.rb', line 13

def self.create(pool, id, user, group, dataset = nil, opts = {})
  new(
    Container.new(
      pool,
      id,
      user,
      group,
      dataset || Container.default_dataset(pool, id),
      load: false
    ),
    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



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/osctld/container/builder.rb', line 252

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

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

  DB::Containers.remove(ct)

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

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

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

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

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

#clear_snapshots(snaps) ⇒ Object



188
189
190
191
192
# File 'lib/osctld/container/builder.rb', line 188

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

#configure(distribution, version, arch) ⇒ Object



184
185
186
# File 'lib/osctld/container/builder.rb', line 184

def configure(distribution, version, arch)
  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)

    base snapshot

Returns:

  • (String)

    snapshot name



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/osctld/container/builder.rb', line 93

def copy_datasets(src, dst, from: nil)
  snap = "osctl-copy-#{from ? 'incr' : 'base'}-#{Time.now.to_i}"
  zfs(:snapshot, nil, src.map { |ds| "#{ds}@#{snap}" }.join(' '))

  zipped = src.zip(dst)

  zipped.each do |src_ds, dst_ds|
    progress("Copying dataset #{src_ds.relative_name}")
    syscmd("zfs send -c #{from ? "-i @#{from}" : ''} #{src_ds}@#{snap} "+
           "| zfs recv -F #{dst_ds}")
  end

  snap
end

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

Parameters:

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

    options

Options Hash (opts):

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


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

def create_dataset(ds, opts = {})
  zfs_opts = {properties: {
    canmount: 'noauto',
  }}
  zfs_opts[:parents] = true if opts[:parents]
  zfs_opts[:properties].update({
    uidmap: ct.uid_map.map(&:to_s).join(','),
    gidmap: ct.gid_map.map(&:to_s).join(','),
  }) if opts[:mapping]

  ds.create!(zfs_opts)
  ds.mount(recursive: true)
end

#create_root_dataset(opts = {}) ⇒ Object



66
67
68
69
# File 'lib/osctld/container/builder.rb', line 66

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

#exist?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/osctld/container/builder.rb', line 62

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

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

Parameters:

  • image (String)

    path

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

    options

Options Hash (opts):

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


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

def from_local_archive(image, opts = {})
  progress('Extracting image')
  syscmd("tar -xzf #{image} -C #{ct.rootfs}")

  shift_dataset

  distribution, version, arch = get_distribution_info(image)

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

#from_stream(ds = nil) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/osctld/container/builder.rb', line 143

def from_stream(ds = nil)
  progress('Writing image stream')

  IO.popen("exec zfs recv -F #{ds || ct.dataset}", 'r+') do |io|
    yield(io)
  end

  if $?.exitstatus != 0
    fail "zfs recv failed with exit status #{$?.exitstatus}"
  end
end

#get_distribution_info(image) ⇒ Object



283
284
285
286
# File 'lib/osctld/container/builder.rb', line 283

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

#groupObject



46
47
48
# File 'lib/osctld/container/builder.rb', line 46

def group
  ct.group
end

#monitorObject



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

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

#poolObject



38
39
40
# File 'lib/osctld/container/builder.rb', line 38

def pool
  ct.pool
end

#progress(msg) ⇒ Object (protected)



289
290
291
292
# File 'lib/osctld/container/builder.rb', line 289

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

#registerObject



233
234
235
236
237
238
239
240
241
242
# File 'lib/osctld/container/builder.rb', line 233

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

#setup_ct_dirObject



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

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

#setup_log_fileObject



219
220
221
222
223
224
# File 'lib/osctld/container/builder.rb', line 219

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

#setup_lxc_configsObject



214
215
216
217
# File 'lib/osctld/container/builder.rb', line 214

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

#setup_lxc_homeObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/osctld/container/builder.rb', line 194

def setup_lxc_home
  progress('Configuring LXC home')

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

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

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

  ct.configure_bashrc
end

#setup_rootfsObject



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

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

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

#setup_user_hook_script_dirObject



226
227
228
229
230
231
# File 'lib/osctld/container/builder.rb', line 226

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

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

#shift_datasetObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/osctld/container/builder.rb', line 155

def shift_dataset
  progress('Configuring UID/GID mapping')

  zfs(:unmount, nil, ct.dataset)
  zfs(
    :set,
    "uidmap=\"#{ct.uid_map.map(&:to_s).join(',')}\" "+
    "gidmap=\"#{ct.gid_map.map(&:to_s).join(',')}\"",
    ct.dataset
  )

  5.times do |i|
    zfs(:mount, nil, ct.dataset)

    f = Tempfile.create(['.ugid-map-test'], ct.dir)
    f.close

    st = File.stat(f.path)
    File.unlink(f.path)

    return if st.uid == ct.root_host_uid && st.gid == ct.root_host_gid

    zfs(:unmount, nil, ct.dataset)
    sleep(1 + i)
  end

  fail 'unable to configure UID/GID mapping'
end

#userObject



42
43
44
# File 'lib/osctld/container/builder.rb', line 42

def user
  ct.user
end

#valid?Boolean

Returns:

  • (Boolean)


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

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

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

  errors.empty?
end