Class: Container

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/models/container.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**params) ⇒ Container

Returns a new instance of Container.



44
45
46
47
48
49
50
51
52
53
# File 'lib/models/container.rb', line 44

def initialize(**params)
  @id = params[:id]
  @image_str = params[:image_str]
  @command = params[:command]
  @created = params[:created]
  @status = params[:status]
  @ports = params[:ports]
  @name = params[:name]
  @raw = params[:raw]
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



4
5
6
# File 'lib/models/container.rb', line 4

def command
  @command
end

#createdObject

Returns the value of attribute created.



4
5
6
# File 'lib/models/container.rb', line 4

def created
  @created
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/models/container.rb', line 4

def id
  @id
end

#image_strObject

Returns the value of attribute image_str.



4
5
6
# File 'lib/models/container.rb', line 4

def image_str
  @image_str
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/models/container.rb', line 4

def name
  @name
end

#portsObject

Returns the value of attribute ports.



4
5
6
# File 'lib/models/container.rb', line 4

def ports
  @ports
end

#regexp_nameObject

Returns the value of attribute regexp_name.



4
5
6
# File 'lib/models/container.rb', line 4

def regexp_name
  @regexp_name
end

#regexp_repoObject

Returns the value of attribute regexp_repo.



4
5
6
# File 'lib/models/container.rb', line 4

def regexp_repo
  @regexp_repo
end

#statusObject

Returns the value of attribute status.



4
5
6
# File 'lib/models/container.rb', line 4

def status
  @status
end

Class Method Details

.commandObject



5
6
7
# File 'lib/models/container.rb', line 5

def self.command
  'docker ps -a'
end

.create(line) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/models/container.rb', line 29

def self.create(line)
  id, image_str, command, created, status, ports, name = line.chomp.split(' ' * 2).reject(&:empty?).map(&:strip)
  ret = new(
    id: id,
    image_str: image_str,
    command: command,
    created: created,
    status: status,
    ports: ports,
    name: name,
    raw: line
  )
  ret
end

.find(keyword:, name:, image:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/models/container.rb', line 9

def self.find(keyword:, name:, image:)
  return all unless keyword || name || image
  ret = keyword ? search(keyword) : all
  if image
    regexp_repo = /#{image}/i
    ret = ret.select do |container|
      container.regexp_repo = regexp_repo
      regexp_repo.match container.image_str
    end
  end
  if name
    regexp_name = /#{name}/i
    ret = ret.select do |container|
      container.regexp_name = regexp_name
      regexp_name.match container.name
    end
  end
  ret
end

Instance Method Details

#delObject



55
56
57
# File 'lib/models/container.rb', line 55

def del
  run("docker rm -f #{id}")
end

#exec(exec_opts, command) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/models/container.rb', line 59

def exec(exec_opts, command)
  [
    'docker exec',
    exec_opts,
    name,
    command
  ]
end

#to_sObject



68
69
70
71
72
73
74
# File 'lib/models/container.rb', line 68

def to_s
  @raw.gsub!(@regexp, &:yellow) if @regexp
  @raw.sub!(/ #{image_str} /) { |_| image_str.gsub(@regexp_repo, '\0'.red) } if @regexp_repo
  @raw.gsub!(/#{name}$/) { |name| name.gsub(@regexp_name, '\0'.blue) } if @regexp_name

  @raw
end