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 |
# 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 @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.
61 62 63 |
# File 'lib/osctl/image/operations/builder/controlled_runscript.rb', line 61 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.
61 62 63 |
# File 'lib/osctl/image/operations/builder/controlled_runscript.rb', line 61 def id @id end |
#name ⇒ Object (readonly, protected)
Returns the value of attribute name.
61 62 63 |
# File 'lib/osctl/image/operations/builder/controlled_runscript.rb', line 61 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.
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/builder/controlled_runscript.rb', line 64 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) if File.exist?(host_path) host_path = nil else break end end if host_path.nil? fail 'unable to create temporary file' end fh = File.open(host_path, 'w') File.chmod(0755, host_path) [fh, builder_path] end |
#execute ⇒ Integer
Returns exit status.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/osctl/image/operations/builder/controlled_runscript.rb', line 39 def execute fh, path = create_file if file File.open(file, 'r') { |f| IO.copy_stream(f, fh) } elsif script fh.write(script) else fail 'programming error' end fh.close begin Operations::Builder::ControlledExec.run(builder, [path], id: id, client: client) ensure unlink(fh.path) end end |
#unlink(path) ⇒ Object (protected)
88 89 90 91 |
# File 'lib/osctl/image/operations/builder/controlled_runscript.rb', line 88 def unlink(path) File.unlink(path) rescue Errno::ENOENT end |