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.



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

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



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

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

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



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
87
88
89
# File 'lib/cli/versions_index.rb', line 57

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 version_cmp(build["version"], version) == 0 && 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(YAML.dump(@data))
  end

  File.expand_path(filename(version))
end

#filename(version) ⇒ Object



91
92
93
94
95
# File 'lib/cli/versions_index.rb', line 91

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

#find_by_checksum(checksum) ⇒ Object



19
20
21
22
23
24
# File 'lib/cli/versions_index.rb', line 19

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cli/versions_index.rb', line 30

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?

  sorted = builds.sort do |build1, build2|
    cmp = version_cmp(build2["version"], build1["version"])
    if cmp == 0
      raise "There is a duplicate version `#{build1["version"]}' " +
              "in index `#{@index_file}'"
    end
    cmp
  end

  sorted[0]["version"]
end

#version_exists?(version) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/cli/versions_index.rb', line 53

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