public
Rubygem
Description: A Capistrano extension for managing and running your app on Amazon EC2.
Clone URL: git://github.com/jnewland/capsize.git
Search Repo:
jnewland (author)
Thu Apr 10 06:41:36 -0700 2008
commit  ea0471f9a749dc711479538f2b09ea275f767db8
tree    3f491eb0862b1f65015a13689ed488beb2999837
parent  bdc77333629324c1aec68df9759b9b5ed2757cdb
capsize / lib / capsize / meta_tasks.rb
100644 179 lines (127 sloc) 6.087 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
#--
# Capistrano Plugin which provides access to the amazon-ec2 gem's methods
#
# Ruby Gem Name:: capsize
# Author:: Glenn Rempe (mailto:grempe@rubyforge.org)
# Author:: Jesse Newland (mailto:jnewland@gmail.com)
# Copyright:: Copyright (c) 2007 Glenn Rempe, Jesse Newland
# License:: Distributes under the same terms as Ruby
# Home:: http://capsize.rubyforge.org
#++
 
Capistrano::Configuration.instance.load do
 
  namespace :ec2 do
 
    namespace :setup do
 
      desc <<-DESC
Initialize Capsize config.
You can run this command as often as you like.
It will not overwrite config files on multiple runs.
- Create :capsize_config_dir
- Create :capsize_secure_config_dir
- Copy capsize.yml.template to :capsize_config_dir/capsize.yml unless it already exists
- Automatically generate :capsize_secure_config_dir/secure.yml unless it already exists
- Instruct user to test configuration with "cap ec2:images:describe"
- Instruct user how to create a new keypair
- Instruct user how to setup a new security group.
DESC
      task :default do
 
        # Make the standard config dir if it doesn't exist already
        unless File.exists?(fetch(:capsize_config_dir))
          FileUtils.mkdir fetch(:capsize_config_dir)
        end
 
        # Make the secure config dir if it doesn't exist already
        unless File.exists?(fetch(:capsize_secure_config_dir))
          FileUtils.mkdir fetch(:capsize_secure_config_dir)
        end
 
        # copy the standard config file template, unless the dest file alread exists
        unless File.exists?("#{fetch(:capsize_secure_config_dir)}/#{fetch(:capsize_secure_config_file_name)}")
          puts "Please enter your EC2 account information."
          puts "We'll then write it to a config file at #{fetch(:capsize_secure_config_dir)}/#{fetch(:capsize_secure_config_file_name)}"
 
          require "yaml"
          set :aws_access_key_id, proc { Capistrano::CLI.ui.ask("AWS Access Key ID : ") }
          set :aws_secret_access_key, proc { Capistrano::CLI.ui.ask("AWS Secret Access Key : ") }
 
          yaml = {}
 
          # Populate production element
          yaml['common'] = {}
          yaml['common']['aws_access_key_id'] = aws_access_key_id
          yaml['common']['aws_secret_access_key'] = aws_secret_access_key
 
          yaml = YAML::dump(yaml).split("\n").collect { |line| line == "common: " ? line += "&common" : line }.join("\n")
 
          env_config =<<EOF
 
 
development:
<<: *common
 
# Uncomment and I only apply to the dev environment
# or overwrite a common value
#foo: 'bar'
 
test:
<<: *common
 
staging:
<<: *common
 
production:
<<: *common
 
# Uncomment and I only apply to the production environment
# or overwrite a common value
#foo: 'baz'
EOF
 
          yaml += env_config
 
          File.open("#{fetch(:capsize_secure_config_dir)}/#{fetch(:capsize_secure_config_file_name)}", 'w') do |file|
            file.write(yaml)
          end
          File.chmod 0664, "#{fetch(:capsize_secure_config_dir)}/#{fetch(:capsize_secure_config_file_name)}"
        else
          puts "Warning : The following file was not copied over since it already exists: " + "#{fetch(:capsize_secure_config_dir)}/#{fetch(:capsize_secure_config_file_name)}"
        end
 
        unless File.exists?("#{fetch(:capsize_config_dir)}/#{fetch(:capsize_config_file_name)}")
          FileUtils.cp("#{fetch(:capsize_examples_dir)}/capsize.yml.template", "#{fetch(:capsize_config_dir)}/#{fetch(:capsize_config_file_name)}", :verbose => true)
        else
          puts "Warning : The following file was not copied over since it already exists: " + "#{fetch(:capsize_config_dir)}/#{fetch(:capsize_config_file_name)}"
        end
 
        check
 
        ec2.keypairs.create
 
        ec2.security_groups.create_with_standard_ports
 
        message = <<-MESSAGE
 
Next up: Pick an Amazon Machine Image and Run it!
 
Now you need to select an Amazon EC2 image ID that you want to start as a new
instance. The easiest way to do that is to get a list of available images from EC2.
 
# Show ALL registered images
cap ec2:images:show
 
# Show MY registered images
cap ec2:images:show OWNER_ID='self'
 
# Show the AMAZON registered images
cap ec2:images:show OWNER_ID='amazon'
 
Select an 'imageId' from the results, and run it:
 
cap ec2:instances:run IMAGE_ID='ami-2bb65342'
 
You should see some progress information scroll by and finally you should see
a description of the key attributes of your running intance (dnsName and instanceId
are likely most important).
 
Now lets connect to it with SSH (this may take a few tries, sometimes it takes a
minute for the new instance to respond to SSH):
 
cap ec2:instances:ssh INSTANCE_ID='i-xxxxxx'
 
If you want to terminate your instance...
 
cap ec2:instances:terminate INSTANCE_ID='i-xxxxxx'
 
Enjoy Capsize!
 
MESSAGE
 
        puts message
 
      end
 
      desc <<-DESC
Test your Capsize config.
Run a simple test which will validate that your Capsize config
is setup and working properly when querying the Amazon EC2 servers.
DESC
      task :check do
 
        begin
          capsize_ec2.describe_images(:owner_id => "amazon")
          puts "Congratulations! Your credentials are verified and you are communicating properly with Amazon's EC2 service."
        rescue Exception => e
          puts "The test of your Capsize config failed with the following error: " + e
        end
 
      end
 
    end
 
  end
 
 
  # TODO : Should these be defined here? Doesn't that mean that every
  # time a deploy:setup is run these tasks are run also? Can they
  # really be run in all circumstances without harm if repeated?
  # Lets discuss...
  #
  #callbacks to make this stuff happen
  #before "deploy:setup", "ec2:setup"
  #after "deploy:setup", "ec2:generate_config"
 
 
end