chef-config 12.7.2 → 12.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,34 +1,34 @@
1
- # Copyright:: Copyright 2010-2016, Chef Software, Inc.
2
- # License:: Apache License, Version 2.0
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
-
16
- #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
17
- # NOTE: This file is generated by running `rake version` in the top level of
18
- # this repo. Do not edit this manually. Edit the VERSION file and run the rake
19
- # task instead.
20
- #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21
-
22
- module ChefConfig
23
- CHEFCONFIG_ROOT = File.dirname(File.expand_path(File.dirname(__FILE__)))
24
- VERSION = "12.7.2"
25
- end
26
-
27
- #
28
- # NOTE: the Chef::Version class is defined in version_class.rb
29
- #
30
- # NOTE: DO NOT Use the Chef::Version class on ChefConfig::VERSIONs. The
31
- # Chef::Version class is for _cookbooks_ only, and cannot handle
32
- # pre-release versions like "10.14.0.rc.2". Please use Rubygem's
33
- # Gem::Version class instead.
34
- #
1
+ # Copyright:: Copyright 2010-2016, Chef Software, Inc.
2
+ # License:: Apache License, Version 2.0
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
17
+ # NOTE: This file is generated by running `rake version` in the top level of
18
+ # this repo. Do not edit this manually. Edit the VERSION file and run the rake
19
+ # task instead.
20
+ #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21
+
22
+ module ChefConfig
23
+ CHEFCONFIG_ROOT = File.dirname(File.expand_path(File.dirname(__FILE__)))
24
+ VERSION = "12.8.1"
25
+ end
26
+
27
+ #
28
+ # NOTE: the Chef::Version class is defined in version_class.rb
29
+ #
30
+ # NOTE: DO NOT Use the Chef::Version class on ChefConfig::VERSIONs. The
31
+ # Chef::Version class is for _cookbooks_ only, and cannot handle
32
+ # pre-release versions like "10.14.0.rc.2". Please use Rubygem's
33
+ # Gem::Version class instead.
34
+ #
@@ -1,28 +1,28 @@
1
- #
2
- # Copyright:: Copyright 2015-2016, Chef Software, Inc.
3
- # License:: Apache License, Version 2.0
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
- #
17
-
18
- module ChefConfig
19
-
20
- def self.windows?
21
- if RUBY_PLATFORM =~ /mswin|mingw|windows/
22
- true
23
- else
24
- false
25
- end
26
- end
27
-
28
- end
1
+ #
2
+ # Copyright:: Copyright 2015-2016, Chef Software, Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ module ChefConfig
19
+
20
+ def self.windows?
21
+ if RUBY_PLATFORM =~ /mswin|mingw|windows/
22
+ true
23
+ else
24
+ false
25
+ end
26
+ end
27
+
28
+ end
@@ -1,178 +1,182 @@
1
- #
2
- # Author:: Daniel DeLeo (<dan@chef.io>)
3
- # Copyright:: Copyright 2014-2016, Chef Software, Inc.
4
- # License:: Apache License, Version 2.0
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
-
19
- require "chef-config/config"
20
- require "chef-config/exceptions"
21
- require "chef-config/logger"
22
- require "chef-config/path_helper"
23
- require "chef-config/windows"
24
-
25
- module ChefConfig
26
- class WorkstationConfigLoader
27
-
28
- # Path to a config file requested by user, (e.g., via command line option). Can be nil
29
- attr_accessor :explicit_config_file
30
-
31
- # TODO: initialize this with a logger for Chef and Knife
32
- def initialize(explicit_config_file, logger = nil)
33
- @explicit_config_file = explicit_config_file
34
- @chef_config_dir = nil
35
- @config_location = nil
36
- @logger = logger || NullLogger.new
37
- end
38
-
39
- def no_config_found?
40
- config_location.nil?
41
- end
42
-
43
- def config_location
44
- @config_location ||= (explicit_config_file || locate_local_config)
45
- end
46
-
47
- def chef_config_dir
48
- if @chef_config_dir.nil?
49
- @chef_config_dir = false
50
- full_path = working_directory.split(File::SEPARATOR)
51
- (full_path.length - 1).downto(0) do |i|
52
- candidate_directory = File.join(full_path[0..i] + [".chef"])
53
- if File.exist?(candidate_directory) && File.directory?(candidate_directory)
54
- @chef_config_dir = candidate_directory
55
- break
56
- end
57
- end
58
- end
59
- @chef_config_dir
60
- end
61
-
62
- def load
63
- # Ignore it if there's no explicit_config_file and can't find one at a
64
- # default path.
65
- return false if config_location.nil?
66
-
67
- if explicit_config_file && !path_exists?(config_location)
68
- raise ChefConfig::ConfigurationError, "Specified config file #{config_location} does not exist"
69
- end
70
-
71
- # Have to set Config.config_file b/c other config is derived from it.
72
- Config.config_file = config_location
73
- read_config(IO.read(config_location), config_location)
74
- end
75
-
76
- # (Private API, public for test purposes)
77
- def env
78
- ENV
79
- end
80
-
81
- # (Private API, public for test purposes)
82
- def path_exists?(path)
83
- Pathname.new(path).expand_path.exist?
84
- end
85
-
86
- private
87
-
88
- def have_config?(path)
89
- if path_exists?(path)
90
- logger.info("Using config at #{path}")
91
- true
92
- else
93
- logger.debug("Config not found at #{path}, trying next option")
94
- false
95
- end
96
- end
97
-
98
- def locate_local_config
99
- candidate_configs = []
100
-
101
- # Look for $KNIFE_HOME/knife.rb (allow multiple knives config on same machine)
102
- if env["KNIFE_HOME"]
103
- candidate_configs << File.join(env["KNIFE_HOME"], "config.rb")
104
- candidate_configs << File.join(env["KNIFE_HOME"], "knife.rb")
105
- end
106
- # Look for $PWD/knife.rb
107
- if Dir.pwd
108
- candidate_configs << File.join(Dir.pwd, "config.rb")
109
- candidate_configs << File.join(Dir.pwd, "knife.rb")
110
- end
111
- # Look for $UPWARD/.chef/knife.rb
112
- if chef_config_dir
113
- candidate_configs << File.join(chef_config_dir, "config.rb")
114
- candidate_configs << File.join(chef_config_dir, "knife.rb")
115
- end
116
- # Look for $HOME/.chef/knife.rb
117
- PathHelper.home(".chef") do |dot_chef_dir|
118
- candidate_configs << File.join(dot_chef_dir, "config.rb")
119
- candidate_configs << File.join(dot_chef_dir, "knife.rb")
120
- end
121
-
122
- candidate_configs.find do |candidate_config|
123
- have_config?(candidate_config)
124
- end
125
- end
126
-
127
- def working_directory
128
- a = if ChefConfig.windows?
129
- env["CD"]
130
- else
131
- env["PWD"]
132
- end || Dir.pwd
133
-
134
- a
135
- end
136
-
137
- def read_config(config_content, config_file_path)
138
- Config.from_string(config_content, config_file_path)
139
- rescue SignalException
140
- raise
141
- rescue SyntaxError => e
142
- message = ""
143
- message << "You have invalid ruby syntax in your config file #{config_file_path}\n\n"
144
- message << "#{e.class.name}: #{e.message}\n"
145
- if file_line = e.message[/#{Regexp.escape(config_file_path)}:[\d]+/]
146
- line = file_line[/:([\d]+)$/, 1].to_i
147
- message << highlight_config_error(config_file_path, line)
148
- end
149
- raise ChefConfig::ConfigurationError, message
150
- rescue Exception => e
151
- message = "You have an error in your config file #{config_file_path}\n\n"
152
- message << "#{e.class.name}: #{e.message}\n"
153
- filtered_trace = e.backtrace.grep(/#{Regexp.escape(config_file_path)}/)
154
- filtered_trace.each { |bt_line| message << " " << bt_line << "\n" }
155
- if !filtered_trace.empty?
156
- line_nr = filtered_trace.first[/#{Regexp.escape(config_file_path)}:([\d]+)/, 1]
157
- message << highlight_config_error(config_file_path, line_nr.to_i)
158
- end
159
- raise ChefConfig::ConfigurationError, message
160
- end
161
-
162
- def highlight_config_error(file, line)
163
- config_file_lines = []
164
- IO.readlines(file).each_with_index { |l, i| config_file_lines << "#{(i + 1).to_s.rjust(3)}: #{l.chomp}" }
165
- if line == 1
166
- lines = config_file_lines[0..3]
167
- else
168
- lines = config_file_lines[Range.new(line - 2, line)]
169
- end
170
- "Relevant file content:\n" + lines.join("\n") + "\n"
171
- end
172
-
173
- def logger
174
- @logger
175
- end
176
-
177
- end
178
- end
1
+ #
2
+ # Author:: Daniel DeLeo (<dan@chef.io>)
3
+ # Copyright:: Copyright 2014-2016, Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require "chef-config/config"
20
+ require "chef-config/exceptions"
21
+ require "chef-config/logger"
22
+ require "chef-config/path_helper"
23
+ require "chef-config/windows"
24
+ require "chef-config/mixin/dot_d"
25
+
26
+ module ChefConfig
27
+ class WorkstationConfigLoader
28
+ include ChefConfig::Mixin::DotD
29
+
30
+ # Path to a config file requested by user, (e.g., via command line option). Can be nil
31
+ attr_accessor :explicit_config_file
32
+
33
+ # TODO: initialize this with a logger for Chef and Knife
34
+ def initialize(explicit_config_file, logger = nil)
35
+ @explicit_config_file = explicit_config_file
36
+ @chef_config_dir = nil
37
+ @config_location = nil
38
+ @logger = logger || NullLogger.new
39
+ end
40
+
41
+ def no_config_found?
42
+ config_location.nil?
43
+ end
44
+
45
+ def config_location
46
+ @config_location ||= (explicit_config_file || locate_local_config)
47
+ end
48
+
49
+ def chef_config_dir
50
+ if @chef_config_dir.nil?
51
+ @chef_config_dir = false
52
+ full_path = working_directory.split(File::SEPARATOR)
53
+ (full_path.length - 1).downto(0) do |i|
54
+ candidate_directory = File.join(full_path[0..i] + [".chef"])
55
+ if File.exist?(candidate_directory) && File.directory?(candidate_directory)
56
+ @chef_config_dir = candidate_directory
57
+ break
58
+ end
59
+ end
60
+ end
61
+ @chef_config_dir
62
+ end
63
+
64
+ def load
65
+ # Ignore it if there's no explicit_config_file and can't find one at a
66
+ # default path.
67
+ if !config_location.nil?
68
+ if explicit_config_file && !path_exists?(config_location)
69
+ raise ChefConfig::ConfigurationError, "Specified config file #{config_location} does not exist"
70
+ end
71
+
72
+ # Have to set Config.config_file b/c other config is derived from it.
73
+ Config.config_file = config_location
74
+ apply_config(IO.read(config_location), config_location)
75
+ end
76
+
77
+ load_dot_d(Config[:config_d_dir]) if Config[:config_d_dir]
78
+ end
79
+
80
+ # (Private API, public for test purposes)
81
+ def env
82
+ ENV
83
+ end
84
+
85
+ # (Private API, public for test purposes)
86
+ def path_exists?(path)
87
+ Pathname.new(path).expand_path.exist?
88
+ end
89
+
90
+ private
91
+
92
+ def have_config?(path)
93
+ if path_exists?(path)
94
+ logger.info("Using config at #{path}")
95
+ true
96
+ else
97
+ logger.debug("Config not found at #{path}, trying next option")
98
+ false
99
+ end
100
+ end
101
+
102
+ def locate_local_config
103
+ candidate_configs = []
104
+
105
+ # Look for $KNIFE_HOME/knife.rb (allow multiple knives config on same machine)
106
+ if env["KNIFE_HOME"]
107
+ candidate_configs << File.join(env["KNIFE_HOME"], "config.rb")
108
+ candidate_configs << File.join(env["KNIFE_HOME"], "knife.rb")
109
+ end
110
+ # Look for $PWD/knife.rb
111
+ if Dir.pwd
112
+ candidate_configs << File.join(Dir.pwd, "config.rb")
113
+ candidate_configs << File.join(Dir.pwd, "knife.rb")
114
+ end
115
+ # Look for $UPWARD/.chef/knife.rb
116
+ if chef_config_dir
117
+ candidate_configs << File.join(chef_config_dir, "config.rb")
118
+ candidate_configs << File.join(chef_config_dir, "knife.rb")
119
+ end
120
+ # Look for $HOME/.chef/knife.rb
121
+ PathHelper.home(".chef") do |dot_chef_dir|
122
+ candidate_configs << File.join(dot_chef_dir, "config.rb")
123
+ candidate_configs << File.join(dot_chef_dir, "knife.rb")
124
+ end
125
+
126
+ candidate_configs.find do |candidate_config|
127
+ have_config?(candidate_config)
128
+ end
129
+ end
130
+
131
+ def working_directory
132
+ a = if ChefConfig.windows?
133
+ env["CD"]
134
+ else
135
+ env["PWD"]
136
+ end || Dir.pwd
137
+
138
+ a
139
+ end
140
+
141
+ def apply_config(config_content, config_file_path)
142
+ Config.from_string(config_content, config_file_path)
143
+ rescue SignalException
144
+ raise
145
+ rescue SyntaxError => e
146
+ message = ""
147
+ message << "You have invalid ruby syntax in your config file #{config_file_path}\n\n"
148
+ message << "#{e.class.name}: #{e.message}\n"
149
+ if file_line = e.message[/#{Regexp.escape(config_file_path)}:[\d]+/]
150
+ line = file_line[/:([\d]+)$/, 1].to_i
151
+ message << highlight_config_error(config_file_path, line)
152
+ end
153
+ raise ChefConfig::ConfigurationError, message
154
+ rescue Exception => e
155
+ message = "You have an error in your config file #{config_file_path}\n\n"
156
+ message << "#{e.class.name}: #{e.message}\n"
157
+ filtered_trace = e.backtrace.grep(/#{Regexp.escape(config_file_path)}/)
158
+ filtered_trace.each { |bt_line| message << " " << bt_line << "\n" }
159
+ if !filtered_trace.empty?
160
+ line_nr = filtered_trace.first[/#{Regexp.escape(config_file_path)}:([\d]+)/, 1]
161
+ message << highlight_config_error(config_file_path, line_nr.to_i)
162
+ end
163
+ raise ChefConfig::ConfigurationError, message
164
+ end
165
+
166
+ def highlight_config_error(file, line)
167
+ config_file_lines = []
168
+ IO.readlines(file).each_with_index { |l, i| config_file_lines << "#{(i + 1).to_s.rjust(3)}: #{l.chomp}" }
169
+ if line == 1
170
+ lines = config_file_lines[0..3]
171
+ else
172
+ lines = config_file_lines[Range.new(line - 2, line)]
173
+ end
174
+ "Relevant file content:\n" + lines.join("\n") + "\n"
175
+ end
176
+
177
+ def logger
178
+ @logger
179
+ end
180
+
181
+ end
182
+ end