Class: Kontena::StacksCache

Inherits:
Object
  • Object
show all
Defined in:
lib/kontena/stacks_cache.rb

Defined Under Namespace

Classes: CachedStack, RegistryClientFactory

Class Method Summary collapse

Class Method Details

.base_pathObject



99
100
101
102
103
104
105
106
107
# File 'lib/kontena/stacks_cache.rb', line 99

def base_path
  return @base_path if @base_path
  @base_path = File.join(Dir.home, '.kontena/cache/stacks')
  unless File.directory?(@base_path)
    require 'fileutils'
    FileUtils.mkdir_p(@base_path)
  end
  @base_path
end

.cache(stack_name) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/kontena/stacks_cache.rb', line 68

def cache(stack_name)
  stack = CachedStack.new(stack_name)
  if stack.cached?
    dputs "Reading from cache: #{stack.path}"
  else
    dputs "Retrieving #{stack.stack_name} from registry"
    content = client.pull(stack_name)
    yaml    = ::YAML.safe_load(content, [], [], true, stack.stack_name.to_s)
    new_stack_name = Kontena::Cli::Stacks::StackName.new(yaml['stack'], yaml['version'])
    new_stack = CachedStack.new(new_stack_name)
    if new_stack.cached?
      dputs "Already cached"
      stack = new_stack
    else
      dputs "Writing #{stack.path}"
      new_stack.write(content)
      dputs "#{new_stack.stack_name} cached to #{new_stack.path}"
      stack = new_stack
    end
  end
  stack
end

.clientObject



95
96
97
# File 'lib/kontena/stacks_cache.rb', line 95

def client
  @client ||= RegistryClientFactory.new.stacks_client
end

.dputs(msg) ⇒ Object



64
65
66
# File 'lib/kontena/stacks_cache.rb', line 64

def dputs(msg)
  Kontena.logger.debug { msg }
end

.pull(stack_name) ⇒ Object



60
61
62
# File 'lib/kontena/stacks_cache.rb', line 60

def pull(stack_name)
  cache(stack_name).read
end

.registry_url(stack_name) ⇒ Object



91
92
93
# File 'lib/kontena/stacks_cache.rb', line 91

def registry_url(stack_name)
  client.full_uri(stack_name)
end