Sha256: f3e0d39bc3706a6428bd4519182c156c3b36079bdbdf8556f38e282fded5bde9

Contents?: true

Size: 1.8 KB

Versions: 17

Compression:

Stored size: 1.8 KB

Contents

module AwsEc2
  class Profile < Base
    include AwsEc2::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("profiles", "tmp")
        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?("run_instances") ? data["run_instances"] : 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 ec2 instance
      "default"
    end

    def profile_file(name)
      "#{AwsEc2.root}/profiles/#{name}.yml"
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
aws-ec2-1.4.9 lib/aws_ec2/profile.rb
aws-ec2-1.4.8 lib/aws_ec2/profile.rb
aws-ec2-1.4.7 lib/aws_ec2/profile.rb
aws-ec2-1.4.6 lib/aws_ec2/profile.rb
aws-ec2-1.4.5 lib/aws_ec2/profile.rb
aws-ec2-1.4.4 lib/aws_ec2/profile.rb
aws-ec2-1.4.3 lib/aws_ec2/profile.rb
aws-ec2-1.4.2 lib/aws_ec2/profile.rb
aws-ec2-1.4.1 lib/aws_ec2/profile.rb
aws-ec2-1.4.0 lib/aws_ec2/profile.rb
aws-ec2-1.3.2 lib/aws_ec2/profile.rb
aws-ec2-1.3.1 lib/aws_ec2/profile.rb
aws-ec2-1.3.0 lib/aws_ec2/profile.rb
aws-ec2-1.2.2 lib/aws_ec2/profile.rb
aws-ec2-1.2.1 lib/aws_ec2/profile.rb
aws-ec2-1.2.0 lib/aws_ec2/profile.rb
aws-ec2-1.1.0 lib/aws_ec2/profile.rb