knife-block 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in knife-block.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 proffalken
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Knife Block
2
+
3
+ Green and Secure IT Limited often have a need to work with multiple chef servers at the same time, be it due to testing/development/live scenarios or purely through working for a number of clients at once.
4
+
5
+ The knife block plugin has been created to enable the use of multiple knife.rb files against multiple chef servers.
6
+
7
+ The premise is that you have a "block" in which you store all your "knives" and you can choose the one best suited to the task.
8
+
9
+ ## Installation
10
+
11
+ gem install knife-block
12
+
13
+ ### How does it work?
14
+
15
+ Knife looks for knife.rb in ~/.chef - all this script does is create a symlink from the required configuration to knife.rb so that knife can act on the appropriate server.
16
+
17
+ Create a knife-<service_name>.rb configuration file in your ~/.chef directory for each Chef server that you wish to connect to.
18
+
19
+ **Please note - this script will check to see if knife.rb exists and whether it is a symlink or not.**
20
+
21
+ **If knife.rb is *not* a symlink, the program will exit immediately and tell you what to do.**
22
+
23
+ #### List all available servers
24
+ (This command will also tell you which server is currently selected)
25
+
26
+ knife block list
27
+
28
+ The available chef servers are:
29
+ * local-testing [ Currently Selected ]
30
+ * opscode-hosted-chef
31
+
32
+ #### Change to a new server
33
+ knife block use <server_name>
34
+
35
+ You are asking to change from local-testing to opscode-hosted-chef. Are you sure? (Y/N) y
36
+ The knife configuration has been updated to use opscode-hosted-chef
37
+
38
+ #### Create a new server
39
+ (Launches "knife configure" and creates $HOME/.chef/knife-<friendlyname>.rb)
40
+
41
+ knife block new <friendlyname>
42
+
43
+
44
+
45
+ These knife plugins are supplied without any warranty or guarantees regarding suitability for purpose.
46
+
47
+ The code requires far more tests than the simple one that currently exists.
48
+
49
+ Having said all of that, it works for us!
50
+
51
+ Copyright: Green and Secure IT Limited 2012
52
+
53
+ License: Apache 2 (http://apache.org/licenses/LICENSE-2.0.html)
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+
6
+ desc "Run Unit Tests"
7
+ Rake::TestTask.new("unit_tests") do |t|
8
+ t.pattern = "test/unit/*_test.rb"
9
+ t.verbose = true
10
+ t.warning = true
11
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/knife-block/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["proffalken"]
6
+ gem.email = ["theprofessor@threedrunkensysadsonthe.net"]
7
+ gem.description = %q{Create and manage knife.rb files for OpsCodes' Chef}
8
+ gem.summary = %q{Create and manage knife.rb files for OpsCodes' Chef}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "knife-block"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Knife::Block::VERSION
17
+ end
@@ -0,0 +1,118 @@
1
+ # knife-block - a plugin to enable the use of multiple knife configuration files
2
+ # to allow interaction with multiple knife servers in an easy and consistent manner
3
+ #
4
+ # Based on a bash script by Kristian Van Der Vliet (vanders@liqwyd.com)
5
+ #
6
+ # Copyright (c) Matthew Macdonald-Wallace / Green and Secure IT Limited 2012
7
+ #
8
+ module GreenAndSecure
9
+ class Block < Chef::Knife
10
+ banner "knife block"
11
+ def run
12
+ puts 'Did you mean to run "knife block list" instead?'
13
+ puts 'Running "knife block list" for you now'
14
+ list = GreenAndSecure::BlockList.new
15
+ list.run
16
+ end
17
+ end
18
+ class BlockList < Chef::Knife
19
+
20
+ banner "knife block list"
21
+
22
+ # list the available environments
23
+ def run
24
+ chef_path = Dir.home + "/.chef"
25
+ ## get the list of available environments by searching ~/.chef for knife.rb files
26
+ file_list = Dir.glob(chef_path+"/knife-*.rb")
27
+ current_server = ""
28
+ if File.exists?(chef_path+"/knife.rb") then
29
+ if !File.symlink?(chef_path+"/knife.rb") then
30
+ puts "#{chef_path}/knife.rb is NOT a symlink."
31
+ puts "Please copy the file to #{chef_path}/knife-<servername>.rb and re-run this command."
32
+ exit 3
33
+ end
34
+ current_server = File.readlink(chef_path+"/knife.rb").split('-')[1 .. -1].join('-').split('.')[0]
35
+ end
36
+ servers = []
37
+ file_list.each do | fn |
38
+ servers << fn.split('-')[1 .. -1].join('-').split('.')[0]
39
+ end
40
+ puts "The available chef servers are:"
41
+ servers.each do |server|
42
+ if server == current_server then
43
+ puts "\t* #{server} [ Currently Selected ]"
44
+ else
45
+ puts "\t* #{server}"
46
+ end
47
+ end
48
+ end
49
+ end
50
+ class BlockUse < Chef::Knife
51
+
52
+ banner "knife block use SERVERNAME"
53
+
54
+ def run
55
+ unless name_args.size == 1
56
+ puts "Please specify a server"
57
+ server_list = GreenAndSecure::BlockList.new
58
+ server_list.run
59
+ exit 1
60
+ end
61
+ chef_path = Dir.home + "/.chef"
62
+ current_server = ""
63
+ if File.exists?(chef_path+"/knife.rb") then
64
+ if !File.symlink?(chef_path+"/knife.rb") then
65
+ puts "#{chef_path}/knife.rb is NOT a symlink."
66
+ puts "Please copy the file to #{chef_path}/knife-<servername>.rb and re-run this command."
67
+ exit 3
68
+ end
69
+ current_server = File.readlink(chef_path+"/knife.rb").split('-')[1 .. -1].join('-').split('.')[0]
70
+ end
71
+ new_server = name_args.first
72
+ ui.confirm("You are asking to change from #{current_server} to #{new_server}. Are you sure")
73
+ if File.exists?(chef_path+"/knife.rb")
74
+ File.unlink(chef_path+"/knife.rb")
75
+ end
76
+ File.symlink(chef_path+"/knife-#{new_server}.rb",chef_path+"/knife.rb")
77
+ if File.exists?(chef_path+"/knife.rb") then
78
+ current_server = File.readlink(chef_path+"/knife.rb").split('-')[1 .. -1].join('-').split('.')[0]
79
+ end
80
+ puts "The knife configuration has been updated to use #{current_server}"
81
+ end
82
+ end
83
+ class BlockNew < Chef::Knife
84
+ banner "knife block new SERVERNAME"
85
+ def run
86
+ chef_path = Dir.home + "/.chef"
87
+ puts "This will create a new knife configuration file for you to use with knife-block"
88
+ unless name_args.size == 1
89
+ @config_name = ui.ask_question("Please provide a friendly name for the new configuration file: ")
90
+ end
91
+ @chef_server = ui.ask_question("Please enter the url to your Chef Server (http://chef.domain.com:4000): ")
92
+ require 'ohai'
93
+ require 'chef/knife/configure'
94
+ if File.exists?(chef_path+"/knife.rb") then
95
+ if !File.symlink?(chef_path+"/knife.rb") then
96
+ puts "#{chef_path}/knife.rb already exists!"
97
+ puts "Please copy the file to #{chef_path}/knife-<servername>.rb and re-run this command."
98
+ exit 3
99
+ end
100
+ end
101
+ knife_config = Chef::Knife::Configure.new
102
+ knife_config.config[:config_file] = "#{chef_path}/knife-#{@config_name}.rb"
103
+ knife_config.config[:chef_server_url] = @chef_server
104
+ knife_config.run
105
+ puts "#{chef_path}/knife-#{@config_name}.rb has been sucessfully created"
106
+ GreenAndSecure::BlockList.new.run
107
+ ui.confirm("Would you like to switch to it now? ")
108
+ if File.exists?(chef_path+"/knife.rb")
109
+ File.unlink(chef_path+"/knife.rb")
110
+ end
111
+ File.symlink(chef_path+"/knife-#{@config_name}.rb",chef_path+"/knife.rb")
112
+ if File.exists?(chef_path+"/knife.rb") then
113
+ current_server = File.readlink(chef_path+"/knife.rb").split('-')[1 .. -1].join('-').split('.')[0]
114
+ end
115
+ puts "The knife configuration has been updated to use #{current_server}"
116
+ end
117
+ end
118
+ end
File without changes
@@ -0,0 +1,5 @@
1
+ module Knife
2
+ module Block
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ # Tests for knifeblock
2
+ #
3
+ # Copyright (c) 2012 Green and Secure IT Limited / Matthew Macdonald-Wallace
4
+
5
+ require 'chef/knife'
6
+ require './lib/chef/knife/block'
7
+ require 'test/unit'
8
+
9
+ class TestKnifeBlock < Test::Unit::TestCase
10
+
11
+ def test_list_file
12
+ chef_path = "#{ENV['WORKSPACE']}/.chef/"
13
+ knifeblock = GreenAndSecure::BlockList.new
14
+ assert(!File.symlink?(chef_path+"/knife.rb"), "Knife.rb was not a symlink")
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-block
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - proffalken
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-26 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Create and manage knife.rb files for OpsCodes' Chef
15
+ email:
16
+ - theprofessor@threedrunkensysadsonthe.net
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - knife-block.gemspec
27
+ - lib/chef/knife/block.rb
28
+ - lib/knife-block.rb
29
+ - lib/knife-block/version.rb
30
+ - test/unit/knifeblock_test.rb
31
+ homepage: ''
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.10
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Create and manage knife.rb files for OpsCodes' Chef
55
+ test_files:
56
+ - test/unit/knifeblock_test.rb
57
+ has_rdoc: