diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index afe2129a9..cbd2962b6 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -427,6 +427,24 @@ class ApplicationController < ActionController::Base container.to_json end + # 实训中间层pod配置 + def shixun_container_limit shixun + container = [] + shixun.shixun_service_configs.each do |config| + mirror = config.mirror_repository + if mirror.name.present? + container << {:image => mirror.name, + :cpuLimit => config.cpu_limit, + :cpuRequest => config.lower_cpu_limit, + :memoryLimit => "#{config.memory_limit}M", + :memoryRequest => "#{config.request_limit}M", + :resourceLimit => "#{config.resource_limit}K", + :type => mirror.try(:main_type) == "1" ? "main" : "sub"} + end + end + container.to_json + end + # 毕设任务列表的赛选 def course_work(task, **option) logger.info("#############{option}") diff --git a/app/controllers/challenges_controller.rb b/app/controllers/challenges_controller.rb index d56c3ecbd..856afeb01 100644 --- a/app/controllers/challenges_controller.rb +++ b/app/controllers/challenges_controller.rb @@ -154,7 +154,7 @@ class ChallengesController < ApplicationController current_myshixun = @shixun.current_myshixun(current_user.id) @challenges = @shixun.challenges.includes(:games).where("games.user_id" => (current_myshixun ? current_user.id : nil)) - @editable = current_user.manager_of_shixun?(@shixun) && @shixun.status == 0 + @editable = @shixun.status == 0 # before_action:有判断权限,如果没发布,则肯定是管理人员 @user = current_user end @@ -290,7 +290,7 @@ class ChallengesController < ApplicationController def challenge_params params.require(:challenge).permit(:subject, :task_pass, :difficulty, :score, :st, :modify_time, :test_set_average, :path, :exec_path, :show_type, :original_picture_path, :test_set_score, - :expect_picture_path, :picture_path, :web_route, :answer) + :expect_picture_path, :picture_path, :web_route, :answer, :exec_time) end def chooce_params diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 76e15f9e2..7e7b6e082 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -33,7 +33,7 @@ class GamesController < ApplicationController is_teacher = @user.is_teacher? # 实训超时设置 - time_limit = @shixun.exec_time + time_limit = game_challenge.exec_time # 上一关、下一关 prev_game = @game.prev_of_current_game(@shixun.id, @game.myshixun_id, game_challenge.position) @@ -66,7 +66,7 @@ class GamesController < ApplicationController shixun_tomcat = edu_setting('cloud_bridge') service_host = edu_setting('vnc_url') uri = "#{shixun_tomcat}/bridge/vnc/getvnc" - params = {tpiID: @myshixun.id, :containers => "#{Base64.urlsafe_encode64(container_limit(@shixun.mirror_repositories))}"} + params = {tpiID: @myshixun.id, :containers => "#{Base64.urlsafe_encode64(shixun_container_limit(@shixun))}"} res = uri_post uri, params if res && res['code'].to_i != 0 raise("实训云平台繁忙(繁忙等级:99)") @@ -547,7 +547,7 @@ class GamesController < ApplicationController br_params = {:tpiID => "#{@myshixun.id}", :tpiGitURL => "#{gitUrl}", :buildID => "#{@game.id}", :instanceChallenge => "#{step}", :testCases => "#{testCases}", :resubmit => "#{resubmit}", :times => params[:first].to_i, :podType => @shixun.webssh, :content_modified => content_modified, - :containers => "#{Base64.urlsafe_encode64(container_limit(@shixun.mirror_repositories))}", + :containers => "#{Base64.urlsafe_encode64(shixun_container_limit(@shixun))}", :persistenceName => @shixun.identifier, :tpmScript => "#{tpmScript}", :timeLimit => "#{@shixun.exec_time}", :isPublished => (@shixun.status < 2 ? 0 : 1) } diff --git a/app/controllers/myshixuns_controller.rb b/app/controllers/myshixuns_controller.rb index a4a3b7222..909533301 100644 --- a/app/controllers/myshixuns_controller.rb +++ b/app/controllers/myshixuns_controller.rb @@ -217,7 +217,7 @@ class MyshixunsController < ApplicationController shixun_tomcat = edu_setting('tomcat_webssh') uri = "#{shixun_tomcat}/bridge/webssh/getConnectInfo" params = {tpiID:@myshixun.id, podType:@myshixun.shixun.try(:webssh), - containers:(Base64.urlsafe_encode64(container_limit @myshixun.shixun.mirror_repositories))} + containers:(Base64.urlsafe_encode64(shixun_container_limit @myshixun.shixun))} res = uri_post uri, params if res && res['code'].to_i != 0 tip_exception("实训云平台繁忙(繁忙等级:92)") diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index b3069bc5d..f079aed1e 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -315,9 +315,13 @@ class ShixunsController < ApplicationController # 镜像-实训关联表 ShixunMirrorRepository.create!(:shixun_id => @shixun.id, :mirror_repository_id => main_type.to_i) if main_type.present? + # 实训主镜像服务配置 + ShixunServiceConfig.create!(:shixun_id => @shixun.id, :mirror_repository_id => main_type.to_i) if sub_type.present? sub_type.each do |mirror| ShixunMirrorRepository.create!(:shixun_id => @shixun.id, :mirror_repository_id => mirror) + # 实训子镜像服务配置 + ShixunServiceConfig.create!(:shixun_id => @shixun.id, :mirror_repository_id => mirror) end end @@ -395,6 +399,18 @@ class ShixunsController < ApplicationController use_scope = 0 end @shixun.update_attributes!(:use_scope => use_scope) + # 超级管理员和运营人员才能保存 中间层服务器pod信息的配置 + if current_user.admin? || current_user.business? + @shixun.shixun_service_configs.destroy_all + params[:mirror_id].each_with_index do |mirror_id, index| + ShixunServiceConfig.create!(:shixun_id => @shixun.id, + :cpu_limit => params[:cpu_limit][index], + :lower_cpu_limit => params[:lower_cpu_limit][index], + :memory_limit => params[:memory_limit][index], + :request_limit => params[:request_limit][index], + :mirror_repository_id => mirror_id) + end + end rescue Exception => e uid_logger_error(e.message) tip_exception("实训保存失败") diff --git a/app/models/challenge.rb b/app/models/challenge.rb index 07a49196d..18b177e1c 100644 --- a/app/models/challenge.rb +++ b/app/models/challenge.rb @@ -20,7 +20,7 @@ class Challenge < ApplicationRecord # acts_as_attachable scope :base_attrs, -> { select([:id, :subject, :position, :shixun_id, :st, :score, :path, :task_pass, :modify_time, - :web_route, :answer]) } + :web_route, :answer, :exec_time]) } scope :choose_type, -> { where(st: 1) } scope :practice_type, -> { where(st: 0) } diff --git a/app/models/shixun.rb b/app/models/shixun.rb index 556a4400f..18d5c5fae 100644 --- a/app/models/shixun.rb +++ b/app/models/shixun.rb @@ -30,6 +30,8 @@ class Shixun < ApplicationRecord has_one :shixun_info, dependent: :destroy belongs_to :user + # 实训服务配置 + has_many :shixun_service_configs, :dependent => :destroy scope :search_by_name, ->(keyword) { where("name like ? or description like ? ", diff --git a/app/models/shixun_service_config.rb b/app/models/shixun_service_config.rb new file mode 100644 index 000000000..6d106fc07 --- /dev/null +++ b/app/models/shixun_service_config.rb @@ -0,0 +1,4 @@ +class ShixunServiceConfig < ApplicationRecord + belongs_to :shixun + belongs_to :mirror_repository +end diff --git a/app/views/challenges/edit.json.jbuilder b/app/views/challenges/edit.json.jbuilder index 64848a4e6..2774b5a05 100644 --- a/app/views/challenges/edit.json.jbuilder +++ b/app/views/challenges/edit.json.jbuilder @@ -7,7 +7,7 @@ json.chooses do end if @tab == 0 # 本关任务tab的编辑模式 - json.(@challenge, :id, :subject, :task_pass, :difficulty, :score) + json.(@challenge, :id, :subject, :task_pass, :difficulty, :score, :exec_time) json.tags @challenge.challenge_tags.map(&:name) elsif @tab == 1 # 评测设置的编辑模式 diff --git a/spec/models/shixun_service_config_spec.rb b/spec/models/shixun_service_config_spec.rb new file mode 100644 index 000000000..d79cd19f7 --- /dev/null +++ b/spec/models/shixun_service_config_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe ShixunServiceConfig, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end