lib/vagrant-orchestrate/plugin.rb in vagrant-orchestrate-0.2.1 vs lib/vagrant-orchestrate/plugin.rb in vagrant-orchestrate-0.3.0

- old
+ new

@@ -74,14 +74,56 @@ # sure that the log level is an integer, as Log4r requires. level = nil unless level.is_a?(Integer) # Set the logging level on all "vagrant" namespaced # logs as long as we have a valid level. - if level - Log4r::Logger.new("vagrant_orchestrate").tap do |logger| - logger.outputters = Log4r::Outputter.stderr - logger.level = level + @logger = Log4r::Logger.new("vagrant_orchestrate").tap do |logger| + logger.outputters = Log4r::Outputter.stderr + logger.level = level || 6 + end + end + + def self.read_git_branch + @logger.debug("Reading git branch") + if ENV["GIT_BRANCH"] + git_branch = ENV["GIT_BRANCH"] + @logger.debug("Read git branch #{git_branch} from GIT_BRANCH environment variable") + else + command = "git rev-parse --abbrev-ref HEAD" + git_branch = `#{command}`.chomp + if git_branch.include? "fatal" + @logger.error("Unable to determine git branch `#{command}`. Is this a git repo?") + git_branch = nil + else + @logger.debug("Read git branch #{git_branch} using `#{command}`") end + end + git_branch + end + + def self.load_servers_for_branch + setup_logging + + git_branch = read_git_branch + return [] if git_branch.nil? + + begin + fail "servers.json not found" unless File.exist?("servers.json") + @logger.debug("Reading servers.json") + contents = IO.read("servers.json") + @logger.debug("Read servers.json:\n: #{contents}") + + environments = JSON.parse(contents)["environments"] + if environments.key? git_branch + return environments[git_branch]["servers"] + else + @logger.info("No environment found for #{git_branch}, no servers loaded.") + return [] + end + rescue StandardError => ex + # Don't break the user's whole vagrantfile if we can't load the environment + @logger.error(ex.message) + return [] end end end end end