Class: Linecook::Image

Inherits:
Object
  • Object
show all
Includes:
Crypto, Downloader
Defined in:
lib/linecook-gem/image.rb

Constant Summary collapse

IMAGE_PATH =
File.join(Config::LINECOOK_HOME, 'images').freeze

Constants included from Downloader

Downloader::LOCK_WAIT_TIMEOUT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Crypto

#decrypt, #encrypt, keygen

Methods included from Downloader

#download, #unzip

Constructor Details

#initialize(name, group, tag) ⇒ Image

Returns a new instance of Image.


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/linecook-gem/image.rb', line 19

def initialize(name, group, tag)
  @name = name
  @group = group ? "#{name}-#{group.gsub(/-|\//,'_')}" : name
  @tag = tag
  @id = if @tag == 'latest'
    id = File.basename(latest).split('.')[0]
    @tag = id.split('-').last
    id
  elsif tag
    "#{@group}-#{@tag}"
  else
    @group
  end
  @path = image_path
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group


16
17
18
# File 'lib/linecook-gem/image.rb', line 16

def group
  @group
end

#idObject (readonly)

Returns the value of attribute id


16
17
18
# File 'lib/linecook-gem/image.rb', line 16

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name


16
17
18
# File 'lib/linecook-gem/image.rb', line 16

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path


16
17
18
# File 'lib/linecook-gem/image.rb', line 16

def path
  @path
end

#tagObject (readonly)

Returns the value of attribute tag


16
17
18
# File 'lib/linecook-gem/image.rb', line 16

def tag
  @tag
end

Instance Method Details

#clean(retention) ⇒ Object


67
68
69
# File 'lib/linecook-gem/image.rb', line 67

def clean(retention)
  provider.clean(retention, @group)
end

#fetchObject


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/linecook-gem/image.rb', line 35

def fetch
  return if File.exists?(@path)

  Dir.mktmpdir("#{@id}-download") do |tmpdir|
    tmppath = File.join(tmpdir, File.basename(@path))
    download(url, tmppath)
    FileUtils.mkdir_p(File.dirname(@path))
    decrypt(tmppath, dest: @path)
    FileUtils.rm_f(tmppath)
  end

end

#latestObject


63
64
65
# File 'lib/linecook-gem/image.rb', line 63

def latest
  provider.latest(@group)
end

#listObject


59
60
61
# File 'lib/linecook-gem/image.rb', line 59

def list
  provider.list(group: @group)
end

#uploadObject


48
49
50
51
52
53
# File 'lib/linecook-gem/image.rb', line 48

def upload
  puts "Encrypting and uploading image #{@path}"
  encrypted = encrypt(@path)
  provider.upload(encrypted, group: @group)
  FileUtils.rm_f(encrypted)
end

#urlObject


55
56
57
# File 'lib/linecook-gem/image.rb', line 55

def url
  provider.url(@id, group: @group)
end