Class: Squidward::Command::Run

Inherits:
Secured show all
Defined in:
lib/squidward/commands/run.rb

Overview

Core class of the whole application. It simply runs everything once it has been configured.

Instance Method Summary collapse

Methods inherited from Secured

#ask, #ask_for_credentials, #ask_for_password, #echo_off, #echo_on, #read_credentials, #store_credentials

Methods inherited from Base

#configuration, #display, #logger, #store_configuration

Constructor Details

#initializeRun

Returns a new instance of Run.



5
6
7
8
9
10
# File 'lib/squidward/commands/run.rb', line 5

def initialize()
  super
  aws_config = {}
  aws_config[:access_key_id], aws_config[:secret_access_key] = @credentials
  AWS::S3::Base.establish_connection!(aws_config)
end

Instance Method Details

#bucketObject

Returns the bucket configured for the current user



69
70
71
# File 'lib/squidward/commands/run.rb', line 69

def bucket
  return (self.configuration[:default_bucket] or "backup")
end

#compute_md5(filename) ⇒ Object

Computes the MD5 for the uploaded file, in-case you wanna do corruption detection



75
76
77
78
79
80
81
82
83
# File 'lib/squidward/commands/run.rb', line 75

def compute_md5(filename)
  md5_digest = Digest::MD5.new
  File.open(filename, 'r') do |file|
    file.each_line do |line|
      md5_digest << line
    end
  end
  return md5_digest
end

#databaseObject

Singleton instance of the database



59
60
61
# File 'lib/squidward/commands/run.rb', line 59

def database
  @database ||= SDBTools::Database.new(@credentials[0],  @credentials[1], :logger => self.logger)
end

#domainsObject

Returns every configured domain for the current user



64
65
66
# File 'lib/squidward/commands/run.rb', line 64

def domains
  return self.configuration[:domains]
end

#dump_domain(domain_name) ⇒ Object

Dumps the domain contents to YAML file



42
43
44
45
46
47
48
49
50
51
# File 'lib/squidward/commands/run.rb', line 42

def dump_domain(domain_name)
  temp_folder = File.expand_path(File.join(CONF_PATH, TEMP_PATH))
  FileUtils.mkpath(temp_folder)
  temp_file = File.join(temp_folder, generate_dump_filename(domain_name))
  results = database.domain(domain_name).selection().results
  File.open(temp_file, "w+") { |io| YAML::dump(results, io) }
  `tar -czf "#{temp_file}.tar.gz" -C "#{File.dirname(temp_file)}" "#{File.basename(temp_file)}"`
  File.delete(temp_file)
  return "#{temp_file}.tar.gz"
end

#ensure_bucket!(bucket_name) ⇒ Object

If the configured bucket does not exists on AWS S3 it will create it, else it will just retrieve it.



22
23
24
25
26
27
28
# File 'lib/squidward/commands/run.rb', line 22

def ensure_bucket!(bucket_name)
  begin 
     AWS::S3::Bucket.find(bucket_name) 
  rescue
    AWS::S3::Bucket.create(bucket_name) 
  end
end

#generate_dump_filename(domain_name) ⇒ Object

Generates a unique filename for the current domain backup



54
55
56
# File 'lib/squidward/commands/run.rb', line 54

def generate_dump_filename(domain_name)
  "#{domain_name}-#{Time.now.utc.strftime("%Y%m%d%H%M%S%Z%Y")}"
end

#index(args = {}) ⇒ Object

Runs the backup for each configured simpledb domain



13
14
15
16
17
18
# File 'lib/squidward/commands/run.rb', line 13

def index(args = {})
  ensure_bucket!(bucket)
  domains.each do |domain|
    internal_run(domain)
  end
end

#internal_run(pair = {}) ⇒ Object

Performs a backup for a specific domain



31
32
33
34
35
36
37
38
39
# File 'lib/squidward/commands/run.rb', line 31

def internal_run(pair = {})
  domain_name, virtual_path = pair[:domain], (pair[:vpath] or "")
  return unless database.domain_exists?(pair[:domain])
  dump_file = dump_domain(domain_name)
  dump_file_md5 = Base64.encode64(compute_md5(dump_file).digest)
  upload_to = File.join(virtual_path, generate_dump_filename(domain_name))
  AWS::S3::S3Object.store("#{upload_to}.tar.gz", open(dump_file), bucket, :content_md5 => dump_file_md5)
  File.delete(dump_file)
end