Created
May 9, 2011 00:04
-
-
Save nstielau/961827 to your computer and use it in GitHub Desktop.
A Knife plugin to set node environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Knife plugin to set node environment | |
# See http://wiki.opscode.com/display/chef/Environments | |
# | |
## Install | |
# Place in .chef/plugins/knife/set_environment.rb | |
# | |
## Usage | |
# Nick-Stielaus-MacBook-Pro:chef-repo nstielau$ knife node set_environment mynode.net my_env | |
# Looking for mynode.net | |
# Setting environment to my_env | |
# 1 items found | |
# | |
# Node Name: mynode.net | |
# Environment: my_env | |
# FQDN: mynode.net | |
# IP: 66.111.39.46 | |
# Run List: role[base], role[web_app] | |
# Roles: base, web_app | |
# Recipes timezone::default, hosts::default, sudo::default, web_app::default | |
# Platform: ubuntu 10.04 | |
require 'chef/knife' | |
module SomeNamespace | |
class NodeSetEnvironment < Chef::Knife | |
deps do | |
require 'chef/search/query' | |
require 'chef/knife/search' | |
end | |
banner "knife node set_environment NODE ENVIRONMENT" | |
def run | |
unless @node_name = name_args[1] | |
ui.error "You need to specify a node" | |
exit 1 | |
end | |
unless @environment = name_args[2] | |
ui.error "You need to specify an environment" | |
exit 1 | |
end | |
puts "Looking for #{@node_name}" | |
searcher = Chef::Search::Query.new | |
result = searcher.search(:node, "name:#{@node_name}") | |
knife_search = Chef::Knife::Search.new | |
node = result.first.first | |
if node.nil? | |
puts "Could not find a node named #{@node_name}" | |
exit 1 | |
end | |
puts "Setting environment to #{@environment}" | |
node.chef_environment(@environment) | |
node.save | |
knife_search = Chef::Knife::Search.new | |
knife_search.name_args = ['node', "name:#{@node_name}"] | |
knife_search.run | |
end | |
end | |
end |
+1 name_args[0]
and name_args[1]
. It's too bad we can't edit this.
There's a bug with knife_search
? I filed a ticket here: chef/chef#1822
I think, there are few changes required in the script
unless @node_name = name_args[1] -> unless @node_name = name_args[0]
unless @node_name = name_args[2] -> unless @node_name = name_args[1]
node = result.first.first -> node = result.first[0]
Finally, Instead of the following group of lines:
knife_search = Chef::Knife::Search.new
knife_search.name_args = ['node', "name:#{@node_name}"]
knife_search.run
have these instead:
puts node.chef_environment
puts node.name
It Works perfectly now!!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
+1 on moving the name_args[] to 0 and 1