forgeplus/app/controllers/ci/cloud_accounts_controller.rb

130 lines
3.9 KiB
Ruby
Raw Normal View History

2020-08-13 23:38:32 +08:00
class Ci::CloudAccountsController < Ci::BaseController
2020-09-10 16:01:24 +08:00
include Ci::CloudAccountManageable
2020-08-06 14:26:19 +08:00
skip_before_action :connect_to_ci_db, only: %i[create bind trustie_bind]
2020-09-03 17:05:38 +08:00
before_action :load_project, only: %i[create activate]
2020-10-16 11:57:20 +08:00
before_action :authorize_owner!, only: %i[create activate]
2020-08-26 16:23:59 +08:00
before_action :load_repo, only: %i[activate]
before_action :load_all_repo, only: %i[unbind]
2020-10-13 19:49:52 +08:00
before_action :find_cloud_account, only: %i[show oauth_grant]
2020-09-10 16:01:24 +08:00
before_action :validate_params!, only: %i[create bind]
2020-09-22 11:59:33 +08:00
before_action only: %i[create bind] do
2020-09-22 15:50:16 +08:00
connect_to_ci_database(master_db: true)
2020-09-22 11:59:33 +08:00
end
2020-07-10 14:08:16 +08:00
def create
2020-09-23 16:22:18 +08:00
flag, msg = check_bind_cloud_account!
2022-05-30 10:25:43 +08:00
return tip_exception(msg) if flag === true
2020-07-10 14:08:16 +08:00
ActiveRecord::Base.transaction do
2020-09-10 16:01:24 +08:00
@cloud_account = bind_account!
if @cloud_account.blank?
2022-05-30 10:25:43 +08:00
tip_exception('激活失败, 请检查你的云服务器信息是否正确.')
raise ActiveRecord::Rollback
else
current_user.set_drone_step!(User::DEVOPS_UNVERIFIED)
2020-09-04 17:35:29 +08:00
render_ok(redirect_url: @cloud_account.authenticate_url)
end
end
rescue Exception => ex
2022-05-30 10:25:43 +08:00
tip_exception(ex.message)
end
def activate
2022-05-30 10:25:43 +08:00
return tip_exception('请先认证') unless current_user.ci_certification?
begin
@cloud_account = Ci::CloudAccount.find params[:id]
ActiveRecord::Base.transaction do
if @repo
2022-05-30 10:25:43 +08:00
return tip_exception('该项目已经激活') if @repo.repo_active?
2021-01-26 17:15:53 +08:00
@repo.activate!(@project)
else
2020-09-18 15:26:14 +08:00
@repo = Ci::Repo.auto_create!(@ci_user, @project)
@user.update_column(:user_syncing, false)
end
2020-09-11 09:55:21 +08:00
result = bind_hook!(current_user, @cloud_account, @repo)
@project.update_columns(open_devops: true, gitea_webhook_id: result['id'])
2020-09-18 15:26:14 +08:00
@cloud_account.update_column(:ci_user_id, @ci_user.user_id)
end
render_ok
rescue Exception => ex
2022-05-30 10:25:43 +08:00
tip_exception(ex.message)
end
end
def show
end
def bind
2020-09-23 16:22:18 +08:00
flag, msg = check_bind_cloud_account!
2022-05-30 10:25:43 +08:00
return tip_exception(msg) if flag === true
2020-09-23 16:22:18 +08:00
ActiveRecord::Base.transaction do
2020-09-10 16:01:24 +08:00
@cloud_account = bind_account!
if @cloud_account.blank?
2022-05-30 10:25:43 +08:00
tip_exception('激活失败, 请检查你的云服务器信息是否正确.')
raise ActiveRecord::Rollback
else
current_user.set_drone_step!(User::DEVOPS_UNVERIFIED)
end
end
rescue Exception => ex
2022-05-30 10:25:43 +08:00
tip_exception(ex.message)
end
def trustie_bind
account = params[:account].to_s
2022-05-30 10:25:43 +08:00
return tip_exception("account不能为空.") if account.blank?
flag, msg = check_trustie_bind_cloud_account!
2022-05-30 10:25:43 +08:00
return tip_exception(msg) if flag === true
ActiveRecord::Base.transaction do
@cloud_account = trustie_bind_account!
if @cloud_account.blank?
2022-05-30 10:25:43 +08:00
tip_exception('激活失败, 请检查你的云服务器信息是否正确.')
raise ActiveRecord::Rollback
2020-07-15 10:22:41 +08:00
else
current_user.set_drone_step!(User::DEVOPS_UNVERIFIED)
2020-07-15 10:22:41 +08:00
end
end
rescue Exception => ex
2022-05-30 10:25:43 +08:00
tip_exception(ex.message)
end
def unbind
ActiveRecord::Base.transaction do
if current_user.ci_cloud_account.server_type == Ci::CloudAccount::SERVER_TYPE_TRUSTIE
if @repos
@repos.each do |repo|
repo.deactivate!
end
end
end
unbind_account!
render_ok
end
rescue Exception => ex
2022-05-30 10:25:43 +08:00
tip_exception(ex.message)
end
2020-10-13 19:49:52 +08:00
def oauth_grant
2020-10-15 19:49:43 +08:00
password = params[:password].to_s
2022-05-30 10:25:43 +08:00
return tip_exception('你输入的密码不正确.') unless current_user.check_password?(password)
2020-10-13 19:49:52 +08:00
2020-10-15 17:00:02 +08:00
oauth = current_user.oauths.last
2022-05-30 10:25:43 +08:00
return tip_exception("服务器出小差了.") if oauth.blank?
2020-10-15 17:00:02 +08:00
result = gitea_oauth_grant!(password, oauth)
2022-05-30 10:25:43 +08:00
return tip_exception('授权失败.') unless result === true
2020-10-15 19:40:50 +08:00
current_user.set_drone_step!(User::DEVOPS_CERTIFICATION)
2020-10-13 19:49:52 +08:00
end
private
2020-09-10 16:01:24 +08:00
def validate_params!
Ci::CreateCloudAccountForm.new(devops_params).validate!
2020-09-04 15:46:07 +08:00
end
2020-07-10 14:08:16 +08:00
end