Class: Backup::SshActor

Inherits:
Object
  • Object
show all
Defined in:
lib/backup/ssh_helpers.rb

Overview

end Class Command

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SshActor

Returns a new instance of SshActor.



92
93
94
# File 'lib/backup/ssh_helpers.rb', line 92

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly) Also known as: c

Returns the value of attribute config.



89
90
91
# File 'lib/backup/ssh_helpers.rb', line 89

def config
  @config
end

#sessionObject (readonly)

def self.new_for_ssh(server_name)

a = new(server_name)

end



88
89
90
# File 'lib/backup/ssh_helpers.rb', line 88

def session
  @session
end

Instance Method Details

#cleanup_directory(dir, keep) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/backup/ssh_helpers.rb', line 129

def cleanup_directory(dir, keep)
  puts "Cleaning up" 
  cleanup = <<-END
  LOOK_IN="#{dir}"; COUNT=`ls -1 $LOOK_IN | wc -l`; MAX=#{keep}; if (( $COUNT > $MAX )); then let "OFFSET=$COUNT-$MAX"; i=1; for f in `ls -1 $LOOK_IN | sort`; do if (( $i <= $OFFSET )); then CMD="rm $LOOK_IN/$f"; echo $CMD; $CMD; fi; let "i=$i + 1"; done; else true; fi
  END
  run cleanup # todo make it so that even this can be overridden
end

#closeObject



121
122
123
# File 'lib/backup/ssh_helpers.rb', line 121

def close
  @session.close
end

#connectObject



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/backup/ssh_helpers.rb', line 96

def connect
  c[:servers].each do |server| # todo, make this actually work
  @session = Net::SSH.start(
       server,
       :port         => c[:port],
       :username     => c[:ssh_user],
       :host_key     => "ssh-rsa",
       :keys         => [ c[:identity_key] ],
       :auth_methods => %w{ publickey } )
  end
end

#on_remote(&block) ⇒ Object



115
116
117
118
119
# File 'lib/backup/ssh_helpers.rb', line 115

def on_remote(&block)
  connect
  self.instance_eval(&block)
  close
end

#run(cmd, options = {}, &block) ⇒ Object



108
109
110
111
112
113
# File 'lib/backup/ssh_helpers.rb', line 108

def run(cmd, options={}, &block)
  #logger.debug "executing #{cmd.strip.inspect}"
  puts "executing #{cmd.strip.inspect}"
  command = Command.new(@server_name, cmd, block, options, self)
  command.process! # raises an exception if command fails on any server
end

#verify_directory_exists(dir) ⇒ Object



125
126
127
# File 'lib/backup/ssh_helpers.rb', line 125

def verify_directory_exists(dir)
  run "if [ -d '#{dir}' ]; then true; else mkdir -p '#{dir}'; fi"
end