Roman2K-capistrano-fixes 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2008-2009 Roman Le Négrate
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.mdown ADDED
@@ -0,0 +1,31 @@
1
+ # Capistrano fixes
2
+
3
+ Originally written for making Capistrano behave in a consistent way with `Capfile`'s generated for websites hosted on [Flucti][] VPS's. I figured it might be useful to other people, especially those deploying from Windows machines.
4
+
5
+ To figure out what sort of improvement this library brings, please read the [source code](http://github.com/Roman2K/capistrano-fixes/tree/master/lib).
6
+
7
+ ## Installation
8
+
9
+ $ gem install Roman2K-capistrano-fixes -s http://gems.github.com/
10
+
11
+ Or:
12
+
13
+ $ gem install capistrano-fixes -s http://gems.flucti.com/
14
+
15
+ ## Usage
16
+
17
+ At the top of the `Capfile` (or `config/deploy.rb`), add:
18
+
19
+ require 'capistrano_fixes'
20
+ Capistrano::Fixes.apply! self
21
+
22
+ If the assets (images, JavaScript files, stylesheets, etc...) are located at the document root instead of underneath a `public/` folder, add the following call, anywhere inside the `Capfile`:
23
+
24
+ root_contains_assets!
25
+
26
+ ## Credits
27
+
28
+ Written by [Roman Le Négrate](http://roman.flucti.com) ([contact](mailto:roman.lenegrate@gmail.com)). Released under the MIT license: see the `LICENSE` file.
29
+
30
+
31
+ [Flucti]: http://www.flucti.com
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'echoe'
2
+
3
+ Echoe.new('capistrano-fixes', '0.1.2') do |p|
4
+ p.description = "Fixes for Capistrano default recipes"
5
+ p.author = "Roman Le Négrate"
6
+ p.email = "roman.lenegrate@gmail.com"
7
+ p.ignore_pattern = "*.gemspec"
8
+ p.dependencies = ["capistrano ~>2.5", "archive-tar-minitar ~>0.5"]
9
+ p.development_dependencies = ["mocha"]
10
+ p.rdoc_options = %w(--main README.mdown --inline-source --line-numbers --charset UTF-8)
11
+ end
@@ -0,0 +1,41 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{capistrano-fixes}
5
+ s.version = "0.1.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Roman Le N\303\251grate"]
9
+ s.date = %q{2009-03-25}
10
+ s.description = %q{Fixes for Capistrano default recipes}
11
+ s.email = %q{roman.lenegrate@gmail.com}
12
+ s.extra_rdoc_files = ["lib/capistrano_fixes/tasks.rb", "lib/capistrano_fixes.rb", "LICENSE", "README.mdown"]
13
+ s.files = ["lib/capistrano_fixes/tasks.rb", "lib/capistrano_fixes.rb", "LICENSE", "Manifest", "Rakefile", "README.mdown", "test/capistrano_fixes_test.rb", "capistrano-fixes.gemspec"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{}
16
+ s.rdoc_options = ["--main", "README.mdown", "--inline-source", "--line-numbers", "--charset", "UTF-8"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{capistrano-fixes}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Fixes for Capistrano default recipes}
21
+ s.test_files = ["test/capistrano_fixes_test.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 2
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_runtime_dependency(%q<capistrano>, ["~> 2.5"])
29
+ s.add_runtime_dependency(%q<archive-tar-minitar>, ["~> 0.5"])
30
+ s.add_development_dependency(%q<mocha>, [">= 0"])
31
+ else
32
+ s.add_dependency(%q<capistrano>, ["~> 2.5"])
33
+ s.add_dependency(%q<archive-tar-minitar>, ["~> 0.5"])
34
+ s.add_dependency(%q<mocha>, [">= 0"])
35
+ end
36
+ else
37
+ s.add_dependency(%q<capistrano>, ["~> 2.5"])
38
+ s.add_dependency(%q<archive-tar-minitar>, ["~> 0.5"])
39
+ s.add_dependency(%q<mocha>, [">= 0"])
40
+ end
41
+ end
@@ -0,0 +1,44 @@
1
+ namespace(:deploy) do
2
+ desc "Symlink shared files and directories."
3
+ task(:symlink_files) do
4
+ [:shared_dirs, :shared_files].map { |var| fetch(var, []) }.flatten.each do |path|
5
+ source = "#{latest_release}/#{path}"
6
+ destination = "#{shared_path}/#{path}"
7
+
8
+ d = destination
9
+ dp = File.dirname(destination)
10
+ p = previous_release
11
+ r = path
12
+ s = source
13
+
14
+ run <<-CMD
15
+ [ -e #{d} ] || { mkdir -p #{dp} && { if [[ "#{p}" ]] && [ -e #{p}/#{r} ]; then cp -r #{p}/#{r} #{d}; elif [ -e #{s} ]; then cp -r #{s} #{d}; fi } };
16
+ rm -rf #{source} && ln -nfs #{destination} #{source}
17
+ CMD
18
+ end
19
+ end
20
+ after('deploy:finalize_update') { symlink_files }
21
+
22
+ task :delete_mac_os_metadata_files do
23
+ run "cd #{latest_release} && find . | egrep '(^|/)\\._' | xargs rm 2>/dev/null; true"
24
+ end
25
+ after('deploy:finalize_update') { delete_mac_os_metadata_files }
26
+
27
+ desc "Download files from the current revision."
28
+ task :download do
29
+ (ENV["FILES"] || "").split(",").each do |path|
30
+ path = path.strip
31
+ top.download(File.join(current_path, path), path)
32
+ end
33
+ end
34
+ end
35
+
36
+ # Capistrano assumes assets are placed under public/ but static or PHP sites
37
+ # usually don't have such a separate directory for assets, since they're made of
38
+ # such files. Accommodate to its default behaviour by symlinking appropriately.
39
+ def root_contains_assets!
40
+ after('deploy:finalize_update') do
41
+ run %w(images stylesheets javascripts).map { |dir| "ln -ns ../#{dir} #{latest_release}/public/#{dir}" }.join('; ')
42
+ run "ln -ns public/system #{latest_release}/system"
43
+ end
44
+ end
@@ -0,0 +1,147 @@
1
+ module Capistrano::Fixes
2
+ # Make transfers happen over SCP instead of SFTP by default, in order to
3
+ # resolve ~ as the home of the logged in user.
4
+ module SCPAsDefaultTransferProtocol
5
+ def self.included(target)
6
+ target.module_eval do
7
+ alias_method :transfer_without_scp_default, :transfer
8
+ alias_method :transfer, :transfer_with_scp_default
9
+ end
10
+ end
11
+
12
+ def transfer_with_scp_default(direction, from, to, options={}, &block)
13
+ options = {:via => :scp, :recursive => true}.merge(options)
14
+ transfer_without_scp_default(direction, from, to, options, &block)
15
+ end
16
+ end
17
+
18
+ # Fix Windows-style switches that get screwed up by Capistrano when trying to
19
+ # normalize paths.
20
+ #
21
+ # Note about the implementation: the `system' method is not copied and
22
+ # re-aliased on purpose. The target class (strategy base class) calls `super'
23
+ # which forwards the call to ancestor modules, including this one, which in
24
+ # turn delegates to Kernel.
25
+ module WindowsCommandSwitchInversion
26
+ private
27
+ def system(*cmd)
28
+ cmd = cmd.join(' ')
29
+ while cmd.gsub! /([ A-Z])\\([A-Z])\b/, '\1/\2'; end
30
+ super cmd
31
+ end
32
+ end
33
+
34
+ require "capistrano/recipes/deploy/strategy/copy"
35
+ module CommandlessCompression
36
+ def self.included(target)
37
+ target.class_eval do
38
+ alias_method :compression_without_commandless, :compression
39
+ alias_method :compression, :compression_with_commandless
40
+
41
+ alias_method :compress_without_commandless, :compress
42
+ alias_method :compress, :compress_with_commandless
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def system(*args)
49
+ args = args.flatten
50
+ if (proc = args.first).respond_to?(:call)
51
+ proc.call(*args[1..-1])
52
+ else
53
+ super
54
+ end
55
+ end
56
+
57
+ def compression_with_commandless
58
+ orig = compression_without_commandless
59
+ return orig if Kernel.system("which #{orig.compress_command.first} > /dev/null 2>&1")
60
+ Capistrano::Deploy::Strategy::Copy::Compression.new \
61
+ Tarring::EXTENSION,
62
+ [Tarring.method(:compress)],
63
+ Tarring::DECOMPRESSION_COMMAND
64
+ end
65
+
66
+ def compress_with_commandless(*args, &block)
67
+ compress_without_commandless(*args, &block).extend JoinInhibition
68
+ end
69
+
70
+ module JoinInhibition
71
+ def join(*args, &block)
72
+ self
73
+ end
74
+ end
75
+
76
+ require 'archive/tar/minitar'
77
+ require 'zlib'
78
+ module Tarring
79
+ EXTENSION = "tar.gz"
80
+ DECOMPRESSION_COMMAND = %w(tar zxf)
81
+
82
+ # Does the equivalent of:
83
+ #
84
+ # $ cd #{File.dirname directory} && tar czf #{file} #{File.basename directory}`
85
+ #
86
+ # but without invoking any command that would result in an external
87
+ # dependency. Convenient under Windows where there exists no `tar'
88
+ # or `zip' command.
89
+ def self.compress(file, directory)
90
+ # Note: it's very important to open in binary mode on Windows. Not
91
+ # doing so results in invalid tar headers.
92
+ sgz = Zlib::GzipWriter.new(File.open(file, 'wb'))
93
+
94
+ tar = Archive::Tar::Minitar::Output.new(sgz)
95
+ begin
96
+ Dir.chdir(File.dirname(directory)) do
97
+ Dir.glob("#{File.basename directory}/**/*") do |file|
98
+ Archive::Tar::Minitar.pack_file(file, tar)
99
+ end
100
+ end
101
+ ensure
102
+ tar.close # closes `sgz' which in turn closes `file'
103
+ end
104
+ end
105
+ end
106
+ end
107
+
108
+ # Tasks like deploy:upload take a list of files or globs from $FILES which is
109
+ # passed to Dir.glob. The problem is, to that method, the backslash is a
110
+ # special character. So all instances of said character have to be replaced
111
+ # with forward slashes under Windows.
112
+ def self.fix_env!
113
+ ENV["FILES"] &&= ENV["FILES"].tr('\\', '/')
114
+ end
115
+
116
+ # On Windows, assume that the private SSH key is located under My Documents.
117
+ # Works for at least French and English versions of Windows.
118
+ def self.add_ssh_keys_for_windows!(ssh_options)
119
+ if profile_dir = ENV["USERPROFILE"] and (ssh_options[:keys] ||= []).empty?
120
+ glob = "#{profile_dir}/* Documents/{*,**}/*.priv{,.txt}".tr('\\', '/')
121
+ if first = Dir.glob(glob) { |f| break f }
122
+ ssh_options[:keys] << first
123
+ end
124
+ end
125
+ end
126
+
127
+ def self.apply!(context)
128
+ require 'capistrano/configuration/actions/file_transfer'
129
+ Capistrano::Configuration::Actions::FileTransfer.module_eval do
130
+ include SCPAsDefaultTransferProtocol
131
+ end
132
+
133
+ require 'capistrano/recipes/deploy/strategy/base'
134
+ Capistrano::Deploy::Strategy::Base.class_eval do
135
+ include WindowsCommandSwitchInversion
136
+ end
137
+
138
+ require 'capistrano/recipes/deploy/strategy/copy'
139
+ Capistrano::Deploy::Strategy::Copy.class_eval do
140
+ include CommandlessCompression
141
+ end
142
+
143
+ fix_env!
144
+ add_ssh_keys_for_windows!(context.ssh_options)
145
+ context.load File.dirname(__FILE__) + '/capistrano_fixes/tasks.rb'
146
+ end
147
+ end
@@ -0,0 +1,104 @@
1
+ require 'test/unit'
2
+ require 'mocha'
3
+
4
+ require 'capistrano'
5
+ require 'capistrano_fixes'
6
+
7
+ class Capistrano::Fixes::SCPAsDefaultTransferProtocolTest < Test::Unit::TestCase
8
+ M = Capistrano::Fixes::SCPAsDefaultTransferProtocol
9
+
10
+ class FileTransfer
11
+ CALLS = []
12
+ def transfer(*args)
13
+ CALLS << args
14
+ end
15
+ include M
16
+ end
17
+
18
+ def test_transfer
19
+ tr = FileTransfer.new
20
+ tr.transfer(:up, "from", "to")
21
+ assert_equal [[:up, "from", "to", {:via => :scp, :recursive => true}]], FileTransfer::CALLS
22
+ ensure
23
+ FileTransfer::CALLS.clear
24
+ end
25
+ end
26
+
27
+ class Capistrano::Fixes::CommandlessCompressionTest < Test::Unit::TestCase
28
+ M = Capistrano::Fixes::CommandlessCompression
29
+
30
+ class Strategy
31
+ Compression = Struct.new(:compress_command)
32
+
33
+ def deploy!
34
+ system(compress(:file, :directory).join(" "))
35
+ end
36
+
37
+ private
38
+
39
+ def compression
40
+ Compression.new(%w(tar czf))
41
+ end
42
+
43
+ # Copied from Capistrano source code.
44
+ def compress(file, directory)
45
+ compression.compress_command + [file, directory]
46
+ end
47
+
48
+ include M
49
+ end
50
+
51
+ def test_deploy!
52
+ strategy = Strategy.new
53
+
54
+ # Fake absence of `tar' to trigger the use of command-less tarring
55
+ Kernel.stubs(:system).with("which tar > /dev/null 2>&1").returns false
56
+
57
+ # Expect command-less tarring
58
+ M::Tarring.expects(:compress).with(:file, :directory)
59
+
60
+ strategy.deploy!
61
+ end
62
+ end
63
+
64
+ class Capistrano::Fixes::CommandlessCompression::TarringTest < Test::Unit::TestCase
65
+ M = Capistrano::Fixes::CommandlessCompression::Tarring
66
+
67
+ def setup
68
+ @tmp = File.dirname(__FILE__) + "/tmp"
69
+
70
+ # Fix for when the test is not run individually:
71
+ @tmp = File.expand_path(@tmp)
72
+
73
+ teardown
74
+ FileUtils.mkdir_p(@tmp)
75
+ end
76
+
77
+ def teardown
78
+ FileUtils.rm_rf(@tmp)
79
+ end
80
+
81
+ def test_compress
82
+ FileUtils.mkdir("#{@tmp}/foo")
83
+ FileUtils.mkdir("#{@tmp}/foo/bar")
84
+ FileUtils.touch("#{@tmp}/foo/bar/abc")
85
+ FileUtils.mkdir("#{@tmp}/foo/baz")
86
+
87
+ # Compress
88
+ archive = "#{@tmp}/foo.tgz"
89
+ M.compress(archive, "#{@tmp}/foo")
90
+
91
+ # Uncompress
92
+ unpack_dir = "#{@tmp}/unpack"
93
+ FileUtils.mkdir(unpack_dir)
94
+ Dir.chdir(unpack_dir) do
95
+ system(*M::DECOMPRESSION_COMMAND + [archive])
96
+ end
97
+
98
+ expected = [ "#{unpack_dir}/foo",
99
+ "#{unpack_dir}/foo/bar",
100
+ "#{unpack_dir}/foo/bar/abc",
101
+ "#{unpack_dir}/foo/baz" ]
102
+ assert_equal expected, Dir["#{unpack_dir}/**/*"]
103
+ end
104
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Roman2K-capistrano-fixes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - "Roman Le N\xC3\xA9grate"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-25 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: capistrano
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: "2.5"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: archive-tar-minitar
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: "0.5"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: mocha
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description: Fixes for Capistrano default recipes
46
+ email: roman.lenegrate@gmail.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - lib/capistrano_fixes/tasks.rb
53
+ - lib/capistrano_fixes.rb
54
+ - LICENSE
55
+ - README.mdown
56
+ files:
57
+ - lib/capistrano_fixes/tasks.rb
58
+ - lib/capistrano_fixes.rb
59
+ - LICENSE
60
+ - Manifest
61
+ - Rakefile
62
+ - README.mdown
63
+ - test/capistrano_fixes_test.rb
64
+ - capistrano-fixes.gemspec
65
+ has_rdoc: true
66
+ homepage: ""
67
+ post_install_message:
68
+ rdoc_options:
69
+ - --main
70
+ - README.mdown
71
+ - --inline-source
72
+ - --line-numbers
73
+ - --charset
74
+ - UTF-8
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "1.2"
88
+ version:
89
+ requirements: []
90
+
91
+ rubyforge_project: capistrano-fixes
92
+ rubygems_version: 1.2.0
93
+ signing_key:
94
+ specification_version: 2
95
+ summary: Fixes for Capistrano default recipes
96
+ test_files:
97
+ - test/capistrano_fixes_test.rb