Class: RCS::Updater::Payload

Inherits:
Object
  • Object
show all
Includes:
Tracer, TmpDir
Defined in:
lib/rcs-common/updater/payload.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
600

Constants included from Tracer

Tracer::TRACE_YAML_NAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TmpDir

#tmpdir, #windows?

Methods included from Tracer

#thread_name, #trace, #trace_ensure_log_folders, #trace_init, #trace_named_put, #trace_named_remove, #trace_nested_pop, #trace_nested_push, #trace_setup

Constructor Details

#initialize(payload, options = {}) ⇒ Payload

Returns a new instance of Payload.



18
19
20
21
22
23
24
# File 'lib/rcs-common/updater/payload.rb', line 18

def initialize(payload, options = {})
  @options = options
  @payload = payload

  @timeout = options['timeout'].to_i
  @timeout = DEFAULT_TIMEOUT if @timeout <= 0
end

Instance Attribute Details

#filepathObject (readonly)

Returns the value of attribute filepath.



14
15
16
# File 'lib/rcs-common/updater/payload.rb', line 14

def filepath
  @filepath
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/rcs-common/updater/payload.rb', line 13

def options
  @options
end

#outputObject (readonly)

Returns the value of attribute output.



14
15
16
# File 'lib/rcs-common/updater/payload.rb', line 14

def output
  @output
end

#payloadObject (readonly)

Returns the value of attribute payload.



13
14
15
# File 'lib/rcs-common/updater/payload.rb', line 13

def payload
  @payload
end

#return_codeObject (readonly)

Returns the value of attribute return_code.



14
15
16
# File 'lib/rcs-common/updater/payload.rb', line 14

def return_code
  @return_code
end

#storedObject (readonly)

Returns the value of attribute stored.



14
15
16
# File 'lib/rcs-common/updater/payload.rb', line 14

def stored
  @stored
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



13
14
15
# File 'lib/rcs-common/updater/payload.rb', line 13

def timeout
  @timeout
end

Instance Method Details

#[](name) ⇒ Object



36
37
38
# File 'lib/rcs-common/updater/payload.rb', line 36

def [](name)
  instance_variable_get("@#{name}")
end

#ruby?Boolean

Returns:

  • (Boolean)


26
# File 'lib/rcs-common/updater/payload.rb', line 26

def ruby?; options['ruby']; end

#runObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rcs-common/updater/payload.rb', line 40

def run
  @return_code = nil
  @output = ""

  cmd = "#{'ruby ' if ruby?}#{storable? ? filepath : payload}"

  if spawn?
    trace(:debug, "[spawn] #{cmd}")
    return spawn(cmd)
  end

  Timeout::timeout(@timeout) do
    trace(:debug, "Timeout has been set to #{@timeout} sec") if @timeout != DEFAULT_TIMEOUT

    trace(:debug, "[popen] #{cmd}")

    Open3.popen2e(cmd) do |stdin, std_out_err, wait_thr|
        while line = std_out_err.gets
          trace(:debug, "[std_out_err] #{line.strip}")
          @output << line
        end
      @return_code = wait_thr.value.exitstatus
    end
  end

  return @return_code
ensure
  FileUtils.rm_f(filepath) if stored
end

#runnable?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/rcs-common/updater/payload.rb', line 32

def runnable?
  options['exec'] or ruby? or spawn?
end

#spawn?Boolean

Returns:

  • (Boolean)


30
# File 'lib/rcs-common/updater/payload.rb', line 30

def spawn?; options['spawn']; end

#storable?Boolean

Returns:

  • (Boolean)


28
# File 'lib/rcs-common/updater/payload.rb', line 28

def storable?; options['store']; end

#storeObject



70
71
72
73
74
75
76
# File 'lib/rcs-common/updater/payload.rb', line 70

def store
  @filepath = "#{tmpdir}/" + (@options['filename'] || Time.now.to_f.to_s.gsub(".", ""))
  FileUtils.mkdir_p(tmpdir)
  trace(:debug, "Storing payload into #{filepath}")
  File.open(filepath, "wb") { |f| f.write(payload) }
  @stored = true
end