capistrano-multiyaml 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +7 -0
- data/README.md +75 -0
- data/capistrano-multiyaml.gemspec +17 -0
- data/lib/capistrano-multiyaml.rb +101 -0
- metadata +69 -0
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2013 Daniel Silverman <me@agperson.com>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
YAML Multistage Extensions for Capistrano
|
2
|
+
=========================================
|
3
|
+
|
4
|
+
This extension is an alternative to Jamis Buck's multistage extension for Capistrano that stores multistage configuration in a single YAML file rather than in multiple Ruby files. It provides a simple and straightforward way to specify variables, callbacks, and roles for different deployment stages, and the file can be manipulated by any script that understands YAML. Even if the file is only managed by humans, there are still several benefits including centralizing stage/role configuration in one file, discouraging per-stage logic in deference to properly hooked before/after callbacks, and simplified configuration reuse.'
|
5
|
+
|
6
|
+
For more information about the general idea of multistage deployment, see the [docs for the original module](https://github.com/capistrano/capistrano/wiki/2.x-Multistage-Extension/).
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
1. Install the **capistrano-multiyaml** gem using RubyGems, Bundler, or your preferred system.
|
11
|
+
2. Modify your `Capfile` or `deploy.rb` to set a few variables and include the gem:
|
12
|
+
|
13
|
+
set :stages, %w(integration staging production)
|
14
|
+
set :default_stage, "staging"
|
15
|
+
set :multiyaml_stages, "config/stages.yaml"`
|
16
|
+
|
17
|
+
**Note:** `:default_stage` is optional, and `:multiyaml_stages` only needs to be set if you are using a location other than `config/stages.yaml`.
|
18
|
+
|
19
|
+
## Configure Stages
|
20
|
+
|
21
|
+
The easiest way to understand the capabilities of the YAML file is to see a complete one. This example incorporates all of the possible configuration options, and also uses YAML anchor/alias to avoid repetition. Note that while before/after callback hooks can be used to evaluate code, this is not recommended because the goal of this setup is to avoid putting deploy logic in the stage configuration file.
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
--- # stages for application "fooapp"
|
25
|
+
integration:
|
26
|
+
roles:
|
27
|
+
app:
|
28
|
+
'bobsmith-vm.foocorp.com':
|
29
|
+
staging:
|
30
|
+
variables: &vars
|
31
|
+
:data_path: '/nfs/#{application}/#{stage}'
|
32
|
+
:clean_script: '#{deploy_to}/bin/clean-cache.sh'
|
33
|
+
tasks: &tasks
|
34
|
+
- type : after_callback
|
35
|
+
target: 'deploy:setup'
|
36
|
+
action: 'fooapp:link_data_path'
|
37
|
+
- type : before_callback
|
38
|
+
target: 'deploy:restart'
|
39
|
+
action: 'fooapp:run_scripts'
|
40
|
+
roles:
|
41
|
+
web:
|
42
|
+
'lb-stage1.foocorp.com':
|
43
|
+
db:
|
44
|
+
'sql-stage1.db.foocorp.com':
|
45
|
+
app:
|
46
|
+
'app-stage1.foocorp.com': { :primary: true }
|
47
|
+
'app-stage2.foocorp.com':
|
48
|
+
production:
|
49
|
+
variables: *vars
|
50
|
+
tasks: *tasks
|
51
|
+
roles:
|
52
|
+
web:
|
53
|
+
'lb-prod1.foocorp.com':
|
54
|
+
'lb-prod2.foocorp.com':
|
55
|
+
db:
|
56
|
+
'sql-prod1.db.foocorp.com':
|
57
|
+
app:
|
58
|
+
'app-prod1.foocorp.com': { :primary: true }
|
59
|
+
'app-prod2.foocorp.com':
|
60
|
+
'app-prod3.foocorp.com':
|
61
|
+
'app-prod4.foocorp.com': { :read_only: true }
|
62
|
+
```
|
63
|
+
|
64
|
+
* Variable keys are symbols
|
65
|
+
* Other Capistrano variables can be interpreted within variables
|
66
|
+
* Tasks can be one of `after_callback` (using the "after" hook) or `before_callback` (using the "before" hook). Action can be either another task or a code block to be evaluated.
|
67
|
+
* Additional parameters can be passed to servers in an array
|
68
|
+
|
69
|
+
## Running Deploy Actions
|
70
|
+
|
71
|
+
Prefix deploy actions with the name of the stage, i.e. `cap production TASK`. If you do not set a stage and `default_stage` is set, it will be used instead.
|
72
|
+
|
73
|
+
## Caveats
|
74
|
+
|
75
|
+
* Don't name your stage "stage", as this is a reserved word under the multistage extension (deploys won't do anything and in fact it will cause an infinite loop).
|
@@ -0,0 +1,17 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'capistrano-multiyaml'
|
3
|
+
s.version = '1.0.0'
|
4
|
+
s.platform = Gem::Platform::RUBY
|
5
|
+
s.authors = ["Daniel Silverman"]
|
6
|
+
s.email = 'me@agperson.com'
|
7
|
+
s.homepage = 'http://github.com/agperson/capistrano-multiyaml'
|
8
|
+
s.summary = 'Capistrano plugin for storing multistage configuration in a YAML file.'
|
9
|
+
s.description = 'This plugin simplifies and clarifies the multistage deploy process by reading settings from a simple YAML file that can be updated programatically. Even if the file is only managed by humans, there are still several benefits including centralizing stage/role configuration in one file, discouraging per-stage logic in deference to properly hooked before/after callbacks, and simplified configuration reuse.'
|
10
|
+
|
11
|
+
s.add_runtime_dependency "capistrano", "~>2"
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
unless Capistrano::Configuration.respond_to?(:instance)
|
5
|
+
abort "capistrano/multiyaml requires Capistrano 2"
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
# Variable interpolation mechanism stolen from Ruby Facets 2.9.3 rather than
|
10
|
+
# requiring the whole library
|
11
|
+
def String.interpolate(&str)
|
12
|
+
eval "%{#{str.call}}", str.binding
|
13
|
+
end
|
14
|
+
|
15
|
+
Capistrano::Configuration.instance.load do
|
16
|
+
begin
|
17
|
+
yamlfile = YAML.load_file(fetch(:multiyaml_stages, "config/stages.yaml"))
|
18
|
+
rescue
|
19
|
+
abort "Multistage deployment configuration file missing. Populate config/stages.yaml or set :multiyaml_stages to another location to use capistrano/multiyaml for multistage deployment."
|
20
|
+
end
|
21
|
+
|
22
|
+
stages = fetch(:stages, %w(staging production))
|
23
|
+
|
24
|
+
# Loop through YAML configuration file and create a task for each stage.
|
25
|
+
stages.each do |name|
|
26
|
+
desc "Set the target stage to `#{name}'."
|
27
|
+
task(name) do
|
28
|
+
set(:stage, name.to_sym)
|
29
|
+
set(:rails_env, name.to_s)
|
30
|
+
logger.important "Setting stage to #{name}"
|
31
|
+
|
32
|
+
# Load the corresponding stage's YAML configuration and iterate through,
|
33
|
+
# setting roles, variables, and callbacks as specified.
|
34
|
+
config = yamlfile[name.to_s]
|
35
|
+
abort "Invalid or missing stage configuration for #{name}." if config.nil?
|
36
|
+
|
37
|
+
config.each do |section, contents|
|
38
|
+
case section.to_s
|
39
|
+
|
40
|
+
when "variables"
|
41
|
+
contents.each do |key, value|
|
42
|
+
if key.is_a?(Symbol) and not value.nil? then
|
43
|
+
set(key, String.interpolate{value.to_s})
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
when "tasks"
|
48
|
+
contents.each do |task|
|
49
|
+
target = task['target'].to_s
|
50
|
+
action = task['action'].to_s
|
51
|
+
if not target.nil? and not action.nil? then
|
52
|
+
before(target, action) if task['type'] == "before_callback"
|
53
|
+
after( target, action) if task['type'] == "after_callback"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
when "roles"
|
58
|
+
contents.each do |rolename, hosts|
|
59
|
+
hosts.each do |hostname, options|
|
60
|
+
logger.info "Processing host settings for #{hostname} (#{name})"
|
61
|
+
if options.is_a?(Hash) then
|
62
|
+
role(rolename.to_sym, hostname.to_s, options)
|
63
|
+
else
|
64
|
+
role(rolename.to_sym, hostname.to_s)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
else
|
70
|
+
logger.info "Multistage YAML configuration section #{section.to_s} ignored by capistrano/multiyaml."
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
on :load do
|
77
|
+
if stages.include?(ARGV.first)
|
78
|
+
# Execute the specified stage so that recipes required in stage can contribute to task list
|
79
|
+
find_and_execute_task(ARGV.first) if ARGV.any?{ |option| option =~ /-T|--tasks|-e|--explain/ }
|
80
|
+
else
|
81
|
+
# Execute the default stage so that recipes required in stage can contribute tasks
|
82
|
+
find_and_execute_task(default_stage) if exists?(:default_stage)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
namespace :multiyaml do
|
87
|
+
desc "[internal] Ensure that a stage has been selected."
|
88
|
+
task :ensure do
|
89
|
+
if !exists?(:stage)
|
90
|
+
if exists?(:default_stage)
|
91
|
+
logger.important "Defaulting to `#{default_stage}'"
|
92
|
+
find_and_execute_task(default_stage)
|
93
|
+
else
|
94
|
+
abort "No stage specified. Please specify one of: #{stages.join(', ')} (e.g. `cap #{stages.first} #{ARGV.last}')"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
on :start, "multiyaml:ensure", :except => stages
|
101
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: capistrano-multiyaml
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Daniel Silverman
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: capistrano
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2'
|
30
|
+
description: This plugin simplifies and clarifies the multistage deploy process by
|
31
|
+
reading settings from a simple YAML file that can be updated programatically. Even
|
32
|
+
if the file is only managed by humans, there are still several benefits including
|
33
|
+
centralizing stage/role configuration in one file, discouraging per-stage logic
|
34
|
+
in deference to properly hooked before/after callbacks, and simplified configuration
|
35
|
+
reuse.
|
36
|
+
email: me@agperson.com
|
37
|
+
executables: []
|
38
|
+
extensions: []
|
39
|
+
extra_rdoc_files: []
|
40
|
+
files:
|
41
|
+
- LICENSE
|
42
|
+
- README.md
|
43
|
+
- capistrano-multiyaml.gemspec
|
44
|
+
- lib/capistrano-multiyaml.rb
|
45
|
+
homepage: http://github.com/agperson/capistrano-multiyaml
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.24
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Capistrano plugin for storing multistage configuration in a YAML file.
|
69
|
+
test_files: []
|