Class: Server

Inherits:
Screwcap::Base show all
Defined in:
lib/screwcap/server.rb

Instance Method Summary collapse

Methods inherited from Screwcap::Base

#clone_from, #set

Constructor Details

#initialize(opts = {}) ⇒ Server

A server is the address(es) that you run a :task on.

server :myserver, :address => "abc.com", :password => "xxx"
server :app_servers, :addresses => ["abc.com","def.com"], :keys => "~/.ssh/my_key"

Options

  • A server must have a :user.

  • Specify :address or :addresses

  • A :gateway. See the section about gateways for more info.

  • All Other options will be passed directly to Net::SSH.

    • :keys can be used to specify the key to use to connect to the server

    • :password specify the password to connect with. Not recommended. Use keys.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/screwcap/server.rb', line 14

def initialize(opts = {})
  super
  self.__name = opts.delete(:name)
  self.__user = opts.delete(:user)
  self.__options[:keys] = [opts.delete(:key)] if opts[:key]

  servers = opts.delete(:servers)
  self.__gateway = servers.select {|s| s.__options[:is_gateway] == true }.find {|s| s.__name == opts[:gateway] } if servers
  self.__connections = []

  self.__options = opts

  if self.__options[:address] and self.__options[:addresses].nil?
    self.__addresses = [self.__options.delete(:address)] 
  else
    self.__addresses = self.__options[:addresses]
  end

  validate

  self
end

Instance Method Details

#connect!Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/screwcap/server.rb', line 37

def connect!
  self.__addresses.each  do |address|

    # do not re-connect.  return if we have already been connected
    next if self.__connections.any? {|conn| conn[:address] == address }

    begin
      if self.__gateway
        self.__connections << {:address => address, :connection => __gateway.__get_gateway_connection.ssh(address, self.__user, options_for_net_ssh) }
      else
        self.__connections << {:address => address, :connection => Net::SSH.start(address, self.__user, options_for_net_ssh) }
      end
    rescue Net::SSH::AuthenticationFailed => e
      raise Net::SSH::AuthenticationFailed, "Authentication failed for server named #{self.name}.  Please check your authentication credentials."
    end
  end
  self.__connections
end

#upload!(local, remote) ⇒ Object



56
57
58
59
60
# File 'lib/screwcap/server.rb', line 56

def upload! (local, remote)
  self.__connections.each do |conn|
    conn.scp.upload! local, remote
  end
end