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
|
|
|
|
2020-12-18 13:14:30 +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]
|
2021-01-06 14:45:34 +08:00
|
|
|
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-09-23 11:40:13 +08:00
|
|
|
|
2020-07-10 14:08:16 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
2020-09-10 16:01:24 +08:00
|
|
|
@cloud_account = bind_account!
|
2020-09-03 15:52:54 +08:00
|
|
|
if @cloud_account.blank?
|
2022-05-30 10:25:43 +08:00
|
|
|
tip_exception('激活失败, 请检查你的云服务器信息是否正确.')
|
2020-09-03 15:52:54 +08:00
|
|
|
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)
|
2020-09-03 15:52:54 +08:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
rescue Exception => ex
|
2022-05-30 10:25:43 +08:00
|
|
|
tip_exception(ex.message)
|
2020-09-03 15:52:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def activate
|
2022-05-30 10:25:43 +08:00
|
|
|
return tip_exception('请先认证') unless current_user.ci_certification?
|
2020-09-03 15:52:54 +08:00
|
|
|
|
|
|
|
|
begin
|
2020-09-10 18:00:37 +08:00
|
|
|
@cloud_account = Ci::CloudAccount.find params[:id]
|
2020-09-09 09:57:44 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
2020-09-10 18:00:37 +08:00
|
|
|
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)
|
2020-09-10 18:00:37 +08:00
|
|
|
else
|
2020-09-18 15:26:14 +08:00
|
|
|
@repo = Ci::Repo.auto_create!(@ci_user, @project)
|
2020-09-10 18:00:37 +08:00
|
|
|
@user.update_column(:user_syncing, false)
|
|
|
|
|
end
|
|
|
|
|
|
2020-09-11 09:55:21 +08:00
|
|
|
result = bind_hook!(current_user, @cloud_account, @repo)
|
2020-09-09 09:57:44 +08:00
|
|
|
@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)
|
2020-09-09 09:57:44 +08:00
|
|
|
end
|
2020-09-03 15:52:54 +08:00
|
|
|
render_ok
|
|
|
|
|
rescue Exception => ex
|
2022-05-30 10:25:43 +08:00
|
|
|
tip_exception(ex.message)
|
2020-09-03 15:52:54 +08:00
|
|
|
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
|
|
|
|
2020-09-03 15:52:54 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
2020-09-10 16:01:24 +08:00
|
|
|
@cloud_account = bind_account!
|
2020-12-18 13:14:30 +08:00
|
|
|
if @cloud_account.blank?
|
2022-05-30 10:25:43 +08:00
|
|
|
tip_exception('激活失败, 请检查你的云服务器信息是否正确.')
|
2020-12-18 13:14:30 +08:00
|
|
|
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)
|
2020-12-18 13:14:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def trustie_bind
|
|
|
|
|
account = params[:account].to_s
|
2022-05-30 10:25:43 +08:00
|
|
|
return tip_exception("account不能为空.") if account.blank?
|
2020-12-18 13:14:30 +08:00
|
|
|
|
|
|
|
|
flag, msg = check_trustie_bind_cloud_account!
|
2022-05-30 10:25:43 +08:00
|
|
|
return tip_exception(msg) if flag === true
|
2020-12-18 13:14:30 +08:00
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
|
@cloud_account = trustie_bind_account!
|
2020-09-03 15:52:54 +08:00
|
|
|
if @cloud_account.blank?
|
2022-05-30 10:25:43 +08:00
|
|
|
tip_exception('激活失败, 请检查你的云服务器信息是否正确.')
|
2020-09-03 15:52:54 +08:00
|
|
|
raise ActiveRecord::Rollback
|
2020-07-15 10:22:41 +08:00
|
|
|
else
|
2020-09-03 15:52:54 +08:00
|
|
|
current_user.set_drone_step!(User::DEVOPS_UNVERIFIED)
|
2020-07-15 10:22:41 +08:00
|
|
|
end
|
2020-09-03 15:52:54 +08:00
|
|
|
end
|
|
|
|
|
rescue Exception => ex
|
2022-05-30 10:25:43 +08:00
|
|
|
tip_exception(ex.message)
|
2020-09-03 15:52:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def unbind
|
|
|
|
|
ActiveRecord::Base.transaction do
|
2021-01-06 16:45:18 +08:00
|
|
|
if current_user.ci_cloud_account.server_type == Ci::CloudAccount::SERVER_TYPE_TRUSTIE
|
|
|
|
|
if @repos
|
|
|
|
|
@repos.each do |repo|
|
|
|
|
|
repo.deactivate!
|
|
|
|
|
end
|
2021-01-06 14:45:34 +08:00
|
|
|
end
|
|
|
|
|
end
|
2021-01-06 16:45:18 +08:00
|
|
|
unbind_account!
|
2020-09-03 15:52:54 +08:00
|
|
|
render_ok
|
|
|
|
|
end
|
|
|
|
|
rescue Exception => ex
|
2022-05-30 10:25:43 +08:00
|
|
|
tip_exception(ex.message)
|
2020-09-03 15:52:54 +08:00
|
|
|
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
|
2020-09-03 15:52:54 +08:00
|
|
|
|
|
|
|
|
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
|