2010-01-30 04:25:10 +00:00
|
|
|
require 'yaml'
|
|
|
|
|
2010-01-22 07:38:23 +00:00
|
|
|
module Hobo
|
|
|
|
class Env
|
2010-02-02 06:14:40 +00:00
|
|
|
HOBOFILE_NAME = "Hobofile"
|
2010-01-30 09:03:18 +00:00
|
|
|
|
2010-01-31 03:58:07 +00:00
|
|
|
# Initialize class variables used
|
2010-01-31 09:16:44 +00:00
|
|
|
@@persisted_vm = nil
|
2010-01-31 03:58:07 +00:00
|
|
|
@@root_path = nil
|
2010-01-22 07:57:43 +00:00
|
|
|
|
2010-01-26 08:01:17 +00:00
|
|
|
class << self
|
2010-01-31 09:16:44 +00:00
|
|
|
def persisted_vm; @@persisted_vm; end
|
2010-01-31 03:58:07 +00:00
|
|
|
def root_path; @@root_path; end
|
2010-02-02 06:14:40 +00:00
|
|
|
def dotfile_path; File.join(root_path, Hobo.config.dotfile_name); end
|
2010-01-31 03:58:07 +00:00
|
|
|
|
2010-01-30 07:22:03 +00:00
|
|
|
def load!
|
2010-01-31 03:58:07 +00:00
|
|
|
load_root_path!
|
2010-01-30 07:22:03 +00:00
|
|
|
load_config!
|
2010-01-31 09:16:44 +00:00
|
|
|
load_vm!
|
2010-01-30 07:22:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def load_config!
|
2010-02-02 06:14:40 +00:00
|
|
|
load_paths = [
|
|
|
|
File.join(PROJECT_ROOT, "config", "default.rb"),
|
|
|
|
File.join(root_path, HOBOFILE_NAME)
|
|
|
|
]
|
|
|
|
|
|
|
|
load_paths.each do |path|
|
|
|
|
HOBO_LOGGER.info "Loading config from #{path}..."
|
|
|
|
load path if File.exist?(path)
|
|
|
|
end
|
2010-01-31 03:58:07 +00:00
|
|
|
|
2010-02-02 06:14:40 +00:00
|
|
|
# Execute the configurations
|
|
|
|
Config.execute!
|
2010-01-22 07:38:23 +00:00
|
|
|
end
|
2010-01-30 07:22:03 +00:00
|
|
|
|
2010-01-31 09:16:44 +00:00
|
|
|
def load_vm!
|
2010-01-31 06:18:18 +00:00
|
|
|
File.open(dotfile_path) do |f|
|
2010-02-01 02:10:01 +00:00
|
|
|
@@persisted_vm = Hobo::VM.find(f.read)
|
2010-01-31 03:58:07 +00:00
|
|
|
end
|
|
|
|
rescue Errno::ENOENT
|
2010-01-31 09:16:44 +00:00
|
|
|
@@persisted_vm = nil
|
2010-01-30 08:46:56 +00:00
|
|
|
end
|
|
|
|
|
2010-01-31 09:16:44 +00:00
|
|
|
def persist_vm(vm)
|
2010-01-31 06:18:18 +00:00
|
|
|
File.open(dotfile_path, 'w+') do |f|
|
2010-01-31 09:16:44 +00:00
|
|
|
f.write(vm.uuid)
|
2010-01-31 06:18:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-31 03:58:07 +00:00
|
|
|
def load_root_path!(path=Pathname.new(Dir.pwd))
|
|
|
|
if path.to_s == '/'
|
2010-01-31 04:01:34 +00:00
|
|
|
error_and_exit(<<-msg)
|
|
|
|
A `Hobofile` was not found! This file is required for hobo to run
|
|
|
|
since it describes the expected environment that hobo is supposed
|
|
|
|
to manage. Please create a Hobofile and place it in your project
|
|
|
|
root.
|
|
|
|
msg
|
2010-01-31 03:58:07 +00:00
|
|
|
return
|
|
|
|
end
|
2010-01-30 08:46:56 +00:00
|
|
|
|
2010-01-31 03:58:07 +00:00
|
|
|
file = "#{path}/#{HOBOFILE_NAME}"
|
|
|
|
if File.exist?(file)
|
|
|
|
@@root_path = path.to_s
|
|
|
|
return
|
2010-01-30 07:48:05 +00:00
|
|
|
end
|
2010-01-31 03:58:07 +00:00
|
|
|
|
|
|
|
load_root_path!(path.parent)
|
2010-01-30 07:22:03 +00:00
|
|
|
end
|
|
|
|
|
2010-01-31 09:27:18 +00:00
|
|
|
def require_persisted_vm
|
|
|
|
if !persisted_vm
|
|
|
|
error_and_exit(<<-error)
|
|
|
|
The task you're trying to run requires that the hobo environment
|
|
|
|
already be created, but unfortunately this hobo still appears to
|
|
|
|
have no box! You can setup the environment by setting up your
|
|
|
|
Hobofile and running `hobo up`
|
|
|
|
error
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-31 03:58:07 +00:00
|
|
|
def error_and_exit(error)
|
2010-01-31 04:01:34 +00:00
|
|
|
puts <<-error
|
|
|
|
=====================================================================
|
|
|
|
Hobo experienced an error!
|
|
|
|
|
|
|
|
#{error.chomp}
|
|
|
|
=====================================================================
|
|
|
|
error
|
2010-01-31 03:58:07 +00:00
|
|
|
exit
|
2010-01-30 07:22:03 +00:00
|
|
|
end
|
2010-01-22 07:38:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|