# Getting CookbookVersion at runtime

**URL:** <https://discourse.chef.io/t/getting-cookbookversion-at-runtime/5250>\
**Category:** Chef Infra (archive)\
**Created:** [April 24, 2014, 8:29pm UTC](https://discourse.chef.io/t/getting-cookbookversion-at-runtime/5250 "2014-04-24T20:29:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tucker](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@Tucker](https://discourse.chef.io/u/Tucker)\
**Post date:** [April 24, 2014, 8:29pm UTC](https://discourse.chef.io/t/getting-cookbookversion-at-runtime/5250/1 "2014-04-24T20:29:04Z")

</div>

Obligatory first line greeting that is apparently acceptable to ignore-

I have a need to determine a Chef cookbook version at runtime, so I can  
compare it with a local file but I haven’t found the secret sauce to make  
that work. Since I’ve run out of time that I can work on it until later  
today or tomorrow, I thought I’d put this out to the community. I thought  
there was something I could do with Chef::CookbookVersion but now I think  
that’s off track.

I have a hacky way that uses file system paths but that makes me kinda sad  
to use.

–

–tucker

---

<div class="post-metadata">

**Author:** ![Ranjib](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/ranjib/32/1040_2.png) [@Ranjib](https://discourse.chef.io/u/Ranjib)\
**Post date:** [April 24, 2014, 9:15pm UTC](https://discourse.chef.io/t/getting-cookbookversion-at-runtime/5250/2 "2014-04-24T21:15:14Z")

</div>

node.run\_context.cookbook\_collection[cookbook\_name].metadata.version

cool, isnt it?

On Thu, Apr 24, 2014 at 1:29 PM, Tucker [junk@gmail.com](mailto:junk@gmail.com) wrote:

> Obligatory first line greeting that is apparently acceptable to ignore-
> 
> I have a need to determine a Chef cookbook version at runtime, so I can  
> compare it with a local file but I haven't found the secret sauce to make  
> that work. Since I've run out of time that I can work on it until later  
> today or tomorrow, I thought I'd put this out to the community. I thought  
> there was something I could do with Chef::CookbookVersion but now I think  
> that's off track.
> 
> I have a hacky way that uses file system paths but that makes me kinda sad  
> to use.
> 
> --
> 
> --tucker

---

<div class="post-metadata">

**Author:** ![Elvin\_Abordo](https://avatars.discourse-cdn.com/v4/letter/e/4af34b/32.png) [@Elvin\_Abordo](https://discourse.chef.io/u/Elvin_Abordo)\
**Post date:** [April 24, 2014, 9:22pm UTC](https://discourse.chef.io/t/getting-cookbookversion-at-runtime/5250/3 "2014-04-24T21:22:14Z")

</div>

hm interesting. I had posed a similar question a couple of months ago and  
the response i got was from Julian Dunn to use his handler.[1].

Ranjib, how long has that been there?

[1][GitHub - juliandunn/cookbook\_versions\_handler: Development repository for Chef Cookbook Versions Handler](https://github.com/juliandunn/cookbook_versions_handler)

On Thu, Apr 24, 2014 at 5:15 PM, Ranjib Dey [dey.ranjib@gmail.com](mailto:dey.ranjib@gmail.com) wrote:

> node.run\_context.cookbook\_collection[cookbook\_name].metadata.version
> 
> cool, isnt it?
> 
> On Thu, Apr 24, 2014 at 1:29 PM, Tucker [junk@gmail.com](mailto:junk@gmail.com) wrote:
> 
> > Obligatory first line greeting that is apparently acceptable to ignore-
> > 
> > I have a need to determine a Chef cookbook version at runtime, so I can  
> > compare it with a local file but I haven't found the secret sauce to make  
> > that work. Since I've run out of time that I can work on it until later  
> > today or tomorrow, I thought I'd put this out to the community. I thought  
> > there was something I could do with Chef::CookbookVersion but now I think  
> > that's off track.
> > 
> > I have a hacky way that uses file system paths but that makes me kinda  
> > sad to use.
> > 
> > --
> > 
> > --tucker

--  
Elvin Abordo  
Mobile: (845) 475-8744

---

<div class="post-metadata">

**Author:** ![Ranjib](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/ranjib/32/1040_2.png) [@Ranjib](https://discourse.chef.io/u/Ranjib)\
**Post date:** [April 24, 2014, 9:38pm UTC](https://discourse.chef.io/t/getting-cookbookversion-at-runtime/5250/4 "2014-04-24T21:38:19Z")

</div>

pretty long i think. the handler uses the same api. cookbook\_name and  
recipe\_name are two attribute present in any chef recipe, and automatically  
populated with the current name while run\_context is the central object  
that ties together everything(including the cookbook collection) and  
available from node,, recipe etc.

given we are inside the current recipe, it assumes that the current recipe  
is already loaded and available in resource collection, i.e.  
node.run\_context.cookbook\_collection[cookbook\_name] is not nil. this will  
fail with chef-apply but should be safe within chef-solo or client runs

On Thu, Apr 24, 2014 at 2:22 PM, Elvin Abordo [elvin159@gmail.com](mailto:elvin159@gmail.com) wrote:

> hm interesting. I had posed a similar question a couple of months ago and  
> the response i got was from Julian Dunn to use his handler.[1].
> 
> Ranjib, how long has that been there?
> 
> [1][GitHub - juliandunn/cookbook\_versions\_handler: Development repository for Chef Cookbook Versions Handler](https://github.com/juliandunn/cookbook_versions_handler)
> 
> On Thu, Apr 24, 2014 at 5:15 PM, Ranjib Dey [dey.ranjib@gmail.com](mailto:dey.ranjib@gmail.com) wrote:
> 
> > node.run\_context.cookbook\_collection[cookbook\_name].metadata.version
> > 
> > cool, isnt it?
> > 
> > On Thu, Apr 24, 2014 at 1:29 PM, Tucker [junk@gmail.com](mailto:junk@gmail.com) wrote:
> > 
> > > Obligatory first line greeting that is apparently acceptable to ignore-
> > > 
> > > I have a need to determine a Chef cookbook version at runtime, so I can  
> > > compare it with a local file but I haven't found the secret sauce to make  
> > > that work. Since I've run out of time that I can work on it until later  
> > > today or tomorrow, I thought I'd put this out to the community. I thought  
> > > there was something I could do with Chef::CookbookVersion but now I think  
> > > that's off track.
> > > 
> > > I have a hacky way that uses file system paths but that makes me kinda  
> > > sad to use.
> > > 
> > > --
> > > 
> > > --tucker
> 
> --  
> Elvin Abordo  
> Mobile: (845) 475-8744

---

<div class="post-metadata">

**Author:** ![Tucker](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@Tucker](https://discourse.chef.io/u/Tucker)\
**Post date:** [April 24, 2014, 10:05pm UTC](https://discourse.chef.io/t/getting-cookbookversion-at-runtime/5250/5 "2014-04-24T22:05:34Z")

</div>

Awesome. That's much simpler than what we have been doing and avoids the  
issue of different version of Chef doing different things. For reference,  
this is what I just scrapped:

"""  
cookbook\_path = File.join(File.dirname(File.expand\_path( **FILE** )),  
'..')

```
  if File.exist?(File.join(cookbook_path, 'metadata.rb'))
    metadata_file = File.join(cookbook_path, 'metadata.rb')
    metadata = Chef::Cookbook::Metadata.new
    metadata.from_file(metadata_file)
  else
    metadata_file = File.join(cookbook_path, 'metadata.json')
    metadata = Chef::Cookbook::Metadata.new
    metadata.from_json(File.read(metadata_file))
  end

  @version = Chef::Version.new(metadata.version)

```

"""

On Thu, Apr 24, 2014 at 2:38 PM, Ranjib Dey [dey.ranjib@gmail.com](mailto:dey.ranjib@gmail.com) wrote:

> pretty long i think. the handler uses the same api. cookbook\_name and  
> recipe\_name are two attribute present in any chef recipe, and automatically  
> populated with the current name while run\_context is the central object  
> that ties together everything(including the cookbook collection) and  
> available from node,, recipe etc.
> 
> given we are inside the current recipe, it assumes that the current recipe  
> is already loaded and available in resource collection, i.e.  
> node.run\_context.cookbook\_collection[cookbook\_name] is not nil. this will  
> fail with chef-apply but should be safe within chef-solo or client runs
> 
> On Thu, Apr 24, 2014 at 2:22 PM, Elvin Abordo [elvin159@gmail.com](mailto:elvin159@gmail.com) wrote:
> 
> > hm interesting. I had posed a similar question a couple of months ago and  
> > the response i got was from Julian Dunn to use his handler.[1].
> > 
> > Ranjib, how long has that been there?
> > 
> > [1][GitHub - juliandunn/cookbook\_versions\_handler: Development repository for Chef Cookbook Versions Handler](https://github.com/juliandunn/cookbook_versions_handler)
> > 
> > On Thu, Apr 24, 2014 at 5:15 PM, Ranjib Dey [dey.ranjib@gmail.com](mailto:dey.ranjib@gmail.com) wrote:
> > 
> > > node.run\_context.cookbook\_collection[cookbook\_name].metadata.version
> > > 
> > > cool, isnt it?
> > > 
> > > On Thu, Apr 24, 2014 at 1:29 PM, Tucker [junk@gmail.com](mailto:junk@gmail.com) wrote:
> > > 
> > > > Obligatory first line greeting that is apparently acceptable to ignore-
> > > > 
> > > > I have a need to determine a Chef cookbook version at runtime, so I can  
> > > > compare it with a local file but I haven't found the secret sauce to make  
> > > > that work. Since I've run out of time that I can work on it until later  
> > > > today or tomorrow, I thought I'd put this out to the community. I thought  
> > > > there was something I could do with Chef::CookbookVersion but now I think  
> > > > that's off track.
> > > > 
> > > > I have a hacky way that uses file system paths but that makes me kinda  
> > > > sad to use.
> > > > 
> > > > --
> > > > 
> > > > --tucker
> > 
> > --  
> > Elvin Abordo  
> > Mobile: (845) 475-8744

--

--tucker

---

<div class="post-metadata">

**Author:** ![Lamont\_Granquist\_OLD](https://avatars.discourse-cdn.com/v4/letter/l/7feea3/32.png) [@Lamont\_Granquist\_OLD](https://discourse.chef.io/u/Lamont_Granquist_OLD)\
**Post date:** [April 24, 2014, 10:39pm UTC](https://discourse.chef.io/t/getting-cookbookversion-at-runtime/5250/6 "2014-04-24T22:39:53Z")

</div>

There's an obvious Recipe DSL enhancement to add cookbook\_version as a  
method that does that. PRs encouraged.

On 4/24/14, 2:15 PM, Ranjib Dey wrote:

> node.run\_context.cookbook\_collection[cookbook\_name].metadata.version
> 
> cool, isnt it?
> 
> On Thu, Apr 24, 2014 at 1:29 PM, Tucker \<[junk@gmail.com](mailto:junk@gmail.com)  
> [mailto:junk@gmail.com](mailto:junk@gmail.com)\> wrote:
> 
> ```
> Obligatory first line greeting that is apparently acceptable to
> ignore-
> 
> I have a need to determine a Chef cookbook version at runtime, so
> I can compare it with a local file but I haven't found the secret
> sauce to make that work. Since I've run out of time that I can
> work on it until later today or tomorrow, I thought I'd put this
> out to the community. I thought there was something I could do
> with Chef::CookbookVersion but now I think that's off track.
> 
> I have a hacky way that uses file system paths but that makes me
> kinda sad to use.
> 
> -- 
> 
> --tucker
> 
> ```
