Class: Infrataster::Server
- Inherits:
-
Object
- Object
- Infrataster::Server
- Defined in:
- lib/infrataster/server.rb
Constant Summary collapse
- Error =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
- .clear_defined_servers ⇒ Object
- .define(name, *args, &block) ⇒ Object
- .defined_servers ⇒ Object
- .find_by_name(name) ⇒ Object
Instance Method Summary collapse
- #find_available_port ⇒ Object
- #forward_port(port, &block) ⇒ Object
- #from ⇒ Object
- #gateway ⇒ Object
-
#initialize(name, address, options = {}) ⇒ Server
constructor
A new instance of Server.
- #shutdown_gateway ⇒ Object
- #ssh(&block) ⇒ Object
- #ssh_exec(cmd, &block) ⇒ Object
- #ssh_start_args ⇒ Object
Constructor Details
#initialize(name, address, options = {}) ⇒ Server
Returns a new instance of Server.
47 48 49 50 51 |
# File 'lib/infrataster/server.rb', line 47 def initialize(name, address, = {}) @name, @options = name, @address = determine_address(address) @gateway = nil end |
Instance Attribute Details
#address ⇒ Object (readonly)
Returns the value of attribute address.
45 46 47 |
# File 'lib/infrataster/server.rb', line 45 def address @address end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
45 46 47 |
# File 'lib/infrataster/server.rb', line 45 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
45 46 47 |
# File 'lib/infrataster/server.rb', line 45 def @options end |
Class Method Details
.clear_defined_servers ⇒ Object
30 31 32 |
# File 'lib/infrataster/server.rb', line 30 def clear_defined_servers @@servers = [] end |
.define(name, *args, &block) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/infrataster/server.rb', line 14 def define(name, *args, &block) address = args.shift = args.any? ? args.shift : {} if block st = OpenStruct.new block.call(st) address = st.address if st.address st.each_pair { |k, v| [k] = v unless k == :address } end @@servers << Server.new(name, address, ) end |
.defined_servers ⇒ Object
26 27 28 |
# File 'lib/infrataster/server.rb', line 26 def defined_servers @@servers end |
Instance Method Details
#find_available_port ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/infrataster/server.rb', line 59 def find_available_port # find available local port server = TCPServer.new('127.0.0.1', 0) available_port = server.addr[1] server.close available_port end |
#forward_port(port, &block) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/infrataster/server.rb', line 79 def forward_port(port, &block) host, forwarded_port = _forward_port(port) if block_given? return_value = block.call(host, forwarded_port) from.gateway.close(forwarded_port) if from return_value else [host, forwarded_port] end end |
#from ⇒ Object
53 54 55 56 57 |
# File 'lib/infrataster/server.rb', line 53 def from if @options[:from] Server.find_by_name(@options[:from]) end end |
#gateway ⇒ Object
68 69 70 |
# File 'lib/infrataster/server.rb', line 68 def gateway @gateway ||= Net::SSH::Gateway.new(*ssh_start_args) end |
#shutdown_gateway ⇒ Object
72 73 74 75 76 77 |
# File 'lib/infrataster/server.rb', line 72 def shutdown_gateway if @gateway @gateway.shutdown! @gateway = nil end end |
#ssh(&block) ⇒ Object
90 91 92 93 94 |
# File 'lib/infrataster/server.rb', line 90 def ssh(&block) Net::SSH.start(*ssh_start_args) do |ssh| block.call(ssh) end end |
#ssh_exec(cmd, &block) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/infrataster/server.rb', line 96 def ssh_exec(cmd, &block) result = nil ssh do |ssh| result = ssh.exec!(cmd, &block) end result end |
#ssh_start_args ⇒ Object
104 105 106 |
# File 'lib/infrataster/server.rb', line 104 def ssh_start_args @ssh_start_args ||= _ssh_start_args end |