Class: Bosh::Cli::VersionsIndex

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

Instance Method Summary collapse

Methods included from VersionCalc

#major_version, #minor_version, #version_cmp, #version_greater, #version_less, #version_same

Constructor Details

#initialize(storage_dir, name_prefix = nil) ⇒ VersionsIndex

Returns a new instance of VersionsIndex.



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

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



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

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

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cli/versions_index.rb', line 54

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



88
89
90
91
92
# File 'lib/cli/versions_index.rb', line 88

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

#find_by_checksum(checksum) ⇒ Object



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

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(major = nil) ⇒ Object

Return the last version from the version index file as jobs and packages have a non-numeric versioning scheme based on their fingerprint. This function relies on hash insertion order being preserved. While this is the behavior for Ruby 1.9.X and 2.X we should remember that this implementation is a little brittle.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cli/versions_index.rb', line 36

def latest_version(major = nil)
  builds = @data["builds"].values

  if major
    builds = builds.select do |build|
      major_version(build["version"]) == major
    end
  end

  return nil if builds.empty?

  builds.last["version"]
end

#version_exists?(version) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/cli/versions_index.rb', line 50

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