blavosync 0.0.2 → 0.0.5
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.
- data/Blavosync.gemspec +4 -10
- data/Rakefile +1 -3
- data/VERSION +1 -1
- data/lib/blavosync.rb +295 -0
- metadata +3 -9
- data/pkg/blavosync-0.0.0.pre1.gem +0 -0
- data/pkg/blavosync-0.0.1.gem +0 -0
- data/recipes/content_rsync.rb +0 -37
- data/recipes/content_sync.rb +0 -71
- data/recipes/db_sync.rb +0 -104
- data/recipes/shared_sync.rb +0 -96
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.0.
|
8
|
+
s.version = "0.0.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["jayronc"]
|
@@ -23,17 +23,11 @@ Gem::Specification.new do |s|
|
|
23
23
|
"Rakefile",
|
24
24
|
"VERSION",
|
25
25
|
"init.rb",
|
26
|
-
"lib/blavosync.rb"
|
27
|
-
"pkg/blavosync-0.0.0.pre1.gem",
|
28
|
-
"pkg/blavosync-0.0.1.gem",
|
29
|
-
"recipes/content_rsync.rb",
|
30
|
-
"recipes/content_sync.rb",
|
31
|
-
"recipes/db_sync.rb",
|
32
|
-
"recipes/shared_sync.rb"
|
26
|
+
"lib/blavosync.rb"
|
33
27
|
]
|
34
|
-
s.homepage = %q{http://github.com/
|
28
|
+
s.homepage = %q{http://github.com/indierockmedia/Blavosync}
|
35
29
|
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
-
s.require_paths = ["
|
30
|
+
s.require_paths = ["lib"]
|
37
31
|
s.rubygems_version = %q{1.3.5}
|
38
32
|
s.summary = %q{Sync a remote db and rsync content to your development environment.}
|
39
33
|
|
data/Rakefile
CHANGED
@@ -8,10 +8,9 @@ begin
|
|
8
8
|
gem.summary = %Q{Sync a remote db and rsync content to your development environment.}
|
9
9
|
gem.description = %Q{Sync a remote db and rsync content to your development environment. Useful for small teams and developers who are not able to do this manually.}
|
10
10
|
gem.email = "jerrodblavos@mac.com"
|
11
|
-
gem.homepage = "http://github.com/
|
11
|
+
gem.homepage = "http://github.com/indierockmedia/Blavosync"
|
12
12
|
gem.authors = ["jayronc"]
|
13
13
|
gem.has_rdoc = false
|
14
|
-
gem.require_path = 'recipes'
|
15
14
|
|
16
15
|
end
|
17
16
|
Jeweler::GemcutterTasks.new
|
@@ -51,6 +50,5 @@ Rake::RDocTask.new do |rdoc|
|
|
51
50
|
rdoc.rdoc_dir = 'rdoc'
|
52
51
|
rdoc.title = "blavosync #{version}"
|
53
52
|
rdoc.rdoc_files.include('README*')
|
54
|
-
rdoc.rdoc_files.include('recipes/**/*.rb')
|
55
53
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
54
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.5
|
data/lib/blavosync.rb
CHANGED
@@ -0,0 +1,295 @@
|
|
1
|
+
Capistrano::Configuration.instance(:must_exist).load do
|
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
|
+
|
295
|
+
end
|
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.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jayronc
|
@@ -30,21 +30,15 @@ files:
|
|
30
30
|
- VERSION
|
31
31
|
- init.rb
|
32
32
|
- lib/blavosync.rb
|
33
|
-
- pkg/blavosync-0.0.0.pre1.gem
|
34
|
-
- pkg/blavosync-0.0.1.gem
|
35
|
-
- recipes/content_rsync.rb
|
36
|
-
- recipes/content_sync.rb
|
37
|
-
- recipes/db_sync.rb
|
38
|
-
- recipes/shared_sync.rb
|
39
33
|
has_rdoc: true
|
40
|
-
homepage: http://github.com/
|
34
|
+
homepage: http://github.com/indierockmedia/Blavosync
|
41
35
|
licenses: []
|
42
36
|
|
43
37
|
post_install_message:
|
44
38
|
rdoc_options:
|
45
39
|
- --charset=UTF-8
|
46
40
|
require_paths:
|
47
|
-
-
|
41
|
+
- lib
|
48
42
|
required_ruby_version: !ruby/object:Gem::Requirement
|
49
43
|
requirements:
|
50
44
|
- - ">="
|
Binary file
|
data/pkg/blavosync-0.0.1.gem
DELETED
Binary file
|
data/recipes/content_rsync.rb
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
namespace :local do
|
2
|
-
|
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
|
-
system("rsync -avz -e ssh '#{user}@#{domain}:#{content_path}' '#{rails_root}/tmp/'")
|
10
|
-
end
|
11
|
-
|
12
|
-
desc <<-DESC
|
13
|
-
Creates a symlink to public/system from tmp/system
|
14
|
-
DESC
|
15
|
-
task :rsync_restore_content do
|
16
|
-
# from = ENV['FROM'] || 'production'
|
17
|
-
print "\033[1;45m Linking Assets to public directory \033[0m\n"
|
18
|
-
system "ln -nfs #{rails_root}/tmp/system #{rails_root}/public/system"
|
19
|
-
end
|
20
|
-
|
21
|
-
|
22
|
-
desc <<-DESC
|
23
|
-
Wrapper for local:rsync_content and local:rsync_restore_content
|
24
|
-
$> cap local:rsync RAILS_ENV=production
|
25
|
-
DESC
|
26
|
-
task :rsync do
|
27
|
-
transaction do
|
28
|
-
rsync_content
|
29
|
-
rsync_restore_content
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
|
35
|
-
def generate_remote_content_backup
|
36
|
-
run "cd #{shared_path} && tar czf #{rsync_content_backup_file} 'system'"
|
37
|
-
end
|
data/recipes/content_sync.rb
DELETED
@@ -1,71 +0,0 @@
|
|
1
|
-
namespace :local do
|
2
|
-
|
3
|
-
desc <<-DESC
|
4
|
-
Downloads a tarball of shared 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 :backup_content do
|
8
|
-
files = retrieve_local_files('production', 'content')
|
9
|
-
timestamp = most_recent_local_backup(from_env, 'content').to_i
|
10
|
-
last_modified = last_mod_time(content_backup_file).to_i
|
11
|
-
should_redownload = !(timestamp == last_modified)
|
12
|
-
if should_redownload
|
13
|
-
generate_remote_content_backup if last_modified < (Time.now.to_i - (remote_backup_expires))
|
14
|
-
system("mkdir -p #{tmp_dir}")
|
15
|
-
download(content_backup_file, "#{local_content_backup_dir(:env => from_env, :timestamp=>last_modified)}.tar.#{zip_ext}", :via=> :scp) do|ch, name, sent, total|
|
16
|
-
print "\r\033[1;42m #{File.basename(name)}: #{sent}/#{total} -- #{(sent.to_f * 100 / total.to_f).to_i}% \033[0m"
|
17
|
-
end
|
18
|
-
else
|
19
|
-
print "\r\033[1;42m Your Files are already up-to-date \033[0m\n"
|
20
|
-
@current_timestamp = files.first.to_i
|
21
|
-
end
|
22
|
-
util::tmp::check
|
23
|
-
end
|
24
|
-
|
25
|
-
desc <<-DESC
|
26
|
-
Regenerate files.
|
27
|
-
DESC
|
28
|
-
task :force_backup_content do
|
29
|
-
generate_remote_content_backup
|
30
|
-
end
|
31
|
-
|
32
|
-
desc <<-DESC
|
33
|
-
Restores the backed up content (env var FROM specifies which environment
|
34
|
-
was backed up, defaults to RAILS_ENV) to the local development environment app
|
35
|
-
DESC
|
36
|
-
task :restore_content do
|
37
|
-
timestamp = most_recent_local_backup(from_env, 'content')
|
38
|
-
local_dir = local_content_backup_dir(:env => from_env, :timestamp=>timestamp)
|
39
|
-
print "\033[1;45m Local Dir: #{local_dir} \033[0m\n"
|
40
|
-
system "mkdir -p #{local_dir}"
|
41
|
-
system "tar xzf #{local_dir}.tar.#{zip_ext} -C #{local_dir}"
|
42
|
-
print "\033[1;45m Removing old public/system directory \033[0m\n"
|
43
|
-
system "rm -rf public/system"
|
44
|
-
print "\033[1;45m Moving Assets to public directory \033[0m\n"
|
45
|
-
system "mv #{local_dir}/system public/system"
|
46
|
-
print "\033[1;41m Cleaning up \033[0m\n"
|
47
|
-
system "rm -rf #{local_dir}"
|
48
|
-
end
|
49
|
-
|
50
|
-
|
51
|
-
desc <<-DESC
|
52
|
-
Wrapper for local:backup_content and local:restore_content
|
53
|
-
$> cap local:sync_content RAILS_ENV=production RESTORE_ENV=development
|
54
|
-
DESC
|
55
|
-
task :sync_content do
|
56
|
-
transaction do
|
57
|
-
backup_content
|
58
|
-
restore_content
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
def local_content_backup_dir(args={})
|
64
|
-
timestamp = args[:timestamp] || current_timestamp
|
65
|
-
"#{tmp_dir}/#{application}-#{from_env}-content-#{timestamp.to_s.strip}"
|
66
|
-
end
|
67
|
-
|
68
|
-
|
69
|
-
def generate_remote_content_backup
|
70
|
-
run "cd #{shared_path} && tar czf #{tar_content_backup_file} 'system'"
|
71
|
-
end
|
data/recipes/db_sync.rb
DELETED
@@ -1,104 +0,0 @@
|
|
1
|
-
namespace :local do
|
2
|
-
desc <<-DESC
|
3
|
-
Backs up deployable environment's database (identified by the
|
4
|
-
RAILS_ENV environment variable, which defaults to 'production') and copies it to the local machine
|
5
|
-
DESC
|
6
|
-
task :backup_db, :roles => :db do
|
7
|
-
files = retrieve_local_files(from_env, 'db')
|
8
|
-
timestamp = most_recent_local_backup(from_env, 'db').to_i
|
9
|
-
last_modified = last_mod_time(db_backup_zip_file).to_i
|
10
|
-
|
11
|
-
if last_modified < (Time.now.to_i - (remote_backup_expires))
|
12
|
-
generate_remote_db_backup
|
13
|
-
should_redownload = true
|
14
|
-
end
|
15
|
-
should_redownload = !(timestamp == last_modified)
|
16
|
-
if should_redownload
|
17
|
-
system "mkdir -p #{tmp_dir}"
|
18
|
-
download(db_backup_zip_file, "#{local_db_backup_file(:env=>from_env, :timestamp=>last_modified)}.#{zip_ext}", :via=> :scp) do|ch, name, sent, total|
|
19
|
-
print "\r\033[1;42m #{File.basename(name)}: #{sent}/#{total} -- #{(sent.to_f * 100 / total.to_f).to_i}% \033[0m"
|
20
|
-
end
|
21
|
-
else
|
22
|
-
print "\r\033[1;42m Your Files are already up-to-date \033[0m\n"
|
23
|
-
@current_timestamp = files.first.to_i
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
desc <<-DESC
|
28
|
-
Regenerate files.
|
29
|
-
DESC
|
30
|
-
task :force_backup_db do
|
31
|
-
generate_remote_db_backup
|
32
|
-
end
|
33
|
-
|
34
|
-
desc <<-DESC
|
35
|
-
Untars the backup file downloaded from local:backup_db (specified via the FROM env
|
36
|
-
variable, which defalts to RAILS_ENV), and imports (via mysql command line tool) it back into the database
|
37
|
-
defined in the RESTORE_ENV env variable (defaults to development).
|
38
|
-
DESC
|
39
|
-
task :restore_db, :roles => :db do
|
40
|
-
mysql_str = "mysql #{mysql_connection_for(to_env)}"
|
41
|
-
mysql_dump = "mysqldump #{mysql_connection_for(from_env)}"
|
42
|
-
local_db_create = "mysql #{mysql_connection_for(to_env)} -e \"create database if not exists #{mysql_db_for(to_env)}\""
|
43
|
-
remote_backup_file = local_db_backup_file(:env => from_env, :timestamp=>most_recent_local_backup(from_env, 'db')).strip
|
44
|
-
|
45
|
-
puts "\n\033[1;42m Restoring database backup to #{to_env} environment FROM #{remote_backup_file}--#{from_env} using #{mysql_str}\033[0m"
|
46
|
-
system(local_db_create.strip)
|
47
|
-
cmd = ""
|
48
|
-
cmd << <<-CMD
|
49
|
-
#{unzip} -c #{remote_backup_file}.#{zip_ext} > #{remote_backup_file} &&
|
50
|
-
#{mysql_str} < #{remote_backup_file} &&
|
51
|
-
rm -f #{remote_backup_file}
|
52
|
-
CMD
|
53
|
-
system(cmd.strip)
|
54
|
-
util::tmp::check
|
55
|
-
end
|
56
|
-
|
57
|
-
desc <<-DESC
|
58
|
-
Wrapper for local:backup_db and local:restore_db.
|
59
|
-
$> cap local:sync_db RAILS_ENV=production RESTORE_ENV=development
|
60
|
-
DESC
|
61
|
-
task :sync_db do
|
62
|
-
transaction do
|
63
|
-
backup_db
|
64
|
-
restore_db
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
desc <<-DESC
|
69
|
-
Ensure that a fresh remote data dump is retrieved before syncing to the local environment.
|
70
|
-
DESC
|
71
|
-
task :resync_db do
|
72
|
-
util::tmp::clean_remote
|
73
|
-
sync_db
|
74
|
-
end
|
75
|
-
|
76
|
-
end
|
77
|
-
|
78
|
-
|
79
|
-
def generate_remote_db_backup
|
80
|
-
run "mysqldump #{mysql_connection_for(from_env)} > #{db_backup_file}"
|
81
|
-
run "rm -f #{db_backup_zip_file} && #{zip} #{db_backup_file} && rm -f #{db_backup_file}"
|
82
|
-
end
|
83
|
-
|
84
|
-
def local_db_backup_file(args = {})
|
85
|
-
env = args[:env] || 'production'
|
86
|
-
timestamp = args[:timestamp] || current_timestamp
|
87
|
-
"#{tmp_dir}/#{application}-#{env}-db-#{timestamp.to_s.strip}.sql"
|
88
|
-
end
|
89
|
-
|
90
|
-
def mysql_connection_for(environment)
|
91
|
-
db_settings = YAML.load_file(local_db_conf(environment))[environment]
|
92
|
-
pass = pluck_pass_str(db_settings)
|
93
|
-
host = (db_settings['host'].nil?) ? nil : "--host=#{db_settings['host']}"
|
94
|
-
socket = (db_settings['socket'].nil?) ? nil : "--socket=#{db_settings['socket']}"
|
95
|
-
user = (db_settings['username'].nil?) ? nil : "-u #{db_settings['username']}"
|
96
|
-
database = (db_settings['database'].nil?) ? nil : " #{db_settings['database']}"
|
97
|
-
[user, pass, host, socket, database ].join(" ")
|
98
|
-
end
|
99
|
-
|
100
|
-
def mysql_db_for(environment)
|
101
|
-
restore_from = ENV['FROM'] || 'production'
|
102
|
-
@from_db ||= YAML.load_file(local_db_conf(restore_from))[restore_from]
|
103
|
-
@from_database ||= (@from_db['database'].nil?) ? nil : " #{@from_db['database']}"
|
104
|
-
end
|
data/recipes/shared_sync.rb
DELETED
@@ -1,96 +0,0 @@
|
|
1
|
-
set :rails_root, fetch(:blavosync_local_root, Pathname.new('.').realpath )
|
2
|
-
set :content_dir, fetch(:blavosync_content_directories, content_directories ||= "system" )
|
3
|
-
set :content_path, fetch(:blavosync_content_path, File.join(shared_path, content_dir) )
|
4
|
-
set :public_path, fetch(:blavosync_public_path, File.join(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
|
-
|
19
|
-
def local_db_conf(env = nil)
|
20
|
-
env ||= fetch(:to_env)
|
21
|
-
fetch(:config_structure, :rails).to_sym == :sls ?
|
22
|
-
File.join('config', env.to_s, 'database.yml') :
|
23
|
-
File.join('config', 'database.yml')
|
24
|
-
end
|
25
|
-
|
26
|
-
def pluck_pass_str(db_config)
|
27
|
-
db_config['password'].nil? ? '' : "-p'#{db_config['password']}'"
|
28
|
-
end
|
29
|
-
|
30
|
-
def current_timestamp
|
31
|
-
@current_timestamp ||= Time.now.to_i.to_s.strip
|
32
|
-
end
|
33
|
-
|
34
|
-
def retrieve_local_files(env, type)
|
35
|
-
`ls -r #{tmp_dir} | awk -F"-" '{ if ($2 ~ /#{env}/ && $3 ~ /#{type}/) { print $4; } }'`.split(' ')
|
36
|
-
end
|
37
|
-
|
38
|
-
def most_recent_local_backup(env, type)
|
39
|
-
retrieve_local_files(env, type).first.to_i
|
40
|
-
end
|
41
|
-
|
42
|
-
def last_mod_time(path)
|
43
|
-
capture("stat -c%Y #{path}")
|
44
|
-
end
|
45
|
-
|
46
|
-
def server_cache_valid?(path)
|
47
|
-
capture("[ -f #{path} ] || echo '1'").empty? && ((Time.now.to_i - last_mod_time(path)) <= remote_backup_expires)
|
48
|
-
end
|
49
|
-
|
50
|
-
namespace :util do
|
51
|
-
|
52
|
-
namespace :tmp do
|
53
|
-
desc "[capistrano-extensions]: Displays warning if :tmp_dir has more than 10 files or is greater than 50MB"
|
54
|
-
task :check do
|
55
|
-
#[ 5 -le "`ls -1 tmp/cap | wc -l`" ] && echo "Display Me"
|
56
|
-
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}/* }
|
57
|
-
system(cmd)
|
58
|
-
end
|
59
|
-
|
60
|
-
desc "[capistrano-extensions]: Remove the current remote env's backups from :tmp_dir"
|
61
|
-
task :clean_remote do
|
62
|
-
system("rm -rf #{rails_root}/#{tmp_dir}/#{fetch(:application)}-*")
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
namespace :local do
|
69
|
-
desc <<-DESC
|
70
|
-
Wrapper for local:sync_db and local:sync_content
|
71
|
-
$> cap local:sync RAILS_ENV=production RESTORE_ENV=development
|
72
|
-
DESC
|
73
|
-
task :sync do
|
74
|
-
sync_db
|
75
|
-
if content_sync_method == 'tar'
|
76
|
-
sync_content
|
77
|
-
else
|
78
|
-
rsync_content
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
desc <<-DESC
|
83
|
-
Wrapper for local:force_backup_db, local:force_backup_content, and the local:sync to get
|
84
|
-
a completely fresh set of data from the server
|
85
|
-
$> cap local:sync RAILS_ENV=production RESTORE_ENV=development
|
86
|
-
DESC
|
87
|
-
task :sync_init do
|
88
|
-
force_backup_db
|
89
|
-
force_backup_content
|
90
|
-
sync
|
91
|
-
end
|
92
|
-
|
93
|
-
task :test_root do
|
94
|
-
puts "RAILS_ROOT #{rails_root}"
|
95
|
-
end
|
96
|
-
end
|