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_name) ⇒ CachedStack

Returns a new instance of CachedStack.



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

def initialize(stack_name)
  @stack_name = stack_name
end

Instance Attribute Details

#stack_nameObject (readonly)

Returns the value of attribute stack_name.



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

def stack_name
  @stack_name
end

Instance Method Details

#cached?Boolean

Returns:

  • (Boolean)


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

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

#deleteObject



30
31
32
# File 'lib/kontena/stacks_cache.rb', line 30

def delete
  File.unlink(path)
end

#loadObject



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

def load
  ::YAML.safe_load(read, [], [], true, path)
end

#pathObject



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

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

#readObject



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

def read
  File.read(path)
end

#write(content) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
# File 'lib/kontena/stacks_cache.rb', line 21

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