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



44
45
46
47
48
49
50
# File 'lib/cli/packaging_helper.rb', line 44

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



192
193
194
195
196
197
198
199
# File 'lib/cli/packaging_helper.rb', line 192

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)


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

def dry_run?
  @dry_run
end

#file_checksum(path) ⇒ Object



188
189
190
# File 'lib/cli/packaging_helper.rb', line 188

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

#final?Boolean

Returns:

  • (Boolean)


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

def final?
  @final
end

#generate_tarballObject



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
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/cli/packaging_helper.rb', line 120

def generate_tarball
  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
  }

  if final?
    # add version (with its validation) before adding sha1
    @final_index.add_version(fingerprint, item)
    @tarball_path = @final_storage.put_file(fingerprint, tmp_file.path)
    item['sha1'] = file_checksum(@tarball_path)
    @final_index.update_version(fingerprint, item)
  elsif dry_run?
  else
    # add version (with its validation) before adding sha1
    @dev_index.add_version(fingerprint, item)
    @tarball_path = @dev_storage.put_file(fingerprint, tmp_file.path)
    item['sha1'] = file_checksum(@tarball_path)
    @dev_index.update_version(fingerprint, item)
  end

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

#init_indicesObject



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

def init_indices
  @dev_index   = Versions::VersionsIndex.new(@dev_builds_dir)
  @dev_storage = Versions::LocalVersionStorage.new(@dev_builds_dir)

  @final_index = Versions::VersionsIndex.new(@final_builds_dir)
  @final_storage = Versions::LocalVersionStorage.new(@final_builds_dir)

  @final_resolver = Versions::VersionFileResolver.new(@final_storage, @blobstore)
end

#new_version?Boolean

Returns:

  • (Boolean)


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

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

#notesObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cli/packaging_helper.rb', line 31

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.



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

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

#upload_tarball(path) ⇒ Object



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

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

  unless item
    say("Failed to find entry `#{fingerprint}' in index, check local storage")
    return
  end

  if item['blobstore_id']
    return
  end

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

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

  say("Uploaded, blobstore id `#{blobstore_id}'")
  item['blobstore_id'] = blobstore_id
  @final_index.update_version(fingerprint, item)
  @promoted = true
  true
rescue Bosh::Blobstore::BlobstoreError => e
  raise BlobstoreError, "Blobstore error: #{e}"
end

#use_dev_versionObject



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

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

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

  version = @dev_index['version'] || fingerprint

  if !@dev_storage.has_file?(version)
    say('TARBALL MISSING'.make_red)
    return nil
  end

  say('FOUND LOCAL'.make_green)
  @tarball_path = @dev_storage.get_file(version)

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

  if final? && !dry_run?
    # copy from dev index/storage to final index/storage
    @final_index.add_version(fingerprint, item)
    @tarball_path = @final_storage.put_file(version, @tarball_path)
    item['sha1'] = Digest::SHA1.file(@tarball_path).hexdigest
    @final_index.update_version(fingerprint, item)
  end

  @version = version
  @used_dev_version = true
end

#use_final_versionObject



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

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'] || fingerprint
  sha1         = item['sha1']

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

  desc = "#{name} (#{version})"

  @tarball_path = @final_resolver.find_file(blobstore_id, sha1, version, "package #{desc}")

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