capistrano-bootstrap 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem "versionomy", "~> 0.4.4"
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "rspec", "~> 2.8.0"
12
+ gem "yard", "~> 0.7"
13
+ gem "rdoc", "~> 3.12"
14
+ gem "bundler", "~> 1.2.0"
15
+ gem "jeweler", "~> 1.8.4"
16
+ gem "simplecov", ">= 0"
17
+ gem "reek", "~> 1.2.8"
18
+ end
@@ -0,0 +1,57 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ blockenspiel (0.4.5)
5
+ diff-lcs (1.1.3)
6
+ git (1.2.5)
7
+ jeweler (1.8.4)
8
+ bundler (~> 1.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ rdoc
12
+ json (1.7.6)
13
+ multi_json (1.5.0)
14
+ rake (10.0.3)
15
+ rdoc (3.12)
16
+ json (~> 1.4)
17
+ reek (1.2.13)
18
+ ripper_ruby_parser (~> 0.0.7)
19
+ ruby2ruby (~> 1.2.5)
20
+ ruby_parser (~> 2.0)
21
+ sexp_processor (~> 3.0)
22
+ ripper_ruby_parser (0.0.8)
23
+ sexp_processor (~> 3.0)
24
+ rspec (2.8.0)
25
+ rspec-core (~> 2.8.0)
26
+ rspec-expectations (~> 2.8.0)
27
+ rspec-mocks (~> 2.8.0)
28
+ rspec-core (2.8.0)
29
+ rspec-expectations (2.8.0)
30
+ diff-lcs (~> 1.1.2)
31
+ rspec-mocks (2.8.0)
32
+ ruby2ruby (1.2.5)
33
+ ruby_parser (~> 2.0)
34
+ sexp_processor (~> 3.0)
35
+ ruby_parser (2.3.1)
36
+ sexp_processor (~> 3.0)
37
+ sexp_processor (3.2.0)
38
+ simplecov (0.7.1)
39
+ multi_json (~> 1.0)
40
+ simplecov-html (~> 0.7.1)
41
+ simplecov-html (0.7.1)
42
+ versionomy (0.4.4)
43
+ blockenspiel (>= 0.4.5)
44
+ yard (0.8.3)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ bundler (~> 1.2.0)
51
+ jeweler (~> 1.8.4)
52
+ rdoc (~> 3.12)
53
+ reek (~> 1.2.8)
54
+ rspec (~> 2.8.0)
55
+ simplecov
56
+ versionomy (~> 0.4.4)
57
+ yard (~> 0.7)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Lucas Jenss
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,101 @@
1
+ # capistrano-bootstrap
2
+
3
+ This gem can be used to keep capistrano configuration synchronized between multiple projects. If you have several git repositories in which you are using the same or very similar capistrano configurations, this gem is for you.
4
+
5
+
6
+
7
+ ## How to set up?
8
+
9
+ You'll need a new git repository containing your configuration. Lets say you currently use the following directory structure in your projects:
10
+
11
+ project/
12
+ Cap
13
+ config/
14
+ capistrano/
15
+ deploy.rb
16
+ util/
17
+ important.rb
18
+ stuff.rb
19
+ project.rb
20
+ stages/
21
+ production.rb
22
+ staging.rb
23
+ [...]
24
+
25
+ Let us also assume that everything in the `util/` folder as well as `deploy.rb` is never modified, that is, the project specific configuration resides solely in `stages/` and `project.rb`. You'd now create a new git repository containing the following:
26
+
27
+ deploy.rb
28
+ util/
29
+ important.rb
30
+ stuff.rb
31
+
32
+ Then you'd simply remove these files from your project structure, resulting in:
33
+
34
+ project/
35
+ Cap
36
+ config/
37
+ capistrano/
38
+ project.rb
39
+ stages/
40
+ production.rb
41
+ staging.rb
42
+ [...]
43
+
44
+ Now you want to add the following lines to the beginning of your `Cap` file:
45
+
46
+ require 'capistrano-bootstrap'
47
+ CapistranoBootstrap.invoke(
48
+ "git@github.com:you-on-github/capistrano-configuration.git",
49
+ :config_dir => "config/capistrano",
50
+ :target_path => "config/.config_repo"
51
+ )
52
+
53
+ The `invoke` method has the following optional parameters:
54
+
55
+ * `:config_dir` => Where you would like to have your global capistrano config copied to.
56
+ * `:target_path` => The directory to which the configuration repository will be cloned.
57
+
58
+ The parameter values in the above `invoke` example are the default values.
59
+
60
+ By default, capistrano-bootstrap will try to update the configuration repository once every 24 hours. You may overwrite that behavior (i.e. force an update) by invoking your capistrano task like this:
61
+
62
+ CAPBOOTSTRAP_FORCE=1 cap deploy
63
+
64
+ For your convenience, capistrano-bootstrap also generates a `.gitignore` file in the `config_dir` which ignores all content copied from the configuration repository.
65
+
66
+
67
+
68
+ ## How it works
69
+
70
+ capistrano-bootstrap simply clones the specified repository, checks out the latest tag (by version number!) and copies the content to the specified `config_dir`.
71
+
72
+ There _must_ be at least one tag in the configuration repository, otherwise the update will fail. The version comparison/ordering is done using the [Versionomy](https://github.com/dazuma/versionomy) gem, for example:
73
+
74
+ v1.0 < v1.0.1 < v1.1 < v1.1.1 < ...
75
+
76
+
77
+
78
+ ## Caveats
79
+
80
+ * capistrano-bootstrap does not currently **delete** any files, it only overwrites updated files or creates newly created ones.
81
+
82
+
83
+
84
+ ## TODO
85
+
86
+ * Write spec
87
+ * Support file deletion
88
+
89
+
90
+
91
+ ## Boring legal stuff (read: License)
92
+
93
+ Copyright (C) 2013 Lucas Jenß
94
+
95
+ 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:
96
+
97
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
98
+
99
+ 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.
100
+
101
+
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "capistrano-bootstrap"
18
+ gem.homepage = "http://github.com/x3ro/capistrano-bootstrap"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{This gem can be used to keep capistrano configuration synchronized between multiple projects.}
21
+ gem.description = %Q{This gem can be used to keep capistrano configuration synchronized between multiple projects. If you have several git repositories in which you are using the same or very similar capistrano configurations, this gem is for you.}
22
+ gem.email = "lucas@x3ro.de"
23
+ gem.authors = ["Lucas Jenss"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ require 'reek/rake/task'
40
+ Reek::Rake::Task.new do |t|
41
+ t.fail_on_error = true
42
+ t.verbose = false
43
+ t.source_files = 'lib/**/*.rb'
44
+ end
45
+
46
+ task :default => :spec
47
+
48
+ require 'yard'
49
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,143 @@
1
+ require 'fileutils'
2
+ require 'versionomy'
3
+
4
+ class CapistranoBootstrap
5
+ class << self
6
+ # Update configuration repository every day
7
+ UPDATE_INTERVAL = 60 * 60 * 24
8
+
9
+ def invoke(repository, args = {})
10
+ args[:config_dir] ||= "config/capistrano"
11
+ args[:target_path] ||= "config/.config_repo"
12
+
13
+ if !File.directory? args[:target_path]
14
+ clone_repository(repository, "./config/.config_repo")
15
+ elsif repository_needs_update?(args[:target_path])
16
+ update_repository(args[:target_path])
17
+ end
18
+
19
+ tags = get_repository_tags(args[:target_path]).map { |x| Versionomy.parse(x) }.sort
20
+ if configuration_needs_update?(args[:target_path], tags)
21
+ update_capistrano_configuration(args[:target_path], args[:config_dir], tags)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def configuration_needs_update?(target_path, tags)
28
+ return true if !ENV["CAPBOOTSTRAP_FORCE"].nil?
29
+ current_tag = get_current_tag(target_path)
30
+ return true if current_tag.nil?
31
+ current_tag < tags.last
32
+ end
33
+
34
+ # Checks out the latest configuration tag and copies all files to the configuration
35
+ # directory.
36
+ def update_capistrano_configuration(target_path, config_dir, tags)
37
+ update_to = tags.last
38
+ tell("About to update capistrano configuration to tag '#{update_to}'")
39
+
40
+ in_dir_do(target_path) do
41
+ execute_command("git checkout #{update_to}")
42
+ end
43
+ set_current_tag(target_path, update_to)
44
+ tell("Configuration updated to '#{update_to}'")
45
+
46
+ tell("Copying updated configuration to '#{config_dir}'")
47
+ execute_command("cp -rf #{target_path}/* #{config_dir}")
48
+
49
+ write_gitignore(target_path, config_dir)
50
+
51
+ tell("Finished updating configuration at '#{config_dir}' to '#{update_to}'")
52
+ end
53
+
54
+ # Writes a .gitignore file which ignores all files copied from the capistrano
55
+ # configuration repository.
56
+ def write_gitignore(target_path, config_dir)
57
+ tell("Updating #{config_dir}/.gitignore")
58
+ files = in_dir_do(target_path) { Dir.glob("*") }
59
+ files.push(".gitignore")
60
+ in_dir_do(config_dir) do
61
+ File.open(".gitignore", 'w+') { |f| f.write(files.join("\n")) }
62
+ end
63
+ end
64
+
65
+ # The file that contains the currently checked out tag (that is, the last tag
66
+ # whose contents where copied to the configuration directory)
67
+ def tag_file(target_path)
68
+ "#{target_path}/.CAPBOOTSTRAP_TAG"
69
+ end
70
+
71
+ def get_current_tag(target_path)
72
+ dir = tag_file(target_path)
73
+ return nil if !File.exists?(dir)
74
+ lines = File.read(dir).split("\n")
75
+ return nil if lines.length < 1
76
+ Versionomy.parse(lines.first)
77
+ end
78
+
79
+ def set_current_tag(target_path, tag)
80
+ File.open(tag_file(target_path), 'w+') { |f| f.write(tag) }
81
+ end
82
+
83
+ # Returns an array of tags specified for the cloned configuration repository
84
+ def get_repository_tags(target_path)
85
+ in_dir_do(target_path) do
86
+ out = execute_command("git tag")
87
+ raise "Repository in path #{target_path} is not a valid Git repository" if $? != 0
88
+
89
+ tags = out.split("\n")
90
+ raise "Repository in path #{target_path} does not contain any tags!" if tags.length < 1
91
+
92
+ tags
93
+ end
94
+ end
95
+
96
+ def clone_repository(repository, target_path)
97
+ tell "Cloning repository #{repository} to #{target_path}"
98
+ execute_command("git clone #{repository} #{target_path}")
99
+ end
100
+
101
+ # Determines if the repository needs to be updated by checking when it was last
102
+ # touched (by the #update_repository method)
103
+ def repository_needs_update?(target_path)
104
+ return true if !ENV["CAPBOOTSTRAP_FORCE"].nil?
105
+ (Time.now - File.mtime(target_path)) > UPDATE_INTERVAL
106
+ end
107
+
108
+ # Pull the latest contents (including tags) for the repository
109
+ def update_repository(target_path)
110
+ in_dir_do(target_path) do
111
+ tell "Updating local configuration repository at '#{target_path}'"
112
+ execute_command("git checkout master")
113
+ execute_command("git pull origin master --tags")
114
+
115
+ head = execute_command("git rev-parse HEAD")[0,8]
116
+ tell("Updated local configuration repository to #{head}")
117
+ end
118
+ execute_command("touch #{target_path}")
119
+ end
120
+
121
+ def tell(text)
122
+ puts "[CapBootstrap]: #{text}"
123
+ end
124
+
125
+ # Switches to the specified directory, executes the given block and then
126
+ # switches the working directory back to where it was before.
127
+ def in_dir_do(target_path, &block)
128
+ cwd = Dir.pwd
129
+ Dir.chdir(target_path)
130
+ out = block.call
131
+ Dir.chdir(cwd)
132
+ out
133
+ end
134
+
135
+ # Executes the given shell command in the current working directory, and throws an
136
+ # exception in case the command failed (exit status != 0)
137
+ def execute_command(command)
138
+ out = `#{command} 2>&1`
139
+ raise "There was an error executing '#{command}'. Output: \n #{out} \n" if $? != 0
140
+ out
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "CapistranoBootstrap" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'capistrano-bootstrap'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-bootstrap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lucas Jenss
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: versionomy
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.4.4
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: 0.4.4
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.8.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.8.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: yard
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '0.7'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.7'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rdoc
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '3.12'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '3.12'
78
+ - !ruby/object:Gem::Dependency
79
+ name: bundler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 1.2.0
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 1.2.0
94
+ - !ruby/object:Gem::Dependency
95
+ name: jeweler
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 1.8.4
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.8.4
110
+ - !ruby/object:Gem::Dependency
111
+ name: simplecov
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: reek
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: 1.2.8
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: 1.2.8
142
+ description: This gem can be used to keep capistrano configuration synchronized between
143
+ multiple projects. If you have several git repositories in which you are using the
144
+ same or very similar capistrano configurations, this gem is for you.
145
+ email: lucas@x3ro.de
146
+ executables: []
147
+ extensions: []
148
+ extra_rdoc_files:
149
+ - LICENSE.txt
150
+ - README.md
151
+ files:
152
+ - .document
153
+ - .rspec
154
+ - Gemfile
155
+ - Gemfile.lock
156
+ - LICENSE.txt
157
+ - README.md
158
+ - Rakefile
159
+ - VERSION
160
+ - lib/capistrano-bootstrap.rb
161
+ - spec/capistrano-bootstrap_spec.rb
162
+ - spec/spec_helper.rb
163
+ homepage: http://github.com/x3ro/capistrano-bootstrap
164
+ licenses:
165
+ - MIT
166
+ post_install_message:
167
+ rdoc_options: []
168
+ require_paths:
169
+ - lib
170
+ required_ruby_version: !ruby/object:Gem::Requirement
171
+ none: false
172
+ requirements:
173
+ - - ! '>='
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ segments:
177
+ - 0
178
+ hash: 1517474287064682130
179
+ required_rubygems_version: !ruby/object:Gem::Requirement
180
+ none: false
181
+ requirements:
182
+ - - ! '>='
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ requirements: []
186
+ rubyforge_project:
187
+ rubygems_version: 1.8.24
188
+ signing_key:
189
+ specification_version: 3
190
+ summary: This gem can be used to keep capistrano configuration synchronized between
191
+ multiple projects.
192
+ test_files: []