class Kubert::FileAccess

Attributes

data[R]
file_name[R]
key[R]
s3_path[R]
type[R]

Public Class Methods

new(type, key = nil) click to toggle source
# File lib/kubert/file_access.rb, line 4
def initialize(type, key = nil)
  @type = type
  @key = key
  @s3_path = Kubert.public_send("s3_#{type}_path")
  @file_name = Kubert.public_send("#{type}_file_name")
  @data = read
end

Public Instance Methods

clean_local() click to toggle source
# File lib/kubert/file_access.rb, line 46
def clean_local
  File.delete(local_path) unless local?
end
found() click to toggle source
# File lib/kubert/file_access.rb, line 12
def found
  data[:data].select {|k, _v| k == key }
end
get() click to toggle source
# File lib/kubert/file_access.rb, line 29
def get
  data[:data][key]
end
local?() click to toggle source
# File lib/kubert/file_access.rb, line 33
def local?
  !s3_path
end
read() click to toggle source
# File lib/kubert/file_access.rb, line 16
def read
  YAML.load(file_read).with_indifferent_access
end
set(value) click to toggle source
# File lib/kubert/file_access.rb, line 20
def set(value)
  if value.nil?
    data[:data].delete(key)
  else
    data[:data][key] = value
  end
  self
end
write() click to toggle source
# File lib/kubert/file_access.rb, line 37
def write
  return write_local if local?
  s3_object.put(body: data.to_plain_yaml, content_encoding: 'application/octet-stream', content_type: "text/vnd.yaml", metadata: {author: `whoami`})
end
write_local() click to toggle source
# File lib/kubert/file_access.rb, line 42
def write_local
  File.write(local_path, data.to_plain_yaml)
end

Private Instance Methods

file_read() click to toggle source
# File lib/kubert/file_access.rb, line 54
def file_read
  return s3_object.get.body.read unless local?
  File.read(local_path)
end
local_path() click to toggle source
# File lib/kubert/file_access.rb, line 59
def local_path
  Kubert.ky_configuration["#{type}_path"] # options set & memoized elsewhere if needed
end
s3_object() click to toggle source
# File lib/kubert/file_access.rb, line 63
def s3_object
  @s3_object ||= begin
    require 'aws-sdk'
    s3 = Aws::S3::Resource.new
    match_data = s3_path.match(/\As3:\/\/(?<bucket>[^\/]+)\/(?<folder_path>\S+[^\/])\/?\z/)
    bucket = s3.bucket(match_data[:bucket])
    bucket.objects(prefix: "#{match_data[:folder_path]}/#{Kubert.current_namespace}/#{file_name}").first
  rescue LoadError
    puts "ERROR: s3_#{type}_path defined, but aws-sdk gem not available... it is not a hard dependency of kubert, but kubert requires it for s3 read/write if s3_#{type}_path defined"
  end
end