class Bosh::Cli::Versions::VersionsIndex

Constants

CURRENT_INDEX_VERSION

Attributes

index_file[R]
storage_dir[R]

Public Class Methods

load_index_yaml(index_path) click to toggle source
# File lib/cli/versions/versions_index.rb, line 7
def self.load_index_yaml(index_path)
  contents = load_yaml_file(index_path, nil)
  # Psych.load returns false if file is empty
  return nil if contents === false
  unless contents.kind_of?(Hash)
    raise Bosh::Cli::InvalidIndex, "Invalid versions index data type, #{contents.class} given, Hash expected"
  end
  contents
end
new(storage_dir) click to toggle source
# File lib/cli/versions/versions_index.rb, line 29
def initialize(storage_dir)
  @storage_dir = File.expand_path(storage_dir)
  @index_file  = File.join(@storage_dir, 'index.yml')

  if File.file?(@index_file)
    reload
  else
    init_index({})
  end
end
write_index_yaml(index_path, contents) click to toggle source
# File lib/cli/versions/versions_index.rb, line 17
def self.write_index_yaml(index_path, contents)
  unless contents.kind_of?(Hash)
    raise Bosh::Cli::InvalidIndex, "Invalid versions index data type, #{contents.class} given, Hash expected"
  end
  File.open(index_path, 'w') do |f|
    f.write(Psych.dump(contents))
  end
end

Public Instance Methods

[](key) click to toggle source
# File lib/cli/versions/versions_index.rb, line 40
def [](key)
  @data['builds'][key]
end
add_version(new_key, new_build) click to toggle source

both (tmp_file_path=nil only used by release)

# File lib/cli/versions/versions_index.rb, line 53
def add_version(new_key, new_build)
  version = new_build['version']

  if version.blank?
    raise Bosh::Cli::InvalidIndex, "Cannot save index entry without a version: '#{new_build}'"
  end

  if @data['builds'][new_key]
    raise "Trying to add duplicate entry '#{new_key}' into index '#{@index_file}'"
  end

  each do |key, build|
    if key != new_key && build['version'] == version
      raise "Trying to add duplicate version '#{version}' into index '#{@index_file}'"
    end
  end

  @data['builds'][new_key] = new_build

  save

  version
end
each(&block) click to toggle source
# File lib/cli/versions/versions_index.rb, line 44
def each(&block)
  @data['builds'].each(&block)
end
find_key_by_version(version) click to toggle source
# File lib/cli/versions/versions_index.rb, line 107
def find_key_by_version(version)
  key_and_build = find { |_, build| build['version'] == version }

  key_and_build.first unless key_and_build.nil?
end
format_version() click to toggle source
# File lib/cli/versions/versions_index.rb, line 121
def format_version
  format_version_string = @data['format-version']
  SemiSemantic::Version.parse(format_version_string)
rescue ArgumentError, SemiSemantic::ParseError
  raise InvalidIndex, "Invalid versions index version in '#{@index_file}', " +
    "'#{format_version_string}' given, SemiSemantic version expected"
end
reload() click to toggle source
# File lib/cli/versions/versions_index.rb, line 134
def reload
  init_index(VersionsIndex.load_index_yaml(@index_file))
end
remove_version(key) click to toggle source
# File lib/cli/versions/versions_index.rb, line 96
def remove_version(key)
  build = @data['builds'][key]
  unless build
    raise "Cannot remove non-existent entry with key '#{key}'"
  end

  @data['builds'].delete(key)

  save
end
save() click to toggle source
# File lib/cli/versions/versions_index.rb, line 129
def save
  create_directories
  VersionsIndex.write_index_yaml(@index_file, @data)
end
select(&block) click to toggle source
# File lib/cli/versions/versions_index.rb, line 48
def select(&block)
  @data['builds'].select(&block)
end
to_s() click to toggle source
# File lib/cli/versions/versions_index.rb, line 117
def to_s
  @data['builds'].to_s
end
update_version(key, new_build) click to toggle source
# File lib/cli/versions/versions_index.rb, line 77
def update_version(key, new_build)
  old_build = @data['builds'][key]
  unless old_build
    raise "Cannot update non-existent entry with key '#{key}'"
  end

  if old_build['blobstore_id']
    raise "Cannot update entry '#{old_build}' with a blobstore id"
  end

  if new_build['version'] != old_build['version']
    raise "Cannot update entry '#{old_build}' with a different version: '#{new_build}'"
  end

  @data['builds'][key] = new_build

  save
end
version_strings() click to toggle source
# File lib/cli/versions/versions_index.rb, line 113
def version_strings
  @data['builds'].map { |_, build| build['version'].to_s }
end

Private Instance Methods

create_directories() click to toggle source
# File lib/cli/versions/versions_index.rb, line 140
def create_directories
  begin
    FileUtils.mkdir_p(@storage_dir)
  rescue SystemCallError => e
    raise InvalidIndex, "Cannot create index storage directory: #{e}"
  end

  begin
    FileUtils.touch(@index_file)
  rescue SystemCallError => e
    raise InvalidIndex, "Cannot create index file: #{e}"
  end
end
init_index(data) click to toggle source
# File lib/cli/versions/versions_index.rb, line 154
def init_index(data)
  @data = data || {}
  @data['builds'] ||= {}
  @data['format-version'] ||= CURRENT_INDEX_VERSION.to_s
end