Module: Chef::Knife::AwsBase

Defined in:
lib/chef/knife/aws_base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/chef/knife/aws_base.rb', line 7

def self.included(includer)
  includer.class_eval do

    deps do
      require 'fog'
      require 'readline'
      require 'chef/json_compat'
    end

    option :aws_credential_file,
           :long => "--aws-credential-file FILE",
           :description => "File containing AWS credentials as used by aws cmdline tools",
           :proc => Proc.new { |key| Chef::Config[:knife][:aws_credential_file] = key }

    option :aws_access_key_id,
           :short => "-A ID",
           :long => "--aws-access-key-id KEY",
           :description => "Your AWS Access Key ID",
           :proc => Proc.new { |key| Chef::Config[:knife][:aws_access_key_id] = key }

    option :aws_secret_access_key,
           :short => "-K SECRET",
           :long => "--aws-secret-access-key SECRET",
           :description => "Your AWS API Secret Access Key",
           :proc => Proc.new { |key| Chef::Config[:knife][:aws_secret_access_key] = key }

    option :region,
           :long => "--region REGION",
           :description => "Your AWS region",
           :proc => Proc.new { |key| Chef::Config[:knife][:region] = key }
  end
end

Instance Method Details

#connectionObject

Raises:

  • (StandardError)


40
41
42
# File 'lib/chef/knife/aws_base.rb', line 40

def connection
  raise StandardError, "Abstract Method Not Implemented: 'connection'"
end

#locate_config_value(key) ⇒ Object



45
46
47
48
49
# File 'lib/chef/knife/aws_base.rb', line 45

def locate_config_value(key)
  key = key.to_sym
  config={} if config.nil?
  config[key] || Chef::Config[:knife][key]
end

#msg_pair(label, value, color = :cyan) ⇒ Object



51
52
53
54
55
# File 'lib/chef/knife/aws_base.rb', line 51

def msg_pair(label, value, color=:cyan)
  if value && !value.to_s.empty?
    puts "#{ui.color(label, color)}: #{value}"
  end
end

#validate!(keys = [:aws_access_key_id, :aws_secret_access_key]) ⇒ Object



57
58
59
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
85
86
87
88
89
90
# File 'lib/chef/knife/aws_base.rb', line 57

def validate!(keys=[:aws_access_key_id, :aws_secret_access_key])
  errors = []

  unless Chef::Config[:knife][:aws_credential_file].nil?
    unless (Chef::Config[:knife].keys & [:aws_access_key_id, :aws_secret_access_key]).empty?
      errors << "Either provide a credentials file or the access key and secret keys but not both."
    end
    # File format:
    # AWSAccessKeyId=somethingsomethingdarkside
    # AWSSecretKey=somethingsomethingcomplete
    #               OR
    # aws_access_key_id = somethingsomethingdarkside
    # aws_secret_access_key = somethingsomethingdarkside

    aws_creds = []
    File.read(Chef::Config[:knife][:aws_credential_file]).each_line do | line |
      aws_creds << line.split("=").map(&:strip) if line.include?("=")
    end
    entries = Hash[*aws_creds.flatten]
    Chef::Config[:knife][:aws_access_key_id] = entries['AWSAccessKeyId'] || entries['aws_access_key_id']
    Chef::Config[:knife][:aws_secret_access_key] = entries['AWSSecretKey'] || entries['aws_secret_access_key']
  end

  keys.each do |k|
    pretty_key = k.to_s.gsub(/_/, ' ').gsub(/\w+/){ |w| (w =~ /(ssh)|(aws)/i) ? w.upcase  : w.capitalize }
    if Chef::Config[:knife][k].nil?
      errors << "You did not provide a valid '#{pretty_key}' value."
    end
  end

  if errors.each{|e| ui.error(e)}.any?
    exit 1
  end
end