Skip to content

Instantly share code, notes, and snippets.

@spuder
Created April 1, 2016 20:43
Show Gist options
  • Save spuder/32f803d439ccc2f10ef382ccd6bef6fa to your computer and use it in GitHub Desktop.
Save spuder/32f803d439ccc2f10ef382ccd6bef6fa to your computer and use it in GitHub Desktop.
A working, but ugly way to fetch from a url
ruby_block 'get build' do
block do
require "net/https"
require "uri"
require "json"
uri = URI.parse("http://nexus.example.com/nexus/service/local/repositories/foobar/content/master-5678.zip?describe=info")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request.initialize_http_header({ "Accept" => "application/json" })
response = http.request(request)
metadata = JSON.parse(response.body)
# metadata['data']['md5Hash'] is a string like "1234"
# Save the string to the node_state https://docs.chef.io/recipes.html#node-run-state
node.run_state['artifact_md5'] = metadata['data']['md5Hash']
end
action :run
end
@spuder
Copy link
Author

spuder commented Apr 1, 2016

The following works as a library

module Foo
  module Helper

    def nexus_md5
      require 'json'
      artifact_info = Chef::HTTP.new("http://nexus.example.com/nexus/service/local/repositories/foobar/content/master-5678.zip?describe=info")
      headers = { "Accept" => "application/json" }
      response_xml = artifact_info.request('get',artifact_info.url)
      response_json = JSON.parse( artifact_info.request('get', artifact_info.url, headers ) )
      return response_json['data']['md5Hash']
    end
  end
end

However it has a huge limitation in that remote_file only works with sha256 hashes. Your artifact repository would need to expose that metadata aswell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment