Chef and ruby scripts

Hi,

I've been working on ruby/chef gem lately and ran into following tiny
tutorial

I was wondering if you have official tutorial that would embrace
implementation of Chef/knife into ruby scripts just like on the first code
snippet....

I'm asking so I won't have to "guess" the code and the syntax but to know
it well by using official literature.

If you have something like that please share. I'd appreciate it.

Regards,

J.

Does chef-apply fit your need?

Looks handy but not quite what I'm looking for

I was looking for something that looks like the snippet below... Where can
I find what is inside Chef:: and that syntax? Below you see Config and
Knife and Internet is very cheap with this kind of results :confused:

require("chef")
require("chef/knife")Chef::Config.from_file(knifePath) #knifePath is
declared a priori
cookbooks = Chef::Knife.run(["cookbook", "list"])
puts(cookbooks) # => nil

On Wed, Nov 6, 2013 at 1:32 AM, Brian Akins brian@akins.org wrote:

Does chef-apply fit your need?
http://www.opscode.com/blog/2013/02/06/chef-11-in-depth-client-improvements/

On Wednesday, November 6, 2013 at 1:08 AM, Jasna Benčić wrote:

Looks handy but not quite what I'm looking for

I was looking for something that looks like the snippet below... Where can I find what is inside Chef:: and that syntax? Below you see Config and Knife and Internet is very cheap with this kind of results :confused:

require("chef") require("chef/knife") Chef::Config.from_file(knifePath) #knifePath is declared a priori cookbooks = Chef::Knife.run(["cookbook", "list"]) puts(cookbooks) # => nil

The code in knife commands is optimized for being a command line utility. This means that many of the methods will not have useful return values, they’ll print output to the terminal instead, and they might call exit at any time. So I would recommend against trying to use knife commands directly within a script, since they do things that library code should not do. One exception is if you simply want to string a handful of commands together one after the other.

Knife includes the exec command which is a way to execute scripts with all of your knife configuration already loaded and with some helper functions included into the root context. This might be closer to what you want: http://docs.opscode.com/knife_exec.html

--
Daniel DeLeo

Jasna,

If you want to explore the Chef library code, there's a bunch of ways to do
that, like using pry gem 1

$ pry
1 pry(main)> require 'chef'
=> true
2 pry(main)> ls Chef
constants:
ApiClient Client CookbookLoader DataBag
EventDispatch Handler Mixin
Provider ResourceDefinitionList RunList ShellOut
WebUIUser
Certificate Config CookbookSynchronizer DataBagItem
Exceptions IndexQueue NIL_ARGUMENT
Recipe ResourceReporter Runner Util
Checksum Cookbook CookbookVersion
DelayedEvaluator FileAccessControl JSONCompat Node
Resource REST RunStatus
VERSION
ChecksumCache CookbookCacheCleaner CouchDB
EncryptedDataBagItem FileCache Log
OpenIDRegistration ResourceCollection Role
ScanAccessControl Version
CHEF_ROOT CookbookCollection Daemon Environment
Formatters MinimalCookbookVersion Platform
ResourceDefinition RunContext Search
VersionConstraint
Object.methods: yaml_tag
[3] pry(main)>

There's a ton of ways to use this to explore the code.

You can also hit up the RubyDoc page 2 for Chef, and see the generated
documentation from the code.

A lot depends on what you're looking to do - interact with the local
filesystem, or the Chef Server, etc.

There's plenty of recommendations around, and if you're looking to use
knife-specific interactions, they'd be in Chef::Knife, however from the
behavior you've described in the snippet, you might want to perform other
interactions with the Chef Server API.

YYMV,
-Mike

On Thu, Nov 7, 2013 at 12:12 PM, Daniel DeLeo dan@kallistec.com wrote:

On Wednesday, November 6, 2013 at 1:08 AM, Jasna Benčić wrote:

Looks handy but not quite what I'm looking for

I was looking for something that looks like the snippet below... Where
can I find what is inside Chef:: and that syntax? Below you see Config and
Knife and Internet is very cheap with this kind of results :confused:

require("chef")
require("chef/knife")Chef::Config.from_file(knifePath) #knifePath is declared a priori
cookbooks = Chef::Knife.run(["cookbook", "list"])
puts(cookbooks) # => nil

The code in knife commands is optimized for being a command line utility.
This means that many of the methods will not have useful return values,
they’ll print output to the terminal instead, and they might call exit at
any time. So I would recommend against trying to use knife commands
directly within a script, since they do things that library code should not
do. One exception is if you simply want to string a handful of commands
together one after the other.

Knife includes the exec command which is a way to execute scripts with
all of your knife configuration already loaded and with some helper
functions included into the root context. This might be closer to what you
want: knife exec

--
Daniel DeLeo