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)


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

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

#deleteObject



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

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



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

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
29
# File 'lib/kontena/stacks_cache.rb', line 21

def write(content)
  puts "WHATHAT??? #{stack_name.inspect} #{stack_name.version} #{stack_name.stack_name}"
  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