vagrant/lib/hobo/env.rb

96 lines
2.2 KiB
Ruby
Raw Normal View History

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"
# Initialize class variables used
@@persisted_vm = nil
@@root_path = nil
2010-01-26 08:01:17 +00:00
class << self
def persisted_vm; @@persisted_vm; end
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
def load!
load_root_path!
load_config!
load_vm!
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-02-02 06:14:40 +00:00
# Execute the configurations
Config.execute!
2010-01-22 07:38:23 +00:00
end
def load_vm!
2010-01-31 06:18:18 +00:00
File.open(dotfile_path) do |f|
@@persisted_vm = Hobo::VM.find(f.read)
end
rescue Errno::ENOENT
@@persisted_vm = nil
end
def persist_vm(vm)
2010-01-31 06:18:18 +00:00
File.open(dotfile_path, 'w+') do |f|
f.write(vm.uuid)
2010-01-31 06:18:18 +00:00
end
end
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
return
end
file = "#{path}/#{HOBOFILE_NAME}"
if File.exist?(file)
@@root_path = path.to_s
return
end
load_root_path!(path.parent)
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
def error_and_exit(error)
2010-01-31 04:01:34 +00:00
puts <<-error
=====================================================================
Hobo experienced an error!
#{error.chomp}
=====================================================================
error
exit
end
2010-01-22 07:38:23 +00:00
end
end
end