# File lib/rudy/cli/aws/ec2/instances.rb, line 27
27:     def instances_create
28:       
29:       opts = {                 # Defaults
30:         :group => 'default',
31:         :size => 'm1.small',
32:         :zone => @@global.zone
33:       }
34:             
35:       if @option.address
36:         raise "Cannot specify both -a and -n" if @option.newaddress
37:         unless Rudy::AWS::EC2::Addresses.exists?(@option.address)
38:           raise "#{@option.address} is not allocated to you" 
39:         end
40:         if Rudy::AWS::EC2::Addresses.associated?(@option.address)
41:           raise "#{@option.address} is already associated!" 
42:         end
43:       end
44:       
45:       # These can be sent directly to EC2 class
46:       [:group, :ami, :size, :keypair, :private].each do |n|
47:         opts[n] = @option.send(n) if @option.send(n)
48:       end
49:       
50:       li "Creating #{opts[:size]} instance in #{@@global.zone}"
51:       
52:       unless opts[:keypair]
53:         li "You did not specify a keypair. Unless you've prepared a user account".bright
54:         li "on this image (#{opts[:ami]}) you will not be able to log in to it.".bright
55:         exit unless Annoy.proceed?(:low)
56:       end
57:       
58:       instances = Rudy::AWS::EC2::Instances.list_group(opts[:group], :running)
59:       
60:       if instances && instances.size > 0
61:         instance_count = (instances.size == 1) ? 'is 1 instance' : "are #{instances.size} instances"
62:         li "There #{instance_count} running in the #{opts[:group]} group."
63:         exit unless Annoy.proceed?(:low)
64:       end
65:       
66:       if @option.newaddress
67:         print "Creating address... "
68:         address = Rudy::AWS::EC2::Addresses.create
69:         li "#{address.ipaddress}"
70:         @option.address = address.ipaddress
71:       end
72:          
73:       execute_action do
74:         first_instance = true
75:         Rudy::AWS::EC2::Instances.create(opts) do |inst| # Rudy::AWS::EC2::Instance objects
76:         
77:           # Assign IP address to only the first instance
78:           if first_instance && @option.address
79:             li "Associating #{@option.address} to #{inst.awsid}"
80:             Rudy::AWS::EC2::Addresses.associate(@option.address, inst.awsid)
81:             first_instance = false
82:           end
83:         
84:           print_stobject(inst)
85:         end
86:       end
87:     end