knife-block 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +4 -0
- data/Gemfile +1 -0
- data/Rakefile +1 -1
- data/lib/chef/knife/block.rb +149 -120
- data/lib/knife-block/version.rb +1 -1
- metadata +3 -3
- data/libpeerconnection.log +0 -0
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/Rakefile
CHANGED
data/lib/chef/knife/block.rb
CHANGED
@@ -5,148 +5,177 @@
|
|
5
5
|
#
|
6
6
|
# Copyright (c) Matthew Macdonald-Wallace / Green and Secure IT Limited 2012
|
7
7
|
#
|
8
|
-
module GreenAndSecure
|
9
|
-
def check_block_setup
|
10
|
-
if File.exists?(::Chef::Knife::chef_config_dir+"/knife.rb") then
|
11
|
-
if !File.symlink?(::Chef::Knife::chef_config_dir+"/knife.rb") then
|
12
|
-
puts "#{::Chef::Knife::chef_config_dir}/knife.rb is NOT a symlink."
|
13
|
-
puts "Please copy the file to #{::Chef::Knife::chef_config_dir}/knife-<servername>.rb and re-run this command."
|
14
|
-
exit 3
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
module_function :check_block_setup
|
19
8
|
|
20
|
-
|
21
|
-
|
9
|
+
# Monkey patch ::Chef::Knife to give us back the config file, cause there's
|
10
|
+
# no public method to actually use the lookup logic
|
11
|
+
class Chef
|
12
|
+
class Knife
|
13
|
+
def get_config_file
|
14
|
+
locate_config_file if not config[:config_file]
|
15
|
+
File.dirname(config[:config_file])
|
22
16
|
end
|
23
|
-
|
17
|
+
end
|
18
|
+
end
|
24
19
|
|
25
|
-
|
26
|
-
|
27
|
-
|
20
|
+
module GreenAndSecure
|
21
|
+
@@knife = ::Chef::Knife.new
|
22
|
+
def chef_config_base
|
23
|
+
@@knife.get_config_file
|
24
|
+
end
|
25
|
+
module_function :chef_config_base
|
26
|
+
|
27
|
+
def check_block_setup
|
28
|
+
base = GreenAndSecure::chef_config_base
|
29
|
+
if File.exists?(base+"/knife.rb") then
|
30
|
+
if !File.symlink?(base+"/knife.rb") then
|
31
|
+
puts "#{base}/knife.rb is NOT a symlink."
|
32
|
+
puts "Please copy the file to #{base}/knife-<servername>.rb and re-run this command."
|
33
|
+
exit 3
|
34
|
+
end
|
28
35
|
end
|
29
|
-
|
36
|
+
end
|
37
|
+
module_function :check_block_setup
|
38
|
+
|
39
|
+
def printable_server(server_config)
|
40
|
+
File.basename(server_config, ".rb").split('-')[1 .. -1].join('-')
|
41
|
+
end
|
42
|
+
module_function :printable_server
|
43
|
+
|
44
|
+
# Returns path to berkshelf
|
45
|
+
def berkshelf_path
|
46
|
+
@berkshelf_path ||= ENV['BERKSHELF_PATH'] || File.expand_path('~/.berkshelf')
|
47
|
+
end
|
48
|
+
module_function :berkshelf_path
|
49
|
+
|
50
|
+
class Block < Chef::Knife
|
30
51
|
|
31
|
-
class Block < Chef::Knife
|
32
52
|
banner "knife block"
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
53
|
+
|
54
|
+
def run
|
55
|
+
GreenAndSecure::check_block_setup
|
56
|
+
list = GreenAndSecure::BlockList.new
|
57
|
+
if name_args.size == 1 and list.servers.include?(name_args[0])
|
58
|
+
use = GreenAndSecure::BlockUse.new
|
59
|
+
use.name_args = name_args
|
60
|
+
use.run
|
61
|
+
else
|
62
|
+
puts 'Did you mean to run "knife block list" instead?'
|
63
|
+
puts 'Running "knife block list" for you now'
|
64
|
+
list.run
|
65
|
+
end
|
46
66
|
end
|
67
|
+
end
|
47
68
|
|
48
|
-
|
69
|
+
class BlockList < Chef::Knife
|
49
70
|
|
50
|
-
|
71
|
+
banner "knife block list"
|
51
72
|
|
52
|
-
|
53
|
-
|
54
|
-
|
73
|
+
@current_server = nil
|
74
|
+
def current_server
|
75
|
+
GreenAndSecure::check_block_setup
|
55
76
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
77
|
+
@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
|
82
|
+
end
|
62
83
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
84
|
+
@servers = []
|
85
|
+
def servers
|
86
|
+
## 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 |
|
88
|
+
GreenAndSecure::printable_server(fn)
|
89
|
+
end
|
90
|
+
end
|
70
91
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
end
|
92
|
+
# list the available environments
|
93
|
+
def run
|
94
|
+
GreenAndSecure::check_block_setup
|
95
|
+
puts "The available chef servers are:"
|
96
|
+
servers.each do |server|
|
97
|
+
if server == current_server then
|
98
|
+
puts "\t* #{server} [ Currently Selected ]"
|
99
|
+
else
|
100
|
+
puts "\t* #{server}"
|
101
|
+
end
|
102
|
+
end
|
83
103
|
end
|
104
|
+
end
|
105
|
+
|
106
|
+
class BlockUse < Chef::Knife
|
107
|
+
|
108
|
+
banner "knife block use SERVERNAME"
|
109
|
+
|
110
|
+
def run
|
111
|
+
unless name_args.size == 1
|
112
|
+
puts "Please specify a server"
|
113
|
+
server_list = GreenAndSecure::BlockList.new
|
114
|
+
server_list.run
|
115
|
+
exit 1
|
116
|
+
end
|
117
|
+
list = GreenAndSecure::BlockList.new
|
118
|
+
new_server = name_args.first
|
84
119
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
File.unlink(::Chef::Knife::chef_config_dir+"/knife.rb")
|
120
|
+
base = GreenAndSecure::chef_config_base
|
121
|
+
if File.exists?(base+"/knife-#{new_server}.rb")
|
122
|
+
if File.exists?(base+"/knife.rb")
|
123
|
+
File.unlink(base+"/knife.rb")
|
124
|
+
end
|
125
|
+
File.symlink(base+"/knife-#{new_server}.rb",
|
126
|
+
base+"/knife.rb")
|
127
|
+
puts "The knife configuration has been updated to use #{new_server}"
|
128
|
+
|
129
|
+
# update berkshelf
|
130
|
+
berks = GreenAndSecure::berkshelf_path+"/config.json"
|
131
|
+
berks_new = GreenAndSecure::berkshelf_path+"/config-#{new_server}.json"
|
132
|
+
if File.exists?(berks_new)
|
133
|
+
if File.exists?(berks)
|
134
|
+
File.unlink(berks)
|
101
135
|
end
|
102
|
-
File.symlink(
|
103
|
-
::Chef::Knife::chef_config_dir+"/knife.rb")
|
104
|
-
puts "The knife configuration has been updated to use #{new_server}"
|
105
|
-
|
106
|
-
# update berkshelf
|
107
|
-
berks = GreenAndSecure::berkshelf_path+"/config.json"
|
108
|
-
berks_new = GreenAndSecure::berkshelf_path+"/config-#{new_server}.json"
|
109
|
-
if File.exists?(berks_new)
|
110
|
-
if File.exists?(berks)
|
111
|
-
File.unlink(berks)
|
112
|
-
end
|
113
|
-
File.symlink(berks_new, berks)
|
136
|
+
File.symlink(berks_new, berks)
|
114
137
|
puts "The berkshelf configuration has been updated to use #{new_server}"
|
115
138
|
else
|
116
139
|
puts "Berkshelf configuration for #{new_server} not found"
|
117
140
|
end
|
118
|
-
|
141
|
+
else
|
119
142
|
puts "Knife configuration for #{new_server} not found, aborting switch"
|
120
|
-
|
121
|
-
end
|
143
|
+
end
|
122
144
|
end
|
145
|
+
end
|
146
|
+
|
147
|
+
class BlockNew < Chef::Knife
|
123
148
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
149
|
+
banner "knife block new SERVERNAME"
|
150
|
+
|
151
|
+
def run
|
152
|
+
puts "This will create a new knife configuration file for you to use with knife-block"
|
153
|
+
unless name_args.size == 1
|
154
|
+
@config_name = ui.ask_question("Please provide a friendly name for the new configuration file: ")
|
155
|
+
else
|
156
|
+
@config_name = name_args.first
|
157
|
+
end
|
158
|
+
|
159
|
+
@chef_server = ui.ask_question("Please enter the url to your Chef Server: ")
|
160
|
+
@client_name = ui.ask_question("Please enter the name of the Chef client: ")
|
161
|
+
|
162
|
+
require 'ohai'
|
163
|
+
require 'chef/knife/configure'
|
164
|
+
|
165
|
+
GreenAndSecure::check_block_setup
|
166
|
+
knife_config = Chef::Knife::Configure.new
|
167
|
+
knife_config.config[:config_file] = "#{GreenAndSecure::chef_config_base}/knife-#{@config_name}.rb"
|
168
|
+
knife_config.config[:chef_server_url] = @chef_server
|
169
|
+
knife_config.config[:node_name] = @client_name
|
170
|
+
knife_config.config[:client_key] = "#{GreenAndSecure::chef_config_base}/#{@client_name}-#{@config_name}.pem"
|
171
|
+
knife_config.run
|
172
|
+
|
173
|
+
puts "#{GreenAndSecure::chef_config_base}/knife-#{@config_name}.rb has been sucessfully created"
|
174
|
+
GreenAndSecure::BlockList.new.run
|
175
|
+
use = GreenAndSecure::BlockUse.new
|
176
|
+
use.name_args = [ @config_name ]
|
177
|
+
use.run
|
151
178
|
end
|
179
|
+
end
|
152
180
|
end
|
181
|
+
|
data/lib/knife-block/version.rb
CHANGED
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.
|
4
|
+
version: 0.0.8
|
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-
|
12
|
+
date: 2013-09-20 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Create and manage knife.rb files for OpsCodes' Chef
|
15
15
|
email:
|
@@ -19,6 +19,7 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- .gitignore
|
22
|
+
- .travis.yml
|
22
23
|
- Gemfile
|
23
24
|
- LICENSE
|
24
25
|
- README.md
|
@@ -27,7 +28,6 @@ files:
|
|
27
28
|
- lib/chef/knife/block.rb
|
28
29
|
- lib/knife-block.rb
|
29
30
|
- lib/knife-block/version.rb
|
30
|
-
- libpeerconnection.log
|
31
31
|
- test/unit/knifeblock_test.rb
|
32
32
|
homepage: ''
|
33
33
|
licenses: []
|
data/libpeerconnection.log
DELETED
File without changes
|