Class: OsCtl::Image::Operations::Builder::ControlledRunscript
- Inherits:
-
OsCtl::Image::Operations::Base
- Object
- OsCtl::Image::Operations::Base
- OsCtl::Image::Operations::Builder::ControlledRunscript
- Includes:
- Lib::Utils::Log, Lib::Utils::System
- Defined in:
- lib/osctl/image/operations/builder/controlled_runscript.rb
Instance Attribute Summary collapse
- #builder ⇒ Builder readonly
-
#client ⇒ Object
readonly
protected
Returns the value of attribute client.
- #file ⇒ String readonly
-
#id ⇒ Object
readonly
protected
Returns the value of attribute id.
-
#name ⇒ Object
readonly
protected
Returns the value of attribute name.
- #script ⇒ String readonly
Instance Method Summary collapse
-
#create_file ⇒ File, String
protected
File, path relative from builder’s rootfs.
-
#execute ⇒ Integer
Exit status.
-
#initialize(builder, file: nil, script: nil, id: nil, name: nil, client: nil) ⇒ ControlledRunscript
constructor
A new instance of ControlledRunscript.
- #unlink(path) ⇒ Object protected
Methods inherited from OsCtl::Image::Operations::Base
Constructor Details
#initialize(builder, file: nil, script: nil, id: nil, name: nil, client: nil) ⇒ ControlledRunscript
Returns a new instance of ControlledRunscript.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/osctl/image/operations/builder/controlled_runscript.rb', line 25 def initialize(builder, file: nil, script: nil, id: nil, name: nil, client: nil) if (file && script) || (!file && !script) raise ArgumentError, 'provide file or script' end super() @builder = builder @file = file @script = script @id = id @name = name || '.osctl-image-runscript' @client = client || OsCtldClient.new end |
Instance Attribute Details
#builder ⇒ Builder (readonly)
11 12 13 |
# File 'lib/osctl/image/operations/builder/controlled_runscript.rb', line 11 def builder @builder end |
#client ⇒ Object (readonly, protected)
Returns the value of attribute client.
63 64 65 |
# File 'lib/osctl/image/operations/builder/controlled_runscript.rb', line 63 def client @client end |
#file ⇒ String (readonly)
14 15 16 |
# File 'lib/osctl/image/operations/builder/controlled_runscript.rb', line 14 def file @file end |
#id ⇒ Object (readonly, protected)
Returns the value of attribute id.
63 64 65 |
# File 'lib/osctl/image/operations/builder/controlled_runscript.rb', line 63 def id @id end |
#name ⇒ Object (readonly, protected)
Returns the value of attribute name.
63 64 65 |
# File 'lib/osctl/image/operations/builder/controlled_runscript.rb', line 63 def name @name end |
#script ⇒ String (readonly)
17 18 19 |
# File 'lib/osctl/image/operations/builder/controlled_runscript.rb', line 17 def script @script end |
Instance Method Details
#create_file ⇒ File, String (protected)
Returns file, path relative from builder’s rootfs.
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/builder/controlled_runscript.rb', line 66 def create_file host_path = nil builder_path = nil 5.times do builder_path = File.join('/', "#{name}#{SecureRandom.hex(5)}") host_path = File.join(builder.attrs[:rootfs], builder_path) break unless File.exist?(host_path) host_path = nil end if host_path.nil? raise 'unable to create temporary file' end fh = File.open(host_path, 'w') File.chmod(0o755, host_path) [fh, builder_path] end |
#execute ⇒ Integer
Returns exit status.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/osctl/image/operations/builder/controlled_runscript.rb', line 41 def execute fh, path = create_file if file File.open(file, 'r') { |f| IO.copy_stream(f, fh) } elsif script fh.write(script) else raise 'programming error' end fh.close begin Operations::Builder::ControlledExec.run(builder, [path], id:, client:) ensure unlink(fh.path) end end |
#unlink(path) ⇒ Object (protected)
88 89 90 91 92 |
# File 'lib/osctl/image/operations/builder/controlled_runscript.rb', line 88 def unlink(path) File.unlink(path) rescue Errno::ENOENT # ignore end |