educoder/app/controllers/shixuns_controller.rb

273 lines
8.5 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# encoding: utf-8
# REDO: 创建版本库权限控制
class ShixunsController < ApplicationController
layout 'base_shixun'
before_filter :require_login
before_filter :find_shixun, :except => [ :index, :new, :create]
before_filter :shixun_view_allow, :only => [:show]
before_filter :require_manager, :only => [ :settings, :add_script, :publish]
include ApplicationHelper
def shixun_monitor
# monitor_filter
# if @had_exec
# @tpm = Myshixun.where(:user_id => User.current, :shixun_id => @shixun).first
# end
#
# respond_to do |format|
# format.js
# end
end
# jenkins回调脚本
# gameID 及实训ID
def publish
jenkins_shixuns = Redmine::Configuration['jenkins_shixuns']
fragments = []
testSet = []
testCases = []
gameInfo = []
begin
challenges = @shixun.challenges.includes(:test_sets)
challenges.each do |challenge|
pipeline_script = challenge.try(:pipeline_script)
challenge_script = {:challengeStage => "#{challenge.position}", :challengePipeline => "#{pipeline_script}"}
fragments << challenge_script
challenge.test_sets.each do |test_set|
test_cases = {:caseId => test_set.id.to_s, :input => test_set.input, :output => test_set.output}
testSet << test_cases
end
test_sets_list = {:challengeStage => challenge.position.to_s, :challengeTestCases => testSet}
testCases << test_sets_list
end
gameInfo = {:trainingID => @shixun.id.to_s, :operationEnvironment => @shixun.language, :challengePipelines => fragments, :testCases => testCases}
fragments = fragments.to_json
uri = URI("#{jenkins_shixuns}/jenkins-exec/game/publishGame")
params = {gameInfo:gameInfo.to_json}
res = uri_exec uri, params
if res['code'] == 0
@shixun.update_attribute(:status, 1)
@shixun.update_attribute(:script, res["msg"])
else
logger.error("res['code'] is #{res['code']}, res['msg'] is #{res['code']}")
@jenkin_error = true
raise("服务器异常,请联系系统管理员")
end
rescue
respond_to do |format|
format.js
end
end
end
# copy_shixun复制一个新的实训模块包括版本库
# copy_myshixun自动创建系列game,game中只包含状态等信息公共信息从Challeges中读取
# 开启过实训的则直接跳入my实训页面
def shixun_exec
unless allow_shixun_exec(@shixun)
render_403
end
myshixun = Myshixun.where(:user_id => User.current.id, :shixun_id => @shixun.id).first
unless myshixun.blank?
logger.info("current task id is #{myshixun.current_task}")
redirect_to myshixun_game_path(myshixun.current_task, :myshixun_id => myshixun)
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)
if !gshixun.id.nil?
myshixun = copy_myshixun(@shixun, gshixun)
challenges = @shixun.challenges
# 之所以增加user_id是为了方便统计查询性能
challenges.each_with_index do |challenge, index|
status = (index == 0 ? 0 : 3)
Game.create!(:challenge_id => challenge.id, :myshixun_id => myshixun.id, :status => status, :user_id => myshixun.user_id, :open_time => Time.now)
end
else
raise("网络异常,请稍后重试")
end
redirect_to myshixun_game_path(myshixun.current_task, :myshixun_id => myshixun)
rescue Exception => e
flash[:error] = e.message
logger.info("failed to exec shixun: current task id is #{e}")
@g.delete_project(gshixun.id) if !gshixun.id.nil?
redirect_to shixun_challenges_path(@shixun)
raise ActiveRecord::Rollback
end
end
end
def new
@shixun = Shixun.new
respond_to do |format|
format.html{render :layout => 'new_base'}
format.json
end
end
def create
@shixun = Shixun.new(params[:shixun])
@shixun.user_id = User.current.id
@shixun.language = params[:language]
(params[:shixun][:is_public] == "1" ? @shixun.is_public = true : @shixun.is_public = false)
respond_to do |format|
if @shixun.save
m = ShixunMember.new(:user_id => User.current.id, :role => 1)
@shixun.shixun_members << m
format.html { redirect_to @shixun, notice: l(:notice_successful_create) }
format.js
else
format.html { render action: "new" }
format.js
end
end
end
def show
respond_to do |format|
format.html{redirect_to shixun_challenges_path(@shixun)}
end
end
def edit
end
def update
@shixun.attributes = params[:shixun]
@shixun.language = params[:language]
params[:shixun][:is_public] == "on" ? @shixun.is_public = 1 : @shixun.is_public = 0
@shixun.save
respond_to do |format|
format.html{redirect_to settings_shixun_url(@shixun)}
end
end
def destroy
end
def settings
@repository = Repository.where(:shixun_id => @shixun, :type => "Repository::Gitlab").first
unless @repository.nil?
gitlab_address = Redmine::Configuration['gitlab_address']
creator = @shixun.owner.try(:login)
@repos_url = gitlab_address+"/" + creator + "/" + @repository.identifier+"."+"git"
end
end
# 添加实训脚本
def add_script
if @shixun.update_attribute(:script, params[:shixun_script])
@notice = "脚本添加成功"
else
@notice = "脚本添加失败"
end
end
# 创建实训job
def shixun_job_create
if @shixun.challenges.count == 0
@notice = "实训开启失败:请先发布实训任务"
return
elsif Repository.where(:shixun_id => @shixun.id, :type => "Repository::Gitlab").count == 0
@notice = "实训开启失败:请先创建版本库"
return
end
@shixun.update_attribute(:status, 1)
end
# 更新实训job
def shixun_job_update
# jobName = "#{@shixun.id}"
# pipeLine = "#{Base64.encode64(@shixun.script)}"
# uri = URI("http://123.59.135.74:9999/jenkins-exec/api/updateJob")
# params = {jobName: jobName, pipeLine: pipeLine}
# res = uri_exec uri, params
training_shixun_notice res
if res['code'] == 0
@shixun.update_attribute(:status, 1)
end
end
# Find shixun of id params[:id]
def find_shixun
@shixun = Shixun.find(params[:id])
rescue ActiveRecord::RecordNotFound
render_404
end
def monitor_filter
if User.current.id == @shixun.user_id
@notice = l(:label_shixun_mine)
else
if has_exec_cur_shixun(@shixun)
@notice = l(:label_shixun_had_forked)
@had_exec = true
else
@notice = l(:label_shixun_exec)
@had_exec = false
end
end
end
private
# REDO: 新增类型copy的时候
# 复制项目
# gshixun --> gitlab project
def copy_myshixun shixun, gshixun
myshixun = Myshixun.new
myshixun.attributes = shixun.attributes.dup.except("id","user_id","visits","gpid","status")
myshixun.shixun_id = shixun.id
myshixun.user_id = User.current.id
myshixun.gpid = gshixun.id
jenkins_shixuns = Redmine::Configuration['jenkins_shixuns']
if myshixun.save!
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!
rep_url = git_repository_url(myshixun, "Myshixun")
uri = URI("#{jenkins_shixuns}/jenkins-exec/game/openGameInstance")
pipeLine = "#{shixun.script}"
params = {jobNameForInstance: "myshixun_#{myshixun.id}", gamePipelineScript: pipeLine, instanceGitURL:rep_url}
res = uri_exec uri, params
if (res && res['code'] != 0)
raise("Job 创建失败")
end
return myshixun
end
end
def training_shixun_notice res
if res['code'] == 0
@notice = "实训开启成功"
else
@notice = res['msg'].nil? ? "实训开启失败" : res['msg']
end
end
# 权限控制
def shixun_view_allow
if @shixun.is_public?
true
else
User.current.member_of?(@shixun) ? true : false
end
end
# 实训管理员或者超级管理员
def require_manager
render_403 unless (User.current.manager_of_shixun?(@shixun) || User.current.admin?)
end
end