Script intended to remove Vault & Consul setup before run re-provision them on server.
This is my first Ruby script, so will be perfect to see my mistakes here.
#!/usr/bin/env ruby
require 'net/ssh'
$me = File.basename(__FILE__)
print "\n[#{$me}] Vault & Consul DESTROY started on the #{$HOST}.\n"
if not ARGV.empty? then
$HOST = ARGV[0]
puts "\n[#{$me}] Working on the #{$HOST}"
else
puts "[#{$me}] ERROR: host to destroy must be in first argument. Exit."
exit 1
end
$USER = 'knife';
$RSA_KEY = 'ssh/id_rsa';
$HOMES = {
"vault" => "/opt/vault/",
"consul" => "/opt/consul/"
}
if File.exist?($RSA_KEY) then
File.chmod(0600, $RSA_KEY)
else
puts "[#{$me}] ERROR: no RSA file #{$RSA_KEY} found. Exit."
exit 1
end
def service_check(service)
Net::SSH.start($HOST, $USER, :keys => $RSA_KEY) do |ssh|
process_status = ssh.exec!("sudo ps aux | grep #{service} | grep -v grep | wc -l")
return process_status
end
end
def service_delete(service_path)
Net::SSH.start($HOST, $USER, :keys => $RSA_KEY) do |ssh|
res = ssh.exec!("if test -d #{service_path}; then
ls -l #{service_path}
else
echo \"ERROR: no #{service_path} directory found. Exit.\"
exit 1
fi; echo $?")
puts res
end
end
def service_stop(service)
Net::SSH.start($HOST, $USER, :keys => $RSA_KEY) do |ssh|
if ssh.exec!("sudo service #{service} stop; echo $?") then
puts "\nService #{service} stopped"
else
puts "ERROR during #{service} stop"
end
end
end
def service_cleanup(service)
_initdir = '/etc/init.d'
_upstartdir = '/etc/init'
_logdir = '/var/log'
_bindir = '/usr/local/bin'
Net::SSH.start($HOST, $USER, :keys => $RSA_KEY) do |ssh|
cleaned = ssh.exec!("
# /etc/init.d/vault
if test -e #{_initdir}/#{service}; then
sudo ls -l #{_initdir}/#{service}
else
echo \"File #{_initdir}/#{service} not found, skip\"
fi
# /etc/init/vault
if test -e #{_upstartdir}/#{service}; then
sudo ls -l #{_upstartdir}/#{service}
else
echo \"File #{_initdir}/#{service} not found, skip\"
fi
# /var/log/vault
if test -e #{_logdir}/#{service}.log; then
sudo ls -l #{_logdir}/#{service}.log
else
echo \"File #{_initdir}/#{service} not found, skip\"
fi
# /usr/local/bin/vault
if test -e #{_bindir}/#{service}; then
sudo ls -l #{_bindir}/#{service}
else
echo \"File #{_initdir}/#{service} not found, skip\"
fi
")
puts cleaned
end
end
def service_destroy(service)
# 1 if service up and running
# 0 otherwise
s_status = service_check(service)
if s_status.to_i == 1
puts "Stopping #{service} on the #{$HOST}"
service_stop(service)
else
puts "\nService #{service} already stopped: #{service_check(service)}"
end
print "\nRemoving #{service} data in #{$HOMES[service]}...\n\n"
service_delete($HOMES[service])
print "\nCleaning up after service.\n\n"
service_cleanup(service)
end
for service in $HOMES.each_key
service_destroy(service)
end
And it's execution:
$ ./scripts/destroy.rb setevoy.vault.local [destroy.rb] Vault & Consul DESTROY started on the $HOST. [destroy.rb] Working on the setevoy.vault.local Service vault already stopped: 0 Removing vault data in /opt/vault/... total 8 drwxr-xr-x 2 root root 4096 Feb 12 13:46 conf.d drwxr-xr-x 2 root root 4096 Feb 12 13:38 data 0 Cleaning up after service. -rwxr-xr-x 1 root root 919 Feb 12 13:38 /etc/init.d/vault File /etc/init.d/vault not found, skip -rw-r--r-- 1 root root 2687 Feb 15 13:44 /var/log/vault.log -rwxr-xr-x 1 root root 33614224 Feb 12 13:35 /usr/local/bin/vault Service consul already stopped: 0 Removing consul data in /opt/consul/... total 12 drwxr-xr-x 2 root root 4096 Feb 12 13:46 conf.d drwxr-xr-x 4 root root 4096 Feb 12 13:38 data drwxr-xr-x 3 root root 4096 Feb 12 13:46 web-ui 0 Cleaning up after service. -rwxr-xr-x 1 root root 935 Feb 12 13:38 /etc/init.d/consul File /etc/init.d/consul not found, skip -rw-r--r-- 1 root root 19883 Feb 15 15:55 /var/log/consul.log -rwxr-xr-x 1 root root 20417720 Jan 14 21:33 /usr/local/bin/consul
def
. \$\endgroup\$ – setevoy Feb 16 '16 at 12:55