Class: Flydata::Mysql::CompatibilityCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/flydata/command/sync.rb

Defined Under Namespace

Classes: CompatibilityError

Constant Summary collapse

SELECT_QUERY_TMPLT =
"SELECT %s"

Instance Method Summary collapse

Constructor Details

#initialize(de_hash, dump_dir = nil) ⇒ CompatibilityCheck

Returns a new instance of CompatibilityCheck.



1337
1338
1339
1340
1341
# File 'lib/flydata/command/sync.rb', line 1337

def initialize(de_hash, dump_dir=nil)
  @db_opts = [:host, :port, :username, :password, :database].inject({}) {|h, sym| h[sym] = de_hash[sym.to_s]; h}
  @dump_dir = dump_dir
  @errors=[]
end

Instance Method Details

#checkObject



1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
# File 'lib/flydata/command/sync.rb', line 1343

def check
  self.methods.grep(/^check_/).each do |m|
    begin
      send(m)
    rescue CompatibilityError => e
      @errors << e
    end
  end
  print_errors
end

#check_mysql_protocol_tcp_compatObject



1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
# File 'lib/flydata/command/sync.rb', line 1386

def check_mysql_protocol_tcp_compat
  query = "mysql -u #{@db_opts[:username]} -h #{@db_opts[:host]} -P #{@db_opts[:port]} #{@db_opts[:database]} -e \"SHOW GRANTS;\" --protocol=tcp"
  query << " -p#{@db_opts[:password]}" unless @db_opts[:password].to_s.empty?

  Open3.popen3(query) do |stdin, stdout, stderr|
    stdin.close
    while !stderr.eof?
      line = stderr.gets
      unless /Warning: Using a password on the command line interface can be insecure./ === line
        raise CompatibilityError, "Cannot connect to MySQL database. Please make sure you can connect with this command:\n      $ mysql -u #{@db_opts[:username]} -h #{@db_opts[:host]} -P #{@db_opts[:port]} #{@db_opts[:database]} --protocol=tcp -p"
      end
    end
  end
end

#check_mysql_row_mode_compatObject



1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
# File 'lib/flydata/command/sync.rb', line 1401

def check_mysql_row_mode_compat
  sys_var_to_check = {'@@binlog_format'=>'ROW', '@@binlog_checksum'=>'NONE', '@@log_bin_use_v1_row_events'=>1}
  errors={}

  client = Mysql2::Client.new(@db_opts)

  begin
    sys_var_to_check.each_key do |sys_var|
      sel_query = SELECT_QUERY_TMPLT % sys_var
      begin
        result = client.query(sel_query)
        unless result.first[sys_var] == sys_var_to_check[sys_var]
          errors[sys_var]=result.first[sys_var]
        end
      rescue Mysql2::Error => e
        if e.message =~ /Unknown system variable/
          unless e.message =~ /(binlog_checksum|log_bin_use_v1_row_events)/
            errors[sys_var] = false
          end
        else
          raise e
        end
      end
    end
  ensure
    client.close
  end
  unless errors.empty?
    error_explanation = ""
    errors.each_key do |err_key|
      error_explanation << "\n    * #{err_key} is #{errors[err_key]} but should be #{sys_var_to_check[err_key]}"
    end
    raise CompatibilityError, "These system variable(s) are not the correct value: #{error_explanation}\n   Please change these system variables for FlyData Sync to run correctly"
  end
end

#check_mysql_user_compatObject

Raises:



1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
# File 'lib/flydata/command/sync.rb', line 1363

def check_mysql_user_compat
  client = Mysql2::Client.new(@db_opts)
  grants_sql = "SHOW GRANTS"
  correct_db = ["ON (\\*|#{@db_opts[:database]})","TO '#{@db_opts[:username]}"]
  necessary_permission_fields= ["SELECT","RELOAD","LOCK TABLES","REPLICATION SLAVE","REPLICATION CLIENT"]
  all_privileges_field= ["ALL PRIVILEGES"]
  result = client.query(grants_sql)
  # Do not catch MySQL connection problem because check should stop if no MySQL connection can be made.
  client.close
  missing_priv = []
  result.each do |res|
    # SHOW GRANTS should only return one column
    res_value = res.values.first
    if correct_db.all? {|perm| res_value.match(perm)}
      necessary_permission_fields.each do |priv|
        missing_priv << priv unless res_value.match(priv)
      end
      return true if missing_priv.empty? or all_privileges_field.all? {|d| res_value.match(d)}
    end
  end
  raise CompatibilityError, "The user '#{@db_opts[:username]}' does not have the correct permissions to run FlyData Sync\n    * These privileges are missing: #{missing_priv.join(", ")}"
end

#check_writing_permissionsObject



1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
# File 'lib/flydata/command/sync.rb', line 1437

def check_writing_permissions
  write_errors = []
  paths_to_check = ["~/.flydata"]
  paths_to_check << @dump_dir unless @dump_dir.to_s.empty?
  paths_to_check.each do |path|
    full_path = File.expand_path(path)
    full_path = File.dirname(full_path) unless File.directory?(full_path)
    write_errors << full_path unless File.writable?(full_path)
  end
  unless write_errors.empty?
    error_dir = write_errors.join(", ")
    raise CompatibilityError, "We cannot access the directories: #{error_dir}"
  end
end


1354
1355
1356
1357
1358
1359
1360
1361
# File 'lib/flydata/command/sync.rb', line 1354

def print_errors
  return if @errors.empty?
  puts "There may be some compatibility issues with your MySQL credentials: "
  @errors.each do |error|
    puts "  * #{error.message}"
  end
  raise "Please correct these errors if you wish to run FlyData Sync"
end