Module: Bosh::Cli::PackagingHelper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dry_runObject

Returns the value of attribute dry_run.



7
8
9
# File 'lib/cli/packaging_helper.rb', line 7

def dry_run
  @dry_run
end

Instance Method Details

#buildObject



39
40
41
42
43
44
45
# File 'lib/cli/packaging_helper.rb', line 39

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



207
208
209
210
211
212
213
214
# File 'lib/cli/packaging_helper.rb', line 207

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)


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

def dry_run?
  @dry_run
end

#file_checksum(path) ⇒ Object



203
204
205
# File 'lib/cli/packaging_helper.rb', line 203

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

#final?Boolean

Returns:

  • (Boolean)


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

def final?
  @final
end

#generate_tarballObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/cli/packaging_helper.rb', line 133

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

  version = fingerprint
  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



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

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

#new_version?Boolean

Returns:

  • (Boolean)


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

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

#notesObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cli/packaging_helper.rb', line 26

def notes
  notes = []

  if @will_be_promoted
    new_final_version = @version
    notes << "new final version #{new_final_version}"
  elsif new_version?
    notes << 'new version'
  end

  notes
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.



222
223
224
225
226
227
228
229
230
# File 'lib/cli/packaging_helper.rb', line 222

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

#upload_tarball(path) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/cli/packaging_helper.rb', line 171

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 = fingerprint

  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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/cli/packaging_helper.rb', line 104

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

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

  version = fingerprint
  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



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
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
# File 'lib/cli/packaging_helper.rb', line 47

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      = fingerprint

  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