Permalink
Browse files

updated cloud

  • Loading branch information...
1 parent 9f2b86b commit ce6309864d79c44a08e6cf46c618cedd8d39d48b @mikz committed Apr 17, 2012
Showing with 88 additions and 48 deletions.
  1. +3 −3 examples/cloud/cloud.gemspec
  2. +25 −6 examples/cloud/lib/cloud/cli.rb
  3. +60 −39 examples/cloud/spec/cloud_spec.rb
@@ -14,9 +14,9 @@ Gem::Specification.new do |gem|
gem.name = "cloud"
gem.require_paths = ["lib"]
gem.version = Cloud::VERSION
-
+
gem.add_dependency "fog"
- gem.add_dependency "thor"
-
+ gem.add_dependency "thor", '>= 0.15.0.rc2'
+
gem.add_development_dependency "rspec"
end
@@ -6,26 +6,45 @@ module Cloud
class CLI < Thor
desc "create IMAGE_ID", "create new server on EC2 from IMAGE_ID"
-
+
method_option :type, default: "t1.micro", desc: "Specify how big instance should be created", type: :string
method_option :key, default: Socket.gethostname, desc: "Specify which key pair should be used for instance", type: :string
-
+
def create(image_id)
server = connection.servers.create image_id: image_id, flavor_id: options[:type], key_name: options[:key]
server.wait_for { print "."; ready? }
say "started #{image_id} #{server.flavor_id} instance #=> id: #{server.id}, dns: #{server.dns_name}"
server
end
+ desc "list", "list all EC2 servers"
+ def list
+ servers = connection.servers.all
+ return say "No servers found" unless servers.any?
+
+ servers.each do |server|
+ say "#{server.id}\t#{server.dns_name}\t#{server.image_id}\t#{server.flavor_id}"
+ end
+ end
+
+ desc "destroy ID", "destroy server with ID"
+ def destroy(id)
+ server = connection.servers.get(id)
+ return say "No server with id #{id} found" unless server
+
+ server.destroy
+ say "Server #{id} will be destroyed in a minute"
+ end
+
private
def connection
@connection ||= Fog::Compute::AWS.new(
- aws_secret_access_key: ENV['AWS_SECRET'],
- aws_access_key_id: ENV['AWS_KEY'],
+ aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
+ aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
region: 'eu-west-1'
)
end
end
-
-end
+
+end
@@ -1,66 +1,87 @@
require "cloud"
+Fog::Mock.delay = 0
+
describe Cloud::CLI do
before { Fog.mock! }
after { Fog.unmock! }
+ after { Fog::Mock.reset }
- context 'create task' do
- let(:task) { Cloud::CLI.all_tasks['create'] }
+ let(:shell) { Thor::Base.shell.new }
+ let(:stdout) { StringIO.new }
+ let(:output) { stdout.string }
+ before { shell.stub(:stdout) { stdout } }
- subject { task }
+ let(:key) { ENV["AWS_ACCESS_KEY_ID"] = 'key' }
+ let(:secret) { ENV["AWS_SECRET_ACCESS_KEY"] = 'secret' }
- it { should be }
+ let(:connection) { Fog::Compute::AWS.new(aws_access_key_id: key, aws_secret_access_key: secret, region: "eu-west-1") }
+ let(:key_pair) { connection.key_pairs.create(name: "hostname") }
- context 'type option' do
- subject { task.options[:type] }
- its(:default){ should == "t1.micro" }
- end
+ let(:task) { Cloud::CLI.start(command, shell: shell) }
- context 'key option' do
- subject { task.options[:key] }
- its(:default){ should == Socket.gethostname }
- end
- end
+ subject { task && output }
- context "cli" do
- let(:cli) { Cloud::CLI.new(['ami-id'], ["--key=#{key_pair.name}", "--type=t1.mini"]) }
- let(:server) { cli.invoke(:create) }
- let(:null_shell) { double("shell").as_null_object }
+ context "creates server" do
+ let(:command) { %W(create ami-id --key=#{key_pair.name} --type=t1.mini) }
- let(:connection) { Fog::Compute::AWS.new(aws_access_key_id: ENV["AWS_KEY"], aws_secret_access_key: ENV["AWS_SECRET"], region: "eu-west-1") }
- let(:key_pair) { connection.key_pairs.create(name: "hostname") }
+ it("prints ami of server") { should match('started ami-id') }
+ it("prints type of server") { should match('t1.mini') }
+ end
- before { Thor::Base.shell = null_shell }
- after { key_pair.destroy }
+ context 'list servers' do
+ let(:command) { %W(list) }
- before { ENV["AWS_KEY"], ENV["AWS_SECRET"] = "key", "secret" }
- after { ENV["AWS_KEY"] = ENV["AWS_SECRET"] = nil }
+ let(:custom) { connection.servers.create(image_id: 'custom-ami') }
+ let(:other) { connection.servers.create(image_id: 'other-ami') }
- subject { cli }
+ before do
+ custom.wait_for(&:ready?)
+ other.wait_for(&:ready?)
+ end
- it 'returns fog server' do
- cli.create('ami-id').should be_kind_of(Fog::Compute::AWS::Server)
+ it("prints ami of servers") do
+ should match(custom.image_id)
+ should match(other.image_id)
end
- context "server" do
- subject { server }
+ it "prints dns of servers" do
+ should match(custom.dns_name)
+ should match(other.dns_name)
+ end
+ end
- its(:key_name) { should == key_pair.name }
- its(:image_id) { should == "ami-id" }
- its(:flavor_id) { should == "t1.mini" }
+ context "destroy server" do
+ let(:command) { %W(destroy #{server.id}) }
+ let(:server) { connection.servers.create(image_id: 'server-ami') }
+
+ before { server.wait_for(&:ready?) }
+
+ it "prints message" do
+ should match("Server #{server.id} will be destroyed")
end
- context "output" do
- let(:stdout) { StringIO.new }
- let(:output) { server && stdout.string }
- let(:stub_shell) { null_shell.stub(:say){ |*args| stdout.puts *args } && null_shell }
+ context "which doesn't exist" do
+ let(:command) { %w(destroy some-id) }
- before { Thor::Base.shell = stub_shell }
+ it "prints message" do
+ should match("No server with id some-id found")
+ end
+ end
+ end
- subject { output }
+ context 'create task' do
+ let(:task) { Cloud::CLI.all_tasks['create'] }
- it { should match(/started ami-id/) }
- it { should match(/t1\.mini/) }
+ context 'type option' do
+ subject { task.options[:type] }
+ its(:default){ should == "t1.micro" }
+ end
+
+ context 'key option' do
+ subject { task.options[:key] }
+ its(:default){ should == Socket.gethostname }
end
end
+
end

0 comments on commit ce63098

Please sign in to comment.