Skip to content

Commit

Permalink
wrote a rake task to selectively update cms seeds locally from stagin…
Browse files Browse the repository at this point in the history
…g if staging is newer
  • Loading branch information
stanleypliu committed Jul 24, 2020
1 parent 604d8a8 commit c115105
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ group :development do
gem 'capistrano-git-with-submodules', '2.0.3'
gem 'capistrano-service'
gem 'awesome_print'
gem 'net-sftp'
# gem 'listen', '~> 3.1.5'
# gem 'spring-watcher-listen', '~> 2.0.0'
#
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,8 @@ GEM
thor (~> 0.19)
net-scp (2.0.0)
net-ssh (>= 2.6.5, < 6.0.0)
net-sftp (3.0.0)
net-ssh (>= 5.0.0, < 7.0.0)
net-ssh (5.2.0)
nio4r (2.5.2)
nokogiri (1.10.7)
Expand Down Expand Up @@ -1341,6 +1343,7 @@ DEPENDENCIES
minitest (~> 5.10, != 5.10.2)
mocha (~> 1.0.0)
neat
net-sftp
nokogiri (~> 1.10.4)
pg (~> 0.21)
phantompdf (~> 1.2.2)
Expand Down
46 changes: 29 additions & 17 deletions lib/tasks/staging_seeds.rake
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,46 @@ namespace :comfy do
task :seed_import => :environment do |_t|
require 'net/ssh'
require 'net/scp'

puts "Importing CMS Seed data from Staging Folder to #{local} ..."

# Locally stored seeds
local = ComfortableMexicanSofa.config.seeds_path
def check_timestamp(file, session, local, local_file, remote_file)
newer = Time.at(file.attributes.mtime) >= File.stat(local_file).mtime
if newer
puts "#{file.name} is newer than the local copy, downloading..."
session.scp.download!(remote_file, local)
end
end

# Locally stored seeds - assumes you already have the local folder -
# it won't create it
local = ComfortableMexicanSofa.config.seeds_path + '/protected-planet'
remote = 'ProtectedPlanet/current/db/cms_seeds/protected-planet'

puts "Importing CMS Seed data from Staging Folder to #{local} ..."

# SSH into staging server with Net::SSH
# Check to see if local CMS seeds are newer than local and download any
Net::SSH.start(ENV['PP_STAGING'], ENV['PP_USER']) do |ssh|
ssh.sftp.dir.foreach('ProtectedPlanet/current/db/cms_seeds/protected-planet') do |f|
Net::SSH.start(ENV['PP_STAGING'], ENV['PP_USER']) do |session|
session.sftp.dir.glob(remote, '**/*').each do |file|
# Go through the various files and folders and check to see if they exist locally
if Dir.children(local).include?(f)
if File.stat(f).file?
local_file_changed = File.stat(f).mtime > Time.at(sftp.stat(f).mtime)
elsif File.stat(f).directory
ssh.sftp.dir.foreach(f) do |file|
local_file_changed = File.stat(file).mtime > Time.at(sftp.stat(file).mtime)
end
remote_file = File.join(remote, file.name)
local_file = File.join(local, file.name)

# There are files with non-ASCII characters (i.e. accented) in the CMS files
if Dir.glob('**/*', base: local).include?(file.name.force_encoding('UTF-8'))
if File.file?(File.join(local, file.name))
check_timestamp(file, session, local, local_file, remote_file)
end
else

puts "#{file.name} doesn\'t exist locally, downloading"
# File doesn't exist locally, so download it (in any folder required)
session.scp.download!(remote_file, local, recursive: true)
end
end
# ssh.scp.download!('ProtectedPlanet/current/db/cms_seeds', local, recursive: true)
end

puts "Replacing your local seed data..."
puts "Finished downloads, now replacing your local seed data..."

Rake::Task["comfy:cms_seeds:import[protected-planet, protectedplanet]"].invoke


end
end

0 comments on commit c115105

Please sign in to comment.