Class: Infrataster::Server

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

Constant Summary collapse

Error =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @name, @options = name, options
  @address = determine_address(address)
  @gateway = nil
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



45
46
47
# File 'lib/infrataster/server.rb', line 45

def address
  @address
end

#nameObject (readonly)

Returns the value of attribute name.



45
46
47
# File 'lib/infrataster/server.rb', line 45

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



45
46
47
# File 'lib/infrataster/server.rb', line 45

def options
  @options
end

Class Method Details

.clear_defined_serversObject



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
  options = args.any? ? args.shift : {}
  if block
    st = OpenStruct.new
    block.call(st)
    address = st.address if st.address
    st.each_pair { |k, v| options[k] = v unless k == :address }
  end
  @@servers << Server.new(name, address, options)
end

.defined_serversObject



26
27
28
# File 'lib/infrataster/server.rb', line 26

def defined_servers
  @@servers
end

.find_by_name(name) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/infrataster/server.rb', line 34

def find_by_name(name)
  server = @@servers.find {|s| s.name == name }
  unless server
    raise Error, "Server definition for '#{name}' is not found."
  end
  server
end

Instance Method Details

#find_available_portObject



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

#fromObject



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

#gatewayObject



68
69
70
# File 'lib/infrataster/server.rb', line 68

def gateway
  @gateway ||= Net::SSH::Gateway.new(*ssh_start_args)
end

#shutdown_gatewayObject



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_argsObject



104
105
106
# File 'lib/infrataster/server.rb', line 104

def ssh_start_args
  @ssh_start_args ||= _ssh_start_args
end