I am trying to learn ruby scripting. Currently I am creating a script which should accept user input and do some action like below:
Go to server1 and restart mysqld
Below is my Script:
#!/usr/bin/ruby
puts "on which environment you want to run the script?"
srenv = STDIN.gets.chomp()
puts "#{srenv}"
puts "Hi you have chosen to run the script on #{srenv}, now is it Cluster or not?"
srenvdl = STDIN.gets.chomp()
puts "#{srenvdl}"
if srenvdl == "y"
text = String.new
File.open("cluster.txt") { |f| text = f.read }
words = text.split(/[^a-zA-Z1-9]/)
puts "Now enter your services names:"
sernm = STDIN.gets.chomp()
sernmw = sernm.split(/[^a-zA-Z1-9]/)
for x in words
for each in sernmw
puts "Go to #{x} and restart #{each}!!"
end
end
else
puts "Sorry now we are only supporting Clusters."
exit
end
It is an interactive script and it's asking user to enter the service name and reads the cluster server names from the file which I want to skip. Also I am not able to skip the cluster name while reading from the file.
ruby script.rb cluster1:mysqld,nginx cluster2:memcache,sendmail
I have anoter file cluster.txt where I have stored the cluster server names as below:
cluster1:server1,server11,server111
cluster2:server2,server22,server222
Now I want the script should return me the result as like below:
Go to server1 and restart mysqld
Go to server1 and restart nginx
Go to server11 and restart mysqld
Go to server11 and restart nginx
Go to server111 and restart mysqld
Go to server111 and restart nginx
Go to server2 and restart memcache
Go to server2 and restart sendmail
Go to server22 and restart memcache
Go to server22 and restart sendmail
Go to server222 and restart memcache
Go to server222 and restart sendmail