Class: Maze::Client::BitBarClientUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/client/bb_client_utils.rb

Constant Summary collapse

BB_READY_FILE =
'bb.ready'
BB_KILL_FILE =
'bb.kill'
BB_USER_PREFIX =
'BB_USER_'
BB_KEY_PREFIX =
'BB_KEY_'

Class Method Summary collapse

Class Method Details

.account_credentials(tms_uri) ⇒ Object

Requests an unused account id from the test-management-service

Parameters:

  • tms_uri (String)

    The URI of the test-management-service



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/maze/client/bb_client_utils.rb', line 70

def (tms_uri)
  interval_seconds = 10

  credentials = Maze::Wait.new(interval: interval_seconds, timeout: 1800).until do
    output = (tms_uri)
    case output.code
    when '200'
      body = JSON.parse(output.body, {symbolize_names: true})
      @account_id =  = body[:id]
      $logger.info "Using account #{}, expiring at #{body[:expiry]}"
      {
        username: ENV["#{BB_USER_PREFIX}#{}"],
        access_key: ENV["#{BB_KEY_PREFIX}#{}"]
      }
    when '409'
      # All accounts are in use, wait for one to become available
      $logger.info("All accounts are currently in use, retrying in #{interval_seconds}s")
      false
    else
      # Something has gone wrong, throw an error
      $logger.error "Unexpected status code (#{output.code}) received from test-management server: #{output.body}"
      raise
    end
  end

  return credentials if credentials

  raise "Could not fetch BitBar credentials in time"
end

.release_account(tms_uri) ⇒ Object

Informs the test-management-service that in-use account id is no longer in use

Parameters:

  • tms_uri (String)

    The URI of the test-management-service



116
117
118
119
120
121
122
123
124
# File 'lib/maze/client/bb_client_utils.rb', line 116

def (tms_uri)
  $logger.info "Release account #{@account_id}"
  uri = URI("#{tms_uri}/account/release?account_id=#{@account_id}")
  request = Net::HTTP::Get.new(uri)
  request['Authorization'] = Maze.config.tms_token
  res = Net::HTTP.start(uri.hostname, uri.port) do |http|
    http.request(request)
  end
end

.request_account_index(tms_uri) ⇒ Object

Makes the HTTP call to acquire an account id

Parameters:

  • tms_uri (String)

    The URI of the test-management-service



104
105
106
107
108
109
110
111
112
# File 'lib/maze/client/bb_client_utils.rb', line 104

def (tms_uri)
  uri = URI("#{tms_uri}/account/request")
  request = Net::HTTP::Get.new(uri)
  request['Authorization'] = Maze.config.tms_token
  res = Net::HTTP.start(uri.hostname, uri.port) do |http|
    http.request(request)
  end
  res
end

.start_local_tunnel(sb_local, username, access_key) ⇒ Object

Starts the BitBar local tunnel

Parameters:

  • sb_local (String)

    path to the SBSecureTunnel binary

  • username (String)

    Username to start the tunnel with

  • access_key (String)

    access key



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/maze/client/bb_client_utils.rb', line 135

def start_local_tunnel(sb_local, username, access_key)
  # Make sure the ready/kill files are already deleted
  File.delete(BB_READY_FILE) if File.exist?(BB_READY_FILE)
  File.delete(BB_KILL_FILE) if File.exist?(BB_KILL_FILE)

  $logger.info 'Starting SBSecureTunnel'
  command = "#{sb_local} --username #{username} --authkey #{access_key} --acceptAllCerts " \
          "--ready #{BB_READY_FILE} --kill #{BB_KILL_FILE}"

  output = start_tunnel_thread(command)

  success = Maze::Wait.new(timeout: 30).until do
    File.exist?(BB_READY_FILE)
  end
  unless success
    $logger.error "Failed: #{output}"
  end
end

.stop_local_tunnelObject

Stops the local tunnel



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/maze/client/bb_client_utils.rb', line 155

def stop_local_tunnel
  FileUtils.touch(BB_KILL_FILE)

  success = Maze::Wait.new(timeout: 30).until do
    @tunnel_thread.nil? || !@tunnel_thread.alive?
  end

  if success
    $logger.info("Shutdown SBSecureTunnel")
  else
    $logger.error("Failed to shutdown SBSecureTunnel!")
  end

  @tunnel_thread = nil
  File.delete(BB_READY_FILE) if File.exist?(BB_READY_FILE)
  File.delete(BB_KILL_FILE) if File.exist?(BB_KILL_FILE)
end

.upload_app(api_key, app, app_id_file = nil) ⇒ Object

Uploads an app to BitBar for later consumption

Parameters:

  • api_key (String)

    The BitBar API key

  • app (String)

    A path to the application file

  • app_id_file (String) (defaults to: nil)

    the file to write the uploaded app url to BitBar



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/maze/client/bb_client_utils.rb', line 21

def upload_app(api_key, app, app_id_file=nil)
  uuid_regex = /\A[0-9]+\z/

  if uuid_regex.match? app
    $logger.info "Using pre-uploaded app with ID #{app}"
    app_uuid = app
  else
    expanded_app = Maze::Helper.expand_path(app)
    $logger.info "Uploading app: #{expanded_app}"

    # Upload the app to BitBar
    uri = URI('https://cloud.bitbar.com/api/me/files')
    request = Net::HTTP::Post.new(uri)
    request.basic_auth(api_key, '')
    request.set_form({ 'file' => File.new(expanded_app, 'rb') }, 'multipart/form-data')

    res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
      http.request(request)
    end

    # Pull the UUID from the response
    begin
      response = JSON.parse res.body
      if response.key?('id')
        app_uuid = response['id'].to_s
        $logger.info "Uploaded app ID: #{app_uuid}"
        $logger.info 'You can use this ID to avoid uploading the same app more than once.'
      else
        $logger.error "Unexpected response body: #{response}"
        raise 'App upload failed'
      end
    rescue JSON::ParserError
      $logger.error "Expected JSON response, received: #{res}"
      raise
    end
  end

  unless app_id_file.nil?
    $logger.info "Writing uploaded app id to #{app_id_file}"
    File.write(Maze::Helper.expand_path(app_id_file), app_uuid)
  end

  app_uuid
end

.use_local_tunnel?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/maze/client/bb_client_utils.rb', line 126

def use_local_tunnel?
  Maze.config.start_tunnel && !Maze.config.aws_public_ip
end