Class: Maze::Client::BitBarClientUtils
- Inherits:
-
Object
- Object
- Maze::Client::BitBarClientUtils
- 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
-
.account_credentials(tms_uri) ⇒ Object
Requests an unused account id from the test-management-service.
-
.release_account(tms_uri) ⇒ Object
Informs the test-management-service that in-use account id is no longer in use.
-
.request_account_index(tms_uri) ⇒ Object
Makes the HTTP call to acquire an account id.
-
.start_local_tunnel(sb_local, username, access_key) ⇒ Object
Starts the BitBar local tunnel.
-
.stop_local_tunnel ⇒ Object
Stops the local tunnel.
-
.upload_app(api_key, app, app_id_file = nil) ⇒ Object
Uploads an app to BitBar for later consumption.
- .use_local_tunnel? ⇒ Boolean
Class Method Details
.account_credentials(tms_uri) ⇒ Object
Requests an unused account id from 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 account_credentials(tms_uri) interval_seconds = 10 credentials = Maze::Wait.new(interval: interval_seconds, timeout: 1800).until do output = request_account_index(tms_uri) case output.code when '200' body = JSON.parse(output.body, {symbolize_names: true}) @account_id = account_id = body[:id] $logger.info "Using account #{account_id}, expiring at #{body[:expiry]}" { username: ENV["#{BB_USER_PREFIX}#{account_id}"], access_key: ENV["#{BB_KEY_PREFIX}#{account_id}"] } 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
116 117 118 119 120 121 122 123 124 |
# File 'lib/maze/client/bb_client_utils.rb', line 116 def release_account(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
104 105 106 107 108 109 110 111 112 |
# File 'lib/maze/client/bb_client_utils.rb', line 104 def request_account_index(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
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_tunnel ⇒ Object
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
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 = Maze::Helper.(app) $logger.info "Uploading 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(, '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.(app_id_file), app_uuid) end app_uuid end |