Class: Alphonse::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/alphonse/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, user, config) ⇒ Connection

Returns a new instance of Connection.



9
10
11
12
13
14
15
16
17
18
# File 'lib/alphonse/connection.rb', line 9

def initialize(host, user, config)
  @config = config
  @host = host
  @user = user
  
  raise Alphonse::MissingUserError, "User has not been specified" unless user
  raise Alphonse::MissingHostError, "Host has not been specified" unless host
  
  @session = Net::SSH::Session.new(host, user)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/alphonse/connection.rb', line 8

def config
  @config
end

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/alphonse/connection.rb', line 8

def host
  @host
end

#sessionObject (readonly)

Returns the value of attribute session.



8
9
10
# File 'lib/alphonse/connection.rb', line 8

def session
  @session
end

#userObject (readonly)

Returns the value of attribute user.



8
9
10
# File 'lib/alphonse/connection.rb', line 8

def user
  @user
end

Instance Method Details

#closeObject



47
48
49
# File 'lib/alphonse/connection.rb', line 47

def close
  session.close
end

#connection_stringObject



20
21
22
# File 'lib/alphonse/connection.rb', line 20

def connection_string
  "#{user}@#{host}"
end

#send_and_execute(command = "") ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/alphonse/connection.rb', line 24

def send_and_execute(command = "")
  commands = parse(command)

  session.open(10)
  
  results = []
  commands.each do |cmd|
    Alphonse.logger.task cmd
    result = session.run(cmd)
    results << result
    if result.failure?
      Alphonse.logger.error result.output
      break
    else
      Alphonse.logger.debug result.output if config[:verbose]
      Alphonse.logger.info "Completed in #{result.duration}s"
    end
    
  end
  results
end