Module: Validations

Included in:
Backup, Create, List, Restore
Defined in:
lib/validations.rb

Overview

Common methods used for setting up or verifing

Instance Method Summary collapse

Instance Method Details

#check_backup_path(raise_msg = 'backup_path is not a valid directory') ⇒ Bool

Check that the backup_path is valid

Parameters:

  • raise_msg (String) (defaults to: 'backup_path is not a valid directory')

    exception to raise if the backup_path is not a valid direcotry. if set to nil no exception will be rasied

Returns:

  • (Bool)


20
21
22
23
24
25
26
# File 'lib/validations.rb', line 20

def check_backup_path(raise_msg = 'backup_path is not a valid directory')
  if !File.directory?($backup_path)
    raise raise_msg if raise_msg
    return false
  end
  true
end

#check_mogile_domain(domain, raise_msg = 'Domain does not exist in MogileFS') ⇒ Bool

Check if mogile domain is valid

Parameters:

  • raise_msg (String) (defaults to: 'Domain does not exist in MogileFS')

    exception to raise if domain does not exist. if set to nil no exception will be raised

Returns:

  • (Bool)


110
111
112
113
114
115
116
117
118
# File 'lib/validations.rb', line 110

def check_mogile_domain(domain, raise_msg = 'Domain does not exist in MogileFS')
  require('domain')
  domain = Domain.find_by_namespace(self.domain)
  if !domain
    raise raise_msg if raise_msg
    return false
  end
  true
end

#check_settings_file(raise_msg = 'settings.yml not found in path. This must not be a backup profile. See: mogbak help create') ⇒ Bool

Check that the settings.yml file exists in the backup_path

Parameters:

  • raise_msg (String) (defaults to: 'settings.yml not found in path. This must not be a backup profile. See: mogbak help create')

    exception to raise if the file is missing. if set to nil no exception will be rasied

Returns:

  • (Bool)


7
8
9
10
11
12
13
14
# File 'lib/validations.rb', line 7

def check_settings_file(raise_msg = 'settings.yml not found in path.  This must not be a backup profile. See: mogbak help create')
  if File.exists?("#{$backup_path}/settings.yml")
    return true
  else
    raise raise_msg if raise_msg
    return false
  end
end

#connect_sqlite(raise_msg = nil) ⇒ Bool

Connect to sqlite metadata db

Parameters:

  • raise_msg (String) (defaults to: nil)

    exception to raise if we cannot connect. if set to nil no exception will be rasied

Returns:

  • (Bool)


46
47
48
49
50
51
52
53
54
55
# File 'lib/validations.rb', line 46

def connect_sqlite(raise_msg = nil)
  begin
    ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => "#{$backup_path}/db.sqlite", :timeout => 10000)
  rescue Exception => e
    raise raise_msg if raise_msg
    raise e if $debug
    return false
  end
  true
end

#create_sqlite_db(raise_msg = "Could not create #{$backup_path}/db.sqlite - check permissions") ⇒ Bool

Create database for metadata

Parameters:

  • raise_msg (String) (defaults to: "Could not create #{$backup_path}/db.sqlite - check permissions")

    exception to raise if database cannot be created. nil will raise no exception

Returns:

  • (Bool)


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/validations.rb', line 31

def create_sqlite_db(raise_msg = "Could not create #{$backup_path}/db.sqlite - check permissions")
  begin
    if !File.exists?("#{$backup_path}/db.sqlite")
      SQLite3::Database.new("#{$backup_path}/db.sqlite")
    end
  rescue Exception => e
    raise raise_msg if raise_msg
    return false
  end
  true
end

#migrate_sqlite(raise_msg = "could not run migrations on #{$backup_path}/db.sqlite") ⇒ Bool

Run ActiveRecord migrations on the sqlite database

Parameters:

  • raise_msg (String) (defaults to: "could not run migrations on #{$backup_path}/db.sqlite")

    exception to raise if migrations fail. if set to nil no exception will be rasied

Returns:

  • (Bool)


60
61
62
63
64
65
66
67
68
69
# File 'lib/validations.rb', line 60

def migrate_sqlite(raise_msg = "could not run migrations on #{$backup_path}/db.sqlite")
  #run migrations
  begin
    ActiveRecord::Migrator.up(File.expand_path(File.dirname(__FILE__)) + '/../db/migrate/')
  rescue
    raise raise_msg if raise_msg
    return false
  end
  true
end

#mogile_db_connect(raise_msg = 'Could not connect to MySQL database') ⇒ Bool

Connect to MogileFS mysql server

Parameters:

  • raise_msg (String) (defaults to: 'Could not connect to MySQL database')

    exception to raise if we cannot connect. if set to nil no exception will be rasied

Returns:

  • (Bool)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/validations.rb', line 74

def mogile_db_connect(raise_msg = 'Could not connect to MySQL database')
  #Verify that we can connect to the mogilefs mysql server
  begin
    ActiveRecord::Base.establish_connection({:adapter => "mysql2",
                                             :host => @db_host,
                                             :port => @db_port,
                                             :username => @db_user,
                                             :password => @db_pass,
                                             :database => @db,
                                             :reconnect => true})
  rescue Exception => e
    raise raise_msg if raise_msg
    return false
  end
  true
end

#mogile_tracker_connect(raise_msg = 'Could not connect to MogileFS tracker') ⇒ Bool

Connect to mogile tracker

Parameters:

  • raise_msg (String) (defaults to: 'Could not connect to MogileFS tracker')

    exception to raise if we cannot connect. if set to nil no exception will be raised

Returns:

  • (Bool)


94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/validations.rb', line 94

def mogile_tracker_connect(raise_msg = 'Could not connect to MogileFS tracker')
  host = ["#{@tracker_ip}:#{@tracker_port}"]
  begin
  $mg = MogileFS::MogileFS.new(:domain => @domain, :hosts => host)
  rescue Exception => e
    if $debug
      raise e
    end
    raise raise_msg if raise_msg
    return false
  end
end