class Kontena::StacksCache::CachedStack

Attributes

stack_name[R]

Public Class Methods

new(stack_name) click to toggle source
# File lib/kontena/stacks_cache.rb, line 9
def initialize(stack_name)
  @stack_name = stack_name
end

Public Instance Methods

cached?() click to toggle source
# File lib/kontena/stacks_cache.rb, line 34
def cached?
  return false unless stack_name.version
  File.exist?(path)
end
delete() click to toggle source
# File lib/kontena/stacks_cache.rb, line 30
def delete
  File.unlink(path)
end
load() click to toggle source
# File lib/kontena/stacks_cache.rb, line 17
def load
  ::YAML.safe_load(read, [], [], true, path)
end
path() click to toggle source
# 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
read() click to toggle source
# File lib/kontena/stacks_cache.rb, line 13
def read
  File.read(path)
end
write(content) click to toggle source
# 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

Private Instance Methods

base_path() click to toggle source
# File lib/kontena/stacks_cache.rb, line 47
def base_path
  Kontena::StacksCache.base_path
end