Class: Image

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**params) ⇒ Image

Returns a new instance of Image.



63
64
65
66
67
68
69
70
# File 'lib/models/image.rb', line 63

def initialize(**params)
  @repository = params[:repository]
  @tag = params[:tag]
  @id = params[:id]
  @created = params[:created]
  @size = params[:size]
  @raw = params[:raw]
end

Instance Attribute Details

#createdObject

Returns the value of attribute created.



3
4
5
# File 'lib/models/image.rb', line 3

def created
  @created
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/models/image.rb', line 3

def id
  @id
end

#regexp_repoObject

Returns the value of attribute regexp_repo.



3
4
5
# File 'lib/models/image.rb', line 3

def regexp_repo
  @regexp_repo
end

#regexp_tagObject

Returns the value of attribute regexp_tag.



3
4
5
# File 'lib/models/image.rb', line 3

def regexp_tag
  @regexp_tag
end

#repositoryObject

Returns the value of attribute repository.



3
4
5
# File 'lib/models/image.rb', line 3

def repository
  @repository
end

#sizeObject

Returns the value of attribute size.



3
4
5
# File 'lib/models/image.rb', line 3

def size
  @size
end

#tagObject

Returns the value of attribute tag.



3
4
5
# File 'lib/models/image.rb', line 3

def tag
  @tag
end

Class Method Details

.build(dockerfile, *tags) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/models/image.rb', line 22

def self.build(dockerfile, *tags)
  run 'docker',
      'build',
      '--force-rm',
      '-f',
      dockerfile,
      '-t',
      tags.join(' -t '),
      '.'
end

.commandObject



14
15
16
# File 'lib/models/image.rb', line 14

def self.command
  'docker images'
end

.create(line) ⇒ Object



57
58
59
60
61
# File 'lib/models/image.rb', line 57

def self.create(line)
  repository, tag, id, created, size = line.chomp.split(' ' * 2).reject(&:empty?).map(&:strip)
  ret = new(repository: repository, tag: tag, id: id, created: created, size: size, raw: line)
  ret
end

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/models/image.rb', line 33

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

.headersObject



4
5
6
7
8
9
10
11
12
# File 'lib/models/image.rb', line 4

def self.headers
  {
    repository: 'REPOSITORY',
    tag: 'TAG',
    id: 'IMAGE ID',
    created: 'CREATED',
    size: 'SIZE'
  }
end

.launch(image_name, run_opts, command) ⇒ Object



18
19
20
# File 'lib/models/image.rb', line 18

def self.launch(image_name, run_opts, command)
  ['docker', 'run', run_opts, image_name, command]
end

.pull(name) ⇒ Object



77
78
79
# File 'lib/models/image.rb', line 77

def self.pull(name)
  run("docker pull #{name}")
end

.push(repo_name) ⇒ Object



53
54
55
# File 'lib/models/image.rb', line 53

def self.push(repo_name)
  run("docker push #{repo_name}")
end

.tag(name, *as_names) ⇒ Object



85
86
87
# File 'lib/models/image.rb', line 85

def self.tag(name, *as_names)
  run("docker tag #{name} #{as_names.join(' ')}")
end

Instance Method Details

#delObject



72
73
74
75
# File 'lib/models/image.rb', line 72

def del
  instance = /<none>/.match(image_name) ? id : image_name
  run("docker rmi -f #{instance}")
end

#image_nameObject



89
90
91
# File 'lib/models/image.rb', line 89

def image_name
  "#{repository}:#{tag || 'latest'}"
end

#launch(run_opts, command) ⇒ Object



93
94
95
# File 'lib/models/image.rb', line 93

def launch(run_opts, command)
  self.class.launch(image_name, run_opts, command)
end

#pushObject



81
82
83
# File 'lib/models/image.rb', line 81

def push
  self.class.push(image_name)
end

#to_a(*cols) ⇒ Object



97
98
99
100
101
# File 'lib/models/image.rb', line 97

def to_a(*cols)
  cols = Image.headers.keys if cols.empty?
  ret = cols.map { |col| send(col).strip }
  ret
end

#to_sObject



103
104
105
106
107
108
# File 'lib/models/image.rb', line 103

def to_s
  @raw.gsub!(@regexp, &:yellow) if @regexp
  @raw.sub!(repository, repository.gsub(@regexp_repo, '\0'.red)) if @regexp_repo
  @raw.gsub!(/#{tag}$/) { |tag| tag.gsub(@regexp_tag, '\0'.blue) } if @regexp_tag
  @raw
end