|
|
|
@ -14,34 +14,39 @@ class CreateShixunService < ApplicationService
|
|
|
|
|
shixun.user_id = user.id
|
|
|
|
|
main_mirror = MirrorRepository.find params[:main_type]
|
|
|
|
|
sub_mirrors = MirrorRepository.where(id: params[:sub_type])
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
|
shixun.save!
|
|
|
|
|
# 获取脚本内容
|
|
|
|
|
shixun_script = get_shixun_script(shixun, main_mirror, sub_mirrors)
|
|
|
|
|
# 创建额外信息
|
|
|
|
|
ShixunInfo.create!(shixun_id: shixun.id, evaluate_script: shixun_script, description: params[:description])
|
|
|
|
|
# 创建合作者
|
|
|
|
|
shixun.shixun_members.create!(user_id: user.id, role: 1)
|
|
|
|
|
# 创建镜像
|
|
|
|
|
ShixunMirrorRepository.create!(:shixun_id => shixun.id, :mirror_repository_id => main_mirror.id)
|
|
|
|
|
# 创建主服务配置
|
|
|
|
|
ShixunServiceConfig.create!(:shixun_id => shixun.id, :mirror_repository_id => main_mirror.id)
|
|
|
|
|
# 创建子镜像相关数据(实训镜像关联表,子镜像服务配置)
|
|
|
|
|
sub_mirrors.each do |sub|
|
|
|
|
|
ShixunMirrorRepository.create!(:shixun_id => shixun.id, :mirror_repository_id => sub.id)
|
|
|
|
|
# 实训子镜像服务配置
|
|
|
|
|
name = sub.name #查看镜像是否有名称,如果没有名称就不用服务配置
|
|
|
|
|
ShixunServiceConfig.create!(:shixun_id => shixun.id, :mirror_repository_id => sub.id) if name.present?
|
|
|
|
|
end
|
|
|
|
|
# 创建版本库
|
|
|
|
|
repo_path = repo_namespace(user.login, shixun.identifier)
|
|
|
|
|
GitService.add_repository(repo_path: repo_path)
|
|
|
|
|
shixun.update_column(:repo_name, repo_path.split(".")[0])
|
|
|
|
|
# 如果是云上实验室,创建相关记录
|
|
|
|
|
if !Laboratory.current.main_site?
|
|
|
|
|
Laboratory.current.laboratory_shixuns.create!(shixun: shixun, ownership: true)
|
|
|
|
|
begin
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
|
shixun.save!
|
|
|
|
|
# 获取脚本内容
|
|
|
|
|
shixun_script = get_shixun_script(shixun, main_mirror, sub_mirrors)
|
|
|
|
|
# 创建额外信息
|
|
|
|
|
ShixunInfo.create!(shixun_id: shixun.id, evaluate_script: shixun_script, description: params[:description])
|
|
|
|
|
# 创建合作者
|
|
|
|
|
shixun.shixun_members.create!(user_id: user.id, role: 1)
|
|
|
|
|
# 创建镜像
|
|
|
|
|
ShixunMirrorRepository.create!(:shixun_id => shixun.id, :mirror_repository_id => main_mirror.id)
|
|
|
|
|
# 创建主服务配置
|
|
|
|
|
ShixunServiceConfig.create!(:shixun_id => shixun.id, :mirror_repository_id => main_mirror.id)
|
|
|
|
|
# 创建子镜像相关数据(实训镜像关联表,子镜像服务配置)
|
|
|
|
|
sub_mirrors.each do |sub|
|
|
|
|
|
ShixunMirrorRepository.create!(:shixun_id => shixun.id, :mirror_repository_id => sub.id)
|
|
|
|
|
# 实训子镜像服务配置
|
|
|
|
|
name = sub.name #查看镜像是否有名称,如果没有名称就不用服务配置
|
|
|
|
|
ShixunServiceConfig.create!(:shixun_id => shixun.id, :mirror_repository_id => sub.id) if name.present?
|
|
|
|
|
end
|
|
|
|
|
# 创建版本库
|
|
|
|
|
repo_path = repo_namespace(user.login, shixun.identifier)
|
|
|
|
|
GitService.add_repository(repo_path: repo_path)
|
|
|
|
|
shixun.update_column(:repo_name, repo_path.split(".")[0])
|
|
|
|
|
# 如果是云上实验室,创建相关记录
|
|
|
|
|
if !Laboratory.current.main_site?
|
|
|
|
|
Laboratory.current.laboratory_shixuns.create!(shixun: shixun, ownership: true)
|
|
|
|
|
end
|
|
|
|
|
return shixun
|
|
|
|
|
end
|
|
|
|
|
return shixun
|
|
|
|
|
rescue => e
|
|
|
|
|
Rails.logger.error("shixun_create_error: #{e.message}")
|
|
|
|
|
raise("创建实训失败!")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|