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.



65
66
67
68
69
70
# File 'lib/offshore/client/host.rb', line 65

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.



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

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



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

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
  with_http << ":#{port}" if port
  "#{with_http}"
end

#connectionObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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
  options = {:timeout => timeout_seconds}
  options[:ssl] = {:verify => false}

  @connection = connection_class.new(base_uri, options) 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



100
101
102
103
104
105
# File 'lib/offshore/client/host.rb', line 100

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



108
109
110
# File 'lib/offshore/client/host.rb', line 108

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

#pathObject



78
79
80
# File 'lib/offshore/client/host.rb', line 78

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

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



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

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}): #{http_response.status},\n#{hash}"
      rescue
        message = "Error in offshore connection (#{append_path}): #{http_response.status}\n#{hash}"
      end
      raise message
    end
  end
end

#snapshot_nameObject



72
73
74
75
76
# File 'lib/offshore/client/host.rb', line 72

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

#suite_startObject



82
83
84
85
86
# File 'lib/offshore/client/host.rb', line 82

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

#suite_stopObject



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

def suite_stop
  hash = post(:suite_stop)
end

#test_start(name) ⇒ Object



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

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

#test_stop(name) ⇒ Object



96
97
98
# File 'lib/offshore/client/host.rb', line 96

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