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

Class Method 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

author, config?, config_schema, default_config, get_config, homepage, human_name, license, register_plugin, set_config, update_config, version

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

Class Method Details

.metadataObject

Plugin metadata



77
78
79
80
81
82
# File 'app/cyclid/plugins/transport/dockerapi.rb', line 77

def self.
  super.merge!(version: Cyclid::Api::VERSION,
               license: 'Apache-2.0',
               author: 'Liqwyd Ltd.',
               homepage: 'http://docs.cyclid.io')
end

Instance Method Details

#download(io, path) ⇒ Object

Copy data from remote file to local IO



71
72
73
74
# File 'app/cyclid/plugins/transport/dockerapi.rb', line 71

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

#exec(cmd, args = {}) ⇒ Object

Execute a command via the Docker API



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

def exec(cmd, args = {})
  sudo = args[:sudo] || false
  path = args[:path]

  command = build_command(cmd, sudo, 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



66
67
68
# File 'app/cyclid/plugins/transport/dockerapi.rb', line 66

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