Sha256: b51cf375aa6f1a4f4bf1ff29c29d6288fb97ad5a9d0ade2f3869c160e37289d1

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

module Balancer
  class Profile < Base
    include Balancer::Template

    def load
      return @profile_params if @profile_params

      check!

      file = profile_file(profile_name)
      @profile_params = load_profile(file)
    end

    def check!
      file = profile_file(profile_name)
      return if File.exist?(file)

      puts "Unable to find a #{file.colorize(:green)} profile file."
      puts "Please double check that it exists or that you specified the right profile.".colorize(:red)
      exit 1
    end

    def load_profile(file)
      return {} unless File.exist?(file)

      puts "Using profile: #{file}".colorize(:green)
      text = RenderMePretty.result(file, context: context)
      begin
        data = YAML.load(text)
      rescue Psych::SyntaxError => e
        tmp_file = file.sub(".balancer", "tmp")
        FileUtils.mkdir_p(File.dirname(tmp_file))
        IO.write(tmp_file, text)
        puts "There was an error evaluating in your yaml file #{file}".colorize(:red)
        puts "The evaludated yaml file has been saved at #{tmp_file} for debugging."
        puts "ERROR: #{e.message}"
        exit 1
      end
      data ? data : {} # in case the file is empty
      data.has_key?("create_load_balancer") ? data["create_load_balancer"] : data
    end

    # Determines a valid profile_name. Falls back to default
    def profile_name
      # allow user to specify the path also
      if @options[:profile] && File.exist?(@options[:profile])
        filename_profile = File.basename(@options[:profile], '.yml')
      end

      name = derandomize(@name)
      if File.exist?(profile_file(name))
        name_profile = name
      end

      filename_profile ||
      @options[:profile] ||
      name_profile || # conventional profile is the name of the elb
      "default"
    end

    def profile_file(name)
      "#{Balancer.root}/.balancer/#{name}.yml"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
balancer-0.2.0 lib/balancer/profile.rb