client_management/lib/tasks/delete_dbs.rake

44 lines
1.6 KiB
Ruby
Raw Normal View History

2021-01-19 04:48:03 +00:00
require 'net/ssh'
require 'pathname'
namespace :create_site do
desc "Delete databases Script"
task :delete_dbs,[:server_name,:dbs] => :environment do |task,args|
site_server = SiteServer.where(:server_name=>args.server_name).first
ip = site_server.ip
2023-02-23 04:52:58 +00:00
server_port = site_server.port
2021-01-19 04:48:03 +00:00
user = site_server.account
password = site_server.password
@password = password
dbs = args.dbs.split
begin
2023-02-23 04:52:58 +00:00
Net::SSH.start(ip , user , { password: password, port: server_port}) do |ssh|
2021-01-19 04:48:03 +00:00
end
rescue Net::SSH::HostKeyMismatch
system("ssh-keygen -f \"$HOME/.ssh/known_hosts\" -R #{ip}")
rescue Errno::ENOTTY
system("ssh-add \"$HOME/.ssh/id_rsa\"")
end
2023-02-23 04:52:58 +00:00
Net::SSH.start(ip , user , { password: password, port: server_port}) do |ssh|
2021-01-19 04:48:03 +00:00
dbs.each do |db|
puts "deleting database #{db}"
exec_ssh_command_by_sudo(ssh,"bash -l -c 'echo \"db.dropDatabase()\" | mongo --shell \'#{db}\''")
puts "finish deleting database #{db}"
end
end
puts "finished deleting databases rake"
end
def exec_ssh_command_by_sudo(session,command)
output = session.exec!("echo '#{@password}' | sudo -S #{command}")
# output = session.exec!("echo '#{@password}' | sudo -S -s #{command}")
if output.include?("sudo:") && output.include?("command not found")
output = session.exec!(command)
end
2022-07-21 16:10:04 +00:00
return output.encode!("UTF-8", :invalid => :replace, :undef => :replace, :replace => '')
2021-01-19 04:48:03 +00:00
end
def update_infos(info)
puts info
@site_construct.infos = @site_construct.infos.push(info)
@site_construct.save!
return @site_construct.infos
end
end