1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
#
# Author:: John Keiser (<jkeiser@opscode.com>)
# Copyright:: Copyright (c) 2012 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# 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.
#
require 'chef/knife'
class Chef
module ChefFS
class Knife < Chef::Knife
# Workaround for CHEF-3932
def self.deps
super do
require 'chef/config'
require 'chef/chef_fs/parallelizer'
require 'chef/chef_fs/config'
require 'chef/chef_fs/file_pattern'
require 'chef/chef_fs/path_utils'
yield
end
end
def self.inherited(c)
super
# Ensure we always get to do our includes, whether subclass calls deps or not
c.deps do
end
c.options.merge!(options)
end
option :repo_mode,
:long => '--repo-mode MODE',
:description => "Specifies the local repository layout. Values: static, everything, hosted_everything. Default: everything/hosted_everything"
option :chef_repo_path,
:long => '--chef-repo-path PATH',
:description => 'Overrides the location of chef repo. Default is specified by chef_repo_path in the config'
option :concurrency,
:long => '--concurrency THREADS',
:description => 'Maximum number of simultaneous requests to send (default: 10)'
def configure_chef
super
Chef::Config[:repo_mode] = config[:repo_mode] if config[:repo_mode]
Chef::Config[:concurrency] = config[:concurrency].to_i if config[:concurrency]
# --chef-repo-path overrides all other paths
if config[:chef_repo_path]
Chef::Config[:chef_repo_path] = config[:chef_repo_path]
Chef::Config::PATH_VARIABLES.each do |variable_name|
Chef::Config[variable_name.to_sym] = chef_repo_paths.map { |path| File.join(path, "#{variable_name[0..-6]}s") }
end
end
@chef_fs_config = Chef::ChefFS::Config.new(Chef::Config, Dir.pwd, config)
Chef::ChefFS::Parallelizer.threads = (Chef::Config[:concurrency] || 10) - 1
if Chef::Config[:chef_server_url].to_sym == :local
local_url = start_local_server
Chef::Config[:chef_server_url] = local_url
Chef::Config[:client_key] = nil
Chef::Config[:validation_key] = nil
end
end
def chef_fs
@chef_fs_config.chef_fs
end
def create_chef_fs
@chef_fs_config.create_chef_fs
end
def local_fs
@chef_fs_config.local_fs
end
def create_local_fs
@chef_fs_config.create_local_fs
end
def pattern_args
@pattern_args ||= pattern_args_from(name_args)
end
def pattern_args_from(args)
args.map { |arg| pattern_arg_from(arg) }
end
def pattern_arg_from(arg)
# TODO support absolute file paths and not just patterns? Too much?
# Could be super useful in a world with multiple repo paths
if !@chef_fs_config.base_path && !Chef::ChefFS::PathUtils.is_absolute?(arg)
# Check if chef repo path is specified to give a better error message
ui.error("Attempt to use relative path '#{arg}' when current directory is outside the repository path")
exit(1)
end
Chef::ChefFS::FilePattern.relative_to(@chef_fs_config.base_path, arg)
end
def format_path(entry)
@chef_fs_config.format_path(entry)
end
def parallelize(inputs, options = {}, &block)
Chef::ChefFS::Parallelizer.parallelize(inputs, options, &block)
end
def locate_config_file
super
if !config[:config_file]
# If the config file doesn't already exist, find out where it should be,
# and create it.
repo_dir = discover_repo_dir(Dir.pwd)
if repo_dir
dot_chef = File.join(repo_dir, ".chef")
if !File.directory?(dot_chef)
Dir.mkdir(dot_chef)
end
knife_rb = File.join(dot_chef, "knife.rb")
if !File.exist?(knife_rb)
ui.warn("No configuration found. Creating .chef/knife.rb in #{repo_dir} ...")
File.open(knife_rb, "w") do |file|
file.write <<EOM
chef_server_url 'local'
chef_repo_path File.dirname(File.dirname(__FILE__))
cookbook_path File.join(chef_repo_path, "cookbooks")
EOM
end
end
config[:config_file] = knife_rb
end
end
end
def discover_repo_dir(dir)
%w(.chef cookbooks data_bags environments roles).each do |subdir|
return dir if File.directory?(File.join(dir, subdir))
end
# If this isn't it, check the parent
parent = File.dirname(dir)
if parent && parent != dir
discover_repo_dir(parent)
else
nil
end
end
def start_local_server
begin
require 'chef_zero/server'
rescue LoadError
STDERR.puts <<EOM
ERROR: chef-zero must be installed to use local-server mode! To install:
gem install chef-zero
EOM
exit(1)
end
require 'chef/chef_fs/chef_fs_data_store'
server_options = {}
server_options[:data_store] = ChefFSChefFSDataStore.new(local_fs)
server_options[:log_level] = Chef::Log.level
server_options[:port] = 8889
server = ChefZero::Server.new(server_options)
server.start_background
server.url
end
end
end
end
|