Skip to content

Instantly share code, notes, and snippets.

@miketheman
Forked from nstielau/set_environment.rb
Created December 29, 2011 16:12
Show Gist options
  • Save miketheman/1534766 to your computer and use it in GitHub Desktop.
Save miketheman/1534766 to your computer and use it in GitHub Desktop.
A Knife plugin to set node environment
## 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[0]
ui.error "You need to specify a node"
exit 1
end
unless @environment = name_args[1]
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
@mmzoo
Copy link

mmzoo commented Jan 11, 2012

Works lovely, thanks

@miketheman
Copy link
Author

I found a bug in this.

It allows me to set an environment that does not exist. That's pretty bad, and I'm not going to fix it right now.

@mmzoo
Copy link

mmzoo commented Jun 15, 2012

Bug or feature? Which would be "valid" environments to you?

@miketheman
Copy link
Author

That's a very good point. Should knife pre-validate environments, or should I be extra careful? I don't know the answer right now.

@mmzoo
Copy link

mmzoo commented Jun 16, 2012

I think anybody who uses knife should be careful :) The validation should be on higher layers, not in knife. Knife really is just a command-line tool to directly interact with the chef server. It's a little bit like a doctor cutting open a patient - and there should be no limits at all on the knife.

In practice, I never misspelled an environment, but I often forgot to set any environment at all. So for everything that is related to production, I have a CLI layer on top of knife. That is where the actual validation should take place (otherwise you won't be able to keep this gist reusable).

@dulyanov
Copy link

dulyanov commented Mar 4, 2013

The above plugin fails with chef 11.0.0 on the last step of searching for and displaying the node info due to missing config[:rows] and config[:start] parameters in /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.0.0/lib/chef/knife/search.rb

Apparently they are nil, any idea how to pass default values in?

@claco
Copy link

claco commented Sep 23, 2013

@dulyanov

  knife_search.config[:start] = 0
  knife_search.config[:rows] = 1

@bknowles
Copy link

See my clone at https://gist.github.com/bknowles/7572785 that includes these changes. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment