Skip to content

Instantly share code, notes, and snippets.

@jtimberman
Created May 5, 2010 01:40
Show Gist options
  • Save jtimberman/390262 to your computer and use it in GitHub Desktop.
Save jtimberman/390262 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# Author:: Joshua Timberman <[email protected]>
# Description:: Thor script that configures knife for a different platform.
#
# Copyright:: 2010, Opscode, Inc <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Users that manage multiple Opscode Platform organizations can use this
# script to generate a new knife.rb based on a different organization name.
# Simple run it as:
#
# pl_switch.rb -o ORGNAME
#
# And it will write out a new ~/.chef/knife.rb.
#
# It does assume that the username on the Opscode Platform is the same as the
# local logged in user.
require 'rubygems'
require 'thor'
require 'erubis'
class PlatformSwitch < Thor
map "-o" => :platform
desc "platform", "Configure a platform knife.rb"
def platform(org)
# Uses a predefined user for access to the platform.
knifeerb = <<ERB
log_level :info
log_location STDOUT
node_name '#{ENV['USER']}'
client_key '#{ENV['HOME']}/.chef/#{ENV['USER']}.pem'
validation_client_name '<%= org %>-validator'
validation_key '#{ENV['HOME']}/.chef/<%= org %>-validator.pem'
chef_server_url 'https://api.opscode.com/organizations/<%= org%>'
cache_type 'BasicFile'
cache_options( :path => '#{ENV['HOME']}/.chef/checksums' )
cookbook_path [ './cookbooks', './site-cookbooks' ]
ERB
eruby = Erubis::Eruby.new(knifeerb)
outfile = File.open("#{ENV['HOME']}/.chef/knife.rb", "w")
outfile.puts eruby.result(binding())
end
end
PlatformSwitch.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment