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.



10
11
12
13
14
15
16
# File 'lib/kontena/stacks_cache.rb', line 10

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.



7
8
9
# File 'lib/kontena/stacks_cache.rb', line 7

def stack
  @stack
end

#version ⇒ Object

Returns the value of attribute version.



8
9
10
# File 'lib/kontena/stacks_cache.rb', line 8

def version
  @version
end

Instance Method Details

#cached? ⇒ Boolean

Returns:

  • (Boolean)


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

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

#delete ⇒ Object



35
36
37
# File 'lib/kontena/stacks_cache.rb', line 35

def delete
  File.unlink(path)
end

#load ⇒ Object



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

def load
  YAML.safe_load(read)
end

#path ⇒ Object



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

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



18
19
20
# File 'lib/kontena/stacks_cache.rb', line 18

def read
  File.read(path)
end

#write(content) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
# File 'lib/kontena/stacks_cache.rb', line 26

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