Class: Account
- Inherits:
-
Object
- Object
- Account
- Defined in:
- lib/cosmo/account.rb
Instance Attribute Summary collapse
-
#machine ⇒ Object
readonly
Returns the value of attribute machine.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #cp(options = {:files => [], :to => []}) ⇒ Object
-
#initialize(machine, user) ⇒ Account
constructor
A new instance of Account.
- #local? ⇒ Boolean
- #pwd ⇒ Object
- #run(c) ⇒ Object
- #ssh_exec!(ssh, command) ⇒ Object
- #sync(options = {:to => []}) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(machine, user) ⇒ Account
Returns a new instance of Account.
4 5 6 7 |
# File 'lib/cosmo/account.rb', line 4 def initialize machine, user @machine = machine @user = user end |
Instance Attribute Details
#machine ⇒ Object (readonly)
Returns the value of attribute machine.
2 3 4 |
# File 'lib/cosmo/account.rb', line 2 def machine @machine end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
2 3 4 |
# File 'lib/cosmo/account.rb', line 2 def user @user end |
Instance Method Details
#cp(options = {:files => [], :to => []}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cosmo/account.rb', line 62 def cp = {:files => [], :to => []} puts "please supply a list of servers" if [:to].empty? puts "please supply a list of files" if [:files].empty? unless [:files].empty? and [:to].empty? [:files].each do |file| [:to].each do |server| unless server.machine == 'localhost' server.run "mkdir -p #{File.dirname(file)}" self.run "scp '#{self.user}@#{self.machine}:#{file}' #{server.user}@#{server.machine}:#{pwd}" end end end end end |
#local? ⇒ Boolean
84 85 86 |
# File 'lib/cosmo/account.rb', line 84 def local? self.machine == "localhost" end |
#pwd ⇒ Object
56 57 58 59 60 |
# File 'lib/cosmo/account.rb', line 56 def pwd full = ENV['PWD'] home = ENV['HOME'] full.sub(home, "~") end |
#run(c) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/cosmo/account.rb', line 40 def run c s = " #{self}:".ljust(40) + "#{c} " unless Cosmo::Cosmo.dry? Net::SSH.start(self.machine, self.user) do |ssh| e = ssh_exec! ssh, c if e[2] == 0 puts s.green else puts s.red end end else puts s.blue end end |
#ssh_exec!(ssh, command) ⇒ Object
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 |
# File 'lib/cosmo/account.rb', line 9 def ssh_exec!(ssh, command) stdout_data = "" stderr_data = "" exit_code = nil exit_signal = nil ssh.open_channel do |channel| channel.exec(command) do |ch, success| unless success abort "FAILED: couldn't execute command (ssh.channel.exec)" end channel.on_data do |ch,data| stdout_data+=data end channel.on_extended_data do |ch,type,data| stderr_data+=data end channel.on_request("exit-status") do |ch,data| exit_code = data.read_long end channel.on_request("exit-signal") do |ch, data| exit_signal = data.read_long end end end ssh.loop [stdout_data, stderr_data, exit_code, exit_signal] end |
#sync(options = {:to => []}) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/cosmo/account.rb', line 77 def sync = {:to => []} [:to].each do |remote| remote.run "mkdir -p #{pwd}" self.run "rsync -a --exclude '*.xyz' #{pwd} #{remote}:#{File.dirname(pwd)}" end end |
#to_s ⇒ Object
88 89 90 |
# File 'lib/cosmo/account.rb', line 88 def to_s "#{self.user}@#{self.machine}" end |