实训重置

This commit is contained in:
cxt 2017-06-23 14:33:18 +08:00
parent b67357a3a8
commit 3970efa0fd
5 changed files with 114 additions and 100 deletions

View File

@ -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
# 代码预览

View File

@ -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

View File

@ -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

View File

@ -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表示已经有修改

View File

@ -13,13 +13,15 @@
</div>
<script>
$(function(){
<% if @reset == "1" %>
<% if !@error.blank? %>
notice_box("<%= @error %>");
<% elsif @reset && @reset == "1" %>
notice_sure_box("实训更新啦,系统已自动为您重置");
<% end %>
});
var control = 0; // 版本库控制 0表示点击放大 1表示点击缩小
var control_1 = 0; // 测评控制 0表示点击放大 1表示点击缩小
var nGameRes = $("#games_repository_contents"); // 版本库区域
var nGameRes = $("#games_repository_contents"); // 版本库区域
var nGameEva = $("#games_valuation_contents"); // 评测区域
var nRIcon = $("#extend_and_zoom").children("i"); // 版本库放大缩小按钮
var nCode = $("#file_entry_content").find(".CodeMirror-scroll"); // 版本库代码区域