knife-block 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,5 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+
4
+ gem 'rake'
5
+ gem 'chef'
3
6
  # Specify your gem's dependencies in knife-block.gemspec
4
7
  gemspec
5
- rake
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require 'rake/testtask'
4
4
 
5
5
 
6
6
  desc "Run Unit Tests"
7
- Rake::TestTask.new("test") do |t|
7
+ Rake::TestTask.new("default") do |t|
8
8
  t.pattern = "test/unit/*_test.rb"
9
9
  t.verbose = true
10
10
  t.warning = true
@@ -11,41 +11,86 @@
11
11
  class Chef
12
12
  class Knife
13
13
  def get_config_file
14
- locate_config_file if not config[:config_file]
14
+
15
+ # locate_config_file is only compatible with Chef 11
16
+ _chef11 = ::Chef::Version.new('11.0.0')
17
+ if GreenAndSecure.current_chef_version >= _chef11
18
+ locate_config_file if not config[:config_file]
19
+ else
20
+ GreenAndSecure.locate_config_file config
21
+ end
22
+
15
23
  File.dirname(config[:config_file])
16
24
  end
17
25
  end
18
26
  end
19
27
 
20
28
  module GreenAndSecure
29
+ @@current_chef_version = ::Chef::Version.new(::Chef::VERSION)
21
30
  @@knife = ::Chef::Knife.new
31
+
32
+ def current_chef_version
33
+ @@current_chef_version
34
+ end
35
+
22
36
  def chef_config_base
23
37
  @@knife.get_config_file
24
38
  end
25
- module_function :chef_config_base
39
+
40
+ # Copied from chef/knife.rb
41
+ #
42
+ # Chef 11 defines `locate_config_file` in its chef/knife.rb but Chef 10
43
+ # has this snippet of code inlined in chef/knife.rb:configure_chef.
44
+ def locate_config_file(config)
45
+ candidate_configs = []
46
+
47
+ # Look for $KNIFE_HOME/knife.rb (allow multiple knives config on same machine)
48
+ if ENV['KNIFE_HOME']
49
+ candidate_configs << File.join(ENV['KNIFE_HOME'], 'knife.rb')
50
+ end
51
+ # Look for $PWD/knife.rb
52
+ if Dir.pwd
53
+ candidate_configs << File.join(Dir.pwd, 'knife.rb')
54
+ end
55
+ # Look for $UPWARD/.chef/knife.rb
56
+ if ::Chef::Knife.chef_config_dir
57
+ candidate_configs << File.join(::Chef::Knife.chef_config_dir, 'knife.rb')
58
+ end
59
+ # Look for $HOME/.chef/knife.rb
60
+ if ENV['HOME']
61
+ candidate_configs << File.join(ENV['HOME'], '.chef', 'knife.rb')
62
+ end
63
+
64
+ candidate_configs.each do |candidate_config|
65
+ candidate_config = File.expand_path(candidate_config)
66
+ if File.exist?(candidate_config)
67
+ config[:config_file] = candidate_config
68
+ break
69
+ end
70
+ end
71
+ end
26
72
 
27
73
  def check_block_setup
28
74
  base = GreenAndSecure::chef_config_base
29
75
  if File.exists?(base+"/knife.rb") then
30
- if !File.symlink?(base+"/knife.rb") then
76
+ unless File.symlink?(base+"/knife.rb")
31
77
  puts "#{base}/knife.rb is NOT a symlink."
32
78
  puts "Please copy the file to #{base}/knife-<servername>.rb and re-run this command."
33
79
  exit 3
34
80
  end
35
81
  end
36
82
  end
37
- module_function :check_block_setup
38
83
 
39
84
  def printable_server(server_config)
40
85
  File.basename(server_config, ".rb").split('-')[1 .. -1].join('-')
41
86
  end
42
- module_function :printable_server
43
87
 
44
88
  # Returns path to berkshelf
45
89
  def berkshelf_path
46
90
  @berkshelf_path ||= ENV['BERKSHELF_PATH'] || File.expand_path('~/.berkshelf')
47
91
  end
48
- module_function :berkshelf_path
92
+
93
+ extend self
49
94
 
50
95
  class Block < Chef::Knife
51
96
 
@@ -71,20 +116,22 @@ module GreenAndSecure
71
116
  banner "knife block list"
72
117
 
73
118
  @current_server = nil
119
+
74
120
  def current_server
75
121
  GreenAndSecure::check_block_setup
76
122
 
77
123
  @current_server ||= if File.exists?(GreenAndSecure::chef_config_base+"/knife.rb") then
78
- GreenAndSecure::printable_server(File.readlink(GreenAndSecure::chef_config_base+"/knife.rb"))
79
- else
80
- nil
81
- end
124
+ GreenAndSecure::printable_server(File.readlink(GreenAndSecure::chef_config_base+"/knife.rb"))
125
+ else
126
+ nil
127
+ end
82
128
  end
83
129
 
84
130
  @servers = []
131
+
85
132
  def servers
86
133
  ## get the list of available environments by searching ~/.chef for knife.rb files
87
- @servers ||= Dir.glob(GreenAndSecure::chef_config_base+"/knife-*.rb").sort.map do | fn |
134
+ @servers ||= Dir.glob(GreenAndSecure::chef_config_base+"/knife-*.rb").sort.map do |fn|
88
135
  GreenAndSecure::printable_server(fn)
89
136
  end
90
137
  end
@@ -112,34 +159,39 @@ module GreenAndSecure
112
159
  puts "Please specify a server"
113
160
  server_list = GreenAndSecure::BlockList.new
114
161
  server_list.run
115
- exit 1
162
+ exit 1
116
163
  end
164
+
117
165
  list = GreenAndSecure::BlockList.new
118
166
  new_server = name_args.first
119
167
 
120
168
  base = GreenAndSecure::chef_config_base
169
+
121
170
  if File.exists?(base+"/knife-#{new_server}.rb")
171
+
122
172
  if File.exists?(base+"/knife.rb")
123
173
  File.unlink(base+"/knife.rb")
124
174
  end
125
- File.symlink(base+"/knife-#{new_server}.rb",
126
- base+"/knife.rb")
175
+
176
+ File.symlink(base+"/knife-#{new_server}.rb", base+"/knife.rb")
127
177
  puts "The knife configuration has been updated to use #{new_server}"
128
178
 
129
179
  # update berkshelf
130
180
  berks = GreenAndSecure::berkshelf_path+"/config.json"
131
181
  berks_new = GreenAndSecure::berkshelf_path+"/config-#{new_server}.json"
132
182
  if File.exists?(berks_new)
183
+
133
184
  if File.exists?(berks)
134
185
  File.unlink(berks)
135
186
  end
187
+
136
188
  File.symlink(berks_new, berks)
137
- puts "The berkshelf configuration has been updated to use #{new_server}"
138
- else
139
- puts "Berkshelf configuration for #{new_server} not found"
140
- end
189
+ puts "The berkshelf configuration has been updated to use #{new_server}"
141
190
  else
142
- puts "Knife configuration for #{new_server} not found, aborting switch"
191
+ puts "Berkshelf configuration for #{new_server} not found"
192
+ end
193
+ else
194
+ puts "Knife configuration for #{new_server} not found, aborting switch"
143
195
  end
144
196
  end
145
197
  end
@@ -173,7 +225,7 @@ module GreenAndSecure
173
225
  puts "#{GreenAndSecure::chef_config_base}/knife-#{@config_name}.rb has been sucessfully created"
174
226
  GreenAndSecure::BlockList.new.run
175
227
  use = GreenAndSecure::BlockUse.new
176
- use.name_args = [ @config_name ]
228
+ use.name_args = [@config_name]
177
229
  use.run
178
230
  end
179
231
  end
@@ -1,5 +1,5 @@
1
1
  module Knife
2
2
  module Block
3
- VERSION = "0.0.8"
3
+ VERSION = "0.0.9"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-block
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-20 00:00:00.000000000 Z
12
+ date: 2013-09-25 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Create and manage knife.rb files for OpsCodes' Chef
15
15
  email:
@@ -49,10 +49,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
49
  version: '0'
50
50
  requirements: []
51
51
  rubyforge_project:
52
- rubygems_version: 1.8.23
52
+ rubygems_version: 1.8.26
53
53
  signing_key:
54
54
  specification_version: 3
55
55
  summary: Create and manage knife.rb files for OpsCodes' Chef
56
56
  test_files:
57
57
  - test/unit/knifeblock_test.rb
58
- has_rdoc: