blavosync 0.1.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Blavosync.gemspec +20 -5
- data/History.txt +2 -0
- data/Manifest.txt +15 -0
- data/README.markdown +91 -0
- data/VERSION +1 -1
- data/lib/blavosync/lib/ey_logger.rb +125 -0
- data/lib/blavosync/lib/ey_logger_hooks.rb +14 -0
- data/lib/blavosync/recipes/content_rsync.rb +39 -0
- data/lib/blavosync/recipes/content_tar.rb +64 -0
- data/lib/blavosync/recipes/database.rb +79 -0
- data/lib/blavosync/recipes/local.rb +167 -0
- data/lib/blavosync/recipes/util.rb +21 -0
- data/lib/blavosync/recipes.rb +15 -0
- data/lib/blavosync.rb +2 -294
- data/test/test_blavosync.rb +11 -0
- data/test/test_helper.rb +2 -0
- metadata +18 -6
- data/README.rdoc +0 -118
- data/init.rb +0 -3
data/Blavosync.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{blavosync}
|
8
|
-
s.version = "0.1
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["jayronc"]
|
@@ -14,22 +14,37 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.email = %q{jerrodblavos@mac.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
|
-
"README.
|
17
|
+
"README.markdown"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
"Blavosync.gemspec",
|
21
|
+
"History.txt",
|
21
22
|
"LICENSE",
|
22
|
-
"
|
23
|
+
"Manifest.txt",
|
24
|
+
"README.markdown",
|
23
25
|
"Rakefile",
|
24
26
|
"VERSION",
|
25
|
-
"
|
26
|
-
"lib/blavosync.rb"
|
27
|
+
"lib/blavosync.rb",
|
28
|
+
"lib/blavosync/lib/ey_logger.rb",
|
29
|
+
"lib/blavosync/lib/ey_logger_hooks.rb",
|
30
|
+
"lib/blavosync/recipes.rb",
|
31
|
+
"lib/blavosync/recipes/content_rsync.rb",
|
32
|
+
"lib/blavosync/recipes/content_tar.rb",
|
33
|
+
"lib/blavosync/recipes/database.rb",
|
34
|
+
"lib/blavosync/recipes/local.rb",
|
35
|
+
"lib/blavosync/recipes/util.rb",
|
36
|
+
"test/test_blavosync.rb",
|
37
|
+
"test/test_helper.rb"
|
27
38
|
]
|
28
39
|
s.homepage = %q{http://github.com/indierockmedia/Blavosync}
|
29
40
|
s.rdoc_options = ["--charset=UTF-8"]
|
30
41
|
s.require_paths = ["lib"]
|
31
42
|
s.rubygems_version = %q{1.3.5}
|
32
43
|
s.summary = %q{Sync a remote db and rsync content to your development environment.}
|
44
|
+
s.test_files = [
|
45
|
+
"test/test_blavosync.rb",
|
46
|
+
"test/test_helper.rb"
|
47
|
+
]
|
33
48
|
|
34
49
|
if s.respond_to? :specification_version then
|
35
50
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
data/History.txt
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
History.txt
|
2
|
+
Manifest.txt
|
3
|
+
README.txt
|
4
|
+
Rakefile
|
5
|
+
lib/blavosync.rb
|
6
|
+
lib/blavosync/lib/ey_logger.rb
|
7
|
+
lib/blavosync/lib/ey_logger_hooks.rb
|
8
|
+
lib/blavosync/recipes.rb
|
9
|
+
lib/blavosync/recipes/local.rb
|
10
|
+
lib/blavosync/recipes/util.rb
|
11
|
+
lib/blavosync/recipes/database.rb
|
12
|
+
lib/blavosync/recipes/content_rsync.rb
|
13
|
+
lib/blavosync/recipes/content_tar.rb
|
14
|
+
test/test_blavosync.rb
|
15
|
+
test/test_helper.rb
|
data/README.markdown
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
== blavosync
|
2
|
+
|
3
|
+
INSTALLATION:
|
4
|
+
Add this line to your config/deploy.rb file
|
5
|
+
|
6
|
+
require 'blavosync/recipes'
|
7
|
+
|
8
|
+
USAGE:
|
9
|
+
adds the following tasks to projects using capistrano
|
10
|
+
|
11
|
+
------------------------------------------------------------
|
12
|
+
cap local:backup_content
|
13
|
+
------------------------------------------------------------
|
14
|
+
Downloads a tarball of shared content (identified by the :shared_content and
|
15
|
+
:content _ directories properties) from a deployable environment (RAILS_ENV) to
|
16
|
+
the local filesystem.
|
17
|
+
|
18
|
+
------------------------------------------------------------
|
19
|
+
cap local:backup _ db
|
20
|
+
------------------------------------------------------------
|
21
|
+
Backs up deployable environment's database and copies it to
|
22
|
+
the local machine.
|
23
|
+
|
24
|
+
------------------------------------------------------------
|
25
|
+
cap local:force _ backup _ content
|
26
|
+
------------------------------------------------------------
|
27
|
+
Regenerate files.
|
28
|
+
|
29
|
+
------------------------------------------------------------
|
30
|
+
cap local:force _ backup _ db
|
31
|
+
------------------------------------------------------------
|
32
|
+
Regenerate files.
|
33
|
+
|
34
|
+
------------------------------------------------------------
|
35
|
+
cap local:restore _ content
|
36
|
+
------------------------------------------------------------
|
37
|
+
Restores the backed up content (env var FROM specifies which environment
|
38
|
+
was backed up, defaults to RAILS_ENV) to the local development environment app
|
39
|
+
|
40
|
+
------------------------------------------------------------
|
41
|
+
cap local:restore _ db
|
42
|
+
------------------------------------------------------------
|
43
|
+
Untars the backup file downloaded from local:backup_db and imports
|
44
|
+
(via mysql command line tool) it back into the development database.
|
45
|
+
|
46
|
+
------------------------------------------------------------
|
47
|
+
cap local:resync _ db
|
48
|
+
------------------------------------------------------------
|
49
|
+
Ensure that a fresh remote data dump is retrieved before syncing to the local
|
50
|
+
environment
|
51
|
+
|
52
|
+
------------------------------------------------------------
|
53
|
+
cap local:sync
|
54
|
+
------------------------------------------------------------
|
55
|
+
Wrapper for local:sync _ db and local:sync _ content
|
56
|
+
$> cap local:sync
|
57
|
+
|
58
|
+
------------------------------------------------------------
|
59
|
+
cap local:sync_content
|
60
|
+
------------------------------------------------------------
|
61
|
+
Wrapper for local:backup _ content and local:restore _ content
|
62
|
+
$> cap local:sync_content
|
63
|
+
|
64
|
+
------------------------------------------------------------
|
65
|
+
cap local:sync_db
|
66
|
+
------------------------------------------------------------
|
67
|
+
Wrapper for local:backup _ db and local:restore _ db.
|
68
|
+
$> cap local:sync _ db
|
69
|
+
|
70
|
+
------------------------------------------------------------
|
71
|
+
cap local:sync_init
|
72
|
+
------------------------------------------------------------
|
73
|
+
Wrapper for local:force _ backup _ db, local:force _ backup _ content, and the
|
74
|
+
local:sync to get
|
75
|
+
a completely fresh set of data from the server
|
76
|
+
$> cap local:sync
|
77
|
+
|
78
|
+
|
79
|
+
== Note on Patches/Pull Requests
|
80
|
+
|
81
|
+
* Fork the project.
|
82
|
+
* Make your feature addition or bug fix.
|
83
|
+
* Add tests for it. This is important so I don't break it in a
|
84
|
+
future version unintentionally.
|
85
|
+
* Commit, do not mess with rakefile, version, or history.
|
86
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
87
|
+
* Send me a pull request. Bonus points for topic branches.
|
88
|
+
|
89
|
+
== Copyright
|
90
|
+
|
91
|
+
Copyright (c) 2009 jayronc. See LICENSE for details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1
|
1
|
+
0.2.1
|
@@ -0,0 +1,125 @@
|
|
1
|
+
require 'tmpdir'
|
2
|
+
require 'fileutils'
|
3
|
+
module Capistrano
|
4
|
+
|
5
|
+
class Logger
|
6
|
+
|
7
|
+
def ey_log(level, message, line_prefix = nil)
|
8
|
+
EYLogger.log(level, message, line_prefix) if EYLogger.setup?
|
9
|
+
log_without_ey_logging(level, message, line_prefix)
|
10
|
+
end
|
11
|
+
|
12
|
+
unless method_defined?(:log_without_ey_logging)
|
13
|
+
alias_method :log_without_ey_logging, :log
|
14
|
+
alias_method :log, :ey_log
|
15
|
+
end
|
16
|
+
|
17
|
+
def close
|
18
|
+
device.close if @needs_close
|
19
|
+
EYLogger.close if EYLogger.setup?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class EYLogger
|
24
|
+
|
25
|
+
# Sets up the EYLogger to beging capturing capistrano's logging. You should pass the capistrno configuration
|
26
|
+
# and the deploy type as a string. The deploy type is for reporting purposes only but must be included.
|
27
|
+
def self.setup(configuration, deploy_type, options = {})
|
28
|
+
@_configuration = configuration
|
29
|
+
@_deploy_type = deploy_type.gsub(/:/, "_")
|
30
|
+
@_log_path = options[:deploy_log_path] || Dir.tmpdir
|
31
|
+
@_log_path << "/" unless @_log_path =~ /\/$/
|
32
|
+
FileUtils.mkdir_p(@_log_path)
|
33
|
+
@_setup = true
|
34
|
+
@_success = true
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.log(level, message, line_prefix=nil)
|
38
|
+
return nil unless setup?
|
39
|
+
@release_name = @_configuration[:release_name] if @release_name.nil?
|
40
|
+
@_log_file_path = @_log_path + @release_name + ".log" unless @_log_file_path
|
41
|
+
@_deploy_log_file = File.open(@_log_file_path, "w") if @_deploy_log_file.nil?
|
42
|
+
|
43
|
+
indent = "%*s" % [Logger::MAX_LEVEL, "*" * (Logger::MAX_LEVEL - level)]
|
44
|
+
message.each do |line|
|
45
|
+
if line_prefix
|
46
|
+
@_deploy_log_file << "#{indent} [#{line_prefix}] #{line.strip}\n"
|
47
|
+
else
|
48
|
+
@_deploy_log_file << "#{indent} #{line.strip}\n"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.post_process
|
54
|
+
unless ::Interrupt === $!
|
55
|
+
puts "\n\nPlease wait while the log file is processed\n"
|
56
|
+
# Should dump the stack trace of an exception if there is one
|
57
|
+
error = $!
|
58
|
+
unless error.nil?
|
59
|
+
@_deploy_log_file << error.message << "\n"
|
60
|
+
@_deploy_log_file << error.backtrace.join("\n")
|
61
|
+
@_success = false
|
62
|
+
end
|
63
|
+
self.close
|
64
|
+
|
65
|
+
hooks = [:any]
|
66
|
+
hooks << self.successful? ? :success : :failure
|
67
|
+
puts "Executing Post Processing Hooks"
|
68
|
+
hooks.each do |h|
|
69
|
+
@_post_process_hooks[h].each do |key|
|
70
|
+
@_configuration.parent.find_and_execute_task(key)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
puts "Finished Post Processing Hooks"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Adds a post processing hook.
|
78
|
+
#
|
79
|
+
# Provide a task name to execute. These tasks are executed after capistrano has actually run its course.
|
80
|
+
#
|
81
|
+
# Takes a key to control when the hook is executed.'
|
82
|
+
# :any - always executed
|
83
|
+
# :success - only execute on success
|
84
|
+
# :failure - only execute on failure
|
85
|
+
#
|
86
|
+
# ==== Example
|
87
|
+
# Capistrano::EYLogger.post_process_hook( "ey_logger:upload_log_to_slice", :any)
|
88
|
+
#
|
89
|
+
def self.post_process_hook(task, key = :any)
|
90
|
+
@_post_process_hooks ||= Hash.new{|h,k| h[k] = []}
|
91
|
+
@_post_process_hooks[key] << task
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.setup?
|
95
|
+
!!@_setup
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.deploy_type
|
99
|
+
@_deploy_type
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.successful?
|
103
|
+
!!@_success
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.failure?
|
107
|
+
!@_success
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.log_file_path
|
111
|
+
@_log_file_path
|
112
|
+
end
|
113
|
+
|
114
|
+
def self.remote_log_file_name
|
115
|
+
@_log_file_name ||= "#{@_configuration[:release_name]}-#{@_deploy_type}-#{self.successful? ? "SUCCESS" : "FAILURE"}.log"
|
116
|
+
end
|
117
|
+
|
118
|
+
def self.close
|
119
|
+
@_deploy_log_file.flush unless @_deploy_log_file.nil?
|
120
|
+
@_deploy_log_file.close unless @_deploy_log_file.nil?
|
121
|
+
@_setup = false
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "ey_logger")
|
2
|
+
|
3
|
+
# These tasks are setup to use with the logger as post commit hooks.
|
4
|
+
Capistrano::Configuration.instance(:must_exist).load do
|
5
|
+
namespace :ey_logger do
|
6
|
+
task :upload_log_to_slice, :except => { :no_release => true} do
|
7
|
+
logger = Capistrano::EYLogger
|
8
|
+
run "mkdir -p #{shared_path}/deploy_logs"
|
9
|
+
put File.open(logger.log_file_path).read, "#{shared_path}/deploy_logs/#{logger.remote_log_file_name}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
Capistrano::EYLogger.post_process_hook("ey_logger:upload_log_to_slice")
|
@@ -0,0 +1,39 @@
|
|
1
|
+
Capistrano::Configuration.instance(:must_exist).load do
|
2
|
+
namespace :local do
|
3
|
+
desc <<-DESC
|
4
|
+
Rsyncs the your production content (identified by the :shared_content and
|
5
|
+
:content_directories properties) from a deployable environment (RAILS_ENV) to the local filesystem.
|
6
|
+
DESC
|
7
|
+
task :rsync_content do
|
8
|
+
from = ENV['FROM'] || 'production'
|
9
|
+
if exists?(:domain)
|
10
|
+
remote_domain = fetch(:domain)
|
11
|
+
end
|
12
|
+
if exists?(:rsync_domain)
|
13
|
+
remote_domain = fetch(:rsync_domain)
|
14
|
+
end
|
15
|
+
system("rsync -avz -e ssh '#{user}@#{remote_domain}:#{content_path}' '#{rails_root}/tmp/'")
|
16
|
+
end
|
17
|
+
|
18
|
+
desc <<-DESC
|
19
|
+
Creates a symlink to public/system from tmp/system
|
20
|
+
DESC
|
21
|
+
task :rsync_restore_content do
|
22
|
+
# from = ENV['FROM'] || 'production'
|
23
|
+
print "\033[1;45m Linking Assets to public directory \033[0m\n"
|
24
|
+
system "ln -nfs #{rails_root}/tmp/system #{rails_root}/public/system"
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
desc <<-DESC
|
29
|
+
Wrapper for local:rsync_content and local:rsync_restore_content
|
30
|
+
$> cap local:rsync RAILS_ENV=production
|
31
|
+
DESC
|
32
|
+
task :rsync do
|
33
|
+
transaction do
|
34
|
+
rsync_content
|
35
|
+
rsync_restore_content
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
Capistrano::Configuration.instance(:must_exist).load do
|
2
|
+
|
3
|
+
namespace :local do
|
4
|
+
desc <<-DESC
|
5
|
+
Downloads a tarball of shared content (identified by the :shared_content and
|
6
|
+
:content_directories properties) from a deployable environment (RAILS_ENV) to the local filesystem.
|
7
|
+
DESC
|
8
|
+
task :backup_content do
|
9
|
+
files = retrieve_local_files('production', 'content')
|
10
|
+
timestamp = most_recent_local_backup(from_env, 'content').to_i
|
11
|
+
last_modified = last_mod_time(content_backup_file).to_i
|
12
|
+
should_redownload = !(timestamp == last_modified)
|
13
|
+
if should_redownload
|
14
|
+
generate_remote_content_backup if last_modified < (Time.now.to_i - (remote_backup_expires))
|
15
|
+
system("mkdir -p #{tmp_dir}")
|
16
|
+
download(content_backup_file, "#{local_content_backup_dir(:env => from_env, :timestamp=>last_modified)}.tar.#{zip_ext}", :via=> :scp) do|ch, name, sent, total|
|
17
|
+
print "\r\033[1;42m #{File.basename(name)}: #{sent}/#{total} -- #{(sent.to_f * 100 / total.to_f).to_i}% \033[0m"
|
18
|
+
end
|
19
|
+
else
|
20
|
+
print "\r\033[1;42m Your Files are already up-to-date \033[0m\n"
|
21
|
+
@current_timestamp = files.first.to_i
|
22
|
+
end
|
23
|
+
util::tmp::check
|
24
|
+
end
|
25
|
+
|
26
|
+
desc <<-DESC
|
27
|
+
Regenerate files.
|
28
|
+
DESC
|
29
|
+
task :force_backup_content do
|
30
|
+
generate_remote_content_backup
|
31
|
+
end
|
32
|
+
|
33
|
+
desc <<-DESC
|
34
|
+
Restores the backed up content (env var FROM specifies which environment
|
35
|
+
was backed up, defaults to RAILS_ENV) to the local development environment app
|
36
|
+
DESC
|
37
|
+
task :restore_content do
|
38
|
+
timestamp = most_recent_local_backup(from_env, 'content')
|
39
|
+
local_dir = local_content_backup_dir(:env => from_env, :timestamp=>timestamp)
|
40
|
+
print "\033[1;45m Local Dir: #{local_dir} \033[0m\n"
|
41
|
+
system "mkdir -p #{local_dir}"
|
42
|
+
system "tar xzf #{local_dir}.tar.#{zip_ext} -C #{local_dir}"
|
43
|
+
print "\033[1;45m Removing old public/system directory \033[0m\n"
|
44
|
+
system "rm -rf public/system"
|
45
|
+
print "\033[1;45m Moving Assets to public directory \033[0m\n"
|
46
|
+
system "mv #{local_dir}/system public/system"
|
47
|
+
print "\033[1;41m Cleaning up \033[0m\n"
|
48
|
+
system "rm -rf #{local_dir}"
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
desc <<-DESC
|
53
|
+
Wrapper for local:backup_content and local:restore_content
|
54
|
+
$> cap local:sync_content RAILS_ENV=production RESTORE_ENV=development
|
55
|
+
DESC
|
56
|
+
task :sync_content do
|
57
|
+
transaction do
|
58
|
+
backup_content
|
59
|
+
restore_content
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
Capistrano::Configuration.instance(:must_exist).load do
|
2
|
+
|
3
|
+
namespace :local do
|
4
|
+
desc <<-DESC
|
5
|
+
Backs up deployable environment's database (identified by the
|
6
|
+
RAILS_ENV environment variable, which defaults to 'production') and copies it to the local machine
|
7
|
+
DESC
|
8
|
+
task :backup_db, :roles => :db do
|
9
|
+
files = retrieve_local_files(from_env, 'db')
|
10
|
+
timestamp = most_recent_local_backup(from_env, 'db').to_i
|
11
|
+
last_modified = last_mod_time(db_backup_zip_file).to_i
|
12
|
+
|
13
|
+
if last_modified < (Time.now.to_i - (remote_backup_expires))
|
14
|
+
generate_remote_db_backup
|
15
|
+
should_redownload = true
|
16
|
+
end
|
17
|
+
should_redownload = !(timestamp == last_modified)
|
18
|
+
if should_redownload
|
19
|
+
system "mkdir -p #{tmp_dir}"
|
20
|
+
download(db_backup_zip_file, "#{local_db_backup_file(:env=>from_env, :timestamp=>last_modified)}.#{zip_ext}", :via=> :scp) do|ch, name, sent, total|
|
21
|
+
print "\r\033[1;42m #{File.basename(name)}: #{sent}/#{total} -- #{(sent.to_f * 100 / total.to_f).to_i}% \033[0m"
|
22
|
+
end
|
23
|
+
else
|
24
|
+
print "\r\033[1;42m Your Files are already up-to-date \033[0m\n"
|
25
|
+
@current_timestamp = files.first.to_i
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
desc <<-DESC
|
30
|
+
Regenerate files.
|
31
|
+
DESC
|
32
|
+
task :force_backup_db do
|
33
|
+
generate_remote_db_backup
|
34
|
+
end
|
35
|
+
|
36
|
+
desc <<-DESC
|
37
|
+
Untars the backup file downloaded from local:backup_db (specified via the FROM env
|
38
|
+
variable, which defalts to RAILS_ENV), and imports (via mysql command line tool) it back into the database
|
39
|
+
defined in the RESTORE_ENV env variable (defaults to development).
|
40
|
+
DESC
|
41
|
+
task :restore_db, :roles => :db do
|
42
|
+
mysql_str = "mysql #{mysql_connection_for(to_env)}"
|
43
|
+
mysql_dump = "mysqldump #{mysql_connection_for(from_env)}"
|
44
|
+
local_db_create = "mysql #{mysql_connection_for(to_env)} -e \"create database if not exists #{mysql_db_for(to_env)}\""
|
45
|
+
remote_backup_file = local_db_backup_file(:env => from_env, :timestamp=>most_recent_local_backup(from_env, 'db')).strip
|
46
|
+
|
47
|
+
puts "\n\033[1;42m Restoring database backup to #{to_env} environment FROM #{remote_backup_file}--#{from_env} using #{mysql_str}\033[0m"
|
48
|
+
system(local_db_create.strip)
|
49
|
+
cmd = ""
|
50
|
+
cmd << <<-CMD
|
51
|
+
#{unzip} -c #{remote_backup_file}.#{zip_ext} > #{remote_backup_file} &&
|
52
|
+
#{mysql_str} < #{remote_backup_file} &&
|
53
|
+
rm -f #{remote_backup_file}
|
54
|
+
CMD
|
55
|
+
system(cmd.strip)
|
56
|
+
util::tmp::check
|
57
|
+
end
|
58
|
+
|
59
|
+
desc <<-DESC
|
60
|
+
Wrapper for local:backup_db and local:restore_db.
|
61
|
+
$> cap local:sync_db RAILS_ENV=production RESTORE_ENV=development
|
62
|
+
DESC
|
63
|
+
task :sync_db do
|
64
|
+
transaction do
|
65
|
+
backup_db
|
66
|
+
restore_db
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
desc <<-DESC
|
71
|
+
Ensure that a fresh remote data dump is retrieved before syncing to the local environment.
|
72
|
+
DESC
|
73
|
+
task :resync_db do
|
74
|
+
util::tmp::clean_remote
|
75
|
+
sync_db
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
# set :rails_root, fetch(:blavosync_local_root, Pathname.new('.').realpath )
|
2
|
+
# set :content_dir, fetch(:blavosync_content_directories, "system" )
|
3
|
+
# set :content_path, fetch(:blavosync_content_path, File.join(fetch(:shared_path), content_dir) )
|
4
|
+
# set :public_path, fetch(:blavosync_public_path, File.join(fetch(:latest_release), 'public') )
|
5
|
+
# set :remote_backup_expires, fetch(:blavosync_remote_backup_expires, 100000 )
|
6
|
+
# set :zip, fetch(:blavosync_zip_command, "gzip" )
|
7
|
+
# set :unzip, fetch(:blavosync_unzip_command, "gunzip" )
|
8
|
+
# set :zip_ext, fetch(:blavosync_compressed_extension, "gz" )
|
9
|
+
# set :tmp_dir, fetch(:blavosync_tmp_dir, "tmp" )
|
10
|
+
# set :content_sync_method, fetch(:blavosync_content_sync_method, 'rsync' )
|
11
|
+
# set :from_env, fetch(:blavosync_from_env, (ENV['FROM_ENV'].nil? ? 'production' : ENV['RAILS_ENV']) )
|
12
|
+
# set :to_env, fetch(:blavosync_to_env, (ENV['TO_ENV'].nil? ? 'development' : ENV['TO_ENV']) )
|
13
|
+
# set :rsync_content_backup_file, fetch(:blavosync_rsync_content_backup_file, "#{shared_path}/system" )
|
14
|
+
# set :tar_content_backup_file, fetch(:blavosync_tar_content_backup_file, "#{shared_path}/backup_#{from_env}_content.tar.#{zip_ext}" )
|
15
|
+
# set :db_backup_file, fetch(:blavosync_db_backup_file, "#{shared_path}/backup_#{from_env}_db.sql" )
|
16
|
+
# set :db_backup_zip_file, fetch(:blavosync_db_backup_zip_file, "#{db_backup_file}.#{zip_ext}" )
|
17
|
+
#
|
18
|
+
Capistrano::Configuration.instance(:must_exist).load do |configuration|
|
19
|
+
|
20
|
+
def rails_root
|
21
|
+
Pathname.new('.').realpath
|
22
|
+
end
|
23
|
+
def content_dir
|
24
|
+
"system"
|
25
|
+
end
|
26
|
+
def content_path
|
27
|
+
File.join(fetch(:shared_path), content_dir)
|
28
|
+
end
|
29
|
+
def public_path
|
30
|
+
File.join(fetch(:latest_release), 'public')
|
31
|
+
end
|
32
|
+
def remote_backup_expires
|
33
|
+
100000
|
34
|
+
end
|
35
|
+
def zip
|
36
|
+
"gzip"
|
37
|
+
end
|
38
|
+
def unzip
|
39
|
+
"gunzip"
|
40
|
+
end
|
41
|
+
def zip_ext
|
42
|
+
"gz"
|
43
|
+
end
|
44
|
+
def tmp_dir
|
45
|
+
"tmp"
|
46
|
+
end
|
47
|
+
def content_sync_method
|
48
|
+
'rsync'
|
49
|
+
end
|
50
|
+
def from_env
|
51
|
+
(ENV['FROM_ENV'].nil? ? 'production' : ENV['RAILS_ENV'])
|
52
|
+
end
|
53
|
+
def to_env
|
54
|
+
(ENV['TO_ENV'].nil? ? 'development' : ENV['TO_ENV'])
|
55
|
+
end
|
56
|
+
def rsync_content_backup_file
|
57
|
+
"#{shared_path}/system"
|
58
|
+
end
|
59
|
+
def tar_content_backup_file
|
60
|
+
"#{shared_path}/backup_#{from_env}_content.tar.#{zip_ext}"
|
61
|
+
end
|
62
|
+
def db_backup_file
|
63
|
+
"#{shared_path}/backup_#{from_env}_db.sql"
|
64
|
+
end
|
65
|
+
def db_backup_zip_file
|
66
|
+
"#{db_backup_file}.#{zip_ext}"
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
def local_content_backup_dir(args={})
|
73
|
+
timestamp = args[:timestamp] || current_timestamp
|
74
|
+
"#{tmp_dir}/#{fetch(:application)}-#{from_env}-content-#{timestamp.to_s.strip}"
|
75
|
+
end
|
76
|
+
|
77
|
+
def generate_remote_tar_content_backup
|
78
|
+
run "cd #{fetch(:shared_path)} && tar czf #{rsync_content_backup_file} 'system'"
|
79
|
+
end
|
80
|
+
|
81
|
+
def local_db_conf(env = nil)
|
82
|
+
env ||= fetch(:to_env)
|
83
|
+
fetch(:config_structure, :rails).to_sym == :sls ?
|
84
|
+
File.join('config', env.to_s, 'database.yml') :
|
85
|
+
File.join('config', 'database.yml')
|
86
|
+
end
|
87
|
+
|
88
|
+
def pluck_pass_str(db_config)
|
89
|
+
db_config['password'].nil? ? '' : "-p'#{db_config['password']}'"
|
90
|
+
end
|
91
|
+
|
92
|
+
def current_timestamp
|
93
|
+
@current_timestamp ||= Time.now.to_i.to_s.strip
|
94
|
+
end
|
95
|
+
|
96
|
+
def retrieve_local_files(env, type)
|
97
|
+
`ls -r #{tmp_dir} | awk -F"-" '{ if ($2 ~ /#{env}/ && $3 ~ /#{type}/) { print $4; } }'`.split(' ')
|
98
|
+
end
|
99
|
+
|
100
|
+
def most_recent_local_backup(env, type)
|
101
|
+
retrieve_local_files(env, type).first.to_i
|
102
|
+
end
|
103
|
+
|
104
|
+
def last_mod_time(path)
|
105
|
+
capture("stat -c%Y #{path}")
|
106
|
+
end
|
107
|
+
|
108
|
+
def server_cache_valid?(path)
|
109
|
+
capture("[ -f #{path} ] || echo '1'").empty? && ((Time.now.to_i - last_mod_time(path)) <= remote_backup_expires)
|
110
|
+
end
|
111
|
+
|
112
|
+
def generate_remote_db_backup
|
113
|
+
run "mysqldump #{mysql_connection_for(from_env)} > #{db_backup_file}"
|
114
|
+
run "rm -f #{db_backup_zip_file} && #{zip} #{db_backup_file} && rm -f #{db_backup_file}"
|
115
|
+
end
|
116
|
+
|
117
|
+
def local_db_backup_file(args = {})
|
118
|
+
env = args[:env] || 'production'
|
119
|
+
timestamp = args[:timestamp] || current_timestamp
|
120
|
+
"#{tmp_dir}/#{fetch(:application)}-#{env}-db-#{timestamp.to_s.strip}.sql"
|
121
|
+
end
|
122
|
+
|
123
|
+
def mysql_connection_for(environment)
|
124
|
+
db_settings = YAML.load_file(local_db_conf(environment))[environment]
|
125
|
+
pass = pluck_pass_str(db_settings)
|
126
|
+
host = (db_settings['host'].nil?) ? nil : "--host=#{db_settings['host']}"
|
127
|
+
socket = (db_settings['socket'].nil?) ? nil : "--socket=#{db_settings['socket']}"
|
128
|
+
user = (db_settings['username'].nil?) ? nil : "-u #{db_settings['username']}"
|
129
|
+
database = (db_settings['database'].nil?) ? nil : " #{db_settings['database']}"
|
130
|
+
[user, pass, host, socket, database ].join(" ")
|
131
|
+
end
|
132
|
+
|
133
|
+
def mysql_db_for(environment)
|
134
|
+
restore_from = ENV['FROM'] || 'production'
|
135
|
+
@from_db ||= YAML.load_file(local_db_conf(restore_from))[restore_from]
|
136
|
+
@from_database ||= (@from_db['database'].nil?) ? nil : " #{@from_db['database']}"
|
137
|
+
end
|
138
|
+
|
139
|
+
namespace :local do
|
140
|
+
|
141
|
+
desc <<-DESC
|
142
|
+
Wrapper for local:sync_db and local:sync_content
|
143
|
+
$> cap local:sync RAILS_ENV=production RESTORE_ENV=development
|
144
|
+
DESC
|
145
|
+
task :sync, :roles =>:app do
|
146
|
+
sync_db
|
147
|
+
if content_sync_method == 'tar'
|
148
|
+
sync_content
|
149
|
+
else
|
150
|
+
rsync_content
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
desc <<-DESC
|
155
|
+
Wrapper for local:force_backup_db, local:force_backup_content, and the local:sync to get
|
156
|
+
a completely fresh set of data from the server
|
157
|
+
$> cap local:sync RAILS_ENV=production RESTORE_ENV=development
|
158
|
+
DESC
|
159
|
+
task :sync_init, :roles =>:app do
|
160
|
+
force_backup_db
|
161
|
+
force_backup_content
|
162
|
+
sync
|
163
|
+
end
|
164
|
+
|
165
|
+
end
|
166
|
+
|
167
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
Capistrano::Configuration.instance(:must_exist).load do
|
2
|
+
|
3
|
+
namespace :util do
|
4
|
+
|
5
|
+
namespace :tmp do
|
6
|
+
desc "[capistrano-extensions]: Displays warning if :tmp_dir has more than 10 files or is greater than 50MB"
|
7
|
+
task :check do
|
8
|
+
#[ 5 -le "`ls -1 tmp/cap | wc -l`" ] && echo "Display Me"
|
9
|
+
cmd = %Q{ [ 10 -le "`ls -1 #{tmp_dir} | wc -l`" ] || [ 50 -le "`du -sh #{tmp_dir} | awk '{print int($1)}'`" ] && printf "\033[1;41m Clean up #{tmp_dir} directory \033[0m\n" && du -sh #{tmp_dir}/* }
|
10
|
+
system(cmd)
|
11
|
+
end
|
12
|
+
|
13
|
+
desc "[capistrano-extensions]: Remove the current remote env's backups from :tmp_dir"
|
14
|
+
task :clean_remote do
|
15
|
+
system("rm -rf #{rails_root}/#{tmp_dir}/#{configuration.fetch(:application)}-*")
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'blavosync/lib/ey_logger'
|
2
|
+
require 'blavosync/lib/ey_logger_hooks'
|
3
|
+
require 'blavosync/recipes/util'
|
4
|
+
require 'blavosync/recipes/local'
|
5
|
+
require 'blavosync/recipes/database'
|
6
|
+
require 'blavosync/recipes/content_tar'
|
7
|
+
require 'blavosync/recipes/content_rsync'
|
8
|
+
|
9
|
+
Capistrano::Configuration.instance(:must_exist).load do
|
10
|
+
|
11
|
+
default_run_options[:pty] = true if respond_to?(:default_run_options)
|
12
|
+
set :keep_releases, 3
|
13
|
+
set :runner, defer { user }
|
14
|
+
|
15
|
+
end
|
data/lib/blavosync.rb
CHANGED
@@ -1,295 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
set :rails_root, fetch(:blavosync_local_root, Pathname.new('.').realpath )
|
4
|
-
set :content_dir, fetch(:blavosync_content_directories, content_directories ||= "system" )
|
5
|
-
set :content_path, fetch(:blavosync_content_path, File.join(shared_path, content_dir) )
|
6
|
-
set :public_path, fetch(:blavosync_public_path, File.join(latest_release, 'public') )
|
7
|
-
set :remote_backup_expires, fetch(:blavosync_remote_backup_expires, 100000 )
|
8
|
-
set :zip, fetch(:blavosync_zip_command, "gzip" )
|
9
|
-
set :unzip, fetch(:blavosync_unzip_command, "gunzip" )
|
10
|
-
set :zip_ext, fetch(:blavosync_compressed_extension, "gz" )
|
11
|
-
set :tmp_dir, fetch(:blavosync_tmp_dir, "tmp" )
|
12
|
-
set :content_sync_method, fetch(:blavosync_content_sync_method, 'rsync' )
|
13
|
-
set :from_env, fetch(:blavosync_from_env, (ENV['FROM_ENV'].nil? ? 'production' : ENV['RAILS_ENV']) )
|
14
|
-
set :to_env, fetch(:blavosync_to_env, (ENV['TO_ENV'].nil? ? 'development' : ENV['TO_ENV']) )
|
15
|
-
set :rsync_content_backup_file, fetch(:blavosync_rsync_content_backup_file, "#{shared_path}/system" )
|
16
|
-
set :tar_content_backup_file, fetch(:blavosync_tar_content_backup_file, "#{shared_path}/backup_#{from_env}_content.tar.#{zip_ext}" )
|
17
|
-
set :db_backup_file, fetch(:blavosync_db_backup_file, "#{shared_path}/backup_#{from_env}_db.sql" )
|
18
|
-
set :db_backup_zip_file, fetch(:blavosync_db_backup_zip_file, "#{db_backup_file}.#{zip_ext}" )
|
19
|
-
|
20
|
-
def local_content_backup_dir(args={})
|
21
|
-
timestamp = args[:timestamp] || current_timestamp
|
22
|
-
"#{tmp_dir}/#{application}-#{from_env}-content-#{timestamp.to_s.strip}"
|
23
|
-
end
|
24
|
-
|
25
|
-
def generate_remote_tar_content_backup
|
26
|
-
run "cd #{shared_path} && tar czf #{rsync_content_backup_file} 'system'"
|
27
|
-
end
|
28
|
-
|
29
|
-
def local_db_conf(env = nil)
|
30
|
-
env ||= fetch(:to_env)
|
31
|
-
fetch(:config_structure, :rails).to_sym == :sls ?
|
32
|
-
File.join('config', env.to_s, 'database.yml') :
|
33
|
-
File.join('config', 'database.yml')
|
34
|
-
end
|
35
|
-
|
36
|
-
def pluck_pass_str(db_config)
|
37
|
-
db_config['password'].nil? ? '' : "-p'#{db_config['password']}'"
|
38
|
-
end
|
39
|
-
|
40
|
-
def current_timestamp
|
41
|
-
@current_timestamp ||= Time.now.to_i.to_s.strip
|
42
|
-
end
|
43
|
-
|
44
|
-
def retrieve_local_files(env, type)
|
45
|
-
`ls -r #{tmp_dir} | awk -F"-" '{ if ($2 ~ /#{env}/ && $3 ~ /#{type}/) { print $4; } }'`.split(' ')
|
46
|
-
end
|
47
|
-
|
48
|
-
def most_recent_local_backup(env, type)
|
49
|
-
retrieve_local_files(env, type).first.to_i
|
50
|
-
end
|
51
|
-
|
52
|
-
def last_mod_time(path)
|
53
|
-
capture("stat -c%Y #{path}")
|
54
|
-
end
|
55
|
-
|
56
|
-
def server_cache_valid?(path)
|
57
|
-
capture("[ -f #{path} ] || echo '1'").empty? && ((Time.now.to_i - last_mod_time(path)) <= remote_backup_expires)
|
58
|
-
end
|
59
|
-
|
60
|
-
def generate_remote_db_backup
|
61
|
-
run "mysqldump #{mysql_connection_for(from_env)} > #{db_backup_file}"
|
62
|
-
run "rm -f #{db_backup_zip_file} && #{zip} #{db_backup_file} && rm -f #{db_backup_file}"
|
63
|
-
end
|
64
|
-
|
65
|
-
def local_db_backup_file(args = {})
|
66
|
-
env = args[:env] || 'production'
|
67
|
-
timestamp = args[:timestamp] || current_timestamp
|
68
|
-
"#{tmp_dir}/#{application}-#{env}-db-#{timestamp.to_s.strip}.sql"
|
69
|
-
end
|
70
|
-
|
71
|
-
def mysql_connection_for(environment)
|
72
|
-
db_settings = YAML.load_file(local_db_conf(environment))[environment]
|
73
|
-
pass = pluck_pass_str(db_settings)
|
74
|
-
host = (db_settings['host'].nil?) ? nil : "--host=#{db_settings['host']}"
|
75
|
-
socket = (db_settings['socket'].nil?) ? nil : "--socket=#{db_settings['socket']}"
|
76
|
-
user = (db_settings['username'].nil?) ? nil : "-u #{db_settings['username']}"
|
77
|
-
database = (db_settings['database'].nil?) ? nil : " #{db_settings['database']}"
|
78
|
-
[user, pass, host, socket, database ].join(" ")
|
79
|
-
end
|
80
|
-
|
81
|
-
def mysql_db_for(environment)
|
82
|
-
restore_from = ENV['FROM'] || 'production'
|
83
|
-
@from_db ||= YAML.load_file(local_db_conf(restore_from))[restore_from]
|
84
|
-
@from_database ||= (@from_db['database'].nil?) ? nil : " #{@from_db['database']}"
|
85
|
-
end
|
86
|
-
|
87
|
-
namespace :util do
|
88
|
-
|
89
|
-
namespace :tmp do
|
90
|
-
desc "[capistrano-extensions]: Displays warning if :tmp_dir has more than 10 files or is greater than 50MB"
|
91
|
-
task :check do
|
92
|
-
#[ 5 -le "`ls -1 tmp/cap | wc -l`" ] && echo "Display Me"
|
93
|
-
cmd = %Q{ [ 10 -le "`ls -1 #{tmp_dir} | wc -l`" ] || [ 50 -le "`du -sh #{tmp_dir} | awk '{print int($1)}'`" ] && printf "\033[1;41m Clean up #{tmp_dir} directory \033[0m\n" && du -sh #{tmp_dir}/* }
|
94
|
-
system(cmd)
|
95
|
-
end
|
96
|
-
|
97
|
-
desc "[capistrano-extensions]: Remove the current remote env's backups from :tmp_dir"
|
98
|
-
task :clean_remote do
|
99
|
-
system("rm -rf #{rails_root}/#{tmp_dir}/#{fetch(:application)}-*")
|
100
|
-
end
|
101
|
-
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
namespace :local do
|
106
|
-
|
107
|
-
desc <<-DESC
|
108
|
-
Wrapper for local:sync_db and local:sync_content
|
109
|
-
$> cap local:sync RAILS_ENV=production RESTORE_ENV=development
|
110
|
-
DESC
|
111
|
-
task :sync do
|
112
|
-
sync_db
|
113
|
-
if content_sync_method == 'tar'
|
114
|
-
sync_content
|
115
|
-
else
|
116
|
-
rsync_content
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
desc <<-DESC
|
121
|
-
Wrapper for local:force_backup_db, local:force_backup_content, and the local:sync to get
|
122
|
-
a completely fresh set of data from the server
|
123
|
-
$> cap local:sync RAILS_ENV=production RESTORE_ENV=development
|
124
|
-
DESC
|
125
|
-
task :sync_init do
|
126
|
-
force_backup_db
|
127
|
-
force_backup_content
|
128
|
-
sync
|
129
|
-
end
|
130
|
-
|
131
|
-
desc <<-DESC
|
132
|
-
Backs up deployable environment's database (identified by the
|
133
|
-
RAILS_ENV environment variable, which defaults to 'production') and copies it to the local machine
|
134
|
-
DESC
|
135
|
-
task :backup_db, :roles => :db do
|
136
|
-
files = retrieve_local_files(from_env, 'db')
|
137
|
-
timestamp = most_recent_local_backup(from_env, 'db').to_i
|
138
|
-
last_modified = last_mod_time(db_backup_zip_file).to_i
|
139
|
-
|
140
|
-
if last_modified < (Time.now.to_i - (remote_backup_expires))
|
141
|
-
generate_remote_db_backup
|
142
|
-
should_redownload = true
|
143
|
-
end
|
144
|
-
should_redownload = !(timestamp == last_modified)
|
145
|
-
if should_redownload
|
146
|
-
system "mkdir -p #{tmp_dir}"
|
147
|
-
download(db_backup_zip_file, "#{local_db_backup_file(:env=>from_env, :timestamp=>last_modified)}.#{zip_ext}", :via=> :scp) do|ch, name, sent, total|
|
148
|
-
print "\r\033[1;42m #{File.basename(name)}: #{sent}/#{total} -- #{(sent.to_f * 100 / total.to_f).to_i}% \033[0m"
|
149
|
-
end
|
150
|
-
else
|
151
|
-
print "\r\033[1;42m Your Files are already up-to-date \033[0m\n"
|
152
|
-
@current_timestamp = files.first.to_i
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
desc <<-DESC
|
157
|
-
Regenerate files.
|
158
|
-
DESC
|
159
|
-
task :force_backup_db do
|
160
|
-
generate_remote_db_backup
|
161
|
-
end
|
162
|
-
|
163
|
-
desc <<-DESC
|
164
|
-
Untars the backup file downloaded from local:backup_db (specified via the FROM env
|
165
|
-
variable, which defalts to RAILS_ENV), and imports (via mysql command line tool) it back into the database
|
166
|
-
defined in the RESTORE_ENV env variable (defaults to development).
|
167
|
-
DESC
|
168
|
-
task :restore_db, :roles => :db do
|
169
|
-
mysql_str = "mysql #{mysql_connection_for(to_env)}"
|
170
|
-
mysql_dump = "mysqldump #{mysql_connection_for(from_env)}"
|
171
|
-
local_db_create = "mysql #{mysql_connection_for(to_env)} -e \"create database if not exists #{mysql_db_for(to_env)}\""
|
172
|
-
remote_backup_file = local_db_backup_file(:env => from_env, :timestamp=>most_recent_local_backup(from_env, 'db')).strip
|
173
|
-
|
174
|
-
puts "\n\033[1;42m Restoring database backup to #{to_env} environment FROM #{remote_backup_file}--#{from_env} using #{mysql_str}\033[0m"
|
175
|
-
system(local_db_create.strip)
|
176
|
-
cmd = ""
|
177
|
-
cmd << <<-CMD
|
178
|
-
#{unzip} -c #{remote_backup_file}.#{zip_ext} > #{remote_backup_file} &&
|
179
|
-
#{mysql_str} < #{remote_backup_file} &&
|
180
|
-
rm -f #{remote_backup_file}
|
181
|
-
CMD
|
182
|
-
system(cmd.strip)
|
183
|
-
util::tmp::check
|
184
|
-
end
|
185
|
-
|
186
|
-
desc <<-DESC
|
187
|
-
Wrapper for local:backup_db and local:restore_db.
|
188
|
-
$> cap local:sync_db RAILS_ENV=production RESTORE_ENV=development
|
189
|
-
DESC
|
190
|
-
task :sync_db do
|
191
|
-
transaction do
|
192
|
-
backup_db
|
193
|
-
restore_db
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
desc <<-DESC
|
198
|
-
Ensure that a fresh remote data dump is retrieved before syncing to the local environment.
|
199
|
-
DESC
|
200
|
-
task :resync_db do
|
201
|
-
util::tmp::clean_remote
|
202
|
-
sync_db
|
203
|
-
end
|
204
|
-
|
205
|
-
desc <<-DESC
|
206
|
-
Rsyncs the your production content (identified by the :shared_content and
|
207
|
-
:content_directories properties) from a deployable environment (RAILS_ENV) to the local filesystem.
|
208
|
-
DESC
|
209
|
-
task :rsync_content do
|
210
|
-
from = ENV['FROM'] || 'production'
|
211
|
-
system("rsync -avz -e ssh '#{user}@#{domain}:#{content_path}' '#{rails_root}/tmp/'")
|
212
|
-
end
|
213
|
-
|
214
|
-
desc <<-DESC
|
215
|
-
Creates a symlink to public/system from tmp/system
|
216
|
-
DESC
|
217
|
-
task :rsync_restore_content do
|
218
|
-
# from = ENV['FROM'] || 'production'
|
219
|
-
print "\033[1;45m Linking Assets to public directory \033[0m\n"
|
220
|
-
system "ln -nfs #{rails_root}/tmp/system #{rails_root}/public/system"
|
221
|
-
end
|
222
|
-
|
223
|
-
|
224
|
-
desc <<-DESC
|
225
|
-
Wrapper for local:rsync_content and local:rsync_restore_content
|
226
|
-
$> cap local:rsync RAILS_ENV=production
|
227
|
-
DESC
|
228
|
-
task :rsync do
|
229
|
-
transaction do
|
230
|
-
rsync_content
|
231
|
-
rsync_restore_content
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
desc <<-DESC
|
236
|
-
Downloads a tarball of shared content (identified by the :shared_content and
|
237
|
-
:content_directories properties) from a deployable environment (RAILS_ENV) to the local filesystem.
|
238
|
-
DESC
|
239
|
-
task :backup_content do
|
240
|
-
files = retrieve_local_files('production', 'content')
|
241
|
-
timestamp = most_recent_local_backup(from_env, 'content').to_i
|
242
|
-
last_modified = last_mod_time(content_backup_file).to_i
|
243
|
-
should_redownload = !(timestamp == last_modified)
|
244
|
-
if should_redownload
|
245
|
-
generate_remote_content_backup if last_modified < (Time.now.to_i - (remote_backup_expires))
|
246
|
-
system("mkdir -p #{tmp_dir}")
|
247
|
-
download(content_backup_file, "#{local_content_backup_dir(:env => from_env, :timestamp=>last_modified)}.tar.#{zip_ext}", :via=> :scp) do|ch, name, sent, total|
|
248
|
-
print "\r\033[1;42m #{File.basename(name)}: #{sent}/#{total} -- #{(sent.to_f * 100 / total.to_f).to_i}% \033[0m"
|
249
|
-
end
|
250
|
-
else
|
251
|
-
print "\r\033[1;42m Your Files are already up-to-date \033[0m\n"
|
252
|
-
@current_timestamp = files.first.to_i
|
253
|
-
end
|
254
|
-
util::tmp::check
|
255
|
-
end
|
256
|
-
|
257
|
-
desc <<-DESC
|
258
|
-
Regenerate files.
|
259
|
-
DESC
|
260
|
-
task :force_backup_content do
|
261
|
-
generate_remote_content_backup
|
262
|
-
end
|
263
|
-
|
264
|
-
desc <<-DESC
|
265
|
-
Restores the backed up content (env var FROM specifies which environment
|
266
|
-
was backed up, defaults to RAILS_ENV) to the local development environment app
|
267
|
-
DESC
|
268
|
-
task :restore_content do
|
269
|
-
timestamp = most_recent_local_backup(from_env, 'content')
|
270
|
-
local_dir = local_content_backup_dir(:env => from_env, :timestamp=>timestamp)
|
271
|
-
print "\033[1;45m Local Dir: #{local_dir} \033[0m\n"
|
272
|
-
system "mkdir -p #{local_dir}"
|
273
|
-
system "tar xzf #{local_dir}.tar.#{zip_ext} -C #{local_dir}"
|
274
|
-
print "\033[1;45m Removing old public/system directory \033[0m\n"
|
275
|
-
system "rm -rf public/system"
|
276
|
-
print "\033[1;45m Moving Assets to public directory \033[0m\n"
|
277
|
-
system "mv #{local_dir}/system public/system"
|
278
|
-
print "\033[1;41m Cleaning up \033[0m\n"
|
279
|
-
system "rm -rf #{local_dir}"
|
280
|
-
end
|
281
|
-
|
282
|
-
|
283
|
-
desc <<-DESC
|
284
|
-
Wrapper for local:backup_content and local:restore_content
|
285
|
-
$> cap local:sync_content RAILS_ENV=production RESTORE_ENV=development
|
286
|
-
DESC
|
287
|
-
task :sync_content do
|
288
|
-
transaction do
|
289
|
-
backup_content
|
290
|
-
restore_content
|
291
|
-
end
|
292
|
-
end
|
293
|
-
end
|
294
|
-
|
1
|
+
module Blavosync
|
2
|
+
VERSION = '0.1.3'
|
295
3
|
end
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: blavosync
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jayronc
|
@@ -21,15 +21,26 @@ extensions: []
|
|
21
21
|
|
22
22
|
extra_rdoc_files:
|
23
23
|
- LICENSE
|
24
|
-
- README.
|
24
|
+
- README.markdown
|
25
25
|
files:
|
26
26
|
- Blavosync.gemspec
|
27
|
+
- History.txt
|
27
28
|
- LICENSE
|
28
|
-
-
|
29
|
+
- Manifest.txt
|
30
|
+
- README.markdown
|
29
31
|
- Rakefile
|
30
32
|
- VERSION
|
31
|
-
- init.rb
|
32
33
|
- lib/blavosync.rb
|
34
|
+
- lib/blavosync/lib/ey_logger.rb
|
35
|
+
- lib/blavosync/lib/ey_logger_hooks.rb
|
36
|
+
- lib/blavosync/recipes.rb
|
37
|
+
- lib/blavosync/recipes/content_rsync.rb
|
38
|
+
- lib/blavosync/recipes/content_tar.rb
|
39
|
+
- lib/blavosync/recipes/database.rb
|
40
|
+
- lib/blavosync/recipes/local.rb
|
41
|
+
- lib/blavosync/recipes/util.rb
|
42
|
+
- test/test_blavosync.rb
|
43
|
+
- test/test_helper.rb
|
33
44
|
has_rdoc: true
|
34
45
|
homepage: http://github.com/indierockmedia/Blavosync
|
35
46
|
licenses: []
|
@@ -58,5 +69,6 @@ rubygems_version: 1.3.5
|
|
58
69
|
signing_key:
|
59
70
|
specification_version: 3
|
60
71
|
summary: Sync a remote db and rsync content to your development environment.
|
61
|
-
test_files:
|
62
|
-
|
72
|
+
test_files:
|
73
|
+
- test/test_blavosync.rb
|
74
|
+
- test/test_helper.rb
|
data/README.rdoc
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
== blavosync
|
2
|
-
|
3
|
-
INSTALLATION:
|
4
|
-
Add this line BELOW load 'config/deploy' in your Capfile
|
5
|
-
|
6
|
-
require 'blavosync'
|
7
|
-
|
8
|
-
|
9
|
-
USAGE:
|
10
|
-
adds the following tasks to projects using capistrano
|
11
|
-
|
12
|
-
------------------------------------------------------------
|
13
|
-
cap local:backup_content
|
14
|
-
------------------------------------------------------------
|
15
|
-
Downloads a tarball of shared content (identified by the :shared_content and
|
16
|
-
:content_directories properties) from a deployable environment (RAILS_ENV) to
|
17
|
-
the local filesystem.
|
18
|
-
|
19
|
-
------------------------------------------------------------
|
20
|
-
cap local:backup_db
|
21
|
-
------------------------------------------------------------
|
22
|
-
Backs up deployable environment's database (idgit loentified by the
|
23
|
-
RAILS_ENV environment variable, which defaults to 'production') and copies it to
|
24
|
-
the local machine
|
25
|
-
|
26
|
-
------------------------------------------------------------
|
27
|
-
cap local:force_backup_content
|
28
|
-
------------------------------------------------------------
|
29
|
-
Regenerate files.
|
30
|
-
|
31
|
-
------------------------------------------------------------
|
32
|
-
cap local:force_backup_db
|
33
|
-
------------------------------------------------------------
|
34
|
-
Regenerate files.
|
35
|
-
|
36
|
-
------------------------------------------------------------
|
37
|
-
cap local:restore_content
|
38
|
-
------------------------------------------------------------
|
39
|
-
Restores the backed up content (env var FROM specifies which environment
|
40
|
-
was backed up, defaults to RAILS_ENV) to the local development environment app
|
41
|
-
|
42
|
-
------------------------------------------------------------
|
43
|
-
cap local:restore_db
|
44
|
-
------------------------------------------------------------
|
45
|
-
Untars the backup file downloaded from local:backup_db (specified via the FROM
|
46
|
-
env
|
47
|
-
variable, which defalts to RAILS_ENV), and imports (via mysql command line tool)
|
48
|
-
it back into the database
|
49
|
-
defined in the RESTORE_ENV env variable (defaults to development).
|
50
|
-
|
51
|
-
------------------------------------------------------------
|
52
|
-
cap local:resync_db
|
53
|
-
------------------------------------------------------------
|
54
|
-
Ensure that a fresh remote data dump is retrieved before syncing to the local
|
55
|
-
environment
|
56
|
-
|
57
|
-
------------------------------------------------------------
|
58
|
-
cap local:sync
|
59
|
-
------------------------------------------------------------
|
60
|
-
Wrapper for local:sync_db and local:sync_content
|
61
|
-
$> cap local:sync RAILS_ENV=production RESTORE_ENV=development
|
62
|
-
|
63
|
-
------------------------------------------------------------
|
64
|
-
cap local:sync_content
|
65
|
-
------------------------------------------------------------
|
66
|
-
Wrapper for local:backup_content and local:restore_content
|
67
|
-
$> cap local:sync_content RAILS_ENV=production RESTORE_ENV=development
|
68
|
-
|
69
|
-
------------------------------------------------------------
|
70
|
-
cap local:sync_db
|
71
|
-
------------------------------------------------------------
|
72
|
-
Wrapper for local:backup_db and local:restore_db.
|
73
|
-
$> cap local:sync_db RAILS_ENV=production RESTORE_ENV=development
|
74
|
-
|
75
|
-
------------------------------------------------------------
|
76
|
-
cap local:sync_init
|
77
|
-
------------------------------------------------------------
|
78
|
-
Wrapper for local:force_backup_db, local:force_backup_content, and the
|
79
|
-
local:sync to get
|
80
|
-
a completely fresh set of data from the server
|
81
|
-
$> cap local:sync RAILS_ENV=production RESTORE_ENV=development
|
82
|
-
|
83
|
-
CONFIGURATION:
|
84
|
-
|
85
|
-
the following variables (with defaults shown) are available in your deploy.rb
|
86
|
-
|
87
|
-
|
88
|
-
set :blavosync_local_root, Pathname.new('.').realpath
|
89
|
-
set :blavosync_content_directories, content_directories ||= "system"
|
90
|
-
set :blavosync_content_path, File.join(shared_path, content_dir)
|
91
|
-
set :blavosync_public_path, File.join(latest_release, 'public')
|
92
|
-
set :blavosync_remote_backup_expires, 100000
|
93
|
-
set :blavosync_zip_command, "gzip"
|
94
|
-
set :blavosync_unzip_command, "gunzip"
|
95
|
-
set :blavosync_compressed_extension, "gz"
|
96
|
-
set :blavosync_tmp_dir, "tmp"
|
97
|
-
set :blavosync_content_sync_method, 'rsync'
|
98
|
-
set :blavosync_from_env, (ENV['FROM_ENV'].nil? ? 'production' : ENV['RAILS_ENV'])
|
99
|
-
set :blavosync_to_env, (ENV['TO_ENV'].nil? ? 'development' : ENV['TO_ENV'])
|
100
|
-
set :blavosync_rsync_content_backup_file, "#{shared_path}/system"
|
101
|
-
set :blavosync_tar_content_backup_file, "#{shared_path}/backup_#{from_env}_content.tar.#{zip_ext}"
|
102
|
-
set :blavosync_db_backup_file, "#{shared_path}/backup_#{from_env}_db.sql"
|
103
|
-
set :blavosync_db_backup_zip_file, "#{db_backup_file}.#{zip_ext}"
|
104
|
-
|
105
|
-
|
106
|
-
== Note on Patches/Pull Requests
|
107
|
-
|
108
|
-
* Fork the project.
|
109
|
-
* Make your feature addition or bug fix.
|
110
|
-
* Add tests for it. This is important so I don't break it in a
|
111
|
-
future version unintentionally.
|
112
|
-
* Commit, do not mess with rakefile, version, or history.
|
113
|
-
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
114
|
-
* Send me a pull request. Bonus points for topic branches.
|
115
|
-
|
116
|
-
== Copyright
|
117
|
-
|
118
|
-
Copyright (c) 2009 jayronc. See LICENSE for details.
|
data/init.rb
DELETED