summaryrefslogtreecommitdiff
path: root/lib/chef/environment.rb
diff options
context:
space:
mode:
authorNick Stielau <nick.stielau@gmail.com>2013-01-08 08:11:41 -0800
committerBryan McLellan <btm@opscode.com>2013-05-30 09:41:13 -0700
commit9ca147c954ff5e93c2c5d80263486bbfbbbc4666 (patch)
treefc4b577de65490ca4056b5ec9eb6fea79381d3f8 /lib/chef/environment.rb
parent8813848a337e8d0bb93aca57edb022f3c2d1df88 (diff)
downloadchef-9ca147c954ff5e93c2c5d80263486bbfbbbc4666.tar.gz
Adding support for environments when running under chef-solo.
This commit refactors and rebases Kyle Goodwin's original work for CHEF-3356, as per Bryan McLellan's 12/17 comments.
Diffstat (limited to 'lib/chef/environment.rb')
-rw-r--r--lib/chef/environment.rb28
1 files changed, 27 insertions, 1 deletions
diff --git a/lib/chef/environment.rb b/lib/chef/environment.rb
index 00cc253083..0e1b03c87f 100644
--- a/lib/chef/environment.rb
+++ b/lib/chef/environment.rb
@@ -2,6 +2,7 @@
# Author:: Stephen Delano (<stephen@opscode.com>)
# Author:: Seth Falcon (<seth@opscode.com>)
# Author:: John Keiser (<jkeiser@ospcode.com>)
+# Author:: Kyle Goodwin (<kgoodwin@primerevenue.com>)
# Copyright:: Copyright 2010-2011 Opscode, Inc.
# License:: Apache License, Version 2.0
#
@@ -236,7 +237,32 @@ class Chef
end
def self.load(name)
- chef_server_rest.get_rest("environments/#{name}")
+ if Chef::Config[:solo]
+ load_from_file(name)
+ else
+ chef_server_rest.get_rest("environments/#{name}")
+ end
+ end
+
+ def self.load_from_file(name)
+ unless File.directory?(Chef::Config[:environment_path])
+ raise Chef::Exceptions::InvalidEnvironmentPath, "Environment path '#{Chef::Config[:environment_path]}' is invalid"
+ end
+
+ js_file = File.join(Chef::Config[:environment_path], "#{name}.json")
+ rb_file = File.join(Chef::Config[:environment_path], "#{name}.rb")
+
+ if File.exists?(js_file)
+ # from_json returns object.class => json_class in the JSON.
+ Chef::JSONCompat.from_json(IO.read(js_file))
+ elsif File.exists?(rb_file)
+ environment = Chef::Environment.new
+ environment.name(name)
+ environment.from_file(rb_file)
+ environment
+ else
+ raise Chef::Exceptions::EnvironmentNotFound, "Environment '#{name}' could not be loaded from disk"
+ end
end
def destroy