-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* merge from master * ship:docker
- Loading branch information
Showing
14 changed files
with
306 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,6 @@ development: | |
|
||
test: | ||
domain: test.travis-ci.local | ||
billing: | ||
url: 'http://localhost:9292' | ||
auth_key: 't0Ps3Cr3t' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
require 'travis/addons/handlers/base' | ||
require 'travis/addons/config' | ||
require 'raven' | ||
|
||
module Travis | ||
module Addons | ||
module Handlers | ||
class Billing < Base | ||
EVENTS = ['job:started', 'job:finished', 'job:canceled'].freeze | ||
KEY = :billing | ||
|
||
MSGS = { | ||
failed: 'Failed to push stats to billing-v2: %s' | ||
} | ||
|
||
def handle? | ||
billing_url && billing_auth_key | ||
end | ||
|
||
def handle | ||
publish unless Travis::Hub.context.config.enterprise? | ||
end | ||
|
||
private | ||
|
||
def billing_url | ||
@billing_url ||= Travis::Hub.context.config.billing.url if Travis::Hub.context.config.billing | ||
end | ||
|
||
def billing_auth_key | ||
@billing_auth_key ||= Travis::Hub.context.config.billing.auth_key if Travis::Hub.context.config.billing | ||
end | ||
|
||
def publish | ||
send_usage(data) | ||
rescue => e | ||
logger.error MSGS[:failed] % e.message | ||
end | ||
|
||
def send_usage(data) | ||
logger.info "Hub usage #{data}" | ||
Travis::Sidekiq.billing(data) | ||
end | ||
|
||
def data | ||
@data ||= serialize_data | ||
end | ||
|
||
def serialize_data | ||
{ | ||
job: job_data, | ||
repository: repository_data, | ||
owner: owner_data, | ||
build: build_data | ||
} | ||
end | ||
|
||
def job_data | ||
{ | ||
id: object.id, | ||
os: config['os'] || 'linux', | ||
instance_size: meta(:vm_size) || vm_size, | ||
arch: config['arch'] || 'amd64', | ||
started_at: object.started_at, | ||
finished_at: object.finished_at, | ||
virt_type: config['virt'], | ||
queue: object.queue, | ||
vm_size: vm_size, | ||
finished: finished? | ||
} | ||
end | ||
|
||
def repository_data | ||
{ | ||
id: repository.id, | ||
slug: repository.slug, | ||
private: repository.private | ||
} | ||
end | ||
|
||
def owner_data | ||
{ | ||
type: object.owner_type, | ||
id: object.owner_id, | ||
login: object.owner ? object.owner.login : nil | ||
} | ||
end | ||
|
||
def build_data | ||
{ | ||
id: object.build.id, | ||
type: object.build.event_type, | ||
number: object.build.number, | ||
branch: object.build.branch, | ||
sender: build_data_sender | ||
} | ||
end | ||
|
||
def build_data_sender | ||
{ | ||
id: object.build.sender_id, | ||
type: object.build.sender_type | ||
} | ||
end | ||
|
||
def meta(value) | ||
params[:worker_meta][0][value] if params.has_key?(:worker_meta) && params[:worker_meta].is_a?(Array) && params[:worker_meta].first.respond_to?(:keys) | ||
end | ||
|
||
def vm_size | ||
config.dig('vm', 'size') | ||
end | ||
|
||
def config | ||
@config ||= object.config_id ? JobConfig.find(object.config_id).config : {} | ||
end | ||
|
||
def connection | ||
@connection ||= Faraday.new(url: billing_url, ssl: { ca_path: '/usr/lib/ssl/certs' }) do |conn| | ||
conn.basic_auth '_', billing_auth_key | ||
conn.headers['Content-Type'] = 'application/json' | ||
conn.request :json | ||
conn.response :json | ||
conn.adapter :net_http | ||
end | ||
end | ||
|
||
def handle_usage_executions_response(response) | ||
case response.status | ||
when 404 | ||
raise StandardError, "Not found #{response.body['error'] || response.body}" | ||
when 400 | ||
raise StandardError, "Client error #{response.body['error'] || response.body}" | ||
when 422 | ||
raise StandardError, "Unprocessable entity #{response.body['error'] || response.body}" | ||
else | ||
raise StandardError, "Server error #{response.body['error'] || response.body}" | ||
end | ||
end | ||
|
||
def logger | ||
Addons.logger | ||
end | ||
|
||
def finished? | ||
event != 'job:started' | ||
end | ||
|
||
# EventHandler | ||
class EventHandler < Addons::Instrument | ||
def notify_completed | ||
publish | ||
end | ||
end | ||
EventHandler.attach_to(self) | ||
|
||
class BillingError < StandardError; end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require 'travis/addons/handlers/base' | ||
require 'travis/addons/handlers/task' | ||
|
||
module Travis | ||
module Addons | ||
module Handlers | ||
class Intercom < Base | ||
include Handlers::Task | ||
|
||
EVENTS = /(build):(created|started|restarted)/ | ||
|
||
def handle? | ||
owner_type.downcase == 'user' | ||
end | ||
|
||
def handle | ||
params = { | ||
event: :report_build, | ||
owner_id: owner_id, | ||
last_build_at: last_build_at | ||
} | ||
run_task(:intercom, {}, params) | ||
end | ||
|
||
class Instrument < Addons::Instrument | ||
def notify_completed | ||
publish | ||
end | ||
end | ||
Instrument.attach_to(self) | ||
|
||
private | ||
|
||
def last_build_at | ||
DateTime.now | ||
end | ||
|
||
def owner | ||
object.owner || nil | ||
end | ||
|
||
def owner_id | ||
owner.id.to_s if owner | ||
end | ||
|
||
def owner_type | ||
owner ? owner.class.name : '' | ||
end | ||
|
||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
describe Travis::Addons::Handlers::Billing do | ||
let(:build) { FactoryGirl.create(:build) } | ||
let(:job_config) { FactoryGirl.create(:job_config, repository_id: build.repository_id) } | ||
let(:job) { FactoryGirl.create(:job, owner: owner, config_id: job_config.id) } | ||
let(:owner) { FactoryGirl.create(:user) } | ||
let!(:request) do | ||
stub_request(:put, 'http://localhost:9292/usage/executions') | ||
.to_return(status: 200, body: '', headers: {}) | ||
end | ||
|
||
describe 'handle' do | ||
let(:handler) { described_class.new(event_name, id: job.id) } | ||
|
||
context 'job:finished' do | ||
let(:event_name) { 'job:finished' } | ||
|
||
it 'publishes event to billing' do | ||
::Sidekiq::Client.any_instance.expects(:push).with do |payload| | ||
expect(payload['queue']).to eq('billing') | ||
expect(payload['class']).to eq('Travis::Billing::Worker') | ||
expect(payload['args'][1]).to eq('Travis::Billing::Services::UsageTracker') | ||
end | ||
handler.handle | ||
end | ||
end | ||
|
||
context 'job:canceled' do | ||
let(:event_name) { 'job:canceled' } | ||
|
||
it 'publishes event to billing' do | ||
::Sidekiq::Client.any_instance.expects(:push).with do |payload| | ||
expect(payload['queue']).to eq('billing') | ||
expect(payload['class']).to eq('Travis::Billing::Worker') | ||
expect(payload['args'][1]).to eq('Travis::Billing::Services::UsageTracker') | ||
end | ||
handler.handle | ||
end | ||
end | ||
|
||
context 'job:started' do | ||
let(:event_name) { 'job:started' } | ||
|
||
it 'publishes event to billing' do | ||
::Sidekiq::Client.any_instance.expects(:push).with do |payload| | ||
expect(payload['queue']).to eq('billing') | ||
expect(payload['class']).to eq('Travis::Billing::Worker') | ||
expect(payload['args'][1]).to eq('Travis::Billing::Services::UsageTracker') | ||
end | ||
handler.handle | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.