Skip to content

Instantly share code, notes, and snippets.

@bknowles
Forked from miketheman/set_environment.rb
Last active December 28, 2015 22:39
Show Gist options
  • Select an option

  • Save bknowles/7572785 to your computer and use it in GitHub Desktop.

Select an option

Save bknowles/7572785 to your computer and use it in GitHub Desktop.

Revisions

  1. bknowles revised this gist Nov 20, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions set_environment.rb
    Original file line number Diff line number Diff line change
    @@ -61,6 +61,8 @@ def run

    knife_search = Chef::Knife::Search.new
    knife_search.name_args = ['node', "name:#{@node_name}"]
    knife_search.config[:start] = 0
    knife_search.config[:rows] = 1
    knife_search.run

    end
  2. @miketheman miketheman revised this gist Dec 29, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions set_environment.rb
    Original file line number Diff line number Diff line change
    @@ -33,12 +33,12 @@ class NodeSetEnvironment < Chef::Knife
    banner "knife node set_environment NODE ENVIRONMENT"

    def run
    unless @node_name = name_args[1]
    unless @node_name = name_args[0]
    ui.error "You need to specify a node"
    exit 1
    end

    unless @environment = name_args[2]
    unless @environment = name_args[1]
    ui.error "You need to specify an environment"
    exit 1
    end
  3. @nstielau nstielau revised this gist Sep 28, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion set_environment.rb
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@
    require 'chef/knife'

    module SomeNamespace
    class Node < Chef::Knife
    class NodeSetEnvironment < Chef::Knife

    deps do
    require 'chef/search/query'
  4. @nstielau nstielau created this gist May 9, 2011.
    68 changes: 68 additions & 0 deletions set_environment.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    ## 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 Node < 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