# Running chef-client programatically

**URL:** <https://discourse.chef.io/t/running-chef-client-programatically/5829>\
**Category:** Chef Infra (archive)\
**Created:** [October 19, 2014, 10:56am UTC](https://discourse.chef.io/t/running-chef-client-programatically/5829 "2014-10-19T10:56:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brauliobo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/brauliobo/32/109_2.png) [@brauliobo](https://discourse.chef.io/u/brauliobo)\
**Post date:** [October 19, 2014, 10:56am UTC](https://discourse.chef.io/t/running-chef-client-programatically/5829/1 "2014-10-19T10:56:49Z")

</div>

Hello all,

I building an app to control shared hosting deploys using chef and for it  
to work I would like to run chef-client inside this ruby app to be able to  
know and log exactly what chef-client is doing and what feedback it is  
giving. Is there a way to do so? Are there docs?

Cheers  
Braulio

--  
"Lute pela sua ideologia. Seja um com sua ideologia. Viva pela sua  
ideologia. Morra por sua ideologia" P.R. Sarkar

EITA - Educação, Informação e Tecnologias para Autogestão  
[http://cirandas.net/brauliobo](http://cirandas.net/brauliobo)  
[http://eita.org.br](http://eita.org.br)

"Paramapurusha é meu pai e Parama Prakriti é minha mãe. O universo é meu  
lar e todos nós somos cidadãos deste cosmo. Este universo é a imaginação da  
Mente Macrocósmica, e todas as entidades estão sendo criadas, preservadas e  
destruídas nas fases de extroversão e introversão do fluxo imaginativo  
cósmico. No âmbito pessoal, quando uma pessoa imagina algo em sua mente,  
naquele momento, essa pessoa é a única proprietária daquilo que ela  
imagina, e ninguém mais. Quando um ser humano criado mentalmente caminha  
por um milharal também imaginado, a pessoa imaginada não é a propriedade  
desse milharal, pois ele pertence ao indivíduo que o está imaginando. Este  
universo foi criado na imaginação de Brahma, a Entidade Suprema, por isso  
a propriedade deste universo é de Brahma, e não dos microcosmos que também  
foram criados pela imaginação de Brahma. Nenhuma propriedade deste mundo,  
mutável ou imutável, pertence a um indivíduo em particular; tudo é o  
patrimônio comum de todos."  
Restante do texto em

> **[A problemática de hoje em dia - Bráulio Bhavamitra](http://cirandas.net/brauliobo/blog/a-problematica-de-hoje-em-dia)**
>
> "Paramapurusha é meu pai e Parama Prakriti é minha mãe. O universo é meu lar e todos nós somos cidadãos deste cosmo. Este universo é a imaginação da Mente Macrocósmica, e todas as entidades estão sendo criadas, preservadas e destruídas nas fases de...

---

<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:** [October 19, 2014, 4:40pm UTC](https://discourse.chef.io/t/running-chef-client-programatically/5829/2 "2014-10-19T16:40:05Z")

</div>

yeah, you can do that, take a look at the client code[1]. Do you want a  
client-sever like chef run or solo runs? There are three basic things you  
have to do  
a) Configure chef, either via Chef::Config.from\_file, or directly set  
Chef::Config[:key], where key is node\_name, client\_key and chef\_server\_url  
b) build the node & run\_context , this involves generating the ohai data,  
pass it to a node, use that node to build a run\_context  
c) assign run\_list and do a converge

you can see similar codes here[2], [3]

note, this means you have to run your chef-client as root (not a strict  
requirement, but if you are manipulating users, packages, services etc,  
you'll need this).

regards  
ranjib

[1][https://github.com/opscode/chef/blob/master/lib/chef/client.rb#L348](https://github.com/opscode/chef/blob/master/lib/chef/client.rb#L348)  
[2]  
[https://github.com/ranjib/chef-lxc/blob/master/lib/chef/application/lxc.rb#L86](https://github.com/ranjib/chef-lxc/blob/master/lib/chef/application/lxc.rb#L86)  
[3]  
[https://github.com/ranjib/chef-stage/blob/master/libraries/staged\_chef\_client.rb#L34](https://github.com/ranjib/chef-stage/blob/master/libraries/staged_chef_client.rb#L34)

2014-10-19 3:56 GMT-07:00 Bráulio Bhavamitra [brauliobo@gmail.com](mailto:brauliobo@gmail.com):

> Hello all,
> 
> I building an app to control shared hosting deploys using chef and for it  
> to work I would like to run chef-client inside this ruby app to be able to  
> know and log exactly what chef-client is doing and what feedback it is  
> giving. Is there a way to do so? Are there docs?
> 
> Cheers  
> Braulio
> 
> --  
> "Lute pela sua ideologia. Seja um com sua ideologia. Viva pela sua  
> ideologia. Morra por sua ideologia" P.R. Sarkar
> 
> EITA - Educação, Informação e Tecnologias para Autogestão  
> [http://cirandas.net/brauliobo](http://cirandas.net/brauliobo)  
> [http://eita.org.br](http://eita.org.br)
> 
> "Paramapurusha é meu pai e Parama Prakriti é minha mãe. O universo é meu  
> lar e todos nós somos cidadãos deste cosmo. Este universo é a imaginação da  
> Mente Macrocósmica, e todas as entidades estão sendo criadas, preservadas e  
> destruídas nas fases de extroversão e introversão do fluxo imaginativo  
> cósmico. No âmbito pessoal, quando uma pessoa imagina algo em sua mente,  
> naquele momento, essa pessoa é a única proprietária daquilo que ela  
> imagina, e ninguém mais. Quando um ser humano criado mentalmente caminha  
> por um milharal também imaginado, a pessoa imaginada não é a propriedade  
> desse milharal, pois ele pertence ao indivíduo que o está imaginando. Este  
> universo foi criado na imaginação de Brahma, a Entidade Suprema, por isso  
> a propriedade deste universo é de Brahma, e não dos microcosmos que também  
> foram criados pela imaginação de Brahma. Nenhuma propriedade deste mundo,  
> mutável ou imutável, pertence a um indivíduo em particular; tudo é o  
> patrimônio comum de todos."  
> Restante do texto em  
> [A problemática de hoje em dia - Bráulio Bhavamitra](http://cirandas.net/brauliobo/blog/a-problematica-de-hoje-em-dia)

---

<div class="post-metadata">

**Author:** ![kallistec](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kallistec/32/23_2.png) [@kallistec](https://discourse.chef.io/u/kallistec)\
**Post date:** [October 20, 2014, 5:09pm UTC](https://discourse.chef.io/t/running-chef-client-programatically/5829/3 "2014-10-20T17:09:31Z")

</div>

To get information from chef (client or solo) about what it’s doing, you can use the event dispatch system (via a plugin you’ll need to write). The dispatcher code showing all the events is here: [https://github.com/opscode/chef/blob/9b3e925188a41bbea954429ac81ffdf65e936eda/lib/chef/event\_dispatch/base.rb](https://github.com/opscode/chef/blob/9b3e925188a41bbea954429ac81ffdf65e936eda/lib/chef/event_dispatch/base.rb) and one example of using it is the ResourceReporter class that is used by Chef’s analytics product, here: [https://github.com/opscode/chef/blob/master/lib/chef/resource\_reporter.rb](https://github.com/opscode/chef/blob/master/lib/chef/resource_reporter.rb) You can see an external example of this here: [GitHub - theforeman/chef-handler-foreman: This handler allow Chef to send reports to Foreman](https://github.com/theforeman/chef-handler-foreman)

Jon Cowie covers this a little bit in his ChefConf 2104 talk, which you can see here: [https://www.youtube.com/watch?v=vVVPWMHJdvg&list=PL11cZfNdwNyMmx0msapJfuGsLV43C7XsA&index=65](https://www.youtube.com/watch?v=vVVPWMHJdvg&list=PL11cZfNdwNyMmx0msapJfuGsLV43C7XsA&index=65) It’s also covered in his book: [Customizing Chef [Book]](http://shop.oreilly.com/product/0636920032984.do)

If you create a custom reporter that uses IPC, then you don’t need to worry about controlling the chef run from code, which would make some parts of your task simpler.

--  
Daniel DeLeo

On Sunday, October 19, 2014 at 9:40 AM, Ranjib Dey wrote:

> yeah, you can do that, take a look at the client code[1]. Do you want a client-sever like chef run or solo runs? There are three basic things you have to do  
> a) Configure chef, either via Chef::Config.from\_file, or directly set Chef::Config[:key], where key is node\_name, client\_key and chef\_server\_url  
> b) build the node & run\_context , this involves generating the ohai data, pass it to a node, use that node to build a run\_context  
> c) assign run\_list and do a converge
> 
> you can see similar codes here[2], [3]
> 
> note, this means you have to run your chef-client as root (not a strict requirement, but if you are manipulating users, packages, services etc, you'll need this).
> 
> regards  
> ranjib
> 
> [1][https://github.com/opscode/chef/blob/master/lib/chef/client.rb#L348](https://github.com/opscode/chef/blob/master/lib/chef/client.rb#L348)  
> [2][https://github.com/ranjib/chef-lxc/blob/master/lib/chef/application/lxc.rb#L86](https://github.com/ranjib/chef-lxc/blob/master/lib/chef/application/lxc.rb#L86)  
> [3][https://github.com/ranjib/chef-stage/blob/master/libraries/staged\_chef\_client.rb#L34](https://github.com/ranjib/chef-stage/blob/master/libraries/staged_chef_client.rb#L34)
> 
> 2014-10-19 3:56 GMT-07:00 Bráulio Bhavamitra \<[brauliobo@gmail.com](mailto:brauliobo@gmail.com) ([mailto:brauliobo@gmail.com](mailto:brauliobo@gmail.com))\>:
> 
> > Hello all,
> > 
> > I building an app to control shared hosting deploys using chef and for it to work I would like to run chef-client inside this ruby app to be able to know and log exactly what chef-client is doing and what feedback it is giving. Is there a way to do so? Are there docs?
> > 
> > Cheers  
> > Braulio
> > 
> > --  
> > "Lute pela sua ideologia. Seja um com sua ideologia. Viva pela sua ideologia. Morra por sua ideologia" P.R. Sarkar
> > 
> > EITA - Educação, Informação e Tecnologias para Autogestão  
> > [Blog - Bráulio Bhavamitra](http://cirandas.net/brauliobo)  
> > [http://eita.org.br](http://eita.org.br) ([http://eita.org.br/](http://eita.org.br/))
> > 
> > "Paramapurusha é meu pai e Parama Prakriti é minha mãe. O universo é meu lar e todos nós somos cidadãos deste cosmo. Este universo é a imaginação da Mente Macrocósmica, e todas as entidades estão sendo criadas, preservadas e destruídas nas fases de extroversão e introversão do fluxo imaginativo cósmico. No âmbito pessoal, quando uma pessoa imagina algo em sua mente, naquele momento, essa pessoa é a única proprietária daquilo que ela imagina, e ninguém mais. Quando um ser humano criado mentalmente caminha por um milharal também imaginado, a pessoa imaginada não é a propriedade desse milharal, pois ele pertence ao indivíduo que o está imaginando. Este universo foi criado na imaginação de Brahma, a Entidade Suprema, por isso a propriedade deste universo é de Brahma, e não dos microcosmos que também foram criados pela imaginação de Brahma. Nenhuma propriedade deste mundo, mutável ou imutável, pertence a um indivíduo em particular; tudo é o patrimônio comum de todos."  
> > Restante do texto em [A problemática de hoje em dia - Bráulio Bhavamitra](http://cirandas.net/brauliobo/blog/a-problematica-de-hoje-em-dia)
