Class: SlonyCluster

Inherits:
Object
  • Object
show all
Defined in:
lib/slubydoo/slony_cluster.rb

Class Method Summary collapse

Class Method Details

.create_file_on_system(event_node, sfname_remote, text_in_file) ⇒ Object

allows you to create a file remotely using the gem net-sftp



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/slubydoo/slony_cluster.rb', line 110

def create_file_on_system(event_node, sfname_remote, text_in_file)
  bresult = false
  output = ""
  begin
    Net::SFTP.start(event_node["node_host"], event_node["node_system_username"], :password => event_node["node_system_password"]) do |sftp|
      io = StringIO.new(text_in_file)
      sftp.upload! io, sfname_remote
    end
    Net::SSH.start(event_node["node_host"], event_node["node_system_username"], :password => event_node["node_system_password"]) do |ssh|
      output = ssh.exec!("chmod u+x #{sfname_remote}" << " > #{app_config.slubydoo_log_file_path} 2>&1 &") # exec prints the bresults to $stdout, catch into output
    end # Net::SSH.start
    puts "This file was created: #{sfname_remote}\nThe host was #{event_node["node_host"]}\nThe text in the file is: #{text_in_file}\n #{output}\n"
    bresult = true
  rescue Exception => ex 
    puts "Failed to create file: #{sfname_remote}\nError message: #{ex}\n"
    puts "\nThe host was #{event_node["node_host"]}\n" rescue ""
    bresult =  false
  end # begin
  bresult
end

.delete_file_on_system(event_node, sfname_remote) ⇒ Object

allows you to delete a file remotely using the gem net-ssh



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/slubydoo/slony_cluster.rb', line 132

def delete_file_on_system(event_node, sfname_remote)
  bresult = false
  output = ""
  begin
    Net::SSH.start(event_node["node_host"], event_node["node_system_username"], :password => event_node["node_system_password"]) do |ssh|
      output = ssh.exec!("rm #{sfname_remote}" << " > #{app_config.slubydoo_log_file_path} 2>&1 &")
    end # Net::SSH.start
    puts "This file was deleted: #{sfname_remote}\nThe host was #{event_node["node_host"]}\n #{output}\n"
    bresult = true
  rescue Exception => ex 
    puts "Failed to delete file: #{sfname_remote}\nError message: #{ex}\n"
    puts "\nThe host was #{event_node["node_host"]}\n" rescue ""
  end # begin
  bresult
end

.execute_command_on_system(event_node, in_command) ⇒ Object

allows you to take a command and execute it remotely using the gem net-ssh



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

def execute_command_on_system(event_node, in_command)
  bresult = false
  output = ""
  begin
    Net::SSH.start(event_node["node_host"], event_node["node_system_username"], :password => event_node["node_system_password"]) do |ssh|
      output = ssh.exec!(in_command << " > #{app_config.slubydoo_log_file_path} 2>&1 &")
    end # Net::SSH.start
    puts "This command was excecuted: #{in_command}\nThe host was #{event_node["node_host"]} #{output}\n"
    bresult = true
  rescue Exception => ex
    puts "Failed to execute command: #{in_command}. Error message: #{ex}\n"
  end # begin
  bresult
end

.initialize_nodes(mn, sn) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/slubydoo/slony_cluster.rb', line 26

def initialize_nodes(mn, sn)
  bresult = false
  nodes = []
  nodes << mn
  nodes << sn
  nodes.flatten.each do |node| 
    begin
      conf_string = connection_namespace(mn, sn) 
      conf_string << init_cluster if node["node_type"] == "master"
      conf_string << create_cluster_store(mn, node) if node["node_type"] == "slave"
      sfname = "#{app_config.slubydoo_slonik_dir_path}#{node["node_name"]}_init_" << String.create_random << ".sh"
      bresult = create_file_on_system(node, sfname, conf_string)
      bresult = execute_command_on_system(node, "#{node["node_slonik_cmd_path"]} #{sfname}") if bresult == true
      bresult = delete_file_on_system(node, sfname) if bresult == true and app_config.slubydoo_delete_files_on_remote_hosts
      sleep app_config.slubydoo_sleep_time
    rescue Exception => ex
      puts "Error while initializing nodes: #{ex}\n" if bresult != true
    end
  end
  bresult
end

.master_and_slave_processes(start_stop, mn = SlonyNode.master_node, sn = SlonyNode.slave_nodes, log_path = app_config.slubydoo_log_file_path, conditions = app_config.slubydoo_master_slave_process_conditions) ⇒ Object

slon is the daemon application that “runs” Slony-I replication. A slon instance must be run for each node in a Slony-I cluster.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/slubydoo/slony_cluster.rb', line 49

def master_and_slave_processes(start_stop, mn = SlonyNode.master_node, sn = SlonyNode.slave_nodes, log_path = app_config.slubydoo_log_file_path, conditions = app_config.slubydoo_master_slave_process_conditions )
  bresult = false
  sn = [mn] << sn
  begin
    sn.flatten.each do |node|
      puts %{#{node["node_slon_cmd_path"]} will #{start_stop} after #{Time.now} on db=#{node["node_database_name"]} host=#{node["node_host"]}\n}
      filestr = %{#{node["node_slon_cmd_path"]} #{conditions} #{node["node_cluster_name"]} #{conninfo(node)}} if start_stop == "start"
      filestr = "killall -15 slon" if start_stop == "stop"
      #sfname = "#{app_config.slubydoo_slonik_dir_path}#{node["node_name"]}_#{start_stop}_process_" << String.create_random << ".sh"
      #bresult = create_file_on_system(node, sfname, filestr)
      #bresult = execute_command_on_system(node, "sh #{sfname} &") if bresult == true
      bresult = execute_command_on_system(node, "#{filestr}")
      bresult = true if start_stop == "stop"
    end
  rescue Exception => ex
    puts "Error with master_and_slave_processes: #{ex}\n"
    bresult = false
  end # begin
  bresult
end

.run_slonik_on_system(mn = SlonyNode.master_node, sn = SlonyNode.slave_nodes, in_text = "", description = "") ⇒ Object

This is used to create all sets, tables, sequences, and subscribe those sets, depending on what arguments are in the yamls that you feed it. For ease of use to create a file and run it with the same method.



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

def run_slonik_on_system(mn = SlonyNode.master_node, sn = SlonyNode.slave_nodes, in_text = "", description = "")
  bresult = false
  if in_text == ""
    puts "You did not supply any text for run_slonik_on_system. Are your yamls configured correctly?\n"
  else
    begin
      new_file_str = "#!/bin/sh\n"
      new_file_str = connection_namespace(mn, sn) << in_text
      sfname = "#{app_config.slubydoo_slonik_dir_path}slony_#{description.downcase.gsub(/[\s]/, '_')}_" << String.create_random << ".sh"
      bresult = create_file_on_system(mn, sfname, new_file_str)
      bresult = execute_command_on_system(mn, "#{mn["node_slonik_cmd_path"]} #{sfname}") if bresult == true
      bresult = delete_file_on_system(mn, sfname) if bresult == true and app_config.slubydoo_delete_files_on_remote_hosts
      sleep app_config.slubydoo_sleep_time # give things a few seconds to catch up
    rescue Exception => ex 
      puts "Error message: #{ex}\nFailed to #{description}  \nThis was the attempted text: \n\n#{new_file_str}\n"
    end # begin
  end # if in_text == ""
  bresult
end

.slubydoo_startObject

Kick it ALL off! Make sure your configs are all set first.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/slubydoo/slony_cluster.rb', line 11

def slubydoo_start
  bresult = false
  begin
    puts "\nBeginning creation of slony nodes, processing of sets, and starting processes.\n"
    mn = SlonyNode.master_node
    sn = SlonyNode.slave_nodes
    bresult = initialize_nodes(mn, sn)
    bresult = master_and_slave_processes("start", mn, sn)  if bresult == true
    bresult = SlonySet.subscribe_sets(mn, sn)  if bresult == true
  rescue Exception => ex
    puts "Error while running slubydoo_start: #{ex}\n" if bresult != true
  end # begin
  bresult
end

.store_paths(master_node = SlonyNode.master_node, slave_nodes = SlonyNode.slave_nodes) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/slubydoo/slony_cluster.rb', line 148

def store_paths(master_node = SlonyNode.master_node, slave_nodes = SlonyNode.slave_nodes)
  str = ""
  master_and_slave_processes("stop")
  slave_nodes.each do |slave_node|
  str << store_path( master_node["node_id"], slave_node["node_id"], conninfo(master_node)) # master node
    str << store_path( slave_node["node_id"], master_node["node_id"], conninfo(slave_node)) # slave nodes
  end
  str
end