From 7f03c7db3af164433432ebeb708c34e4bf28b5b2 Mon Sep 17 00:00:00 2001 From: Wesley Beary Date: Sat, 9 Jan 2010 17:29:27 -0800 Subject: [PATCH] cleanup credential/connection management in bin/fog --- bin/fog | 83 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/bin/fog b/bin/fog index eac9f1963..c826d2952 100755 --- a/bin/fog +++ b/bin/fog @@ -3,8 +3,8 @@ require File.join(File.dirname(__FILE__), '..', 'lib', 'fog') require 'irb' require 'yaml' -module AWS - class << self +module Fog + module Credentials key = (ARGV.first && :"#{ARGV.first}") || :default unless Fog.credentials(key) print("\n To run as '#{key}', add credentials like the following to ~/.fog\n") @@ -21,44 +21,55 @@ YML print(yml) raise ArgumentError.new("No credentials for :#{key}") end + end +end + +module AWS + class << self + key = (ARGV.first && :"#{ARGV.first}") || :default if Fog.credentials(key)[:aws_access_key_id] && Fog.credentials(key)[:aws_secret_access_key] - @@ec2 = Fog::AWS::EC2.new( - :aws_access_key_id => Fog.credentials[:aws_access_key_id], - :aws_secret_access_key => Fog.credentials[:aws_secret_access_key] - ) - - @@s3 = Fog::AWS::S3.new( - :aws_access_key_id => Fog.credentials[:aws_access_key_id], - :aws_secret_access_key => Fog.credentials[:aws_secret_access_key] - ) + def connections + @@connections ||= Hash.new do |hash, key| + credentials = { + :aws_access_key_id => Fog.credentials[:aws_access_key_id], + :aws_secret_access_key => Fog.credentials[:aws_secret_access_key] + } + hash[key] = case key + when :ec2 + Fog::AWS::EC2.new(credentials) + when :s3 + Fog::AWS::S3.new(credentials) + end + end + end def addresses - @@ec2.addresses + connections[:ec2].addresses end def buckets - @@s3.buckets + connections[:s3].buckets end def servers - @@ec2.servers + connections[:ec2].servers end def key_pairs - @@ec2.key_pairs + connections[:ec2].key_pairs end def security_groups - @@ec2.security_groups + connections[:ec2].security_groups end def snapshots - @@ec2.snapshots + connections[:ec2].snapshots end def volumes - @@ec2.volumes + connections[:ec2].volumes end end @@ -68,38 +79,38 @@ end module Rackspace class << self key = (ARGV.first && :"#{ARGV.first}") || :default - unless Fog.credentials(key) - print("\n To run as '#{key}', add credentials like the following to ~/.fog\n") - yml = <<-YML - -:#{key}: - :aws_access_key_id: INTENTIONALLY_LEFT_BLANK - :aws_secret_access_key: INTENTIONALLY_LEFT_BLANK - :rackspace_api_key: INTENTIONALLY_LEFT_BLANK - :rackspace_username: INTENTIONALLY_LEFT_BLANK - :slicehost_password: INTENTIONALLY_LEFT_BLANK - -YML - print(yml) - raise ArgumentError.new("No credentials for :#{key}") - end if Fog.credentials(key)[:rackspace_api_key] && Fog.credentials(key)[:rackspace_username] + def connections + @@connections ||= Hash.new do |hash, key| + credentials = { + :rackspace_api_key => Fog.credentials[:rackspace_api_key], + :rackspace_username => Fog.credentials[:rackspace_username] + } + hash[key] = case key + when :files + Fog::Rackspace::Files.new(credentials) + when :servers + Fog::Rackspace::Servers.new(credentials) + end + end + end + @@servers = Fog::Rackspace::Servers.new( :rackspace_api_key => Fog.credentials[:rackspace_api_key], :rackspace_username => Fog.credentials[:rackspace_username] ) def flavors - @@servers.flavors + connections[:servers].flavors end def images - @@servers.images + connections[:servers].images end def servers - @@servers.servers + connections[:servers].servers end end