Sha256: 7d6cad9143ea5a6334535834856b50bdaddaeb2e558dcb6258f94c0fc17076e8

Contents?: true

Size: 1.88 KB

Versions: 5

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module Capistrano
  module Ops
    module Backup
      module Helper
        def backup_file_name(type)
          regex = type == 'storage' ? "'.{0,}\.tar.gz'" : "'.{0,}\.dump'"
          @backup_file_name ||= capture "cd #{shared_path}/backups && ls -lt | grep -E -i #{regex} | head -n 1 | awk '{print $9}'"
        end

        def backup_file_size
          @backup_file_size ||= capture "cd #{shared_path}/backups && wc -c #{@backup_file_name} | awk '{print $1}'"
        end

        def download_backup(backup_file, type)
          puts "Downloading #{type} backup"
          download! "#{shared_path}/backups/#{backup_file}", backup_file
          puts "Download finished\nDeleting temporary backup..."
          cleanup_backup(backup_file, "Download finished\nDeleting temporary backup...")
        end

        def cleanup_backup(backup_file, message)
          puts message
          execute "cd #{shared_path}/backups && rm #{backup_file}"
          puts 'Temporary backup deleted'
        end

        def question(question, default = 'n', &block)
          print "#{question} #{default.downcase == 'n' ? '(y/N)' : '(Y/n)'}: "
          input = $stdin.gets.strip.downcase
          answer = (input.empty? ? default : input).downcase.to_s

          if %w[y n].include?(answer)
            yield(answer == 'y')
          else
            question(question, default, &block)
          end
        end

        def prepare_env
          @env = "RAILS_ENV=#{fetch(:stage)}"
          @path_cmd = "PATH=$HOME/.rbenv/versions/#{RUBY_VERSION}/bin:$PATH"
          @test_command = "cd #{release_path} && #{@path_cmd} && #{@env}"
        end

        def size_str(size)
          units = %w[B KB MB GB TB]
          e = (Math.log(size) / Math.log(1024)).floor
          s = format('%.2f', size.to_f / 1024**e)
          s.sub(/\.?0*$/, units[e])
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
capistrano-ops-1.0.4 lib/capistrano/ops/backup/helper.rb
capistrano-ops-1.0.3 lib/capistrano/ops/backup/helper.rb
capistrano-ops-1.0.2 lib/capistrano/ops/backup/helper.rb
capistrano-ops-1.0.1 lib/capistrano/ops/backup/helper.rb
capistrano-ops-1.0.0 lib/capistrano/ops/backup/helper.rb