Class: Offshore::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/offshore/client/host.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Host

Returns a new instance of Host.



62
63
64
65
66
67
# File 'lib/offshore/client/host.rb', line 62

def initialize(options)
  @host = options[:host]
  @port = options[:port]
  @path = options[:path]
  @snapshot = options[:snapshot]
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



61
62
63
# File 'lib/offshore/client/host.rb', line 61

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



61
62
63
# File 'lib/offshore/client/host.rb', line 61

def port
  @port
end

Instance Method Details

#base_uriObject

end



21
22
23
24
25
26
# File 'lib/offshore/client/host.rb', line 21

def base_uri
  with_http = host
  with_http = "http://#{host}" unless (host =~ /^https?:/) == 0
  need_port = port || 80
  "#{with_http}:#{need_port}"
end

#connectionObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/offshore/client/host.rb', line 28

def connection
  return @connection if @connection
  connection_class = Faraday.respond_to?(:new) ? ::Faraday : ::Faraday::Connection

  timeout_seconds = 5*60 # 5 minutes
  @connection = connection_class.new(base_uri, :timeout => timeout_seconds) do |builder|
    builder.use Faraday::Request::UrlEncoded  if defined?(Faraday::Request::UrlEncoded)
    builder.adapter Faraday.default_adapter
  end
  @connection
end

#factory_create(name, attributes = {}) ⇒ Object



97
98
99
100
101
102
# File 'lib/offshore/client/host.rb', line 97

def factory_create(name, attributes={})
  data = { :name => name }
  data[:attributes] = attributes
  hash = post(:factory_create, data)
  factory_object(hash)
end

#factory_object(hash) ⇒ Object

TODO: move this to a config block



105
106
107
# File 'lib/offshore/client/host.rb', line 105

def factory_object(hash)
  hash["class_name"].constantize.find(hash["id"])
end

#pathObject



75
76
77
# File 'lib/offshore/client/host.rb', line 75

def path
  @path ||= "/offshore_tests"
end

#post(append_path, attributes = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/offshore/client/host.rb', line 40

def post(append_path, attributes={})
  attributes[:hostname] = `hostname`  # always send hostname
  
  while true do
    http_response = connection.post("#{self.path}/#{append_path}", attributes)
    if http_response.success?
      return MultiJson.decode(http_response.body)
    elsif http_response.status.to_s == Offshore::WAIT_CODE.to_s
      sleep 2 # two seconds and try again
    else
      begin
        hash = MultiJson.decode(http_response.body)
        message = "Error in offshore connection (#{append_path}): #{hash}"
      rescue
        message = "Error in offshore connection (#{append_path}): #{http_response.status}"
      end
      raise message
    end
  end
end

#snapshot_nameObject



69
70
71
72
73
# File 'lib/offshore/client/host.rb', line 69

def snapshot_name
  return nil if @snapshot == true
  return "none" if @snapshot == false
  @snapshot
end

#suite_startObject



79
80
81
82
83
# File 'lib/offshore/client/host.rb', line 79

def suite_start
  attributes = {}
  attributes[:snapshot] = snapshot_name
  hash = post(:suite_start, attributes)
end

#suite_stopObject



85
86
87
# File 'lib/offshore/client/host.rb', line 85

def suite_stop
  hash = post(:suite_stop)
end

#test_start(name) ⇒ Object



89
90
91
# File 'lib/offshore/client/host.rb', line 89

def test_start(name)
  hash = post(:test_start, { :name => name })
end

#test_stop(name) ⇒ Object



93
94
95
# File 'lib/offshore/client/host.rb', line 93

def test_stop(name)
  hash = post(:test_stop, { :name => name })
end