Class: Techinform::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/techinform.rb

Instance Method Summary collapse

Instance Method Details

#clean(prefix = 'local') ⇒ Object



61
62
63
# File 'lib/techinform.rb', line 61

def clean(prefix = 'local')
  puts Backup.new(dry_run: !options[:delete], quiet: options[:quiet]).clean_files(prefix)
end

#decrypt(type, filename) ⇒ Object



44
45
46
47
# File 'lib/techinform.rb', line 44

def decrypt(type, filename)
  puts "lbzip2 is not found. Single-threaded bzip2 will be used. Consider installing lbzip2" if bzip2 == 'bzip2'
  restore(type, filename, nil, true)
end

#deploy(project) ⇒ Object



50
51
52
# File 'lib/techinform.rb', line 50

def deploy(project)
  `cd ~/projects/#{project} && git pull origin master && bundle install && cap production deploy`
end

#restore(type, filename, dbname = nil, just_decrypt = false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/techinform.rb', line 11

def restore(type, filename, dbname = nil, just_decrypt = false)
  require 'highline'
  dbname = filename if dbname.nil?
  if !File.exist?(filename)
    cli = HighLine.new
    path = type == 'pg' ? PostgreBackup.new(database: filename).restore_path : MysqlBackup.new(database: filename).restore_path
    filename = cli.choose(*Dir["#{path}/*"].sort - %w[. ..])
  end
  encrypted = filename.split('.').last == 'gpg'
  if type == 'pg'
    if encrypted && just_decrypt
      puts "Decrypting postgres backup of #{filename}..."
      `pv --wait #{filename} | gpg2 --decrypt  | #{bzip2} > #{File.basename(filename, '.*') + '.bz2'}`
    elsif encrypted
      puts "Restoring postgres backup to database #{dbname}..."
      `pv --wait #{filename} | gpg2 --decrypt | psql #{dbname} > /dev/null`
    else
      `pv --wait #{filename} | #{bunzip2} | psql #{dbname} > /dev/null`
    end
  else
    if encrypted && just_decrypt
      puts "Decrypting mysql backup of #{filename}..."
      `pv --wait #{filename} | gpg2 --decrypt | #{bzip2} > #{File.basename(filename, '.*') + '.bz2'}`
    elsif encrypted
      puts "Restoring mysql backup to database #{dbname}..."
      `pv --wait #{filename} | gpg2 --decrypt | mysql#{" -u#{ENV['DBUSER']}" if !ENV['DBUSER'].nil?}#{" -p#{ENV['PASSWORD']}" if !ENV['PASSWORD'].nil?} #{dbname}`
    else
      `pv --wait #{filename} | #{bunzip2} | mysql#{" -u#{ENV['DBUSER']}" if !ENV['DBUSER'].nil?}#{" -p#{ENV['PASSWORD']}" if !ENV['PASSWORD'].nil?} #{dbname}`
    end
  end
end