Module: Repctl::Servers

Includes:
Config
Included in:
Client, Commands, Helpers
Defined in:
lib/repctl/servers.rb

Instance Method Summary collapse

Instance Method Details

#all_instancesObject



13
14
15
16
17
18
19
# File 'lib/repctl/servers.rb', line 13

def all_instances
  instances = []
  all_servers.each do |s|
    instances << s["instance"]
  end
  instances
end

#all_live_instancesObject



35
36
37
# File 'lib/repctl/servers.rb', line 35

def all_live_instances
  all_live_servers.map {|s| s["instance"]}
end

#all_live_serversObject



25
26
27
28
29
30
31
32
33
# File 'lib/repctl/servers.rb', line 25

def all_live_servers
  s = all_servers.select do |s|
    if pid = get_mysqld_pid(s["instance"])
      mysqld_running?(pid)
    else
      false
    end
  end
end

#all_serversObject



9
10
11
# File 'lib/repctl/servers.rb', line 9

def all_servers
  @servers ||= File.open(SERVER_CONFIG) { |yf| YAML::load( yf ) }
end

#get_mysqld_pid(instance) ⇒ Object

Return the process ID (pid) for an instance. This only consults the PID file.



59
60
61
62
63
64
# File 'lib/repctl/servers.rb', line 59

def get_mysqld_pid(instance)
  server = server_for_instance(instance)
  pidfile = server['pid-file']
  return nil unless File.exist?(pidfile)
  Integer(File.open(pidfile, &:readline).strip)
end

#instance_for(host, port) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/repctl/servers.rb', line 43

def instance_for(host, port)
  @servers.each do |s|
    if s["hostname"] == host && s["port"].to_i == port.to_i
      return s["instance"].to_i
    end
  end
  return nil
end

#live?(instance) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/repctl/servers.rb', line 39

def live?(instance)
  get_mysqld_pid(instance)
end

#mysqld_running?(pid) ⇒ Boolean

See if a MySQL server with the given pid is running.

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/repctl/servers.rb', line 53

def mysqld_running?(pid)
  pids = %x{ ps -e | grep mysqld}.split("\n").map { |row| row =~ /\s*(\d+)\s+/; $1.to_i}
  pids.include?(pid)
end

#server_for_instance(instance) ⇒ Object



21
22
23
# File 'lib/repctl/servers.rb', line 21

def server_for_instance(instance)
  all_servers.select {|s| s["instance"] == Integer(instance)}.shift
end