Class: Sauce::Connect

Inherits:
Object
  • Object
show all
Defined in:
lib/sauce/connect.rb

Defined Under Namespace

Classes: TunnelNotPossibleException

Constant Summary collapse

TIMEOUT =
90

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connect

Returns a new instance of Connect.



14
15
16
17
18
19
20
21
22
23
# File 'lib/sauce/connect.rb', line 14

def initialize(options={})
  @ready = false
  @status = "uninitialized"
  @error = nil

  extract_config options

  error_on_missing_creds
  error_on_missing_executable
end

Instance Attribute Details

#access_keyObject

Returns the value of attribute access_key.



12
13
14
# File 'lib/sauce/connect.rb', line 12

def access_key
  @access_key
end

#errorObject (readonly)

Returns the value of attribute error.



11
12
13
# File 'lib/sauce/connect.rb', line 11

def error
  @error
end

#statusObject (readonly)

Returns the value of attribute status.



11
12
13
# File 'lib/sauce/connect.rb', line 11

def status
  @status
end

#usernameObject

Returns the value of attribute username.



12
13
14
# File 'lib/sauce/connect.rb', line 12

def username
  @username
end

Class Method Details

.connect!(*args) ⇒ Object



198
199
200
201
202
203
204
205
# File 'lib/sauce/connect.rb', line 198

def self.connect!(*args)
  @connection = self.new(*args)
  @connection.connect
  @connection.wait_until_ready
  at_exit do
    @connection.disconnect
  end
end

.ensure_connected(*args) ⇒ Object



207
208
209
210
211
212
213
# File 'lib/sauce/connect.rb', line 207

def self.ensure_connected(*args)
  if @connection
    @connection.wait_until_ready
  else
    connect!(*args)
  end
end

Instance Method Details

#cli_optionsObject



143
144
145
146
147
# File 'lib/sauce/connect.rb', line 143

def cli_options
  cli_options = { readyfile: "sauce_connect.ready" }
  cli_options.merge!(@cli_options) if @cli_options
  cli_options
end

#connectObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/sauce/connect.rb', line 88

def connect
  unless @skip_connection_test
    ensure_connection_is_possible
  end

  puts "[Sauce Connect is connecting to Sauce Labs...]"

  formatted_cli_options = array_of_formatted_cli_options_from_hash(cli_options)

  command_args = ['-u', @username, '-k', @access_key]
  command_args << formatted_cli_options

  command = "exec #{find_sauce_connect} #{command_args.join(' ')} 2>&1"

  unless @quiet
    string_arguments = formatted_cli_options.join(' ')
    puts "[Sauce Connect arguments: '#{string_arguments}' ]"
  end

  @pipe = IO.popen(command)

  @process_status = $?
  at_exit do
    Process.kill("INT", @pipe.pid)
    while @ready
      sleep 1
    end
  end

  Thread.new {
    while( (line = @pipe.gets) )
      if line =~ /remote tunnel VM is now: (.*)/
        @status = $1
      end
      if line =~/You may start your tests\./i
        @ready = true
      end
      if line =~ /- (Problem.*)$/
        @error = $1
        @quiet = false
      end
      if line =~ /== Missing requirements ==/
        @error = "Missing requirements"
        @quiet = false
      end
      if line =~/Invalid API_KEY provided/
        @error = "Invalid API_KEY provided"
        @quiet = false
      end
      $stderr.puts line unless @quiet
    end
    @ready = false
    }
end

#disconnectObject



166
167
168
169
170
171
172
173
# File 'lib/sauce/connect.rb', line 166

def disconnect
  if @ready
    Process.kill("INT", @pipe.pid)
    while @ready
      sleep 1
    end
  end
end

#ensure_connection_is_possibleObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sauce/connect.rb', line 63

def ensure_connection_is_possible
  $stderr.puts "[Checking REST API is contactable...]" unless @quiet
  uri = URI("http://saucelabs.com/rest/v1/#{@username}/tunnels")
  
  response = Net::HTTP.start(uri.host, uri.port) do |http|
    request = Net::HTTP::Get.new uri.request_uri
    request.basic_auth(@username, @access_key)
    response = http.request request
  end

  unless response.kind_of? Net::HTTPOK
    $stderr.puts Sauce::Connect.cant_access_rest_api_message
    raise TunnelNotPossibleException, "Couldn't access REST API"
  end

  begin
    $stderr.puts "[Checking port 443 is open...]" unless @quiet
    socket = TCPSocket.new 'saucelabs.com', 443
  rescue SystemCallError => e
    raise e unless e.class.name.start_with? 'Errno::'
    $stderr.puts Sauce::Connect.port_not_open_message
    raise TunnelNotPossibleException, "Couldn't use port 443"
  end
end

#error_on_missing_credsObject



47
48
49
50
51
52
53
54
55
# File 'lib/sauce/connect.rb', line 47

def error_on_missing_creds
  if @username.nil?
    raise ArgumentError, "Username required to launch Sauce Connect. Please set the environment variable $SAUCE_USERNAME"
  end

  if @access_key.nil?
    raise ArgumentError, "Access key required to launch Sauce Connect. Please set the environment variable $SAUCE_ACCESS_KEY"
  end
end

#error_on_missing_executableObject



57
58
59
60
61
# File 'lib/sauce/connect.rb', line 57

def error_on_missing_executable
  if @sc4_executable.nil?
    raise TunnelNotPossibleException, Sauce::Connect.plzGetSC4
  end
end

#extract_config(options) ⇒ Object

extract options from the options hash with highest priority over Sauce.config but fall back on Sauce.config otherwise



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sauce/connect.rb', line 27

def extract_config options
  @username = options[:username]
  @access_key = options[:access_key]
  @cli_options = options[:connect_options]
  @sc4_executable = options[:sauce_connect_4_executable]
  @skip_connection_test = options[:skip_connection_test]
  @quiet = options[:quiet]
  @timeout = options.fetch(:timeout) { TIMEOUT }

  unless options.fetch(:skip_sauce_config, false)
    require 'sauce/config'
    @config = Sauce::Config.new(options)
    @username ||= @config.username
    @access_key ||= @config.access_key
    @cli_options ||= @config[:connect_options]
    @sc4_executable ||= @config[:sauce_connect_4_executable]
    @skip_connection_test = @config[:skip_connection_test]  
  end
end

#find_sauce_connectObject

Check whether the path, or it’s bin/sc descendant, exists and is executable



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/sauce/connect.rb', line 176

def find_sauce_connect
  paths = [@sc4_executable, File.join("#{@sc4_executable}", "bin", "sc")]

  sc_path = paths.find do |path|
    path_is_connect_executable? path
  end

  if sc_path.nil?
    raise TunnelNotPossibleException, "No executable found at #{sc_path}, or it can't be executed by #{Process.euid}"
  end

  return File.absolute_path sc_path
end

#path_is_connect_executable?(path) ⇒ Boolean

Returns:

  • (Boolean)


190
191
192
193
# File 'lib/sauce/connect.rb', line 190

def path_is_connect_executable? path
  absolute_path = File.absolute_path path
  return (File.exist? absolute_path) && (File.executable? absolute_path) && !(Dir.exist? absolute_path)
end

#wait_until_readyObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/sauce/connect.rb', line 149

def wait_until_ready
  start = Time.now
  while !@ready and (Time.now-start) < @timeout and @error != "Missing requirements"
    sleep 0.5
  end

  if @error == "Missing requirements"
    raise "Missing requirements"
  end

  if !@ready
    error_message = "Sauce Connect failed to connect after #{@timeout} seconds"
    error_message << "\n(Using Sauce Connect at #{@sc4_executable}"
    raise error_message
  end
end