From 3970efa0fd7d67db48e2a9e9dd62dcd1cb37b81f Mon Sep 17 00:00:00 2001 From: cxt Date: Fri, 23 Jun 2017 14:33:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E8=AE=AD=E9=87=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/games_controller.rb | 23 +++++-- app/controllers/myshixuns_controller.rb | 36 ---------- app/controllers/shixuns_controller.rb | 59 +--------------- app/helpers/application_helper.rb | 90 +++++++++++++++++++++++++ app/views/games/show.html.erb | 6 +- 5 files changed, 114 insertions(+), 100 deletions(-) diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 0e8f98746..27db913cf 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -27,12 +27,27 @@ class GamesController < ApplicationController # @tread 判断是否踩 # git_repository_url @myshixun, "Myshixun" def show + @shixun = @myshixun.shixun # 展示全部实训 unless ShixunModify.where(:shixun_id => @myshixun.shixun_id, :myshixun_id => @myshixun.id, :status => 1).first.blank? - redirect_to myshixun_reset_myshixun_path(@myshixun, :reset => 1) + @reset = 1 + #redirect_to myshixun_reset_myshixun_path(@myshixun, :reset => 1) + ActiveRecord::Base.transaction do + begin + # 重置实训 + reset_myshixun @myshixun + + # 新开启实训 + new_myshixun @shixun + rescue Exception => e + flash[:error] = e.message + logger.info("failed to exec shixun: current task id is #{e}") + redirect_to shixun_challenges_path(@shixun) + raise ActiveRecord::Rollback + end + end end - @reset = params[:reset] ? params[:reset] : '0' if @game.status == 1 # 放置页面ajax数据还没获取就关闭 @game.update_attribute(:status, 0) @@ -40,7 +55,6 @@ class GamesController < ApplicationController # logger.info("#################%%%%%%#{@game.outputs.first.out_put}") # @games = @myshixun.games.includes(:challenge) @challenges = Challenge.where(:shixun_id => @myshixun.shixun_id) - @shixun = @myshixun.shixun # @games_count = @games.count @game_challenge = @game.challenge @had_done = @game.had_done @@ -100,7 +114,8 @@ class GamesController < ApplicationController end rescue Exception => e logger.error("game error##############{e.message}") - flash[:error] = e.message + @error = e.message + #flash[:error] = e.message end # 代码预览 diff --git a/app/controllers/myshixuns_controller.rb b/app/controllers/myshixuns_controller.rb index 8fa9ebb2a..a84b90cf5 100644 --- a/app/controllers/myshixuns_controller.rb +++ b/app/controllers/myshixuns_controller.rb @@ -18,42 +18,6 @@ class MyshixunsController < ApplicationController end - def reset_myshixun myshixun - ActiveRecord::Base.transaction do - begin - jenkins_shixuns = Redmine::Configuration['jenkins_shixuns'] - shixun = myshixun.shixun - myshixun_job = Base64.encode64("myshixun_#{myshixun.id}") - repository = myshixun.repository - repository.destroy - myshixun.destroy - g = Gitlab.client - g_project = g.project(myshixun.gpid) - unless g_project.id.nil? - git_rep = g.delete_project(myshixun.gpid) - logger.info("### delete repository result is #{git_rep}") - if git_rep != true - raise("实训云平台繁忙(繁忙等级:87)") - end - end - params = {:jobName => "#{myshixun_job}"} - uri = URI("#{jenkins_shixuns}/jenkins-exec/game/deleteJob") - res = uri_exec uri, params - if res && res['code'] == 0 - shixun_mod = ShixunModify.where(:shixun_id => myshixun.shixun_id, :myshixun_id => myshixun.id, :status => 1).first - shixun_mod.update_attributes(:status => 0) if shixun_mod - else (res && res['code'] != 0 && res['code'] != 1) - raise("实训云平台繁忙(繁忙等级:89)") - end - rescue Exception => e - flash[:error] = "实训云平台繁忙(繁忙等级:89)" - # redirect_to myshixun_path(myshixun) - logger.error("myshixun reset failed #{e}") - raise ActiveRecord::Rollback - end - end - end - # 实训重置只更新脚本 def myshixun_reset ActiveRecord::Base.transaction do diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index 10aac07b9..a71d64c6d 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -245,34 +245,9 @@ class ShixunsController < ApplicationController redirect_to myshixun_game_path(myshixun.current_task, :myshixun_id => myshixun, :reset => params[:reset]) return end - repository = @shixun.repository ActiveRecord::Base.transaction do begin - @g = Gitlab.client - if User.current.gid.nil? - s = Trustie::Gitlab::Sync.new - s.sync_user(User.current) - end - gshixun = @g.fork(@shixun.gpid, User.current.gid) - logger.info("#############gshixun is #{gshixun}") - if !gshixun.id.nil? - myshixun = copy_myshixun(@shixun, gshixun) - @shixun.update_attribute(:myshixun_count, (@shixun.myshixun_count + 1)) - challenges = @shixun.challenges - # 之所以增加user_id是为了方便统计查询性能 - challenges.each_with_index do |challenge, index| - status = (index == 0 ? 0 : 3) - code = CODES.sample(12).join - code = CODES.sample(12).join if Game.where(identifier: code).present? - game =Game.create!(:challenge_id => challenge.id, :myshixun_id => myshixun.id, :status => status, :user_id => myshixun.user_id, :open_time => Time.now, :identifier => code) - # 记录刚开始时默认代开文件原始代码 - # 刚开始fork的一段时间是获取不了content内容的 - game_passed_code(game.try(:id), challenge.try(:path), myshixun.try(:gpid), 0) - end - else - raise("实训云平台繁忙(繁忙等级:81)") - end - logger.info("myshixun id si #{myshixun.id} and current_task id is#{myshixun.current_task.id}") + new_myshixun @shixun redirect_to myshixun_game_path(myshixun.current_task, :myshixun_id => myshixun, :reset => params[:reset]) rescue Exception => e flash[:error] = e.message @@ -437,38 +412,6 @@ class ShixunsController < ApplicationController end private - # REDO: 新增类型copy的时候 - # 复制项目 - # gshixun --> gitlab project - CODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z) - def copy_myshixun shixun, gshixun - myshixun = Myshixun.new - myshixun.attributes = shixun.attributes.dup.except("id","user_id","visits","gpid","status", "identifier","propaedeutics") - myshixun.shixun_id = shixun.id - myshixun.user_id = User.current.id - myshixun.gpid = gshixun.id - jenkins_shixuns = Redmine::Configuration['jenkins_shixuns'] - if myshixun.save! - code = CODES.sample(10).join - code = CODES.sample(10).join if Myshixun.where(identifier: code).present? - myshixun.update_attribute(:identifier, code) - MyshixunMember.create!(:myshixun_id => myshixun.id, :user_id => User.current.id, :role => 1) - rep = Repository.new(:myshixun_id => myshixun.id, :identifier => gshixun.name,:project_id => -1, :shixun_id => -2) - rep.type = "Repository::Gitlab" - rep.save! - login = User.find_by_mail("educoder@163.com").try(:login) - rep_url = Base64.encode64(git_shixun_url shixun, login) - uri = URI("#{jenkins_shixuns}/jenkins-exec/game/openGameInstance") - pipeLine = "#{shixun.script}" - params = {jobNameForInstance: "myshixun_#{myshixun.id}", gameInfo: shixun.gameInfo, instanceGitURL:rep_url} - logger.info("openGameInstance params is #{params}") - res = uri_exec uri, params - if (res && res['code'].to_i != 0) - raise("实训云平台繁忙(繁忙等级:83)") - end - return myshixun - end - end def training_shixun_notice res if res['code'] == 0 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f5de723f7..472d20b21 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -33,6 +33,96 @@ module ApplicationHelper extend Forwardable def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter + def reset_myshixun myshixun + jenkins_shixuns = Redmine::Configuration['jenkins_shixuns'] + shixun = myshixun.shixun + myshixun_job = Base64.encode64("myshixun_#{myshixun.id}") + repository = myshixun.repository + repository.destroy + myshixun.destroy + g = Gitlab.client + g_project = g.project(myshixun.gpid) + unless g_project.id.nil? + git_rep = g.delete_project(myshixun.gpid) + logger.info("### delete repository result is #{git_rep}") + if git_rep != true + raise("实训云平台繁忙(繁忙等级:87)") + end + end + params = {:jobName => "#{myshixun_job}"} + uri = URI("#{jenkins_shixuns}/jenkins-exec/game/deleteJob") + res = uri_exec uri, params + if res && res['code'] == 0 + shixun_mod = ShixunModify.where(:shixun_id => myshixun.shixun_id, :myshixun_id => myshixun.id, :status => 1).first + shixun_mod.update_attributes(:status => 0) if shixun_mod + else (res && res['code'] != 0 && res['code'] != 1) + raise("实训云平台繁忙(繁忙等级:89)") + end + end + + def new_myshixun shixun + g = Gitlab.client + if User.current.gid.nil? + s = Trustie::Gitlab::Sync.new + s.sync_user(User.current) + end + gshixun = g.fork(shixun.gpid, User.current.gid) + logger.info("#############gshixun is #{gshixun}") + if !gshixun.id.nil? + myshixun = copy_myshixun(shixun, gshixun) + shixun.update_attribute(:myshixun_count, (shixun.myshixun_count + 1)) + challenges = shixun.challenges + # 之所以增加user_id是为了方便统计查询性能 + challenges.each_with_index do |challenge, index| + status = (index == 0 ? 0 : 3) + code = CODES.sample(12).join + code = CODES.sample(12).join if Game.where(identifier: code).present? + game =Game.create!(:challenge_id => challenge.id, :myshixun_id => myshixun.id, :status => status, :user_id => myshixun.user_id, :open_time => Time.now, :identifier => code) + # 记录刚开始时默认代开文件原始代码 + # 刚开始fork的一段时间是获取不了content内容的 + game_passed_code(game.try(:id), challenge.try(:path), myshixun.try(:gpid), 0) + end + else + raise("实训云平台繁忙(繁忙等级:81)") + end + logger.info("myshixun id si #{myshixun.id} and current_task id is#{myshixun.current_task.id}") + end + + # REDO: 新增类型copy的时候 + # 复制项目 + # gshixun --> gitlab project + CODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z) + def copy_myshixun shixun, gshixun + myshixun = Myshixun.new + myshixun.attributes = shixun.attributes.dup.except("id","user_id","visits","gpid","status", "identifier","propaedeutics") + myshixun.shixun_id = shixun.id + myshixun.user_id = User.current.id + myshixun.gpid = gshixun.id + jenkins_shixuns = Redmine::Configuration['jenkins_shixuns'] + if myshixun.save! + code = CODES.sample(10).join + code = CODES.sample(10).join if Myshixun.where(identifier: code).present? + myshixun.update_attribute(:identifier, code) + MyshixunMember.create!(:myshixun_id => myshixun.id, :user_id => User.current.id, :role => 1) + rep = Repository.new(:myshixun_id => myshixun.id, :identifier => gshixun.name,:project_id => -1, :shixun_id => -2) + rep.type = "Repository::Gitlab" + rep.save! + login = User.find_by_mail("educoder@163.com").try(:login) + rep_url = Base64.encode64(git_shixun_url shixun, login) + uri = URI("#{jenkins_shixuns}/jenkins-exec/game/openGameInstance") + pipeLine = "#{shixun.script}" + params = {jobNameForInstance: "myshixun_#{myshixun.id}", gameInfo: shixun.gameInfo, instanceGitURL:rep_url} + logger.info("openGameInstance params is #{params}") + res = uri_exec uri, params + if (res && res['code'].to_i != 0) + g = Gitlab.client + g.delete_project(gshixun.id) if !gshixun.id.nil? + raise("实训云平台繁忙(繁忙等级:83)") + end + return myshixun + end + end + # 若实训有更改,则修改已发不实训的标记为已更改 # shixun_modifies表中1表示已更改,进入myshixun需要强制重置,0表示没有修改 # type 0:表示已经是最新的了;1:表示已经有修改 diff --git a/app/views/games/show.html.erb b/app/views/games/show.html.erb index 124d1624a..835cd82f9 100644 --- a/app/views/games/show.html.erb +++ b/app/views/games/show.html.erb @@ -13,13 +13,15 @@