vagrant/lib/hobo/env.rb

96 lines
2.3 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
HOBOFILE_NAME = "hobofile"
HOME = File.expand_path('~/.hobo')
CONFIG = { File.join(HOME, 'config.yml') => '/config/default.yml' }
ENSURE = {
:files => CONFIG.merge({}), #additional files go mhia!
:dirs => [HOME] #additional dirs go mhia!
}
# 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-01-31 06:18:18 +00:00
def dotfile_path; File.join(root_path, Hobo.config[:dotfile_name]); end
def load!
load_root_path!
load_config!
load_vm!
end
2010-01-26 08:01:17 +00:00
def ensure_directories
ENSURE[:dirs].each do |name|
Dir.mkdir(name) unless File.exists?(name)
end
2010-01-22 07:38:23 +00:00
end
2010-01-26 08:01:17 +00:00
def ensure_files
ENSURE[:files].each do |target, default|
File.copy(File.join(PROJECT_ROOT, default), target) unless File.exists?(target)
end
end
def load_config!
2010-01-26 08:01:17 +00:00
ensure_directories
ensure_files
HOBO_LOGGER.info "Loading config from #{CONFIG.keys.first}"
2010-01-30 06:39:45 +00:00
parsed = YAML.load_file(CONFIG.keys.first)
2010-01-26 08:01:17 +00:00
Hobo.config!(parsed)
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 = VirtualBox::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
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