From e688b8dcba201a3d5ac8bf4a3de495a26d9165ff Mon Sep 17 00:00:00 2001 From: Fernando Briano Date: Wed, 22 Jun 2022 09:19:26 +0100 Subject: [PATCH] [API] Generator: Adds build hash from download artifacts to generated code --- .../utils/thor/generate_source.rb | 1 + .../utils/thor/templates/method.erb | 5 +++- rake_tasks/elasticsearch_tasks.rake | 23 +++++++++++-------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/elasticsearch-api/utils/thor/generate_source.rb b/elasticsearch-api/utils/thor/generate_source.rb index 597a8f69e3..f25a7cf4ca 100644 --- a/elasticsearch-api/utils/thor/generate_source.rb +++ b/elasticsearch-api/utils/thor/generate_source.rb @@ -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 diff --git a/elasticsearch-api/utils/thor/templates/method.erb b/elasticsearch-api/utils/thor/templates/method.erb index ba49963edb..124330c776 100644 --- a/elasticsearch-api/utils/thor/templates/method.erb +++ b/elasticsearch-api/utils/thor/templates/method.erb @@ -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| -%> diff --git a/rake_tasks/elasticsearch_tasks.rake b/rake_tasks/elasticsearch_tasks.rake index f22a08f4f0..c567afbb91 100644 --- a/rake_tasks/elasticsearch_tasks.rake +++ b/rake_tasks/elasticsearch_tasks.rake @@ -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 @@ -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) @@ -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 @@ -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 @@ -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}") @@ -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