Class: OsCtl::Image::Operations::Builder::ControlledRunscript

Inherits:
OsCtl::Image::Operations::Base show all
Includes:
Lib::Utils::Log, Lib::Utils::System
Defined in:
lib/osctl/image/operations/builder/controlled_runscript.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from OsCtl::Image::Operations::Base

run

Constructor Details

#initialize(builder, file: nil, script: nil, id: nil, name: nil, client: nil) ⇒ ControlledRunscript

Returns a new instance of ControlledRunscript.

Parameters:

  • builder (Builder)
  • file (String) (defaults to: nil)

    script to execute

  • script (String) (defaults to: nil)

    shell commands to execute

  • id (nil, String) (defaults to: nil)

    optional run identifier

  • name (nil, String) (defaults to: nil)
  • client (nil, OsCtldClient) (defaults to: nil)


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

#builderBuilder (readonly)

Returns:



11
12
13
# File 'lib/osctl/image/operations/builder/controlled_runscript.rb', line 11

def builder
  @builder
end

#clientObject (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

#fileString (readonly)

Returns:

  • (String)


14
15
16
# File 'lib/osctl/image/operations/builder/controlled_runscript.rb', line 14

def file
  @file
end

#idObject (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

#nameObject (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

#scriptString (readonly)

Returns:

  • (String)


17
18
19
# File 'lib/osctl/image/operations/builder/controlled_runscript.rb', line 17

def script
  @script
end

Instance Method Details

#create_fileFile, String (protected)

Returns file, path relative from builder’s rootfs.

Returns:

  • (File, String)

    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

#executeInteger

Returns exit status.

Returns:

  • (Integer)

    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


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