Module: Bosh::Cli::PackagingHelper

Includes:
VersionCalc
Included in:
JobBuilder, PackageBuilder
Defined in:
lib/cli/packaging_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from VersionCalc

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

Instance Attribute Details

#dry_runObject

Returns the value of attribute dry_run.



11
12
13
# File 'lib/cli/packaging_helper.rb', line 11

def dry_run
  @dry_run
end

Instance Method Details

#buildObject



54
55
56
57
58
59
60
# File 'lib/cli/packaging_helper.rb', line 54

def build
  with_indent("  ") do
    use_final_version || use_dev_version || generate_tarball
  end
  upload_tarball(@tarball_path) if final? && !dry_run?
  @will_be_promoted = true if final? && dry_run? && @used_dev_version
end

#checksumObject



225
226
227
228
229
230
231
232
# File 'lib/cli/packaging_helper.rb', line 225

def checksum
  if @tarball_path && File.exists?(@tarball_path)
    file_checksum(@tarball_path)
  else
    raise RuntimeError,
          "cannot read checksum for not yet generated package/job"
  end
end

#dry_run?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/cli/packaging_helper.rb', line 22

def dry_run?
  @dry_run
end

#file_checksum(path) ⇒ Object



221
222
223
# File 'lib/cli/packaging_helper.rb', line 221

def file_checksum(path)
  Digest::SHA1.file(path).hexdigest
end

#final?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/cli/packaging_helper.rb', line 18

def final?
  @final
end

#generate_tarballObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/cli/packaging_helper.rb', line 148

def generate_tarball
  if final?
    err_message = "No matching build found for " +
        "`#{@name}' #{@artefact_type}.\n" +
        "Please consider creating a dev release first.\n" +
        "The fingerprint is `#{fingerprint}'."
    err(err_message)
  end

  current_final = @final_index.latest_version.to_i
  new_minor = minor_version(@dev_index.latest_version(current_final)) + 1

  version = "#{current_final}.#{new_minor}-dev"
  tmp_file = Tempfile.new(name)

  say("Generating...")

  copy_files

  in_build_dir do
    tar_out = `tar -chzf #{tmp_file.path} . 2>&1`
    unless $?.exitstatus == 0
      raise PackagingError, "Cannot create tarball: #{tar_out}"
    end
  end

  item = {
    "version" => version
  }

  unless dry_run?
    @dev_index.add_version(fingerprint, item, tmp_file.path)
    @tarball_path = @dev_index.filename(version)
  end

  @version = version
  @tarball_generated = true
  say("Generated version #{version}".make_green)
  true
end

#init_indicesObject



13
14
15
16
# File 'lib/cli/packaging_helper.rb', line 13

def init_indices
  @dev_index   = VersionsIndex.new(@dev_builds_dir)
  @final_index = VersionsIndex.new(@final_builds_dir)
end

#new_version?Boolean

Returns:

  • (Boolean)


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

def new_version?
  @tarball_generated || @promoted || @will_be_promoted
end

#notesObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cli/packaging_helper.rb', line 40

def notes
  notes = []

  if @will_be_promoted
    new_final_version = @final_index.latest_version.to_i + 1
    notes << "new final version #{new_final_version}"
  elsif new_version?
    notes << "new version"
  end

  notes << "older than latest" if older_version?
  notes
end

#older_version?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
# File 'lib/cli/packaging_helper.rb', line 30

def older_version?
  if @tarball_generated
    false
  elsif @used_final_version
    version_cmp(@version, @final_index.latest_version) < 0
  else
    version_cmp(@version, @dev_index.latest_version) < 0
  end
end

#tracked_permissions(path) ⇒ Object

Git doesn’t really track file permissions, it just looks at executable bit and uses 0755 if it’s set or 0644 if not. We have to mimic that behavior in the fingerprint calculation to avoid the situation where seemingly clean working copy would trigger new fingerprints for artifacts with changed permissions. Also we don’t want current fingerprints to change, hence the exact values below.



240
241
242
243
244
245
246
247
248
# File 'lib/cli/packaging_helper.rb', line 240

def tracked_permissions(path)
  if File.directory?(path)
    "40755"
  elsif File.executable?(path)
    "100755"
  else
    "100644"
  end
end

#upload_tarball(path) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/cli/packaging_helper.rb', line 189

def upload_tarball(path)
  item = @final_index[fingerprint]

  say("Uploading final version #{version}...")

  if item
    say("This package has already been uploaded")
    return
  end

  version = @final_index.latest_version.to_i + 1

  blobstore_id = nil
  File.open(path, "r") do |f|
    blobstore_id = @blobstore.create(f)
  end

  item = {
    "blobstore_id" => blobstore_id,
    "version" => version
  }

  say("Uploaded, blobstore id #{blobstore_id}")
  @final_index.add_version(fingerprint, item, path)
  @tarball_path = @final_index.filename(version)
  @version = version
  @promoted = true
  true
rescue Bosh::Blobstore::BlobstoreError => e
  raise BlobstoreError, "Blobstore error: #{e}"
end

#use_dev_versionObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cli/packaging_helper.rb', line 119

def use_dev_version
  say("Dev version:", "   ")
  item = @dev_index[fingerprint]

  if item.nil?
    say("NOT FOUND".make_red)
    return nil
  end

  version = item["version"]
  filename = @dev_index.filename(version)

  if File.exists?(filename)
    say("FOUND LOCAL".make_green)
  else
    say("TARBALL MISSING".make_red)
    return nil
  end

  if file_checksum(filename) == item["sha1"]
    @tarball_path = filename
    @version = version
    @used_dev_version = true
  else
    say("`#{name} (#{version})' tarball corrupted".make_red)
    return nil
  end
end

#use_final_versionObject



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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cli/packaging_helper.rb', line 62

def use_final_version
  say("Final version:", " ")

  item = @final_index[fingerprint]

  if item.nil?
    say("NOT FOUND".make_red)
    return nil
  end

  blobstore_id = item["blobstore_id"]
  version      = item["version"]

  if blobstore_id.nil?
    say("No blobstore id".make_red)
    return nil
  end

  filename = @final_index.filename(version)
  need_fetch = true

  if File.exists?(filename)
    say("FOUND LOCAL".make_green)
    if file_checksum(filename) == item["sha1"]
      @tarball_path = filename
      need_fetch = false
    else
      say("LOCAL CHECKSUM MISMATCH".make_red)
      need_fetch = true
    end
  end

  if need_fetch
    say("Downloading `#{name} (#{version})'...".make_green)
    tmp_file = File.open(File.join(Dir.mktmpdir, name), "w")
    @blobstore.get(blobstore_id, tmp_file)
    tmp_file.close
    if Digest::SHA1.file(tmp_file.path).hexdigest == item["sha1"]
      @tarball_path = @final_index.add_version(fingerprint,
                                               item,
                                               tmp_file.path)
    else
      err("`#{name}' (#{version}) is corrupted in blobstore " +
              "(id=#{blobstore_id}), " +
              "please remove it manually and re-generate the final release")
    end
  end

  @version = version
  @used_final_version = true
  true
rescue Bosh::Blobstore::NotFound => e
  raise BlobstoreError, "Final version of `#{name}' not found in blobstore"
rescue Bosh::Blobstore::BlobstoreError => e
  raise BlobstoreError, "Blobstore error: #{e}"
end