Class: OsCtld::ContainerControl::Commands::Runscript::Frontend

Inherits:
Frontend
  • Object
show all
Includes:
Utils::Runscript::Frontend
Defined in:
lib/osctld/container_control/commands/runscript.rb

Instance Attribute Summary

Attributes inherited from Frontend

#command_class, #ct

Instance Method Summary collapse

Methods included from Utils::Runscript::Frontend

#add_network_opts, #cleanup_init_script, #init_script, #unlink_file

Methods inherited from Frontend

#initialize, #pipe_runner

Constructor Details

This class inherits a constructor from OsCtld::ContainerControl::Frontend

Instance Method Details

#copy_script(src) ⇒ Object (protected)



63
64
65
66
67
68
69
70
# File 'lib/osctld/container_control/commands/runscript.rb', line 63

def copy_script(src)
  script = Tempfile.create(['.runscript', '.sh'], ct.rootfs)
  script.chmod(0500)

  File.open(src, 'r') { |f| IO.copy_stream(f, script) }
  script.close
  script
end

#execute(opts) ⇒ Integer

Returns exit status.

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :script (String)

    path to the script on the host

  • :args (Array<String>)

    script arguments

  • :stdin (IO)
  • :stdout (IO)
  • :stderr (IO)
  • :run (Boolean)

    run the container if it is stopped?

  • :network (Boolean)

    setup network if the container is run?

Returns:

  • (Integer)

    exit status



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/osctld/container_control/commands/runscript.rb', line 27

def execute(opts)
  opts = opts.clone

  mode =
    if ct.running?
      :running
    elsif !ct.running? && opts[:run] && opts[:network]
      :run_network
    elsif !ct.running? && opts[:run]
      :run
    else
      raise ContainerControl::Error, 'container not running'
    end

  add_network_opts(opts) if opts[:network]

  script = copy_script(opts[:script])
  opts[:script] = File.join('/', File.basename(script.path))

  ret = pipe_runner(
    args: [mode, opts],
    keep_fds: [
      opts[:stdin],
      opts[:stdout],
      opts[:stderr],
    ],
  )
  ret.ok? ? ret.data : ret

ensure
  script.close
  unlink_file(script.path)
  cleanup_init_script
end