Module: Capistrano::Puppeteer::AWS

Defined in:
lib/capistrano/puppeteer/aws.rb

Constant Summary collapse

FLAVOURS =
{
  't1.micro'    => {:ram => 0.6,  :io => 'low',      :compute => 2,    :price => 0.02},

  'm1.small'    => {:ram => 1.7,  :io => 'moderate', :compute => 1,    :price => 0.08},
  'm1.medium'   => {:ram => 3.75, :io => 'moderate', :compute => 2,    :price => 0.16},
  'm1.large'    => {:ram => 7.5,  :io => 'high',     :compute => 4,    :price => 0.32},
  'm1.xlarge'   => {:ram => 15,   :io => 'high',     :compute => 8,    :price => 0.64},

  'm2.xlarge'   => {:ram => 17.1, :io => 'moderate', :compute => 6.5,  :price => 0.45},
  'm2.2xlarge'  => {:ram => 34.2, :io => 'high',     :compute => 13,   :price => 0.90},
  'm2.4xlarge'  => {:ram => 68.4, :io => 'high',     :compute => 26,   :price => 1.80},

  'c1.medium'   => {:ram => 1.7,  :io => 'moderate', :compute => 5,    :price => 0.165},
  'c1.xlarge'   => {:ram => 7,    :io => 'high',     :compute => 20,   :price => 0.66},

  'cc1.4xlarge' => {:ram => 23,   :io => 'v.high',   :compute => 33.5, :price => 1.30},
  'cc1.8xlarge' => {:ram => 60.5, :io => 'v.high',   :compute => 88  , :price => 2.40},

  'cg1.4xlarge' => {:ram => 22,   :io => 'v.high',   :compute => 33.5, :price => 2.10},
}

Class Method Summary collapse

Class Method Details

.extended(configuration) ⇒ Object



27
28
29
30
31
32
33
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/capistrano/puppeteer/aws.rb', line 27

def self.extended(configuration)
  configuration.load do
    set(:cloud_provider)        { abort "Please specify a cloud provider, set :cloud_provider, 'AWS'" } unless exists? :cloud_provider
    set(:aws_ami)               { abort "Please specify a AWS AMI, set :aws_ami, 'ami-a29943cb'" } unless exists? :aws_ami
    set(:aws_secret_access_key) { abort "Please specify an AWS Access Key, set :aws_secret_access_key, 'XXXX'" } unless exists? :aws_secret_access_key
    set(:aws_access_key_id)     { abort "Please specify a AWS AMI, set :aws_access_key_id, 'ZZZ'" } unless exists? :aws_access_key_id
    set(:aws_region)            { abort "Please specify a AWS AMI, set :aws_region, 'us-west-1'" } unless exists? :aws_availability_zone
    set(:aws_availability_zone) { abort "Please specify a AWS AMI, set :aws_availability_zone, 'us-west-1a'" } unless exists? :aws_availability_zone
    set(:aws_key_name)          { abort "Please specify a AWS AMI, set :aws_key_name, 'default'" } unless exists? :aws_key_name
    set(:aws_ssh_key)           { abort "Please specify a AWS AMI, set :aws_ssh_key, 'config/aws.pem'" } unless exists? :aws_ssh_key

   namespace :aws do

      desc <<-DESC
        create an AWS instance.

          cap aws:create [OPTIONS]

        Available options:

          flavour  (required) - The type of EC2 instance to create
          name     (required) - The name of the instance, this will be used as the AWS tag
          iam_role            - An IAM role to apply to the instance

      DESC
      task :create do
        flavour = ENV['flavour'] || abort('please specify a flavour')
        name = ENV['name'] || abort('please specify name')
        iam_role = ENV['iam_role']

        puts "Creating Instance..."
        instance_options = {
          :image_id          => aws_ami,
          :availability_zone => aws_availability_zone,
          :flavor_id         => flavour,
          :key_name          => aws_key_name,
          :tags              => { 'Name' => name },
        }

        instance_options[:iam_instance_profile_name] = iam_role if iam_role

        server = servers.create instance_options
        server.wait_for { ready? }
        server.reload
        ENV['HOSTS'] = server.public_ip_address
      end


      desc 'List Instance types'
      task :flavours do
        puts "%-11s  %-11s   %-7s %-5s  %s" % %w[Name Price/Month RAM Units IO]
        Capistrano::Puppeteer::AWS::FLAVOURS.each do |flavor, opts|
          puts "%-11s  $ %7.2f   %4.1f GB   %4.1f   %s" % [flavor, opts[:price] * 720, opts[:ram], opts[:compute], opts[:io]]
        end
      end

      desc 'List current AWS instances'
      task :list do
        format = '%-15s  %-10s  %-8s  %-10s  %-43s  %-15s  %-10s  %-s %s'
        puts format % %w{Name ID State Zone DNS IP Type CreatedAt ImageID}
        servers.sort {|a,b| (a.tags['Name'] || 'Unknown') <=> (b.tags['Name'] || 'Unknown') }.each do |server|
          puts format % [server.tags['Name'], server.id, server.state, server.availability_zone, server.dns_name, server.private_ip_address, server.flavor_id, server.created_at, server.image_id]
        end
      end

      desc 'Describe an instance'
      desc <<-DESC
        Describe an AWS instance.

          cap aws:show instance_id=...

      DESC
      task :show do
        instance_id = ENV['instance_id'] || abort('provide an instance_id')
        server = servers.get instance_id
        p server
      end

      desc <<-DESC
        Start an AWS instance.

          cap aws:start instance_id=...

      DESC
      task :start do
        instance_id = ENV['instance_id'] || abort('provide an instance_id')

        server = servers.get instance_id
        server.start
      end

      desc <<-DESC
        Stop an AWS instance.

          cap aws:stop instance_id=...

          Options:
          force=true - Forces a stop for a hung instance

      DESC
      task :stop do
        instance_id = ENV['instance_id'] || abort('provide an instance_id')
        force = ENV['force'] =~ /^true$/i

        server = servers.get instance_id
        server.stop force
      end

      desc <<-DESC
        Destroy an AWS instance.

          cap aws:destroy instance_id=...

      DESC
      task :destroy do
        instance_id = ENV['instance_id'] || abort('provide an instance_id')

        server = servers.get instance_id
        server.destroy
      end

      desc <<-DESC
        Reboot an AWS instance.

          cap aws:reboot instance_id=...

          Options:
          force=true - Forces a stop for a hung instance

      DESC
      task :reboot do
        instance_id = ENV['instance_id'] || abort('provide an instance_id')
        force = ENV['force'] =~ /^true$/i

        server = servers.get instance_id
        server.reboot force
      end

      def compute
        @compute ||= Fog::Compute.new(
          :provider              => cloud_provider,
          :region                => aws_region,
          :aws_secret_access_key => aws_secret_access_key,
          :aws_access_key_id     => aws_access_key_id,
        )
      end

      def servers
        @servers ||= compute.servers
      end

    end

  end
end