Skip to content
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

TAM implementation #817

Open
wants to merge 7 commits into
base: master
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
1 change: 1 addition & 0 deletions lib/travis/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module CLI
autoload :Sshkey, 'travis/cli/sshkey'
autoload :Status, 'travis/cli/status'
autoload :Sync, 'travis/cli/sync'
autoload :Tam, 'travis/cli/tam'
autoload :Version, 'travis/cli/version'
autoload :Whatsup, 'travis/cli/whatsup'
autoload :Whoami, 'travis/cli/whoami'
Expand Down
101 changes: 101 additions & 0 deletions lib/travis/cli/tam.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
require 'travis/cli'

module Travis
module CLI
class Tam < ApiCommand
description 'TAM (Travis Artifact Manager) actions'

on('-c', '--create-image', 'Create an image from .travis.lxd.yml')
on('-u', '--update-image', 'Update the image based on .travis.lxd.yml')
on('-d', '--delete-image IMAGE_NAME', 'Delete the image with the given name')
on('-i', '--image-info IMAGE_NAME', 'Get info about the image')
on('-l', '--logs IMAGE_NAME', 'Get the latest build log for the image')
on('-s', '--build-status IMAGE_NAME', 'Get the latest build status for the image')

def run
error('Please specify an action') if !create_image? && !update_image? && !delete_image? && !image_info? && !logs? && !build_status?
error('.travis.lxd.yml file not found in the current directory or is empty') if (create_image? || update_image?) && (!File.exist?('.travis.lxd.yml') || File.read('.travis.lxd.yml').empty?)

authenticate

endpoint = if create_image?
'v3/artifacts/config/create'
elsif update_image?
'v3/artifacts/config/update'
elsif delete_image?
"v3/artifacts/#{CGI.escape(delete_image)}"
elsif image_info?
"v3/artifacts/#{CGI.escape(image_info)}/info"
elsif logs?
"v3/artifacts/#{CGI.escape(logs)}/logs"
else
"v3/artifacts/#{CGI.escape(build_status)}/build_status"
end

if delete_image?
session.delete_raw(endpoint)
elsif create_image? || update_image?
params = {
config: File.read('.travis.lxd.yml')
}

begin
response = session.post_raw(endpoint, JSON.dump(params), 'Content-Type' => 'application/json')
unless response['warnings'].empty?
warn color('Following warnings were generated:', [:bold, 'yellow'])
response['warnings'].each { |warning| warn color(warning, 'yellow') }
end
rescue Travis::Client::ValidationFailed => e
error e.message
return
end
else
data = nil
begin
data = session.get_raw(endpoint)
rescue Travis::Client::ValidationFailed => e
error 'Failed to fetch build status'
return
end
if image_info?
image_information = Travis::Client::ArtifactsImageInfo.new(session, 0)
image_information.update_attributes(data)
end
end

if create_image?
say 'Image created'
return
end

if update_image?
say 'Image updated'
return
end

if delete_image?
warn 'Image deleted!'
return
end

if image_info?
say "Name: #{image_information.name}"
say "Size: #{(image_information.image_size.to_f / 1024**2).round(2)}MB"
say "Description: #{image_information.description.presence || '<no description>'}"
return
end

if logs?
say data['log']
return
end

if build_status?
say data['status']

return
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/travis/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require 'travis/client/request'
require 'travis/client/listener'
require 'travis/client/lint_result'
require 'travis/client/artifacts_image_info'

module Travis
module Client
Expand Down
15 changes: 15 additions & 0 deletions lib/travis/client/artifacts_image_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'travis/client'

module Travis
module Client
class ArtifactsImageInfo < Entity
preloadable

attributes :name, :config_content, :description, :image_size
inspect_info :name

one :artifacts_image_info
many :artifacts_image_infos
end
end
end
2 changes: 2 additions & 0 deletions lib/travis/client/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def parse_message(message)
if @errors = response['errors'] and @errors.any?
readable = @errors.map { |e| "#{e['field']}: #{e['code'].gsub('_', ' ')}" }
message += " (#{readable.join(', ')})"
elsif @errors = response['error_message']
message = response['error_message']
end
message
rescue JSON::ParserError
Expand Down
2 changes: 1 addition & 1 deletion lib/travis/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Travis
VERSION = '1.11.0'
VERSION = '1.12.0'
end
4 changes: 3 additions & 1 deletion travis.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Gem::Specification.new do |s|
# general info
s.name = "travis"
s.version = "1.11.0"
s.version = "1.12.0"
s.required_ruby_version = ">= 2.3.0"
s.description = "CLI and Ruby client library for Travis CI"
s.homepage = "https://github.com/travis-ci/travis.rb"
Expand Down Expand Up @@ -325,13 +325,15 @@ Gem::Specification.new do |s|
"lib/travis/cli/sshkey.rb",
"lib/travis/cli/status.rb",
"lib/travis/cli/sync.rb",
"lib/travis/cli/tam.rb",
"lib/travis/cli/token.rb",
"lib/travis/cli/version.rb",
"lib/travis/cli/whatsup.rb",
"lib/travis/cli/whoami.rb",
"lib/travis/client.rb",
"lib/travis/client/account.rb",
"lib/travis/client/artifact.rb",
"lib/travis/client/artifacts_image_info.rb",
"lib/travis/client/auto_login.rb",
"lib/travis/client/broadcast.rb",
"lib/travis/client/build.rb",
Expand Down