Class: OsCtld::Container::RunConfiguration

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::File, OsCtl::Lib::Utils::Log, Lockable
Defined in:
lib/osctld/container/run_configuration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lockable

#exclusively, included, #inclusively, #init_lock, #lock, #unlock

Constructor Details

#initialize(ct, load_conf: true) ⇒ RunConfiguration

Returns a new instance of RunConfiguration.

Parameters:



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/osctld/container/run_configuration.rb', line 30

def initialize(ct, load_conf: true)
  init_lock
  @ct = ct
  @cpu_package = nil
  @lxcfs_worker = nil
  @init_pid = nil
  @aborted = false
  @do_reboot = false
  @dist_network_configured = false
  self.load_conf(from_file: load_conf)
end

Instance Attribute Details

#archObject (readonly)

Returns the value of attribute arch



25
26
27
# File 'lib/osctld/container/run_configuration.rb', line 25

def arch
  @arch
end

#cpu_packageObject

Returns the value of attribute cpu_package



26
27
28
# File 'lib/osctld/container/run_configuration.rb', line 26

def cpu_package
  @cpu_package
end

#ctContainer (readonly)

Returns:



23
24
25
# File 'lib/osctld/container/run_configuration.rb', line 23

def ct
  @ct
end

#datasetObject (readonly)

Returns the value of attribute dataset



25
26
27
# File 'lib/osctld/container/run_configuration.rb', line 25

def dataset
  @dataset
end

#dist_network_configuredObject

Returns the value of attribute dist_network_configured



26
27
28
# File 'lib/osctld/container/run_configuration.rb', line 26

def dist_network_configured
  @dist_network_configured
end

#distributionObject (readonly)

Returns the value of attribute distribution



25
26
27
# File 'lib/osctld/container/run_configuration.rb', line 25

def distribution
  @distribution
end

#init_pidObject

Returns the value of attribute init_pid



26
27
28
# File 'lib/osctld/container/run_configuration.rb', line 26

def init_pid
  @init_pid
end

#lxcfs_workerObject

Returns the value of attribute lxcfs_worker



26
27
28
# File 'lib/osctld/container/run_configuration.rb', line 26

def lxcfs_worker
  @lxcfs_worker
end

#mountedObject (protected)

Returns the value of attribute mounted



213
214
215
# File 'lib/osctld/container/run_configuration.rb', line 213

def mounted
  @mounted
end

#versionObject (readonly)

Returns the value of attribute version



25
26
27
# File 'lib/osctld/container/run_configuration.rb', line 25

def version
  @version
end

Class Method Details

.load(ct) ⇒ Object

Parameters:



11
12
13
14
15
16
17
18
19
20
# File 'lib/osctld/container/run_configuration.rb', line 11

def self.load(ct)
  ctrc = new(ct, load_conf: false)

  if ctrc.exist?
    ctrc.load_conf
    ctrc
  else
    nil
  end
end

Instance Method Details

#aborted=(v) ⇒ Object



121
122
123
# File 'lib/osctld/container/run_configuration.rb', line 121

def aborted=(v)
  @aborted = v
end

#aborted?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/osctld/container/run_configuration.rb', line 125

def aborted?
  @aborted
end

#assets(add) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/osctld/container/run_configuration.rb', line 42

def assets(add)
  add.file(
    file_path,
    desc: 'Container runtime configuration',
    user: 0,
    group: 0,
    mode: 0400,
    optional: true,
  )
end

#boot_from(dataset, distribution, version, arch, destroy_dataset_on_stop: false) ⇒ Object

Set custom boot dataset



63
64
65
66
67
68
69
70
71
# File 'lib/osctld/container/run_configuration.rb', line 63

def boot_from(dataset, distribution, version, arch, destroy_dataset_on_stop: false)
  exclusively do
    @dataset = dataset
    @distribution = distribution
    @version = version
    @arch = arch
    @destroy_dataset_on_stop = destroy_dataset_on_stop
  end
end

#destroyObject



207
208
209
210
# File 'lib/osctld/container/run_configuration.rb', line 207

def destroy
  File.unlink(file_path)
rescue Errno::ENOENT
end

#destroy_dataset_on_stop?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/osctld/container/run_configuration.rb', line 73

def destroy_dataset_on_stop?
  inclusively { @destroy_dataset_on_stop }
end

#dirString

Countainer dataset mountpoint

Returns:

  • (String)


79
80
81
# File 'lib/osctld/container/run_configuration.rb', line 79

def dir
  dataset.mountpoint
end

#dir_pathObject (protected)



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

def dir_path
  File.join(ct.pool.ct_dir, ct.id)
end

#dist_configure_network?Boolean

Returns:

  • (Boolean)


138
139
140
141
142
# File 'lib/osctld/container/run_configuration.rb', line 138

def dist_configure_network?
  inclusively do
    !dist_network_configured && can_dist_configure_network?
  end
end

#dumpObject



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/osctld/container/run_configuration.rb', line 148

def dump
  {
    'dataset' => dataset.to_s,
    'distribution' => distribution,
    'version' => version,
    'arch' => arch,
    'cpu_package' => cpu_package,
    'lxcfs_worker' => lxcfs_worker && lxcfs_worker.name,
    'destroy_dataset_on_stop' => destroy_dataset_on_stop?,
  }
end

#exist?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/osctld/container/run_configuration.rb', line 144

def exist?
  File.exist?(file_path)
end

#file_pathObject (protected)



219
220
221
# File 'lib/osctld/container/run_configuration.rb', line 219

def file_path
  File.join(dir_path, 'config.yml')
end

#load_conf(from_file: true) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/osctld/container/run_configuration.rb', line 160

def load_conf(from_file: true)
  cfg =
    if from_file && File.exist?(file_path)
      OsCtl::Lib::ConfigFile.load_yaml_file(file_path)
    else
      {}
    end

  @dataset =
    if cfg['dataset']
      OsCtl::Lib::Zfs::Dataset.new(cfg['dataset'], base: cfg['dataset'])
    else
      ct.dataset
    end
  @distribution = cfg['distribution'] || ct.distribution
  @version = cfg['version'] || ct.version
  @arch = cfg['arch'] || ct.arch
  @cpu_package = cfg['cpu_package']
  @lxcfs_worker =
    if cfg['lxcfs_worker']
      Lxcfs::Scheduler.worker_by_name(cfg['lxcfs_worker'])
    elsif ct.lxcfs.enable \
          && File.exist?(File.join(Lxcfs::Server::RUNDIR_RUNSVDIR, "ct.#{ident}", 'run'))
      # Handle legacy per-ct lxcfs instances
      # TODO: remove this when no more per-ct lxcfs instances are running
      Lxcfs::Scheduler.add_legacy_perct_worker(self)
    end
  @destroy_dataset_on_stop =
    if cfg.has_key?('destroy_dataset_on_stop')
      cfg['destroy_dataset_on_stop']
    else
      false
    end
  nil
end

#mount(force: false) ⇒ Object

Mount the container's dataset

Parameters:

  • force (Boolean) (defaults to: false)

    ensure the datasets are mounted even if osctld already mounted them



97
98
99
100
101
# File 'lib/osctld/container/run_configuration.rb', line 97

def mount(force: false)
  return if !force && mounted
  dataset.mount(recursive: true)
  self.mounted = true
end

#mounted?(force: false) ⇒ Boolean

Check if the container's dataset is mounted

Parameters:

  • force (Boolean) (defaults to: false)

    check if the dataset is mounted even if osctld already mounted it

Returns:

  • (Boolean)


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

def mounted?(force: false)
  if force || mounted.nil?
    self.mounted = dataset.mounted?(recursive: true)
  else
    mounted
  end
end

#reboot?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/osctld/container/run_configuration.rb', line 134

def reboot?
  @do_reboot
end

#request_rebootObject

After the current container run stops, start it again



130
131
132
# File 'lib/osctld/container/run_configuration.rb', line 130

def request_reboot
  @do_reboot = true
end

#rootfsString

Container rootfs path

Returns:

  • (String)


85
86
87
88
89
90
91
92
# File 'lib/osctld/container/run_configuration.rb', line 85

def rootfs
  File.join(dir, 'private')

rescue SystemCommandFailed
  # Dataset for staged containers does not have to exist yet, relevant
  # primarily for ct show/list
  nil
end

#runtime_rootfsObject



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

def runtime_rootfs
  pid = init_pid
  fail 'init_pid not set' unless pid

  File.join('/proc', pid.to_s, 'root')
end

#saveObject



196
197
198
199
200
201
202
203
204
205
# File 'lib/osctld/container/run_configuration.rb', line 196

def save
  begin
    Dir.mkdir(dir_path)
  rescue Errno::EEXIST
  end

  regenerate_file(file_path, 0400) do |new|
    new.write(OsCtl::Lib::ConfigFile.dump_yaml(dump))
  end
end