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.



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

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly) Also known as: c

Returns the value of attribute config.



85
86
87
# File 'lib/backup/ssh_helpers.rb', line 85

def config
  @config
end

#sessionObject (readonly)

def self.new_for_ssh(server_name)

a = new(server_name)

end



84
85
86
# File 'lib/backup/ssh_helpers.rb', line 84

def session
  @session
end

Instance Method Details

#cleanup_directory(dir, keep) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/backup/ssh_helpers.rb', line 125

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



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

def close
  @session.close
end

#connectObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/backup/ssh_helpers.rb', line 92

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

#on_remote(&block) ⇒ Object



111
112
113
114
115
# File 'lib/backup/ssh_helpers.rb', line 111

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

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



104
105
106
107
108
109
# File 'lib/backup/ssh_helpers.rb', line 104

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



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

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