Class: DockerHost

Inherits:
Object
  • Object
show all
Extended by:
HTTPUtils
Defined in:
lib/panteras_api/docker_host.rb

Defined Under Namespace

Classes: MesosConsulCommandError

Class Method Summary collapse

Methods included from HTTPUtils

construct_uri, get_response_with_redirect, to_j, valid_url

Class Method Details

.command(command) ⇒ Object



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
69
70
71
72
73
74
75
76
# File 'lib/panteras_api/docker_host.rb', line 42

def self.command(command)
  output = []
  error = []
  begin
    Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
      
      exit_status = wait_thr.value
      
      unless exit_status.success?
        $stderr.puts stderr.readlines
        raise MesosConsulCommandError, "Command '#{command}' returned status #{exit_status.exitstatus}"          
      end
      
      while line = stderr.gets
        $stderr.puts line
        if line.strip.length > 0
          error << line.strip
        end
      end
      
      while line = stdout.gets
       if line.strip.length > 0          
         output << line.strip
       end
      end     
    
    end
    
  rescue Errno::ENOENT => e
    raise MesosConsulCommandError, "Error while running command #{command}: #{e.message}"
  end
    
  return { :output => output, :error => error}
     
end

.inspect(*keys) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/panteras_api/docker_host.rb', line 23

def self.inspect(*keys)
  containers = self.running_containers
  containers.map do |c|
    inspect = self.command("docker inspect #{c[:container_id]}")[:output]
    h = to_j(inspect.join).first    
    task_id = h[:Config][:Env].select { |env| env =~ /MESOS_TASK_ID=/ }.first
    mesos_task_id = task_id.nil? ? '' : task_id.split(/=/)[1] 
    { id: h[:Id], image: h[:Image], name: h[:Name][1..-1], mesos_task_id: mesos_task_id }
  end
end

.inspect_partialObject



34
35
36
# File 'lib/panteras_api/docker_host.rb', line 34

def self.inspect_partial
  self.inspect
end

.psObject



6
7
8
# File 'lib/panteras_api/docker_host.rb', line 6

def self.ps
  self.command("docker ps")[:output]
end

.running_containersObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/panteras_api/docker_host.rb', line 10

def self.running_containers
  output = self.command("docker ps")[:output]
  raise MesosConsulCommandError, "Command 'docker ps' did not return an array of strings" if (output.class != Array || output.length == 0)
  
  result = output.map.with_index do |line, i|
    next if i == 0 
    container_id, image, *center, name = line.split
    { container_id:  container_id, name: name, image: image }       
  end
  
  return result.compact
end