@@ -74,36 +74,38 @@ namespace :es do
7474 end
7575
7676 # Deprecated
77- desc 'Download Elasticsearch artifacts (tests and REST spec) for currently running cluster '
77+ desc 'Download Elasticsearch artifacts (tests and REST spec) for a given version '
7878 task :download_artifacts , :version do |_ , args |
79- json_filename = CURRENT_PATH . join ( 'tmp/artifacts.json' )
79+ require 'net/http'
8080
81+ json_filename = CURRENT_PATH . join ( 'tmp/artifacts.json' )
8182 version_number = args [ :version ] || ENV [ 'STACK_VERSION' ] || version_from_buildkite || version_from_running_cluster
8283
8384 # Create ./tmp if it doesn't exist
8485 Dir . mkdir ( CURRENT_PATH . join ( 'tmp' ) , 0700 ) unless File . directory? ( CURRENT_PATH . join ( 'tmp' ) )
8586
86- # Download json file with package information for version:
87- json_url = "https://artifacts-api.elastic.co/v1/versions/#{ version_number } "
88- download_file! ( json_url , json_filename )
87+ # TODO: If required, bring back the functionality to download a specific build.
88+ # This was implemented in the previous version of the API, but research is needed to see how it
89+ # works in new API.
90+ #
91+ # Get the latest minor from version number, and get latest snapshot
92+ major_minor = version_number . split ( '.' ) [ 0 ..1 ] . join ( '.' )
93+ url = URI ( "https://artifacts-snapshot.elastic.co/elasticsearch/latest/#{ major_minor } .json" )
94+ manifest_url = JSON . parse ( Net ::HTTP . get ( url ) ) [ 'manifest_url' ]
95+ download_file! ( manifest_url , json_filename )
8996
9097 # Parse the downloaded JSON
9198 begin
9299 artifacts = JSON . parse ( File . read ( json_filename ) )
93100 rescue StandardError => e
94- STDERR . puts "[!] Couldn't read JSON file #{ json_filename } "
101+ warn "[!] Couldn't read JSON file #{ json_filename } \n #{ e . message } "
95102 exit 1
96103 end
97104
98- # Either find the artifacts for the exact same build hash from the current running cluster or
99- # use the first one from the list of builds:
100- build_hash_artifact = artifacts [ 'version' ] [ 'builds' ] . find do |build |
101- build . dig ( 'projects' , 'elasticsearch' , 'commit_hash' ) == @build_hash
102- end || artifacts [ 'version' ] [ 'builds' ] . first
103- zip_url = build_hash_artifact . dig ( 'projects' , 'elasticsearch' , 'packages' ) . select { |k , _ | k =~ /rest-resources-zip/ } . map { |_ , v | v [ 'url' ] } . first
104-
105- # Dig into the elasticsearch packages, search for the rest-resources-zip package and return the URL:
106- build_hash_artifact . dig ( 'projects' , 'elasticsearch' , 'packages' ) . select { |k , _ | k =~ /rest-resources-zip/ } . map { |_ , v | v [ 'url' ] } . first
105+ # Search the JSON for the rest-resources-zip file and get the URL
106+ packages = artifacts . dig ( 'projects' , 'elasticsearch' , 'packages' )
107+ rest_resources = packages . select { |k , v | k =~ /rest-resources/ }
108+ zip_url = rest_resources . map { |_ , v | v [ 'url' ] } . first
107109
108110 # Download the zip file
109111 filename = CURRENT_PATH . join ( "tmp/#{ zip_url . split ( '/' ) . last } " )
0 commit comments