module Rmd NAME = 'autoclient/ringmd' TYPE = ['access', 'error', 'realtime', 'web'] class ServerNotFound < StandardError; end class DeployService def self.deploy(server) validate(server) command = "curl -s #{url(server)}/deploy/#{token(server)}" system_cmd(command) end def self.restart(server) validate(server) command = %{echo "cd #{NAME} && sh deploy.sh" | ssh #{master(server)} -p 2200} system_cmd(command) end def self.logs(server) validate(server) command = %{echo "cd #{NAME} && DOCKER_HOST=:2375 docker-compose logs" | ssh #{master(server)} -p 2200} system_cmd(command) end def self.nginx(server, type) validate(server) validate_type(type) command = %{echo "tailf nginx/log/#{type}" | ssh #{nginx(server)}} system_cmd(command) end def self.console(server) validate(server) command = %{echo "cd #{NAME} && DOCKER_HOST=:2375 docker exec -it ringmd_new_web_1 rails c" | ssh #{master(server)} -p 2200} system_cmd(command) end private def self.validate(server) fail(ServerNotFound, 'Server not found in the creds file') unless exists?(server) end def self.validate_type(type) fail(TypeNotFound, "Type is not in #{TYPE}") unless TYPE.include?(type) end def self.exists?(server) Server.all.include?(server) end def self.url(server) Server.get(server).url end def self.token(server) Server.get(server).token end def self.master(server) Server.get(server).master end def self.nginx(server) Server.get(server).nginx end def self.system_cmd(cmd) $stdout.puts(cmd) system(cmd) end end end