Class: Kontena::StacksCache::CachedStack

Inherits:
Object
  • Object
show all
Defined in:
lib/kontena/stacks_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack, version = nil) ⇒ CachedStack

Returns a new instance of CachedStack.



14
15
16
17
18
19
20
# File 'lib/kontena/stacks_cache.rb', line 14

def initialize(stack, version = nil)
  unless version
    stack, version = stack.split(':', 2)
  end
  @stack = stack
  @version = version
end

Instance Attribute Details

#stack ⇒ Object

Returns the value of attribute stack.



11
12
13
# File 'lib/kontena/stacks_cache.rb', line 11

def stack
  @stack
end

#version ⇒ Object

Returns the value of attribute version.



12
13
14
# File 'lib/kontena/stacks_cache.rb', line 12

def version
  @version
end

Instance Method Details

#cached? ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/kontena/stacks_cache.rb', line 43

def cached?
  return false unless version
  File.exist?(path)
end

#delete ⇒ Object



39
40
41
# File 'lib/kontena/stacks_cache.rb', line 39

def delete
  File.unlink(path)
end

#load ⇒ Object



26
27
28
# File 'lib/kontena/stacks_cache.rb', line 26

def load
  YAML.safe_load(read)
end

#path ⇒ Object



48
49
50
51
52
# File 'lib/kontena/stacks_cache.rb', line 48

def path
  path = File.expand_path(File.join(base_path, "#{stack}-#{version}.yml"))
  raise "Path traversal attempted" unless path.start_with?(base_path)
  path
end

#read ⇒ Object



22
23
24
# File 'lib/kontena/stacks_cache.rb', line 22

def read
  File.read(path)
end

#write(content) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
# File 'lib/kontena/stacks_cache.rb', line 30

def write(content)
  raise ArgumentError, "Stack name and version required" unless @stack && @version
  unless File.directory?(File.dirname(path))
    require 'fileutils'
    FileUtils.mkdir_p(File.dirname(path))
  end
  File.write(path, content)
end