capistrano-central_git 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2d2196a8907e8b21b8668cac52fcff822c52b013
4
+ data.tar.gz: ebdbccf3154e3946abba8ed18f8bc6eb018bd40b
5
+ SHA512:
6
+ metadata.gz: 27b0899958981d4c6eaef631f9ceaec6ff2342fbe4279a7124778fea2d45c4333fb9841c730c167d1d1407474c33a968ca80531bda8f331ec4d5a57a3bc77221
7
+ data.tar.gz: 056a1bf4ade98a499b7bad140afd54b044bfe1b8da5ddd991db8cd6ce87e7a1784bbcfdfc0b3de6d35751f3dc82b03e4a39a7ca65395f9f5cab523e2f6bd38dc
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in capistrano-central_git.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Capistrano::CentralGit
2
+
3
+ **Note that this product is still under development. So they can be changed without notices.**
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'capistrano-central_git'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install capistrano-central_git
20
+
21
+ ## Usage
22
+
23
+ In Capfile:
24
+
25
+ ```ruby
26
+ require "capistrano/setup"
27
+ require "capistrano/deploy"
28
+ require "capistrano/central_git"
29
+ ```
30
+
31
+ In config/deploy.rb:
32
+
33
+ ```ruby
34
+ set :deploy_to, "/var/www/my-app"
35
+ set :repo_url, "git@github.com:you/my-app.git"
36
+
37
+ set :scm, :central_git
38
+ set :central_host, "your-build-server"
39
+ set :central_path, "/home/you/central_git"
40
+ ```
41
+
42
+ And make sure that:
43
+
44
+ - Your central_host can access to release servers via ssh.
45
+ - Your central_host includes rsync.
46
+
47
+ ## Configurations
48
+
49
+ | Name | Default | Description |
50
+ |---|---|---|
51
+ | repo_url | | |
52
+ | repo_tree | `nil` | |
53
+ | branch | master | |
54
+ | ssh_options | {} | |
55
+ | keep_releases | 5 | |
56
+ | scm | `nil` | | Must be `central_git` |
57
+ | central_host | `nil` | |
58
+ | central_host_ssh_options | {} | |
59
+ | central_path | /var/www/#{application} | |
60
+ | central_repo_path | #{central_path}/repo | |
61
+ | central_packages_path | #{central_path}/packages |
62
+ | deploy_to | /var/www/#{application} | |
63
+ | release_packages_path | #{deploy_to}/packages | |
64
+ | excludes | [] | |
65
+ | rsync_options | -al | |
66
+ | rsync_rsh | /usr/bin/ssh | |
67
+ | max_parallels | number of hosts | |
68
+ | keep_packages | 5 | |
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/a2ikm/capistrano-central_git.
73
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "capistrano/central_git"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'capistrano/central_git/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "capistrano-central_git"
8
+ spec.version = Capistrano::CentralGit::VERSION
9
+ spec.authors = ["Masato Ikeda"]
10
+ spec.email = ["masato.ikeda@gmail.com"]
11
+
12
+ spec.summary = %q{Build release files on central server.}
13
+ spec.description = %q{Build release files on central server.}
14
+ spec.homepage = "https://github.com/a2ikm/capistrano-central_git"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "capistrano", ">= 3.2"
22
+ spec.add_dependency "parallel"
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
@@ -0,0 +1,59 @@
1
+ module Capistrano::CentralGit
2
+ class Config
3
+ def self.package_name
4
+ @package_name ||= fetch(:package_name, "#{fetch(:release_timestamp)}.tar.gz")
5
+ end
6
+
7
+ def self.central_path
8
+ @central_path ||= Pathname.new(fetch(:central_path, "/var/www/#{fetch(:application)}"))
9
+ end
10
+
11
+ def self.central_repo_path
12
+ @central_repo_path ||= Pathname.new(fetch(:central_repo_path, central_path.join("repo")))
13
+ end
14
+
15
+ def self.central_packages_path
16
+ @central_packages_path ||= Pathname.new(fetch(:central_packages_path, central_path.join("packages")))
17
+ end
18
+
19
+ def self.central_package_path
20
+ @central_package_path ||= Pathname.new(fetch(:central_package_path, central_packages_path.join(package_name)))
21
+ end
22
+
23
+ def self.release_packages_path
24
+ @release_packages_path ||= Pathname.new(fetch(:release_packages_path, shared_path.join("packages")))
25
+ end
26
+
27
+ def self.release_package_path
28
+ @release_package_path ||= Pathname.new(fetch(:remote_packge_path, release_packages_path.join(package_name)))
29
+ end
30
+
31
+ def self.excludes
32
+ @excludes ||= fetch(:excludes, []).push(".git").uniq
33
+ end
34
+
35
+ def self.central_host
36
+ @central_host ||= fetch(:central_host)
37
+ end
38
+
39
+ def self.central_host_ssh_options
40
+ @central_host_ssh_options ||= fetch(:central_host_ssh_options) || fetch(:ssh_options, {})
41
+ end
42
+
43
+ def self.rsync_options
44
+ @rsync_options ||= fetch(:rsync_options, "-al")
45
+ end
46
+
47
+ def self.rsync_rsh
48
+ @rsync_rsh ||= fetch(:rsync_rsh, "/usr/bin/ssh")
49
+ end
50
+
51
+ def self.max_parallels(hosts)
52
+ @max_parallels ||= fetch(:max_parallels, hosts.size).to_i
53
+ end
54
+
55
+ def self.keep_packages
56
+ @keep_packages ||= fetch(:keep_packages, 5).to_i
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,103 @@
1
+ require "delegate"
2
+ require "parallel"
3
+ require "capistrano/central_git/config"
4
+
5
+ module Capistrano::CentralGit
6
+ class SCM < SimpleDelegator
7
+ def initialize(context)
8
+ super(context)
9
+ end
10
+
11
+ def config
12
+ Capistrano::CentralGit::Config
13
+ end
14
+
15
+ def sshkit_host
16
+ @sshkit_host ||= SSHKit::Host.new(config.central_host).tap do |h|
17
+ h.ssh_options = config.central_host_ssh_options
18
+ end
19
+ end
20
+
21
+ def git(*args)
22
+ args.unshift :git
23
+ execute *args
24
+ end
25
+
26
+ def test!(*args)
27
+ __getobj__.test *args
28
+ end
29
+
30
+ def test
31
+ test! " [ -d #{config.central_repo_path}/.git ] "
32
+ end
33
+
34
+ def check
35
+ git :"ls-remote --heads", repo_url
36
+ end
37
+
38
+ def clone
39
+ git :clone, "--recursive", repo_url, config.central_repo_path
40
+ end
41
+
42
+ def update
43
+ git :remote, :update
44
+ git :checkout, fetch(:branch)
45
+ git :pull
46
+ git :submodule, :update, "--init"
47
+ end
48
+
49
+ def create_package
50
+ excludes = config.excludes.flat_map { |e| ["--exclude", e] }
51
+
52
+ tree_top =
53
+ if tree = fetch(:repo_tree)
54
+ tree.slice %r#^/?(.*?)/?$#, 1
55
+ else
56
+ ""
57
+ end
58
+
59
+ execute :mkdir, "-p", config.central_packages_path
60
+
61
+ within tree_top do
62
+ execute :tar, "zcf", config.central_package_path, *excludes, "."
63
+ end
64
+ end
65
+
66
+ def deploy_package
67
+ hosts = ::Capistrano::Configuration.env.filter(release_roles(:all))
68
+ rsync_options = config.rsync_options
69
+ rsync_rsh = config.rsync_rsh
70
+ central_package_path = config.central_package_path
71
+ release_package_path = config.release_package_path
72
+ Parallel.each(hosts, in_threads: config.max_parallels(hosts)) do |host|
73
+ execute :rsync, "#{rsync_options} --rsh='#{rsync_rsh}' #{central_package_path} #{host}:#{release_package_path}"
74
+ end
75
+ end
76
+
77
+ def extract_package
78
+ execute :mkdir, "-p", release_path
79
+ execute :tar, "zxf", config.release_package_path, "-C", release_path
80
+ end
81
+
82
+ def cleanup_package
83
+ packages = capture(:ls, '-xtr', config.central_packages_path).split
84
+ keep_packages = config.keep_packages
85
+ if packages.count >= keep_packages
86
+ info t(:keeping_packages, host: host.to_s, keep_packages: keep_packages, packages: packages.count)
87
+ older_packages = (packages - packages.last(:keep_packages))
88
+ if older_packages.any?
89
+ older_packages_str = older_packages.map do |package|
90
+ config.central_packages_path.join(package)
91
+ end.join(" ")
92
+ execute :rm, '-rf', older_packages_str
93
+ else
94
+ info t(:no_old_packages, host: host.to_s, keep_packages: keep_packages)
95
+ end
96
+ end
97
+ end
98
+
99
+ def fetch_revision
100
+ capture(:git, "rev-list --max-count=1 --abbrev-commit #{fetch(:branch)}").strip
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,5 @@
1
+ module Capistrano
2
+ module CentralGit
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ require "capistrano/central_git/version"
2
+
3
+ module Capistrano
4
+ module CentralGit
5
+ end
6
+ end
7
+
8
+ load File.expand_path("../tasks/central_git.rake", __FILE__)
@@ -0,0 +1,155 @@
1
+ unless defined?(Capistrano::CentralGit::TASK_LOADED)
2
+ Capistrano::CentralGit::TASK_LOADED = true
3
+
4
+ require "capistrano/central_git/scm"
5
+
6
+ namespace :central_git do
7
+
8
+ def central_git_scm
9
+ @central_git_scm ||= Capistrano::CentralGit::SCM.new(self)
10
+ end
11
+
12
+ def run_central(&block)
13
+ SSHKit.config.backend.new(central_git_scm.sshkit_host, &block).run
14
+ end
15
+
16
+ def central_path
17
+ central_git_scm.config.central_path
18
+ end
19
+
20
+ def central_repo_path
21
+ central_git_scm.config.central_repo_path
22
+ end
23
+
24
+ def central_packages_path
25
+ central_git_scm.config.central_packages_path
26
+ end
27
+
28
+ def release_packages_path
29
+ central_git_scm.config.release_packages_path
30
+ end
31
+
32
+ set :git_environmental_variables, ->() {
33
+ {
34
+ git_askpass: "/bin/echo",
35
+ git_ssh: "#{fetch(:tmp_dir)}/#{fetch(:application)}/git-ssh.sh"
36
+ }
37
+ }
38
+
39
+ desc "Upload the git wrapper script, this script guarantees that we can script git without getting an interactive prompt"
40
+ task :wrapper do
41
+ run_central do
42
+ execute :mkdir, "-p", "#{fetch(:tmp_dir)}/#{fetch(:application)}/"
43
+ upload! StringIO.new("#!/bin/sh -e\nexec /usr/bin/ssh -o PasswordAuthentication=no -o StrictHostKeyChecking=no \"$@\"\n"), "#{fetch(:tmp_dir)}/#{fetch(:application)}/git-ssh.sh"
44
+ execute :chmod, "+x", "#{fetch(:tmp_dir)}/#{fetch(:application)}/git-ssh.sh"
45
+ end
46
+ end
47
+
48
+ desc "Check that the repository is reachable"
49
+ task check: :"central_git:wrapper" do
50
+ fetch(:branch)
51
+ run_central do
52
+ with fetch(:git_environmental_variables) do
53
+ central_git_scm.check
54
+ end
55
+ execute :mkdir, "-p", central_path
56
+ end
57
+
58
+ on release_roles :all do |host|
59
+ execute :mkdir, "-p", release_packages_path
60
+ end
61
+ end
62
+
63
+ desc "Clone the repo to the cache"
64
+ task clone: :"central_git:wrapper" do
65
+ run_central do
66
+ if central_git_scm.test
67
+ info t(:mirror_exists, at: central_repo_path)
68
+ else
69
+ with fetch(:git_environmental_variables) do
70
+ central_git_scm.clone
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ desc "Update the repo mirror to reflect the origin state"
77
+ task update: :"central_git:clone" do
78
+ run_central do
79
+ within central_repo_path do
80
+ with fetch(:git_environmental_variables) do
81
+ central_git_scm.update
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ desc "Copy repo to releases"
88
+ task create_release: :"central_git:update" do
89
+ run_central do
90
+ with fetch(:git_environmental_variables) do
91
+ within central_repo_path do
92
+ central_git_scm.create_package
93
+ central_git_scm.deploy_package
94
+ end
95
+ end
96
+ end
97
+
98
+ on release_roles :all do
99
+ within releases_path do
100
+ central_git_scm.extract_package
101
+ end
102
+ end
103
+ end
104
+
105
+ desc 'Determine the revision that will be deployed'
106
+ task :set_current_revision do
107
+ run_central do
108
+ within central_repo_path do
109
+ with fetch(:git_environmental_variables) do
110
+ set :current_revision, central_git_scm.fetch_revision
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ desc "Cleanup packages"
117
+ task cleanup: :"deploy:cleanup" do
118
+ run_central do
119
+ within central_packages_path do
120
+ central_git_scm.cleanup_packages
121
+ end
122
+ end
123
+ end
124
+
125
+ desc "Show configurations"
126
+ task :config do
127
+ config = central_git_scm.config
128
+ hosts = ::Capistrano::Configuration.env.filter(release_roles(:all))
129
+
130
+ pairs = {
131
+ repo_url: fetch(:repo_url),
132
+ repo_tree: fetch(:repo_tree),
133
+ central_host: config.central_host,
134
+ central_path: config.central_path,
135
+ central_repo_path: config.central_repo_path,
136
+ central_packages_path: config.central_packages_path,
137
+ deploy_to: fetch(:deploy_to),
138
+ release_packages_path: config.release_packages_path,
139
+ excludes: config.excludes.join(" "),
140
+ rsync_options: config.rsync_options,
141
+ rsync_rsh: config.rsync_rsh,
142
+ max_parallels: config.max_parallels(hosts),
143
+ keep_packages: config.keep_packages,
144
+ }
145
+
146
+ max_key_length = pairs.keys.map { |k| k.to_s.length }.max
147
+ col = max_key_length + 2
148
+ pairs.each do |key, value|
149
+ puts "#{key.to_s.rjust(col)} #{value}"
150
+ end
151
+
152
+ end
153
+ end
154
+
155
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-central_git
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Masato Ikeda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: capistrano
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: parallel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: Build release files on central server.
70
+ email:
71
+ - masato.ikeda@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - capistrano-central_git.gemspec
84
+ - lib/capistrano/central_git.rb
85
+ - lib/capistrano/central_git/config.rb
86
+ - lib/capistrano/central_git/scm.rb
87
+ - lib/capistrano/central_git/version.rb
88
+ - lib/capistrano/tasks/central_git.rake
89
+ homepage: https://github.com/a2ikm/capistrano-central_git
90
+ licenses: []
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.5
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Build release files on central server.
112
+ test_files: []
113
+ has_rdoc: