Class: Publisher::Ssh

Inherits:
Object
  • Object
show all
Defined in:
lib/depengine/publisher/ssh.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#remote_hostObject

Returns the value of attribute remote_host.



4
5
6
# File 'lib/depengine/publisher/ssh.rb', line 4

def remote_host
  @remote_host
end

#remote_userObject

Returns the value of attribute remote_user.



3
4
5
# File 'lib/depengine/publisher/ssh.rb', line 3

def remote_user
  @remote_user
end

#ssh_key_fileObject

Returns the value of attribute ssh_key_file.



5
6
7
# File 'lib/depengine/publisher/ssh.rb', line 5

def ssh_key_file
  @ssh_key_file
end

Instance Method Details

#remote_execute(command) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/depengine/publisher/ssh.rb', line 7

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

#remove_old_releases(path, num_of_releases_to_keep, release_name_shema) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/depengine/publisher/ssh.rb', line 59

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