Module: NexusSW::LXD::Transport::Mixins::Local

Includes:
Helpers::ExecuteMixin
Included in:
Local
Defined in:
lib/nexussw/lxd/transport/mixins/local.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::ExecuteMixin

#execute

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/nexussw/lxd/transport/mixins/local.rb', line 14

def config
  @config
end

Instance Method Details

#execute_chunked(command, options, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
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
# File 'lib/nexussw/lxd/transport/mixins/local.rb', line 18

def execute_chunked(command, options, &block)
  NIO::WebSocket::Reactor.start
  LXD.with_timeout_and_retries options do
    if options[:capture] == :interactive
      if options[:tty] == false
        Open3.popen2e(command) do |stdin, stdout, th|
          # return immediately if interactive so that stdin may be used
          return Helpers::ExecuteMixin::InteractiveResult.new(command, options, stdin, th).tap do |active|
            chunk_callback(stdout) do |stdout_chunk|
              active.send_output stdout_chunk if stdout_chunk
            end
            yield active
            active.exitstatus = th.value.exitstatus
          end
        end
      else
        status = system command
        status = case status
                 when nil then -1
                 when true then 0
                 when false then 1
                 end
        return Helpers::ExecuteMixin::ExecuteResult.new(command, options, status)
      end
    else
      Open3.popen3(command) do |_stdin, stdout, stderr, th|
        chunk_callback(stdout, stderr, &block) if block_given?
        th.join
        loop do
          return Helpers::ExecuteMixin::ExecuteResult.new(command, options, th.value.exitstatus) if th.value.exited? && mon_out && mon_err && mon_out.closed? && mon_err.closed?
          Thread.pass
        end
      end
    end
  end
end

#initialize(config = {}) ⇒ Object



10
11
12
# File 'lib/nexussw/lxd/transport/mixins/local.rb', line 10

def initialize(config = {})
  @config = config
end

#read_file(path) ⇒ Object



55
56
57
58
# File 'lib/nexussw/lxd/transport/mixins/local.rb', line 55

def read_file(path)
  # return '' unless File.exist? path
  File.read path
end

#write_file(path, content) ⇒ Object



60
61
62
63
64
# File 'lib/nexussw/lxd/transport/mixins/local.rb', line 60

def write_file(path, content)
  File.open path, "w" do |f|
    f.write content
  end
end