Class: Server

Inherits:
Object
  • Object
show all
Defined in:
lib/toss/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties) ⇒ Server

Returns a new instance of Server.



15
16
17
18
19
20
# File 'lib/toss/server.rb', line 15

def initialize(properties)
  @host = properties[:host]
  @user = properties[:user]
  @password = properties[:password]
  @type = properties[:type]
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



11
12
13
# File 'lib/toss/server.rb', line 11

def host
  @host
end

#passwordObject

Returns the value of attribute password.



11
12
13
# File 'lib/toss/server.rb', line 11

def password
  @password
end

#sshObject

Returns the value of attribute ssh.



13
14
15
# File 'lib/toss/server.rb', line 13

def ssh
  @ssh
end

#typeObject

Returns the value of attribute type.



11
12
13
# File 'lib/toss/server.rb', line 11

def type
  @type
end

#userObject

Returns the value of attribute user.



11
12
13
# File 'lib/toss/server.rb', line 11

def user
  @user
end

#working_directoryObject

Returns the value of attribute working_directory.



12
13
14
# File 'lib/toss/server.rb', line 12

def working_directory
  @working_directory
end

Instance Method Details

#cd(path) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/toss/server.rb', line 58

def cd(path)
  if $options[:verbose] 
    puts "#{bwhite host} cd to: #{path}"
  end
  if block_given?
    previous_path = @working_directory
    @working_directory = path
    yield
    @working_directory =  previous_path
  else
    @working_directory = path
  end
end

#delete(path) ⇒ Object



72
73
74
75
76
# File 'lib/toss/server.rb', line 72

def delete(path)
  if exists? "#{path}"
    return run "unlink #{path}"
  end
end

#exec(command, quiet = false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/toss/server.rb', line 31

def exec(command, quiet=false)
  ssh = open_sessions[host] || start_session(self) 
  # capture all stderr and stdout output from a remote process

  if $options[:verbose] 
    puts "#{bwhite host} executing: #{command}"
  end

  if !working_directory.nil? && working_directory != ""
    command = "cd #{working_directory} && " + command
  end

  stdout_data, stderr_data, exit_code, exit_signal = ssh_exec! ssh, command

  if !quiet
    puts "#{bwhite host} said: #{red stderr_data.strip!}" if !stderr_data.nil? && stderr_data != ""
    puts "#{bwhite host} said: #{stdout_data.strip!}" if !stdout_data.nil? && stdout_data != ""
  end

  return exit_code, stdout_data, stderr_data, exit_signal
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/toss/server.rb', line 53

def exists?(path)
  exit_code = run("[ -e #{path} ] && exit 0 || exit 1")
  return exit_code == 0
end

#run(command = '', &block) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/toss/server.rb', line 22

def run(command='', &block)
  if block_given?
    self.instance_eval &block
  else
    result = exec(command, false)
    return result.first
  end
end