module Publisher class Ssh attr_accessor :remote_user attr_accessor :remote_host attr_accessor :ssh_key_file def remote_execute(command) Helper.validates_presence_of remote_user, 'Remote User not set' Helper.validates_presence_of remote_host, 'Remote Host not set' Helper.validates_presence_of ssh_key_file, 'SSH keyfile not set' Helper.validates_presence_of command, 'No valid remote command given' result = '' # joined_filename = File.join(File.dirname($exec_file_path), ssh_key_file) # ssh_key_file = joined_filename unless File.file? ssh_key_file $log.writer.error "Can not find SSH keyfile #{ssh_key_file}" exit 1 end Net::SSH.start(remote_host, remote_user, \ auth_methods: ['publickey'], \ forward_agent: true, \ keys: ssh_key_file, \ timeout: 30) do |session| session.open_channel do |channel| channel.exec command do |ch, success| unless success $log.writer.error "could not execute remote command: '#{command}'" exit 1 end ch.on_data do |_ch, data| # STDOUT of command if data $log.writer.debug data.strip result << data else $log.writer.info 'Warning: Maybe successfully executed but no output found' end end ch.on_extended_data do |_ch, type, data| # STDERR of command $log.writer.error 'could not execute command' $log.writer.error data $log.writer.error type exit 1 end ch.on_close do |_ch| # result << "all done, closing!" end end end end result end def remove_old_releases(path, num_of_releases_to_keep, release_name_shema) releases = remote_execute("ls #{path}").split("\n").select { |dir| dir =~ release_name_shema }.sort num_to_remove = releases.size - num_of_releases_to_keep releases_to_delete = releases.take(num_to_remove < 0 ? 0 : num_to_remove) releases_to_delete.each do |dir| $log.writer.debug "removing #{path}/#{dir}" remote_execute("rm -rf #{path}/#{dir}") end end end end