Skip to content

Commit

Permalink
[API] Generator: Adds build hash from download artifacts to generated…
Browse files Browse the repository at this point in the history
… code
  • Loading branch information
picandocodigo committed Jun 24, 2022
1 parent 3f48c4b commit e688b8d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions elasticsearch-api/utils/thor/generate_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class SourceGenerator < Thor
method_option :tests, type: :boolean, default: false, desc: 'Generate test files'

def generate
@build_hash = File.read(File.expand_path('../../../tmp/rest-api-spec/build_hash',__dir__))
self.class.source_root File.expand_path(__dir__)
generate_source
# -- Tree output
Expand Down
5 changes: 4 additions & 1 deletion elasticsearch-api/utils/thor/templates/method.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

#
# Auto generated from build hash <%= @build_hash %>
# @see https://github.com/elastic/elasticsearch/tree/main/rest-api-spec
#
module Elasticsearch
<%= ' '*(@namespace_depth) %>module API
<%- @module_namespace.each_with_index do |name, i| -%>
Expand Down
23 changes: 13 additions & 10 deletions rake_tasks/elasticsearch_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace :elasticsearch do
end
end

def package_url(filename, build_hash)
def package_url(filename)
begin
artifacts = JSON.parse(File.read(filename))
rescue StandardError => e
Expand All @@ -52,16 +52,18 @@ namespace :elasticsearch do
end

build_hash_artifact = artifacts['version']['builds'].select do |build|
build.dig('projects', 'elasticsearch', 'commit_hash') == build_hash
build.dig('projects', 'elasticsearch', 'commit_hash') == @build_hash
end.first

unless build_hash_artifact
STDERR.puts "[!] Could not find artifact with build hash #{build_hash}, using latest instead"
STDERR.puts "[!] Could not find artifact with build hash #{@build_hash}, using latest instead"

build_hash_artifact = artifacts['version']['builds'].first
@build_hash = artifacts['version']['builds'].first['projects']['elasticsearch']['commit_hash']
end

# Dig into the elasticsearch packages, search for the rest-resources-zip package and return the URL:
build_hash_artifact.dig('projects', 'elasticsearch', 'packages').select { |k,v| k =~ /rest-resources-zip/ }.map { | _, v| v['url'] }.first
build_hash_artifact.dig('projects', 'elasticsearch', 'packages').select { |k, _| k =~ /rest-resources-zip/ }.map { |_, v| v['url'] }.first
end

def download_file!(url, filename)
Expand All @@ -73,8 +75,8 @@ namespace :elasticsearch do
end
puts "Successfully downloaded #{filename}"

unless File.exists?(filename)
STDERR.puts "[!] Couldn't download #{filename}"
unless File.exist?(filename)
warn "[!] Couldn't download #{filename}"
exit 1
end
rescue StandardError => e
Expand All @@ -88,8 +90,8 @@ namespace :elasticsearch do
unless (version_number = args[:version] || ENV['STACK_VERSION'])
# Get version number and build hash of running cluster:
version_number = cluster_info['number']
build_hash = cluster_info['build_hash']
puts "Build hash: #{build_hash}"
@build_hash = cluster_info['build_hash']
puts "Build hash: #{@build_hash}"
end

# Create ./tmp if it doesn't exist
Expand All @@ -100,7 +102,7 @@ namespace :elasticsearch do
download_file!(json_url, json_filename)

# Get the package url from the json file given the build hash
zip_url = package_url(json_filename, build_hash)
zip_url = package_url(json_filename)

# Download the zip file
filename = CURRENT_PATH.join("tmp/#{zip_url.split('/').last}")
Expand All @@ -112,6 +114,7 @@ namespace :elasticsearch do
puts "Unzipping file #{filename}"
`unzip -o #{filename} -d tmp/`
`rm #{filename}`
puts 'Artifacts downloaded in ./tmp'
puts "Artifacts downloaded in ./tmp, build hash #{@build_hash}"
File.write(CURRENT_PATH.join('tmp/rest-api-spec/build_hash'), @build_hash)
end
end

0 comments on commit e688b8d

Please sign in to comment.