module Rmd NAME = 'autoclient/ringmd' TYPE = ['access', 'error', 'realtime', 'web'] class ServerNotFound < StandardError; end class DeployService def self.deploy(server, skip_assets = false) validate(server) deploy_url = if skip_assets "#{url(server)}/deploy/#{token(server)}" else "#{url(server)}/deploy_without_assets/#{token(server)}" end command = "curl -s #{deploy_url}" system_cmd(command) end def self.restart(server) validate(server) command = %{echo "cd #{NAME} && sh restart.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}.log" | ssh #{nginx_url(server)}} system_cmd(command) end def self.bash(server) validate(server) command = %{ssh -t #{master(server)} -p 2200 DOCKER_HOST=:2375 docker exec -it ringmd_new_web_1 bash} system_cmd(command) end def self.config(server) validate(server) command = %{ssh -t #{master(server)} -p 2200 vim #{NAME}/.env} system_cmd(command) $stdout.puts('Please remember to restart the servers for the new config to take effect') 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_url(server) Server.get(server).nginx end def self.system_cmd(cmd) $stdout.puts(cmd) system(cmd) end end end