Class: OsCtl::Image::Operations::Image::Build
- Includes:
- Lib::Utils::Log, Lib::Utils::System
- Defined in:
- lib/osctl/image/operations/image/build.rb
Instance Attribute Summary collapse
- #base_dir ⇒ String readonly
-
#build_dataset ⇒ Object
readonly
protected
Returns the value of attribute build_dataset.
- #build_id ⇒ String readonly
- #builder ⇒ Builder readonly
-
#client ⇒ Object
readonly
protected
Returns the value of attribute client.
- #config_file ⇒ String readonly
- #image ⇒ Image readonly
- #install_dir ⇒ String readonly
-
#opts ⇒ Object
readonly
protected
Returns the value of attribute opts.
- #output_dataset ⇒ String readonly
- #output_dir ⇒ String readonly
- #output_stream ⇒ String readonly
- #output_tar ⇒ String? readonly
- #vpsadminos_dir ⇒ String? readonly
- #work_dataset ⇒ String readonly
-
#work_dir ⇒ Object
readonly
protected
Returns the value of attribute work_dir.
Instance Method Summary collapse
- #build ⇒ Object protected
- #build_environment ⇒ Object protected
- #builder_base_dir ⇒ Object protected
- #builder_install_dir ⇒ Object protected
- #builder_vpsadminos_dir ⇒ Object protected
- #builder_work_dir ⇒ Object protected
- #cached? ⇒ Boolean
- #cleanup ⇒ Object protected
- #cleanup_vpsadminos_dir ⇒ Object protected
- #effective_vendor ⇒ Object
- #execute ⇒ Operations::Image::Build
- #has_config_file? ⇒ Boolean
- #image_attrs ⇒ Object
-
#initialize(base_dir, image, opts) ⇒ Build
constructor
A new instance of Build.
- #log_type ⇒ Object
- #prepare_vpsadminos_dir ⇒ Object protected
- #read_config_file ⇒ Object
- #vpsadminos_revision ⇒ Object protected
- #write_vpsadminos_revision(dir) ⇒ Object protected
Methods inherited from Base
Constructor Details
#initialize(base_dir, image, opts) ⇒ Build
Returns a new instance of Build.
56 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/osctl/image/operations/image/build.rb', line 56 def initialize(base_dir, image, opts) super() @base_dir = base_dir @opts = opts @image = image image.load_config @builder = Builder.new(base_dir, image.builder) builder.load_config @build_id = SecureRandom.hex(4) @build_dataset = File.join(opts[:build_dataset], build_id) @output_dataset = File.join(build_dataset, 'output') @work_dataset = File.join(build_dataset, 'work') @output_dir = opts[:output_dir] @vpsadminos_dir = opts[:vpsadminos_dir] name = [ image.distribution, image.version, image.arch, opts[:vendor] || image.vendor, image.variant ].join('-') @output_tar = File.join(output_dir, "#{name}-archive.tar") if image.datasets.empty? @output_stream = File.join(output_dir, "#{name}-stream.tar") @client = OsCtldClient.new end |
Instance Attribute Details
#base_dir ⇒ String (readonly)
14 15 16 |
# File 'lib/osctl/image/operations/image/build.rb', line 14 def base_dir @base_dir end |
#build_dataset ⇒ Object (readonly, protected)
Returns the value of attribute build_dataset.
131 132 133 |
# File 'lib/osctl/image/operations/image/build.rb', line 131 def build_dataset @build_dataset end |
#build_id ⇒ String (readonly)
38 39 40 |
# File 'lib/osctl/image/operations/image/build.rb', line 38 def build_id @build_id end |
#builder ⇒ Builder (readonly)
20 21 22 |
# File 'lib/osctl/image/operations/image/build.rb', line 20 def builder @builder end |
#client ⇒ Object (readonly, protected)
Returns the value of attribute client.
131 132 133 |
# File 'lib/osctl/image/operations/image/build.rb', line 131 def client @client end |
#config_file ⇒ String (readonly)
35 36 37 |
# File 'lib/osctl/image/operations/image/build.rb', line 35 def config_file @config_file end |
#image ⇒ Image (readonly)
17 18 19 |
# File 'lib/osctl/image/operations/image/build.rb', line 17 def image @image end |
#install_dir ⇒ String (readonly)
32 33 34 |
# File 'lib/osctl/image/operations/image/build.rb', line 32 def install_dir @install_dir end |
#opts ⇒ Object (readonly, protected)
Returns the value of attribute opts.
131 132 133 |
# File 'lib/osctl/image/operations/image/build.rb', line 131 def opts @opts end |
#output_dataset ⇒ String (readonly)
23 24 25 |
# File 'lib/osctl/image/operations/image/build.rb', line 23 def output_dataset @output_dataset end |
#output_dir ⇒ String (readonly)
29 30 31 |
# File 'lib/osctl/image/operations/image/build.rb', line 29 def output_dir @output_dir end |
#output_stream ⇒ String (readonly)
44 45 46 |
# File 'lib/osctl/image/operations/image/build.rb', line 44 def output_stream @output_stream end |
#output_tar ⇒ String? (readonly)
41 42 43 |
# File 'lib/osctl/image/operations/image/build.rb', line 41 def output_tar @output_tar end |
#vpsadminos_dir ⇒ String? (readonly)
47 48 49 |
# File 'lib/osctl/image/operations/image/build.rb', line 47 def vpsadminos_dir @vpsadminos_dir end |
#work_dataset ⇒ String (readonly)
26 27 28 |
# File 'lib/osctl/image/operations/image/build.rb', line 26 def work_dataset @work_dataset end |
#work_dir ⇒ Object (readonly, protected)
Returns the value of attribute work_dir.
131 132 133 |
# File 'lib/osctl/image/operations/image/build.rb', line 131 def work_dir @work_dir end |
Instance Method Details
#build ⇒ Object (protected)
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/osctl/image/operations/image/build.rb', line 133 def build prepared_vpsadminos_dir = prepare_vpsadminos_dir Operations::Builder::UseOrCreate.run( builder, base_dir, vpsadminos_dir: prepared_vpsadminos_dir ) root_uid, root_gid = Operations::Builder::GetRootUgid.run(builder) zfs(:create, '-p', work_dataset) zfs(:create, '-p', output_dataset) image.datasets.each_key do |dataset| zfs(:create, '-p', File.join(output_dataset, dataset)) end @work_dir = zfs(:get, '-H -o value mountpoint', work_dataset).output.strip @output_dir = zfs(:get, '-H -o value mountpoint', output_dataset).output.strip @install_dir = File.join(output_dir, 'private') @config_file = File.join(install_dir, 'container.yml') Dir.mkdir(install_dir) image.datasets.each_key do |dataset| Dir.mkdir(File.join(output_dir, dataset, 'private')) end client.batch do # Directory with image-scripts is by default a part of the OS, i.e. usually # stored on squashfs, which does not support ID mapping. Read-only access # is enough for the build. client.bind_mount(builder.ctid, base_dir, builder_base_dir, map_ids: false) client.bind_mount(builder.ctid, work_dir, builder_work_dir) client.bind_mount(builder.ctid, install_dir, builder_install_dir) if prepared_vpsadminos_dir client.bind_mount( builder.ctid, prepared_vpsadminos_dir, builder_vpsadminos_dir, map_ids: false ) end client.activate_mount(builder.ctid, builder_base_dir) client.activate_mount(builder.ctid, builder_work_dir) client.activate_mount(builder.ctid, builder_install_dir) client.activate_mount(builder.ctid, builder_vpsadminos_dir) if prepared_vpsadminos_dir image.datasets.sort { |a, b| a[0] <=> b[0] }.each do |dataset, mountpoint| install_mountpoint = File.join(builder_install_dir, mountpoint) client.bind_mount( builder.ctid, File.join(output_dir, dataset, 'private'), install_mountpoint ) client.activate_mount(builder.ctid, install_mountpoint) end end rc = Operations::Builder::ControlledExec.run( builder, [ File.join(builder_base_dir, 'bin', 'runner'), 'image', 'build', build_id, builder_work_dir, builder_install_dir, image.name ], id: build_id, client:, env: build_environment ) if rc != 0 raise OperationError, "build of #{image.name} on #{builder.name} failed with " \ "exit status #{rc}" end sys = OsCtl::Lib::Sys.new sys.syncfs(install_dir) # Remount just in case to write-out dirtied pages zfs(:unmount, nil, output_dataset) zfs(:mount, nil, output_dataset) Operations::Image::FixFileCapabilities.run(image, install_dir) Operations::Image::Export.run(self) end |
#build_environment ⇒ Object (protected)
287 288 289 290 291 292 293 |
# File 'lib/osctl/image/operations/image/build.rb', line 287 def build_environment return {} unless vpsadminos_dir ret = { 'OSCTL_IMAGE_VPSADMINOS_DIR' => builder_vpsadminos_dir } ret['OSCTL_IMAGE_VPSADMINOS_REV'] = vpsadminos_revision if vpsadminos_revision ret end |
#builder_base_dir ⇒ Object (protected)
271 272 273 |
# File 'lib/osctl/image/operations/image/build.rb', line 271 def builder_base_dir "/build/basedir.#{build_id}" end |
#builder_install_dir ⇒ Object (protected)
279 280 281 |
# File 'lib/osctl/image/operations/image/build.rb', line 279 def builder_install_dir "/build/installdir.#{build_id}" end |
#builder_vpsadminos_dir ⇒ Object (protected)
283 284 285 |
# File 'lib/osctl/image/operations/image/build.rb', line 283 def builder_vpsadminos_dir "/build/vpsadminos.#{build_id}" end |
#builder_work_dir ⇒ Object (protected)
275 276 277 |
# File 'lib/osctl/image/operations/image/build.rb', line 275 def builder_work_dir "/build/workdir.#{build_id}" end |
#cached? ⇒ Boolean
107 108 109 |
# File 'lib/osctl/image/operations/image/build.rb', line 107 def cached? (output_tar && File.exist?(output_tar)) || File.exist?(output_stream) end |
#cleanup ⇒ Object (protected)
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/osctl/image/operations/image/build.rb', line 230 def cleanup client.batch do client.ignore_error { client.unmount(builder.ctid, builder_work_dir) } client.ignore_error { client.unmount(builder.ctid, builder_vpsadminos_dir) } if vpsadminos_dir image.datasets.sort { |a, b| b[0] <=> a[0] }.each do |_, mountpoint| install_mountpoint = File.join(builder_install_dir, mountpoint) client.ignore_error { client.unmount(builder.ctid, install_mountpoint) } end client.ignore_error { client.unmount(builder.ctid, builder_install_dir) } client.ignore_error { client.unmount(builder.ctid, builder_base_dir) } end if builder.attrs dirs = [builder_base_dir, builder_work_dir, builder_install_dir] dirs << builder_vpsadminos_dir if vpsadminos_dir dirs.each do |dir| Dir.rmdir(File.join(builder.attrs[:rootfs], dir)) rescue Errno::ENOENT # ignore end end zfs(:destroy, nil, work_dataset, valid_rcs: :all) list = zfs(:list, '-H -o name -t snapshot', output_dataset, valid_rcs: :all) if list.success? list.output.split("\n").each do |s| zfs(:destroy, nil, s.strip) end end zfs(:destroy, '-r', output_dataset, valid_rcs: :all) zfs(:destroy, nil, build_dataset, valid_rcs: :all) ensure cleanup_vpsadminos_dir end |
#cleanup_vpsadminos_dir ⇒ Object (protected)
337 338 339 340 341 342 |
# File 'lib/osctl/image/operations/image/build.rb', line 337 def cleanup_vpsadminos_dir return unless @prepared_vpsadminos_dir FileUtils.rm_rf(@prepared_vpsadminos_dir) @prepared_vpsadminos_dir = nil end |
#effective_vendor ⇒ Object
111 112 113 |
# File 'lib/osctl/image/operations/image/build.rb', line 111 def effective_vendor @effective_vendor ||= @opts[:vendor] || image.vendor end |
#execute ⇒ Operations::Image::Build
89 90 91 92 93 94 95 |
# File 'lib/osctl/image/operations/image/build.rb', line 89 def execute log(:info, "Using builder #{builder.name}") build self ensure cleanup end |
#has_config_file? ⇒ Boolean
97 98 99 |
# File 'lib/osctl/image/operations/image/build.rb', line 97 def has_config_file? File.exist?(config_file) end |
#image_attrs ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/osctl/image/operations/image/build.rb', line 115 def image_attrs @image_attrs ||= { distribution: image.distribution, version: image.version, arch: image.arch, vendor: effective_vendor, variant: image.variant } end |
#log_type ⇒ Object
125 126 127 |
# File 'lib/osctl/image/operations/image/build.rb', line 125 def log_type "build #{image.name}@#{builder.name}" end |
#prepare_vpsadminos_dir ⇒ Object (protected)
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/osctl/image/operations/image/build.rb', line 295 def prepare_vpsadminos_dir return unless vpsadminos_dir return @prepared_vpsadminos_dir if @prepared_vpsadminos_dir dir = Dir.mktmpdir('osctl-image-vpsadminos.') FileUtils.cp_r(File.join(vpsadminos_dir, '.'), dir, preserve: true) write_vpsadminos_revision(dir) FileUtils.rm_rf([File.join(dir, '.git'), File.join(dir, 'result')]) FileUtils.chmod_R('u+rwX,go+rX', dir) @prepared_vpsadminos_dir = dir rescue StandardError FileUtils.rm_rf(dir) if dir raise end |
#read_config_file ⇒ Object
101 102 103 104 105 |
# File 'lib/osctl/image/operations/image/build.rb', line 101 def read_config_file ret = OsCtl::Lib::ConfigFile.load_yaml_file(config_file) File.unlink(config_file) ret end |
#vpsadminos_revision ⇒ Object (protected)
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/osctl/image/operations/image/build.rb', line 317 def vpsadminos_revision return @vpsadminos_revision if defined?(@vpsadminos_revision) if ENV['OSCTL_IMAGE_VPSADMINOS_REV'] && !ENV['OSCTL_IMAGE_VPSADMINOS_REV'].empty? return @vpsadminos_revision = ENV['OSCTL_IMAGE_VPSADMINOS_REV'] end output, status = Open3.capture2e( 'git', '-C', vpsadminos_dir, 'rev-parse', '--verify', 'HEAD' ) @vpsadminos_revision = status.success? ? output.strip : nil rescue Errno::ENOENT @vpsadminos_revision = nil end |