Permalink
Browse files

Initial import

  • Loading branch information...
0 parents commit 4b3ef17a241ca191cbe1e88a084bcef3f8ef0084 @proffalken proffalken committed Mar 26, 2012
Showing with 239 additions and 0 deletions.
  1. +17 −0 .gitignore
  2. +4 −0 Gemfile
  3. +22 −0 LICENSE
  4. +29 −0 README.md
  5. +11 −0 Rakefile
  6. +17 −0 knife-block.gemspec
  7. +118 −0 lib/chef/knife/block.rb
  8. +5 −0 lib/knife-block/version.rb
  9. +16 −0 test/unit/knifeblock_test.rb
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in knife-block.gemspec
+gemspec
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 proffalken
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
+# Knife::Block
+
+TODO: Write a gem description
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'knife-block'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install knife-block
+
+## Usage
+
+TODO: Write usage instructions here
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
@@ -0,0 +1,11 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
+require 'rake/testtask'
+
+
+desc "Run Unit Tests"
+Rake::TestTask.new("unit_tests") do |t|
+ t.pattern = "test/unit/*_test.rb"
+ t.verbose = true
+ t.warning = true
+end
@@ -0,0 +1,17 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/knife-block/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["proffalken"]
+ gem.email = ["theprofessor@threedrunkensysadsonthe.net"]
+ gem.description = %q{Create and manage knife.rb files for OpsCodes' Chef}
+ gem.summary = %q{Create and manage knife.rb files for OpsCodes' Chef}
+ gem.homepage = ""
+
+ gem.files = `git ls-files`.split($\)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.name = "knife-block"
+ gem.require_paths = ["lib"]
+ gem.version = Knife::Block::VERSION
+end
@@ -0,0 +1,118 @@
+# knife-block - a plugin to enable the use of multiple knife configuration files
+# to allow interaction with multiple knife servers in an easy and consistent manner
+#
+# Based on a bash script by Kristian Van Der Vliet (vanders@liqwyd.com)
+#
+# Copyright (c) Matthew Macdonald-Wallace / Green and Secure IT Limited 2012
+#
+module GreenAndSecure
+ class Block < Chef::Knife
+ banner "knife block"
+ def run
+ puts 'Did you mean to run "knife block list" instead?'
+ puts 'Running "knife block list" for you now'
+ list = GreenAndSecure::BlockList.new
+ list.run
+ end
+ end
+ class BlockList < Chef::Knife
+
+ banner "knife block list"
+
+ # list the available environments
+ def run
+ chef_path = Dir.home + "/.chef"
+ ## get the list of available environments by searching ~/.chef for knife.rb files
+ file_list = Dir.glob(chef_path+"/knife-*.rb")
+ current_server = ""
+ if File.exists?(chef_path+"/knife.rb") then
+ if !File.symlink?(chef_path+"/knife.rb") then
+ puts "#{chef_path}/knife.rb is NOT a symlink."
+ puts "Please copy the file to #{chef_path}/knife-<servername>.rb and re-run this command."
+ exit 3
+ end
+ current_server = File.readlink(chef_path+"/knife.rb").split('-')[1 .. -1].join('-').split('.')[0]
+ end
+ servers = []
+ file_list.each do | fn |
+ servers << fn.split('-')[1 .. -1].join('-').split('.')[0]
+ end
+ puts "The available chef servers are:"
+ servers.each do |server|
+ if server == current_server then
+ puts "\t* #{server} [ Currently Selected ]"
+ else
+ puts "\t* #{server}"
+ end
+ end
+ end
+ end
+ class BlockUse < Chef::Knife
+
+ banner "knife block use SERVERNAME"
+
+ def run
+ unless name_args.size == 1
+ puts "Please specify a server"
+ server_list = GreenAndSecure::BlockList.new
+ server_list.run
+ exit 1
+ end
+ chef_path = Dir.home + "/.chef"
+ current_server = ""
+ if File.exists?(chef_path+"/knife.rb") then
+ if !File.symlink?(chef_path+"/knife.rb") then
+ puts "#{chef_path}/knife.rb is NOT a symlink."
+ puts "Please copy the file to #{chef_path}/knife-<servername>.rb and re-run this command."
+ exit 3
+ end
+ current_server = File.readlink(chef_path+"/knife.rb").split('-')[1 .. -1].join('-').split('.')[0]
+ end
+ new_server = name_args.first
+ ui.confirm("You are asking to change from #{current_server} to #{new_server}. Are you sure")
+ if File.exists?(chef_path+"/knife.rb")
+ File.unlink(chef_path+"/knife.rb")
+ end
+ File.symlink(chef_path+"/knife-#{new_server}.rb",chef_path+"/knife.rb")
+ if File.exists?(chef_path+"/knife.rb") then
+ current_server = File.readlink(chef_path+"/knife.rb").split('-')[1 .. -1].join('-').split('.')[0]
+ end
+ puts "The knife configuration has been updated to use #{current_server}"
+ end
+ end
+ class BlockNew < Chef::Knife
+ banner "knife block new SERVERNAME"
+ def run
+ chef_path = Dir.home + "/.chef"
+ puts "This will create a new knife configuration file for you to use with knife-block"
+ unless name_args.size == 1
+ @config_name = ui.ask_question("Please provide a friendly name for the new configuration file: ")
+ end
+ @chef_server = ui.ask_question("Please enter the url to your Chef Server (http://chef.domain.com:4000): ")
+ require 'ohai'
+ require 'chef/knife/configure'
+ if File.exists?(chef_path+"/knife.rb") then
+ if !File.symlink?(chef_path+"/knife.rb") then
+ puts "#{chef_path}/knife.rb already exists!"
+ puts "Please copy the file to #{chef_path}/knife-<servername>.rb and re-run this command."
+ exit 3
+ end
+ end
+ knife_config = Chef::Knife::Configure.new
+ knife_config.config[:config_file] = "#{chef_path}/knife-#{@config_name}.rb"
+ knife_config.config[:chef_server_url] = @chef_server
+ knife_config.run
+ puts "#{chef_path}/knife-#{@config_name}.rb has been sucessfully created"
+ GreenAndSecure::BlockList.new.run
+ ui.confirm("Would you like to switch to it now? ")
+ if File.exists?(chef_path+"/knife.rb")
+ File.unlink(chef_path+"/knife.rb")
+ end
+ File.symlink(chef_path+"/knife-#{@config_name}.rb",chef_path+"/knife.rb")
+ if File.exists?(chef_path+"/knife.rb") then
+ current_server = File.readlink(chef_path+"/knife.rb").split('-')[1 .. -1].join('-').split('.')[0]
+ end
+ puts "The knife configuration has been updated to use #{current_server}"
+ end
+ end
+end
@@ -0,0 +1,5 @@
+module Knife
+ module Block
+ VERSION = "0.0.1"
+ end
+end
@@ -0,0 +1,16 @@
+# Tests for knifeblock
+#
+# Copyright (c) 2012 Green and Secure IT Limited / Matthew Macdonald-Wallace
+
+require 'chef/knife'
+require './lib/chef/knife/block'
+require 'test/unit'
+
+class TestKnifeBlock < Test::Unit::TestCase
+
+ def test_list_file
+ chef_path = "#{ENV['WORKSPACE']}/.chef/"
+ knifeblock = GreenAndSecure::BlockList.new
+ assert(!File.symlink?(chef_path+"/knife.rb"), "Knife.rb was not a symlink")
+ end
+end

0 comments on commit 4b3ef17

Please sign in to comment.