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.



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

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

Instance Attribute Details

#stackObject

Returns the value of attribute stack.



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

def stack
  @stack
end

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#cached?Boolean

Returns:

  • (Boolean)


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

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

#deleteObject



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

def delete
  File.unlink(path)
end

#loadObject



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

def load
  YAML.safe_load(read)
end

#pathObject



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

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

#readObject



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

def read
  File.read(path)
end

#write(content) ⇒ Object

Raises:

  • (ArgumentError)


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

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