Class: Account

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmo/account.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#machineObject (readonly)

Returns the value of attribute machine.



2
3
4
# File 'lib/cosmo/account.rb', line 2

def machine
  @machine
end

#userObject (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 options = {:files => [], :to => []} 
  puts "please supply a list of servers" if options[:to].empty?
  puts "please supply a list of files" if options[:files].empty?
  unless options[:files].empty? and options[:to].empty?
    options[:files].each do |file|
      options[: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

Returns:

  • (Boolean)


84
85
86
# File 'lib/cosmo/account.rb', line 84

def local?
  self.machine == "localhost"
end

#pwdObject



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 options = {:to => []}
  options[:to].each do |remote|
    remote.run "mkdir -p #{pwd}"
    self.run "rsync -a --exclude '*.xyz' #{pwd} #{remote}:#{File.dirname(pwd)}"
  end
end

#to_sObject



88
89
90
# File 'lib/cosmo/account.rb', line 88

def to_s
  "#{self.user}@#{self.machine}"
end