jeudi 14 juillet 2016

create node attribute/return value in custom resource for chef

I have a chef resource that needs to return a version. I looked up and fund the best way to publish it as a node attribute. Here is the resource code(dj_artifactory_version) :

require "open-uri"
require "json"


def whyrun_supported?
  true
end

def get_version(name, user, pass, type, organization, art_module, repos, version)
  if (type.match(/snapshot$/i) and version.match(/latest$/i))
     string_object = open("http://ift.tt/29GeDC5}", :http_basic_authentication=>["#{user}", "#{pass}"], :ssl_verify_mode=>OpenSSL::SSL::VERIFY_NONE)
     json_file = JSON.parse(string_object.read)
     version_array = Array.new
     json_file["results"].each do |version|
       version_array.push(version["version"])
     end
     unique_versions=(version_array.uniq).max
     node.set['artifact']['snapshot']['latest'] = unique_versions

Now I use this chef resource in my recipe to get the version :

dj_artifactory_version "test" do
  type "snapshot" # options - snapshot/release
  organization "djcm.billing.api.admin" # layout.organization in artifactory properties.
  modules "paypal" # layout.properties in artifactory properties.
  repos  "djcm-zip-local" # repository name in artifactory
  version "latest" #latest/oldest
end

p "#node{['artifact']['snapshot']['latest']}"

I create default['artifact']['snapshot']['latest'] in default.rb with a value but here even after I run my recipe the old value doesn't change. Interestingly when I print the same in my resource, it print the node with the new value.

What am I doing wrong and is there a better way to publish a value using your own resource ?

Aucun commentaire:

Enregistrer un commentaire