Class: Bosh::Cli::VersionsIndex

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

Instance Method Summary collapse

Constructor Details

#initialize(storage_dir, name_prefix = nil) ⇒ VersionsIndex

Returns a new instance of VersionsIndex.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/cli/versions_index.rb', line 4

def initialize(storage_dir, name_prefix = nil)
  @storage_dir = File.expand_path(storage_dir)
  @index_file  = File.join(@storage_dir, "index.yml")
  @name_prefix = name_prefix

  if File.file?(@index_file)
    init_index(load_yaml_file(@index_file, nil))
  else
    init_index({})
  end
end

Instance Method Details

#[](fingerprint) ⇒ Object



23
24
25
# File 'lib/cli/versions_index.rb', line 23

def [](fingerprint)
  @data["builds"][fingerprint]
end

#add_version(fingerprint, item, tmp_file_path = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cli/versions_index.rb', line 40

def add_version(fingerprint, item, tmp_file_path = nil)
  version = item["version"]

  if version.blank?
    raise InvalidIndex,
          "Cannot save index entry without knowing its version"
  end

  create_directories

  if tmp_file_path
    FileUtils.cp(tmp_file_path, filename(version), :preserve => true)
  end

  @data["builds"].each_pair do |fp, build|
    if build["version"] == version && fp != fingerprint
      raise "Trying to add duplicate version `#{version}' " +
                "into index `#{@index_file}'"
    end
  end

  @data["builds"][fingerprint] = item
  if tmp_file_path
    file_sha1 = Digest::SHA1.file(tmp_file_path).hexdigest
    @data["builds"][fingerprint]["sha1"] = file_sha1
  end

  File.open(@index_file, "w") do |f|
    f.write(Psych.dump(@data))
  end

  File.expand_path(filename(version))
end

#filename(version) ⇒ Object



74
75
76
77
78
# File 'lib/cli/versions_index.rb', line 74

def filename(version)
  name = @name_prefix.blank? ?
      "#{version}.tgz" : "#{@name_prefix}-#{version}.tgz"
  File.join(@storage_dir, name)
end

#find_by_checksum(checksum) ⇒ Object



16
17
18
19
20
21
# File 'lib/cli/versions_index.rb', line 16

def find_by_checksum(checksum)
  @data["builds"].each_pair do |fingerprint, build_data|
    return build_data if build_data["sha1"] == checksum
  end
  nil
end

#latest_version ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/cli/versions_index.rb', line 27

def latest_version
  builds = @data["builds"].values

  return nil if builds.empty?

  version_strings = builds.map { |b| b["version"] }
  Bosh::Common::Version::ReleaseVersion.parse_list(version_strings).latest.to_s
end

#version_exists?(version) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/cli/versions_index.rb', line 36

def version_exists?(version)
  File.exists?(filename(version))
end

#versions ⇒ Object



80
81
82
# File 'lib/cli/versions_index.rb', line 80

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