Skip to content
This repository has been archived by the owner on May 9, 2020. It is now read-only.

Add cookbook support #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add cookbook support #20

wants to merge 1 commit into from

Conversation

gmr
Copy link

@gmr gmr commented Jul 20, 2013

This adds a class for dealing with cookbooks. Unfortunately an annoying nit is that due to the class level attributes attribute, we can not use the attributes attribute for cookbook attributes.

Please let me know if you want anything else for inclusion.

Examples:

for cookbook in chef.Cookbook.list():
... print cookbook
...
build-essential
riak-ssl
meetme-service
sysctl
runit

>>> chef.Cookbook.versions('mycookbook')
[u'0.8.5', u'0.8.6', u'0.8.7', u'0.8.8', u'0.9.0', u'0.9.1', u'0.9.10', u'0.9.11', u'0.9.12', u'0.9.13', u'0.9.2', u'0.9.3', u'0.9.4', u'0.9.5', u'0.9.6', u'0.9.7', u'0.9.8', u'0.9.9']

>>> c = chef.Cookbook('rabbitmq')
>>> c.metadata
{u'conflicting': {}, u'maintainer': u'Opscode, Inc.', u'description': u'Installs and configures RabbitMQ server', u'replacing': {}, u'suggestions': {}, u'maintainer_email': u'[email protected]', u'platforms': {u'scientific': u'>= 0.0.0', u'centos': u'>= 0.0.0', u'amazon': u'>= 0.0.0', u'smartos': u'>= 0.0.0', u'linuxmint': u'>= 0.0.0', u'redhat': u'>= 0.0.0', u'ubuntu': u'>= 0.0.0', u'oracle': u'>= 0.0.0', u'debian': u'>= 0.0.0', u'fedora': u'>= 0.0.0'}, u'dependencies': {u'erlang': u'>= 0.9'}, ...

An annoying nit is that due to the class level attributes attribute, we can not use the attributes attribute for cookbook attributes.
@gliptak gliptak mentioned this pull request May 3, 2016
@wzooff
Copy link

wzooff commented Feb 13, 2019

Hi :) one question - when it will be merged?

@innomation
Copy link

@coderanger I know this PR is old, but it would be nice to support attribute search results. Is there any plan on adding attribute search results to pyChef?

@aryn-lacy
Copy link

@innomation Could you clarify what you are asking for as this PR is for search cookbooks.

@innomation
Copy link

@aryn-lacy Thanks for the reply.

I was hoping to use pyChef to gather the attributes from a node search (in my case, recipe attribute). I couldn't find anything in the pyChef docs about displaying attributes, and sthe only thing online I found was to use bash command to load the attributes. I'd prefer to stay away from using bash/subprocess within my python code if possible.

Any idea if this will be implemented in the future?

@aryn-lacy
Copy link

It may already be implemented but I am attempting to maintain a new fork with all these PRs eventually pulled in. Could you add the bash commands here? I may be able to help you with your code to get this to work.

@innomation
Copy link

innomation commented Apr 24, 2020

I haven't seen any documentation for pulling up particular search node attributes in pyChef. I'm currently pulling a list of hosts from Chef, and then applying the attributes argument to the knife search node command using subprocess.

My script in gathering chef nodes looks like this, based on a query that defines the operating system. It would be ideal if I could add the attributes argument within the chef.Search function. I've tried injecting '-a recipes' a number of ways in the chef.Search function, but none of it works.

import chef

def get_hosts(os,os_ver):
    node_list = []
    nodes = chef.Search('node', 'platform:*{}* AND platform_version:*{}*'.format(os, os_ver))
    node_list = [node['name'] for node in nodes]
    return node_list

I've omitted some of the following script, but am posting to show the snippet on how I'm grabbing the attributes using subprocess from a list of hosts I've gathered in chef.Search function.

def get_cookbooks():
     for node in get_hosts():
         # retrieve recipes from queried nodes
         cmd = shlex.split(("knife search node fqdn:{} -a recipes").format(node))
         process = subprocess.run(cmd, stdout=subprocess.PIPE, universal_newlines=True, stderr=subprocess.STDOUT)

It would be ideal to use the -a option in chef.Search to define which attribute I would like to gather. As far as I'm aware, I don't know of any way to do so in pyChef.

@aryn-lacy
Copy link

Ok so you can absolutely do this already with the library. The recipes attribute is an automatic attribute and accessible via the node object.

import chef

 def get_hosts(os,os_ver):
     node_list = []
     return chef.Search('node', 'platform:*{}* AND platform_version:*{}*'.format(os, os_ver))

def get_cookbooks():
    for node in get_centos_hosts():
        print(node['automatic']['recipes'])

@innomation
Copy link

Thank you @aryn-lacy ! I've made a minor edit to my pasted code, as there were some mistakes, but after making the correct changes with your code, I'm retrieving the node recipes. For my understanding, is the ['automatic'] nodes attribute you've used based on the attribute identified by Ohai at the start of a Chef Infra Client run?

@aryn-lacy
Copy link

Automatic attributes are Ohai attributes but also any other attributes that chef automatically adds like the run_list

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants