Skip to content

[M1-US1] Add handling for artifact manager #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: prd-custom-images-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion config/travis.example.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
sentry: &sentry
dsn: https://tok:[email protected]/app_id

production:
domain: travis-ci.local
notifications:
Expand Down Expand Up @@ -48,3 +48,6 @@ test:
billing:
url: 'http://localhost:9292'
auth_key: 't0Ps3Cr3t'
artifact_manager:
url: 'http://artifact_manager:3000'
auth_key: 't0Ps3Cr3t'
1 change: 1 addition & 0 deletions lib/travis/addons/handlers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
require 'travis/addons/handlers/intercom'
require 'travis/addons/handlers/billing'
require 'travis/addons/handlers/job_config'
require 'travis/addons/handlers/artifact_manager'
96 changes: 96 additions & 0 deletions lib/travis/addons/handlers/artifact_manager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
require 'travis/addons/handlers/base'
require 'travis/addons/config'

module Travis
module Addons
module Handlers
class ArtifactManager < Base
EVENTS = ['job:finished', 'job:canceled'].freeze
KEY = :artifact_manager

MSGS = {
failed: 'Failed to push update to artifact-manager: %s'
}

def handle?
artifact_manager_url && artifact_manager_auth_key
end

def handle
publish unless Travis::Hub.context.config.enterprise?
end

private

def artifact_manager_url
@artifact_manager_url ||= Travis::Hub.context.config.artifact_manager.url if Travis::Hub.context.config.artifact_manager
end

def artifact_manager_auth_key
@artifact_manager_auth_key ||= Travis::Hub.context.config.artifact_manager.auth_key if Travis::Hub.context.config.artifact_manager
end

def publish
send_data if image_creation? && failed?
rescue StandardError => e
logger.error MSGS[:failed] % e.message
end

def send_data
owner_type = object.repository.owner_type.downcase
owner_id = object.repository.owner_id
reason = object.state.to_sym == :canceled ? 'job cancel' : 'job error'
result = connection.patch("/image/#{owner_type}/#{owner_id}/#{image_name}", { state: 'error' , reason: })

logger.error "Artifact manager error: #{result.status} #{result.body}" unless result.success?
end

def image_name
config.dig('vm', 'create', 'name')
end

def image_creation?
!image_name.nil?
end

def failed?
[:failed, :errored, :canceled].include?(object.state.to_sym)
end

def config
@config ||= begin
cfg = object.config_id ? ::JobConfig.find(object.config_id).config : {}
cfg.is_a?(String) && cfg.length > 0 ? JSON.parse(cfg) : cfg
end
end

def connection
@connection ||= Faraday.new(url: artifact_manager_url, ssl: { ca_path: '/usr/lib/ssl/certs' }) do |conn|
conn.request :authorization, :basic, '_', artifact_manager_auth_key
conn.headers['Content-Type'] = 'application/json'
conn.headers['X-Travis-User-Id'] = object.build&.sender_id&.to_s
conn.request :json
conn.response :json
conn.adapter :net_http
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)
end
end
end
end
3 changes: 2 additions & 1 deletion lib/travis/hub/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def jwt_key(type)
limit: { resets: { max: 50, after: 6 * 60 * 60 } },
notifications: ['billing'],
auth: { jwt_private_key: jwt_key(:private), jwt_public_key: jwt_key(:public), http_basic_auth: },
billing: { url: ENV['BILLING_URL'] || 'http://localhost:9292', auth_key: ENV['BILLING_AUTH_KEY'] || 't0Ps3Cr3t' }
billing: { url: ENV['BILLING_URL'] || 'http://localhost:9292', auth_key: ENV['BILLING_AUTH_KEY'] || 't0Ps3Cr3t' },
artifact_manager: { url: ENV['ARTIFACT_MANAGER_URL'] || 'http://artifact_manager:3000', auth_key: ENV['ARTIFACT_MANAGER_AUTH_KEY'] || 't0Ps3Cr3t' }

def metrics
# TODO: cleanup keychain?
Expand Down
2 changes: 1 addition & 1 deletion lib/travis/hub/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def addons
# TODO: move keen to the keychain? it isn't required on enterprise.
# then again, it's not active, unless the keen credentials are
# present in the env.
addons = config.notifications.flatten + %w[insights logsearch scheduler keenio metrics]
addons = config.notifications.flatten + %w[insights logsearch scheduler keenio metrics artifact_manager]
addons << 'merge' if ENV['NOTIFY_MERGE']
addons
end
Expand Down