Class: VagrantPlugins::Orchestrate::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-orchestrate/plugin.rb

Class Method Summary collapse

Class Method Details

.load_servers_for_branchObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/vagrant-orchestrate/plugin.rb', line 105

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

.read_git_branchObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/vagrant-orchestrate/plugin.rb', line 86

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 2>&1"
    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
  ENV["VAGRANT_ORCHESTRATE_GIT_BRANCH"] = git_branch
  git_branch
end

.setup_i18nObject

This initializes the internationalization strings.



54
55
56
57
# File 'lib/vagrant-orchestrate/plugin.rb', line 54

def self.setup_i18n
  I18n.load_path << File.expand_path("locales/en.yml", Orchestrate.source_root)
  I18n.reload!
end

.setup_loggingObject

This sets up our log level to be whatever VAGRANT_LOG is.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/vagrant-orchestrate/plugin.rb', line 60

def self.setup_logging
  require "log4r"

  level = nil
  begin
    level = Log4r.const_get(ENV["VAGRANT_LOG"].upcase)
  rescue NameError
    # This means that the logging constant wasn't found,
    # which is fine. We just keep `level` as `nil`. But
    # we tell the user.
    level = nil
  end

  # Some constants, such as "true" resolve to booleans, so the
  # above error checking doesn't catch it. This will check to make
  # 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.
  @logger = Log4r::Logger.new("vagrant_orchestrate").tap do |logger|
    logger.outputters = Log4r::Outputter.stderr
    logger.level = level || 6
  end
end