ADD devops builds's api
This commit is contained in:
parent
8f96341679
commit
006ff7fd62
|
@ -1,6 +1,6 @@
|
||||||
class DevOps::BuildsController < ApplicationController
|
class ::DevOps::BuildsController < ApplicationController
|
||||||
before_action :require_login
|
before_action :require_login
|
||||||
before_action :find_project
|
before_action :find_repo
|
||||||
|
|
||||||
def index
|
def index
|
||||||
cloud_account = @repo.dev_ops_cloud_account
|
cloud_account = @repo.dev_ops_cloud_account
|
||||||
|
@ -37,7 +37,7 @@ class DevOps::BuildsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def find_project
|
def find_repo
|
||||||
@repo = Repository.find params[:id]
|
@repo = ::Repository.find params[:id]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,12 +14,12 @@ class DevOps::CloudAccountsController < ApplicationController
|
||||||
logger.info "######### create_params: #{create_params}"
|
logger.info "######### create_params: #{create_params}"
|
||||||
|
|
||||||
|
|
||||||
if cloud_account = @project.dev_ops_cloud_account
|
if cloud_account = @repo.dev_ops_cloud_account
|
||||||
cloud_account
|
cloud_account
|
||||||
else
|
else
|
||||||
cloud_account = DevOps::CloudAccount.new(create_params)
|
cloud_account = DevOps::CloudAccount.new(create_params)
|
||||||
cloud_account.user = current_user
|
cloud_account.user = current_user
|
||||||
cloud_account.project_id = @project.id
|
cloud_account.repo_id = @repo.id
|
||||||
cloud_account.save!
|
cloud_account.save!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -64,11 +64,10 @@ class DevOps::CloudAccountsController < ApplicationController
|
||||||
|
|
||||||
private
|
private
|
||||||
def devops_params
|
def devops_params
|
||||||
params.permit(:account, :secret, :ip_num, :project_id)
|
params.permit(:account, :secret, :ip_num, :repo_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_project
|
def find_project
|
||||||
@project = Project.find_by_id params[:project_id]
|
@repo = Repository.find params[:repo_id]
|
||||||
render_not_found("未找到project_id为:#{params[:project_id]}相关的项目") if @project.blank?
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -24,22 +24,22 @@
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(endpoint, path, options={})
|
def get(endpoint, path, options={})
|
||||||
set_request_defaults(endpoint)
|
validate_request_params!(endpoint)
|
||||||
request(:get, endpoint, path, options)
|
request(:get, endpoint, path, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def post(endpoint, path, options={})
|
def post(endpoint, path, options={})
|
||||||
set_request_defaults(endpoint)
|
validate_request_params!(endpoint)
|
||||||
request(:post, endpoint, path, options)
|
request(:post, endpoint, path, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def put(endpoint, path, options={})
|
def put(endpoint, path, options={})
|
||||||
set_request_defaults(endpoint)
|
validate_request_params!(endpoint)
|
||||||
request(:put, endpoint, path, options)
|
request(:put, endpoint, path, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete(endpoint, path, options={})
|
def delete(endpoint, path, options={})
|
||||||
set_request_defaults(endpoint)
|
validate_request_params!(endpoint)
|
||||||
request(:delete, endpoint, path, options)
|
request(:delete, endpoint, path, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -47,27 +47,10 @@
|
||||||
def request(method, endpoint, path, **params)
|
def request(method, endpoint, path, **params)
|
||||||
Rails.logger.info("[drone] request: #{method} #{path} #{params.except(:secret).inspect}")
|
Rails.logger.info("[drone] request: #{method} #{path} #{params.except(:secret).inspect}")
|
||||||
|
|
||||||
client = Faraday.new(path: domain)
|
client = Faraday.new(url: endpoint)
|
||||||
response = client.public_send(method, path, params)
|
response = client.public_send(method, path, params)
|
||||||
result = JSON.parse(response.body)
|
|
||||||
|
|
||||||
Rails.logger.info("[drone] response:#{response.status} #{result.inspect}")
|
json_response(response)
|
||||||
|
|
||||||
if response.status != 200
|
|
||||||
raise DevOps::Drone::Error.parse(result)
|
|
||||||
end
|
|
||||||
|
|
||||||
if result['errcode'].present? && result['errcode'].to_i.nonzero?
|
|
||||||
raise DevOps::Drone::Error.parse(result)
|
|
||||||
end
|
|
||||||
|
|
||||||
result
|
|
||||||
end
|
|
||||||
|
|
||||||
# Sets a base_uri and default_params for requests.
|
|
||||||
# @raise [Error::MissingCredentials] if endpoint not set.
|
|
||||||
def set_request_defaults(endpoint, private_token, sudo=nil)
|
|
||||||
raise "Please set an endpoint to API" unless endpoint
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Checks the response code for common errors.
|
# Checks the response code for common errors.
|
||||||
|
@ -89,8 +72,21 @@
|
||||||
response.parsed_response
|
response.parsed_response
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Checks a base_uri and params for requests.
|
||||||
|
def validate_request_params!(endpoint)
|
||||||
|
raise "Please set an endpoint to API" unless endpoint
|
||||||
|
end
|
||||||
|
|
||||||
def error_message(response)
|
def error_message(response)
|
||||||
"Server responded with code #{response.code}, message: #{response.parsed_response.message}. " \
|
"Server responded with code #{response.code}, message: #{response.parsed_response.message}. " \
|
||||||
"Request URI: #{response.request.base_uri}#{response.request.path}"
|
"Request URI: #{response.request.base_uri}#{response.request.path}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def json_response(response)
|
||||||
|
result = JSON.parse(response.body)
|
||||||
|
status = response.status
|
||||||
|
Rails.logger.info("[drone] response:#{status} #{result.inspect}")
|
||||||
|
|
||||||
|
response.status != 200 ? result.merge!(status: response.status) : result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
class DevOps::CloudAccount < ApplicationRecord
|
class DevOps::CloudAccount < ApplicationRecord
|
||||||
belongs_to :project
|
belongs_to :project
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :repository
|
belongs_to :repository, foreign_key: :repo_id
|
||||||
|
|
||||||
def drone_host
|
def drone_host
|
||||||
[drone_ip, ":80"].join
|
[drone_ip, ":80"].join
|
||||||
|
|
|
@ -3,7 +3,7 @@ class Repository < ApplicationRecord
|
||||||
belongs_to :project, :touch => true
|
belongs_to :project, :touch => true
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
has_one :mirror, foreign_key: :repo_id
|
has_one :mirror, foreign_key: :repo_id
|
||||||
has_one :dev_ops_cloud_account, foreign_key: :repo_id
|
has_one :dev_ops_cloud_account, class_name: 'DevOps::CloudAccount', foreign_key: :repo_id
|
||||||
has_many :version_releases, dependent: :destroy
|
has_many :version_releases, dependent: :destroy
|
||||||
|
|
||||||
validates :identifier, presence: true
|
validates :identifier, presence: true
|
||||||
|
|
|
@ -22,13 +22,12 @@ Rails.application.routes.draw do
|
||||||
get :common
|
get :common
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
resources :builds, only: :index do
|
resources :builds, only: :index do
|
||||||
collection do
|
collection do
|
||||||
get ':number', to: 'builds#detail', as: 'detail'
|
get ':number', to: 'builds#detail', as: 'detail'
|
||||||
get ':number/logs/:stage/:step', to: 'builds#detail', as: 'logs'
|
|
||||||
post ':number', to: 'builds#restart', as: 'restart'
|
post ':number', to: 'builds#restart', as: 'restart'
|
||||||
delete ':number', to: 'builds#delete', as: 'delete'
|
delete ':number', to: 'builds#delete', as: 'delete'
|
||||||
|
get ':number/logs/:stage/:step', to: 'builds#detail', as: 'logs'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue