Class: Bosh::Cli::Versions::VersionsIndex

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cli/versions/versions_index.rb

Constant Summary collapse

CURRENT_INDEX_VERSION =
SemiSemantic::Version.parse('2')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage_dir) ⇒ VersionsIndex

Returns a new instance of VersionsIndex


29
30
31
32
33
34
35
36
37
38
# 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

Instance Attribute Details

#index_fileObject (readonly)

Returns the value of attribute index_file


26
27
28
# File 'lib/cli/versions/versions_index.rb', line 26

def index_file
  @index_file
end

#storage_dirObject (readonly)

Returns the value of attribute storage_dir


27
28
29
# File 'lib/cli/versions/versions_index.rb', line 27

def storage_dir
  @storage_dir
end

Class Method Details

.load_index_yaml(index_path) ⇒ Object


7
8
9
10
11
12
13
14
15
# 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

.write_index_yaml(index_path, contents) ⇒ Object


17
18
19
20
21
22
23
24
# 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

Instance Method Details

#[](key) ⇒ Object


40
41
42
# File 'lib/cli/versions/versions_index.rb', line 40

def [](key)
  @data['builds'][key]
end

#add_version(new_key, new_build) ⇒ Object

both (tmp_file_path=nil only used by release)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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) ⇒ Object


44
45
46
# File 'lib/cli/versions/versions_index.rb', line 44

def each(&block)
  @data['builds'].each(&block)
end

#find_key_by_version(version) ⇒ Object


107
108
109
110
111
# 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_versionObject


121
122
123
124
125
126
127
# 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

#reloadObject


134
135
136
# File 'lib/cli/versions/versions_index.rb', line 134

def reload
  init_index(VersionsIndex.load_index_yaml(@index_file))
end

#remove_version(key) ⇒ Object


96
97
98
99
100
101
102
103
104
105
# 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

#saveObject


129
130
131
132
# File 'lib/cli/versions/versions_index.rb', line 129

def save
  create_directories
  VersionsIndex.write_index_yaml(@index_file, @data)
end

#select(&block) ⇒ Object


48
49
50
# File 'lib/cli/versions/versions_index.rb', line 48

def select(&block)
  @data['builds'].select(&block)
end

#to_sObject


117
118
119
# File 'lib/cli/versions/versions_index.rb', line 117

def to_s
  @data['builds'].to_s
end

#update_version(key, new_build) ⇒ Object


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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_stringsObject


113
114
115
# File 'lib/cli/versions/versions_index.rb', line 113

def version_strings
  @data['builds'].map { |_, build| build['version'].to_s }
end