chef-config 12.8.1 → 12.9.38

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.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
+ # 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.expand_path("../..", __FILE__)
24
+ VERSION = "12.9.38"
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,182 +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
- 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
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