Class: Commands::ConfigAmazon
- Inherits:
-
Object
- Object
- Commands::ConfigAmazon
- Defined in:
- lib/commands/config_amazon.rb
Instance Method Summary collapse
-
#options ⇒ Object
holds the options that were passed you can set any initial defaults here.
- #register(opts, global_options) ⇒ Object
-
#required_options ⇒ Object
required options.
- #run(global_options, amazon) ⇒ Object
Instance Method Details
#options ⇒ Object
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 ||= { } 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, ) opts. = "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| [:access_key] = v end opts.on("--skey AmazonSecretKey", "Required: Amazon secret key or environment AWS_SECRET_ACCESS_KEY") do |v| [:secret_key] = v end end |
#required_options ⇒ Object
required options
13 14 15 16 17 18 |
# File 'lib/commands/config_amazon.rb', line 13 def @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(, amazon) ec2 = amazon.ec2 access_key = [:access_key] secret_key = [: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.('.', "~/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 |