Class: Cyclid::API::Plugins::DockerApi

Inherits:
Transport show all
Includes:
Helpers::Docker
Defined in:
app/cyclid/plugins/transport/dockerapi.rb

Overview

Docker based transport

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Docker

#create_container, #get_container, #load_docker_config

Methods inherited from Transport

#close, #export_env, human_name

Methods inherited from Base

config?, config_schema, default_config, get_config, human_name, register_plugin, set_config, update_config

Constructor Details

#initialize(args = {}) ⇒ DockerApi

Returns a new instance of DockerApi.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/cyclid/plugins/transport/dockerapi.rb', line 29

def initialize(args = {})
  args.symbolize_keys!

  Cyclid.logger.debug "Docker Transport: args=#{args}"

  # Configure Docker
  config = load_docker_config(
    Cyclid.config.plugins
  )
  ::Docker.url = config[:api]

  # Container name & a log target are required
  return false unless args.include?(:host) && \
                      args.include?(:log)

  @container = get_container(args[:host])
  @log = args[:log]

  ctx = args[:ctx]
  @username = ctx[:username]
end

Instance Attribute Details

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



27
28
29
# File 'app/cyclid/plugins/transport/dockerapi.rb', line 27

def exit_code
  @exit_code
end

Instance Method Details

#download(io, path) ⇒ Object

Copy data from remote file to local IO



68
69
70
71
# File 'app/cyclid/plugins/transport/dockerapi.rb', line 68

def download(io, path)
  result = @container.read_file(path)
  io.write(result)
end

#exec(cmd, path = nil) ⇒ Object

Execute a command via the Docker API



52
53
54
55
56
57
58
59
60
# File 'app/cyclid/plugins/transport/dockerapi.rb', line 52

def exec(cmd, path = nil)
  command = build_command(cmd, path, @env)
  Cyclid.logger.debug "command=#{command}"
  result = @container.exec(command, wait: 300) do |_stream, chunk|
    @log.write chunk
  end
  @exit_code = result[2]
  @exit_code.zero? ? true : false
end

#upload(io, path) ⇒ Object

Copy data from local IO object to a remote file



63
64
65
# File 'app/cyclid/plugins/transport/dockerapi.rb', line 63

def upload(io, path)
  @container.store_file(path, io.read)
end