How to parse knife.rb


#1

Hi,

Sorry if this is not the best forum for my question, but it seems to be more appropriate than the regular Chef’s user list.

I’m the author of vagrant-butcher [0], a Vagrant plugin that deletes node and client when the VM is destroyed.

The way the plugin works is by reading knife’s configuration and then using Chef::REST to do the deletions.

This has to change now, though. It’s not possible to install the plugin on the latest version of Vagrant because it requires a version of net-ssh that conflicts with Chef’s. [1]

What I’m trying to do now is to parse knife.rb so that I can feed Ridley [2] the constructor parameters it needs to create a connection to Chef’s API, but this is proving to be quite a challenge.

Has anyone done it before? Does anyone have any pointers to the best way of doing that?

I’ve been experimenting with evaluating the code in knife.rb, but it’s (1) an ugly hack, and (2) not working :slight_smile:

Thanks!

  • cassiano

[0] https://github.com/cassianoleal/vagrant-butcher
[1] https://github.com/cassianoleal/vagrant-butcher/issues/5
[2] https://github.com/reset/ridley


#2

If you don’t care about validity, just take chef/config.rb, and then:

Chef::Config.from_file

And you’re golden. No deps.

Sent from my iPhone

On Mar 21, 2013, at 7:11 PM, “Cassiano Leal” cassianoleal@gmail.com wrote:

Hi,

Sorry if this is not the best forum for my question, but it seems to be more appropriate than the regular Chef’s user list.

I’m the author of vagrant-butcher [0], a Vagrant plugin that deletes node and client when the VM is destroyed.

The way the plugin works is by reading knife’s configuration and then using Chef::REST to do the deletions.

This has to change now, though. It’s not possible to install the plugin on the latest version of Vagrant because it requires a version of net-ssh that conflicts with Chef’s. [1]

What I’m trying to do now is to parse knife.rb so that I can feed Ridley [2] the constructor parameters it needs to create a connection to Chef’s API, but this is proving to be quite a challenge.

Has anyone done it before? Does anyone have any pointers to the best way of doing that?

I’ve been experimenting with evaluating the code in knife.rb, but it’s (1) an ugly hack, and (2) not working :slight_smile:

Thanks!

  • cassiano

[0] https://github.com/cassianoleal/vagrant-butcher
[1] https://github.com/cassianoleal/vagrant-butcher/issues/5
[2] https://github.com/reset/ridley


#3

What do you mean no deps? I need Chef for that, right? Or is Chef::Config in vagrant?

  • cassiano

On Thursday, March 21, 2013 at 23:23, Adam Jacob wrote:

If you don’t care about validity, just take chef/config.rb, and then:

Chef::Config.from_file

And you’re golden. No deps.

Sent from my iPhone

On Mar 21, 2013, at 7:11 PM, “Cassiano Leal” <cassianoleal@gmail.com (mailto:cassianoleal@gmail.com)> wrote:

Hi,

Sorry if this is not the best forum for my question, but it seems to be more appropriate than the regular Chef’s user list.

I’m the author of vagrant-butcher [0], a Vagrant plugin that deletes node and client when the VM is destroyed.

The way the plugin works is by reading knife’s configuration and then using Chef::REST to do the deletions.

This has to change now, though. It’s not possible to install the plugin on the latest version of Vagrant because it requires a version of net-ssh that conflicts with Chef’s. [1]

What I’m trying to do now is to parse knife.rb so that I can feed Ridley [2] the constructor parameters it needs to create a connection to Chef’s API, but this is proving to be quite a challenge.

Has anyone done it before? Does anyone have any pointers to the best way of doing that?

I’ve been experimenting with evaluating the code in knife.rb, but it’s (1) an ugly hack, and (2) not working :slight_smile:

Thanks!

  • cassiano

[0] https://github.com/cassianoleal/vagrant-butcher
[1] https://github.com/cassianoleal/vagrant-butcher/issues/5
[2] https://github.com/reset/ridley


#4

We don’t provide opscode credentials to all of our developers but everyone runs vagrant so I decided to implement this using the key contained in the VM instead of parsing (in their case, a non-existant) knife.rb. This way developers can destroy their node/client without our intervention. It’s inserted just before Vagrant::Action::VM::Halt. The same code is used in our acceptance tests for cleaning up vSphere VMs and eventually EC2 nodes.

module Middleware
class ChefDestroy

def initialize(app, env)
@app = app
@env = env
end

def call(env)
delete_node!
delete_client!
@app.call(@env)
end

def self.spice
@spice_connection || @spice_connection = ::Spice::Connection.new({
server_url: ‘https://manage.opscode.com/organizations/example.com’,
client_name: client_name,
client_key: client_key,
chef_version: ‘10.16.2’
})
end

def self.client_key
begin
@env[:vm].channel.sudo(“cat /etc/chef/client.pem”) do |return_code, data|
return data.to_s if data
end
rescue Vagrant::Errors::VagrantError => e
end
false
end

def self.client_name

This actually comes from that external configuration object as well

'whatever.example.com
end

def self.delete_node!
spice.delete("/nodes/#{client_name}")
end

def self.delete_client!
spice.delete("/clients/#{client_name}")
end
end
end

::Vagrant.actions[:destroy].insert_before(Vagrant::Action::VM::Halt, Middleware::ChefDestroy)

This isn’t very close to the actual code we use as our Vagrant additions are very specific to our org but hopefully it’s enough to give you some ideas if you decide to implement multiple strategies for removing client/node.

Dan

On Thursday, March 21, 2013 at 7:10 PM, Cassiano Leal wrote:

Hi,

Sorry if this is not the best forum for my question, but it seems to be more appropriate than the regular Chef’s user list.

I’m the author of vagrant-butcher [0], a Vagrant plugin that deletes node and client when the VM is destroyed.

The way the plugin works is by reading knife’s configuration and then using Chef::REST to do the deletions.

This has to change now, though. It’s not possible to install the plugin on the latest version of Vagrant because it requires a version of net-ssh that conflicts with Chef’s. [1]

What I’m trying to do now is to parse knife.rb so that I can feed Ridley [2] the constructor parameters it needs to create a connection to Chef’s API, but this is proving to be quite a challenge.

Has anyone done it before? Does anyone have any pointers to the best way of doing that?

I’ve been experimenting with evaluating the code in knife.rb, but it’s (1) an ugly hack, and (2) not working :slight_smile:

Thanks!

  • cassiano

[0] https://github.com/cassianoleal/vagrant-butcher
[1] https://github.com/cassianoleal/vagrant-butcher/issues/5
[2] https://github.com/reset/ridley