Capify-EC2 用于从 Amazon EC2 实例标签生成 Capistrano 命名空间和任务,动态构建要部署的服务器列表。
Capify-EC2 用于从 Amazon EC2 实例标签生成 Capistrano 命名空间和任务,动态构建要部署的服务器列表。
Capify-EC2 is used to generate Capistrano namespaces and tasks from Amazon EC2 instance tags, dynamically building the list of servers to be deployed to.
Note: Capistrano 3 is completely incompatible with Capistrano 2, and therefore Capify-EC2 will not function with Capistrano 3. If you are using Capistrano 3, you can use Cap-EC2, however note that the configuration (config/ec2.yml) is not compatible between Capify-EC2 and Cap-EC2.
gem install capify-ec2or add the gem to your project's Gemfile.
Then, add the gem to your deploy.rb:
require "capify-ec2/capistrano"Create a YML configuration file (default path is ./config/ec2.yml) with the following contents:
:aws_access_key_id: "YOUR ACCESS KEY"
:aws_secret_access_key: "YOUR SECRET"
:aws_params:
:region: 'eu-west-1'
:load_balanced: true
:project_tag: "YOUR APP NAME"You can change the path to your ec2.yml by placing the following in your deploy.rb before calling ec2_roles:
set :ec2_config, 'location/of/my/ec2.yml'
Note: :aws_access_key_id and :aws_secret_access_key are required, unless you provide them via the two alternative methods detailed below under 'AWS Credentials' or have the :use_iam_profile option set to use IAM roles. :region is also required. Other settings are optional.
:project_tag
If this is defined, Capify-EC2 will only create namespaces and tasks for the EC2 instances that include a matching project in a comma separated list 'Project' tag. By default, all instances available to the specified AWS access key will be used.
It is possible to include multiple projects simultaneously by using the :project_tags parameter, like so:
:project_tags:
- "YOUR APP NAME"
- "YOUR OTHER APP NAME":aws_project_tag
Use this option to change which EC2 instance tag Capify-EC2 uses to determine instance project. Defaults to 'Project' if omitted.
:aws_roles_tag
Use this option to change which EC2 instance tag Capify-EC2 uses to determine instance roles. Defaults to 'Roles' if omitted.
:aws_options_tag
Use this option to change which EC2 instance tag Capify-EC2 uses to determine instance options. Defaults to 'Options' if omitted.
:aws_stages_tag
Use this option to change which EC2 instance tag Capify-EC2 uses to determine which instances belong to a stage. Should be used in conjunction with the Capistrano Multistage Extension. Defaults to 'Stages' if omitted.
:load_balanced
When ':load_balanced' is set to 'true', Capify-EC2 uses pre and post-deploy hooks to deregister the instance from an associated Elastic Load Balancer, perform the actual deploy, then finally reregister with the ELB and validated the instance health.
Note: This options only applies to deployments made to an individual instance, using the command cap INSTANCE_NAME_HERE deploy - it doesn't apply to roles.
:use_iam_profile
Use this option to use IAM roles for authentication, rather than an access key id and secret access key.
:aws_session_token
If you are an IAM user that only has access to temporary credentials, you can provide your session token here to authenticate.
By default, Capify-EC2 will attempt to use the credentials found in your ec2.yml configuration as detailed above.
If you wish, you can have Capify-EC2 use the AWS credentials found in your Fog configuration, instead of instead of specifying :aws_access_key_id and :aws_secret_access_key in the YML configuration file. Optionally, if you requires session tokens or federated users, you can set :aws_session_token. Refer to the Fog documentation for details on specifying AWS credentials.
If you wish, you can define AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as environment variables, instead of specifying :aws_access_key_id and :aws_secret_access_key in the YML configuration file. Optionally, if you requires session tokens or federated users, you can set the environment variable AWS_SESSION_TOKEN.
If you have IAM roles set up on your box to allow querying EC2 information you tell Fog to use IAM roles and you will not need to provide any credentials at runtime. For more information on IAM roles read Amazon's IAM documentation.
If you are an IAM user that only has access to temporary credentials, you can use :aws_session_token: "YOUR TOKEN HERE" to authenticate.
Capify-EC2 will attempt to load your AWS credentials first from the ec2.yml configuration file, then from your Fog configuration file, and finally from environment variables. It will display an error if no credentials are found by any of these methods.
You will need to create instance tags using the AWS Management Console or API, to further configure Capify-EC2. The following tags are used:
Tag 'Project'
Used with the :project_tag option in config/ec2.yml to limit Capify-EC2's functionality to a subset of your instances. This can also be a comma separated list of projects involving this machine.
Tag 'Roles'
A comma seperated list of roles that will be converted into Capistrano namespaces, for example 'app,workers', 'app,varnish,workers', 'db' and so on.
Tag 'Options'
A comma seperated list of options which will be defined as 'true' for that instance. See the 'Options' section below for more information on their use.
In our examples, imagine that you have three servers on EC2 named and tagged as follows:
'Name' Tag
'Roles' Tag
'Options' Tag
server-1
web
cron,resque
server-2
db
server-3
web,db,appYou need to add a call to ec2_roles in your deploy.rb, like so:
ec2_roles :name => :webThis will generate the following tasks:
task :server-1 do
role :web, SERVER-1_EC2_PUBLIC_DNS_HERE, :cron=>true, :resque=>true
end
task :server-3 do
role :web, SERVER-3_EC2_PUBLIC_DNS_HERE
end
task :web do
role :web, SERVER-1_EC2_PUBLIC_DNS_HERE, :cron=>true, :resque=>true
role :web, SERVER-3_EC2_PUBLIC_DNS_HERE
endNote that there are no tasks created for 'server-2', as it does not have the role 'web'. If we were to change the ec2_roles definition in your deploy.rb to the following:
ec2_roles :name => :dbThen we will instead see the following tasks generated:
task :server-2 do
role :db, SERVER-2_EC2_PUBLIC_DNS_HERE
end
task :server-3 do
role :db, SERVER-3_EC2_PUBLIC_DNS_HERE
end
task :db do
role :db, SERVER-2_EC2_PUBLIC_DNS_HERE
role :db, SERVER-3_EC2_PUBLIC_DNS_HERE
endIf you want to create tasks for servers using multiple roles, you can call ec2_roles multiple times in your deploy.rb as follows:
ec2_roles :web
ec2_roles :dbWhich would generate the following tasks:
task :server-1 do
role :web, SERVER-1_EC2_PUBLIC_DNS_HERE, :cron=>true, :resque=>true
end
task :server-2 do
role :db, SERVER-2_EC2_PUBLIC_DNS_HERE
end
task :server-3 do
role :web, SERVER-3_EC2_PUBLIC_DNS_HERE
role :db, SERVER-3_EC2_PUBLIC_DNS_HERE
end
task :web do
role :web, SERVER-1_EC2_PUBLIC_DNS_HERE
role :web, SERVER-3_EC2_PUBLIC_DNS_HERE
end
task :db do
role :db, SERVER-2_EC2_PUBLIC_DNS_HERE, :cron=>true, :resque=>true
role :db, SERVER-3_EC2_PUBLIC_DNS_HERE
endYou can define custom variables which will be set as standard Capistrano variables within the scope of the role you define them one, for example:
ec2_roles :name=>"web", :variables => {:rails_env => 'staging'}In this case, instances that that are tagged with the 'web' role will have the custom variable 'rails_env' available to them in any tasks they use. The following tasks would be generated:
task :server-1 do
role :web, SERVER-1_EC2_PUBLIC_DNS_HERE, :cron=>true, :resque=>true, :rails_env=>'staging'
end
task :server-3 do
role :web, SERVER-3_EC2_PUBLIC_DNS_HERE
end
task :web do
role :web, SERVER-1_EC2_PUBLIC_DNS_HERE, :cron=>true, :resque=>true, :rails_env=>'staging'
role :web, SERVER-3_EC2_PUBLIC_DNS_HERE, :rails_env=>'staging'
endIf you are defining a role with no options or variables, you can define it using the following shorthand:
ec2_roles :webAs mentioned in the 'EC2 Tags' section, creating an 'Options' tag on your EC2 instances will define those options as 'true' for the associated instance. This allows you to refine your capistrano tasks. For example, if we had the following group of instances in EC2:
'Name' Tag
'Roles' Tag
'Options' Tag
server-A
web
server-B
web
server-C
web
workerYou could then create a task in your deploy.rb that will only be executed on the worker machine, like so:
task :reload_workers, :only=>{:worker=>true} do
# Do something to a specific server with the 'worker' option in it's EC2 tags.
endtask :reload_workers, :roles => :app, :only=>{:worker=>true} do
# Do something to a specific 'app' server with the 'worker' option in it's EC2 tags.
endAs well as defining Options at an instance level via EC2 tags, you can define an Option in your deploy.rb at the same time as defining the role, as follows:
ec2_roles :name=>"web", :options=>{:worker=>"server-C"}In this case, you set the value of :worker equal to the instance name you want to be a worker. The task definition remains the same:
task :reload_workers => :web, :only=>{:worker=>true} do
# Do something to a specific server whose instance name is equal to the 'worker' option in the role definition.
endOnce you have defined the various roles used by your application, you can deploy to it as you normally would a namespace, for example if you define the following in your deploy.rb:
ec2_roles :web
ec2_roles :appYou can deploy to just the 'web' instances like so:
cap web deployIf you've defined multiple roles, you can deploy to them all by chaining the tasks, like so:
cap web app deployYou can also deploy to individual instances by specifying their 'Name' tag, for example, with the sample servers above:
cap server-1 deploywould deploy only to the instance named 'server-1'.
You can set a role as the default so that it will be included when you run cap deploy without specifying any roles, for example in your deploy.rb:
ec2_roles :name=>"web", :options => {:default=>true}Then run:
cap deployYou can set multiple roles as defaults, so they are all included when you run cap deploy, like so:
ec2_roles :name=>"web", :options => {:default=>true}
ec2_roles :name=>"db", :options => {:default=>true}This feature allows you to deploy your code to instances one at a time, rather than simultaneously. This becomes useful for more complex applications that may take longer to startup after a deployment. Capistrano will perform a full deploy (including any custom hooks) against a single instance, optionally perform a HTTP healthcheck against the instance, then proceed to the next instance if deployment was successful.
After deployment a status report is displayed, indicating on which instances deployment succeeded, failed or did not begin. With some failures, further action may need to be taken manually; for example if an instance is removed from an ELB (see the 'Usage with
暂无开放 Issues,或尚未同步最近议题。