-
Notifications
You must be signed in to change notification settings - Fork 0
/
puppet-3.7.5-externalEnvironments-0.1.diff
222 lines (220 loc) · 8 KB
/
puppet-3.7.5-externalEnvironments-0.1.diff
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
diff --git a/lib/puppet.rb b/lib/puppet.rb
index 954dcbc..6bd1c7b 100644
--- a/lib/puppet.rb
+++ b/lib/puppet.rb
@@ -178,22 +178,39 @@ module Puppet
# The bindings used for initialization of puppet
# @api private
def self.base_context(settings)
- environments = settings[:environmentpath]
- modulepath = Puppet::Node::Environment.split_path(settings[:basemodulepath])
-
- if environments.empty?
- loaders = [Puppet::Environments::Legacy.new]
- else
- loaders = Puppet::Environments::Directories.from_path(environments, modulepath)
- # in case the configured environment (used for the default sometimes)
- # doesn't exist
- default_environment = Puppet[:environment].to_sym
- if default_environment == :production
- loaders << Puppet::Environments::StaticPrivate.new(
- Puppet::Node::Environment.create(Puppet[:environment].to_sym,
- [],
- Puppet::Node::Environment::NO_MANIFEST))
- end
+ environment_locator = settings[:environment_locator]
+
+ case environment_locator
+ when "direnv", "external"
+ if ("direnv" == environment_locator)
+ environments = settings[:environmentpath]
+ modulepath = Puppet::Node::Environment.split_path(settings[:basemodulepath])
+
+ loaders = Puppet::Environments::Directories.from_path(environments, modulepath)
+
+ elsif ("external" == environment_locator)
+ modulepath = Puppet::Node::Environment.split_path(settings[:basemodulepath])
+ loaders = [Puppet::Environments::Exec.new(settings[:external_environment_locator], modulepath)]
+
+ else
+ raise Puppet::Error, "Internal error"
+ end
+
+ # in case the configured environment (used for the default sometimes)
+ # doesn't exist
+ default_environment = Puppet[:environment].to_sym
+ if default_environment == :production
+ loaders << Puppet::Environments::StaticPrivate.new(
+ Puppet::Node::Environment.create(Puppet[:environment].to_sym,
+ [],
+ Puppet::Node::Environment::NO_MANIFEST))
+ end
+
+ when "legacy"
+ loaders = [Puppet::Environments::Legacy.new]
+
+ else
+ raise Puppet::Error, "Invalid environment_locator configuration value: #{environment_locator}"
end
{
diff --git a/lib/puppet/defaults.rb b/lib/puppet/defaults.rb
index abbc728..26365d0 100644
--- a/lib/puppet/defaults.rb
+++ b/lib/puppet/defaults.rb
@@ -242,6 +242,29 @@ module Puppet
all files referenced with `import` statements to exist. This setting was primarily
designed for use with commit hooks for parse-checking.",
},
+ :environment_locator => {
+ :type => :string,
+ :default => "direnv",
+ :desc => "How to locate environments.
+
+ Possible values are:
+ - 'direnv' : directory environments (default)
+ - 'external': (use external program to find environments)
+ - 'legacy' : legacy environment configuration (environments listed in puppet.conf)",
+ },
+ :external_environment_locator => {
+ :default => "none",
+ :desc => "An external command that can produce environment information.
+ The command's output must be a YAML dump of a hash, and that hash must
+ have a `found` key (boolean) that indicates whether environment has
+ been found, and if found, `path` key must exist which informs puppet
+ where data for specified environment reside.
+ For unknown environments, the command should return YAML hash with the
+ key `found` set to false, and use zero as exit status.
+
+ This command makes it straightforward to store your environment mapping
+ information in other data sources like databases or git repository.",
+ },
:environment => {
:default => "production",
:desc => "The environment Puppet is running in. For clients
diff --git a/lib/puppet/environments.rb b/lib/puppet/environments.rb
index 0b2aa51..95a7bd3 100644
--- a/lib/puppet/environments.rb
+++ b/lib/puppet/environments.rb
@@ -272,6 +272,123 @@ module Puppet::Environments
end
end
+
+
+ # Reads environment data from external program, and uses that.
+ #
+ # External program must provide data in YAML format. See file
+ # defaults.rb for details.
+ #
+ # @api private
+ class Exec
+ include EnvironmentLoader
+
+ def initialize(external_environment_locator, global_module_path)
+ @external_environment_locator = external_environment_locator
+ @global_module_path = global_module_path
+ end
+
+ # @!macro loader_get
+ def get(name)
+ envData = get_env_data(name)
+ if (envData[:found] == true) then
+ if valid_directory?(envData[:path])
+ Puppet.notice("External environment locator: environment #{name} found at #{envData[:path]}")
+ create_environment(name)
+ end
+ end
+ end
+
+ # @!macro loader_get_conf
+ def get_conf(name)
+ envData = get_env_data(name)
+ if (envData[:found] == true) then
+ if valid_directory?(envData[:path])
+ return Puppet::Settings::EnvironmentConf.load_from(envData[:path], @global_module_path)
+ end
+ end
+ nil
+ end
+
+
+ private
+
+
+ def get_env_data(name)
+ # Check configuration
+ external_command = @external_environment_locator.split
+ raise ArgumentError, "You must set the 'external_environment_locator' parameter to use the external environment locator" unless external_command != "none"
+
+ # Make sure it's an array
+ raise Puppet::DevError, "Exec commands must be an array" unless external_command.is_a?(Array)
+
+ # Make sure it's fully qualified.
+ raise ArgumentError, "You must set the exec parameter to a fully qualified command" unless Puppet::Util.absolute_path?(external_command[0])
+
+ # Add our name to it.
+ external_command << name
+ begin
+ output = execute(external_command, :failonfail => true, :combine => false)
+ rescue Puppet::ExecutionFailure => detail
+ raise Puppet::Error, "Failed to find environment #{name} via exec: #{detail}", detail.backtrace
+ end
+
+ if output =~ /\A\s*\Z/ # all whitespace
+ Puppet.debug "Empty response for environment #{name} from #{self.name} terminus"
+ return nil
+ else
+ return translate(name, output)
+ end
+ end
+
+
+ # Proxy the execution, so it's easier to test.
+ def execute(command, arguments)
+ Puppet::Util::Execution.execute(command,arguments)
+ end
+
+
+ # Translate the yaml string into Ruby objects.
+ def translate(name, output)
+ YAML.load(output).inject({}) do |hash, data|
+ case data[0]
+ when String
+ hash[data[0].intern] = data[1]
+ when Symbol
+ hash[data[0]] = data[1]
+ else
+ raise Puppet::Error, "key is a #{data[0].class}, not a string or symbol"
+ end
+
+ hash
+ end
+
+ rescue => detail
+ raise Puppet::Error, "Could not load external environment data for #{name}: #{detail}", detail.backtrace
+ end
+
+
+ def valid_directory?(envdir)
+ name = Puppet::FileSystem.basename_string(envdir)
+ Puppet::FileSystem.directory?(envdir) &&
+ Puppet::Node::Environment.valid_name?(name)
+ end
+
+
+ def create_environment(name, setting_values = nil)
+ env_symbol = name.intern
+ setting_values = Puppet.settings.values(env_symbol, Puppet.settings.preferred_run_mode)
+ Puppet::Node::Environment.create(
+ env_symbol,
+ Puppet::Node::Environment.split_path(setting_values.interpolate(:modulepath)),
+ setting_values.interpolate(:manifest),
+ setting_values.interpolate(:config_version)
+ )
+ end
+ end
+
+
+
# Combine together multiple loaders to act as one.
# @api private
class Combined