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_path ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/kontena/stacks_cache.rb', line 106

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, version = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/kontena/stacks_cache.rb', line 75

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

.client ⇒ Object



102
103
104
# File 'lib/kontena/stacks_cache.rb', line 102

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

.dputs(msg) ⇒ Object



71
72
73
# File 'lib/kontena/stacks_cache.rb', line 71

def dputs(msg)
  ENV["DEBUG"] && $stderr.puts(msg)
end

.pull(stack, version = nil) ⇒ Object



67
68
69
# File 'lib/kontena/stacks_cache.rb', line 67

def pull(stack, version = nil)
  cache(stack, version).read
end

.registry_url(stack, version = nil) ⇒ Object



98
99
100
# File 'lib/kontena/stacks_cache.rb', line 98

def registry_url(stack, version = nil)
  client.full_uri(stack, version)
end