Class: Commands::ConfigAmazon

Inherits:
Object
  • Object
show all
Defined in:
lib/commands/config_amazon.rb

Instance Method Summary collapse

Instance Method Details

#optionsObject

holds the options that were passed you can set any initial defaults here



7
8
9
10
# File 'lib/commands/config_amazon.rb', line 7

def options
  @options ||= {
  }
end

#register(opts, global_options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/commands/config_amazon.rb', line 20

def register(opts, global_options)
  opts.banner = "Usage: config_amazon [options]"
  opts.description = "Write the amazon key configuration"

  opts.on("--akey AmazonAccessKey", "Required: Amazon access key, or environment AWS_ACCESS_KEY_ID.") do |v|
    options[:access_key] = v
  end

  opts.on("--skey AmazonSecretKey", "Required: Amazon secret key or environment AWS_SECRET_ACCESS_KEY") do |v|
    options[:secret_key] = v
  end
end

#required_optionsObject

required options



13
14
15
16
17
18
# File 'lib/commands/config_amazon.rb', line 13

def required_options
  @required_options ||= Set.new [
      :access_key,
      :secret_key
  ]
end

#run(global_options, amazon) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/commands/config_amazon.rb', line 34

def run(global_options, amazon)
  ec2 = amazon.ec2

  access_key = options[:access_key]
  secret_key = options[:secret_key]

  info = {
      :aws_access_key_id => access_key,
      :aws_secret_access_key => secret_key
  }

  # make sure the chef dir exists
  chef_dir = "/var/chef"
  `sudo mkdir -p #{chef_dir}`

  # generate the json into a temp file
  json = JSON.pretty_generate(info)
  amazon_path = "#{chef_dir}/amazon.json"
  temp_path = File.expand_path('.', "~/amazon_temp.json")
  File.open(temp_path, 'w') {|f| f.write(json) }

  # now move the file and set permissions
  `sudo cp #{temp_path} #{amazon_path}`
  cmd = "sudo chown `whoami` #{amazon_path} && sudo chmod 0644 #{amazon_path}"
  `#{cmd}`
  # remove the temp file
  `rm -f #{temp_path}`
  puts "Your amazon config has been saved to #{amazon_path}"
end