You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/services/jupyter_service.rb

246 lines
7.5 KiB

5 years ago
#coding=utf-8
module JupyterService
def _open_shixun_jupyter(shixun)
if shixun.is_jupyter?
shixun_tomcat = edu_setting('cloud_bridge')
uri = "#{shixun_tomcat}/bridge/jupyter/get"
tpiID = "tpm#{shixun.id}"
5 years ago
mount = shixun.data_sets.present?
params = {tpiID: tpiID, identifier: shixun.identifier, needMount: mount, gitUrl: '',
5 years ago
:containers => "#{Base64.urlsafe_encode64(shixun_container_limit(shixun))}"}
5 years ago
logger.info "test_juypter: uri->#{uri}, params->#{params}"
res = uri_post uri, params
5 years ago
if res && res['code'].to_i != 0
raise("实训云平台繁忙繁忙等级99")
end
5 years ago
5 years ago
logger.info "test_juypter: #{res}"
@shixun_jupyter_port = res['port']
5 years ago
5 years ago
"#{jupyter_service(res['port'])}/notebooks/data/workspace/myshixun_#{tpiID}/01.ipynb"
5 years ago
end
end
def jupyter_url_with_shixun(shixun)
#打开tpm - juypter接口
_open_shixun_jupyter(shixun)
end
def jupyter_port_with_shixun(shixun)
if @shixun_jupyter_port.to_i <=0
_open_shixun_jupyter(shixun)
end
@shixun_jupyter_port
end
5 years ago
5 years ago
def _open_game_jupyter(myshixun)
5 years ago
## 打开tpi
5 years ago
shixun = myshixun.shixun
5 years ago
if shixun.is_jupyter?
shixun_tomcat = edu_setting('cloud_bridge')
uri = "#{shixun_tomcat}/bridge/jupyter/get"
5 years ago
tpiID = myshixun.id
5 years ago
mount = myshixun.shixun.data_sets.present?
gitUrl = "#{edu_setting('git_address_domain')}/#{myshixun.repo_path}"
params = { tpiID: tpiID,
identifier: shixun.identifier,
myshixunIdentifier: myshixun.identifier,
5 years ago
gitUrl: gitUrl,
needMount: mount,
:containers => "#{Base64.urlsafe_encode64(shixun_container_limit(shixun))}"}
5 years ago
res = uri_post uri, params
logger.info "test_juypter: #{res}"
if res && res['code'].to_i != 0
raise("实训云平台繁忙繁忙等级99")
end
5 years ago
@game_jupyter_port = res['port']
5 years ago
repo_save_path = myshixun.repo_save_path
5 years ago
5 years ago
"#{jupyter_service(res['port'])}/notebooks/data/workspace/myshixun_#{tpiID}/#{repo_save_path}/01.ipynb"
5 years ago
end
end
5 years ago
def jupyter_url_with_game(myshixun)
_open_game_jupyter(myshixun)
5 years ago
end
5 years ago
def jupyter_port_with_game(myshixun)
5 years ago
if @game_jupyter_port.to_i <=0
5 years ago
_open_game_jupyter(myshixun)
5 years ago
end
@game_jupyter_port
end
5 years ago
def jupyter_save_with_shixun(shixun,jupyter_port)
author_name = current_user.real_name
author_email = current_user.git_mail
message = "User submitted"
tpiID = "tpm#{shixun.id}"
5 years ago
src_url = "#{jupyter_service(jupyter_port)}/nbconvert/notebook/data/workspace/myshixun_#{tpiID}/01.ipynb?download=true"
5 years ago
response = Faraday.get(src_url)
5 years ago
5 years ago
if response.status.to_i != 200
raise("获取文件内容失败:#{response.status}")
5 years ago
end
5 years ago
content = response.body.force_encoding('utf-8')
5 years ago
5 years ago
c = GitService.update_file(repo_path: shixun.repo_path,
5 years ago
file_path: "01.ipynb",
message: message,
content: content,
author_name: author_name,
author_email: author_email)
return c.size
end
5 years ago
def jupyter_save_with_game(myshixun,jupyter_port)
5 years ago
author_name = current_user.real_name
author_email = current_user.git_mail
message = "User submitted"
5 years ago
tpiID = myshixun.id
5 years ago
5 years ago
repo_save_path = myshixun.repo_save_path
5 years ago
src_url = "#{jupyter_service(jupyter_port)}/nbconvert/notebook/data/workspace/myshixun_#{tpiID}/#{repo_save_path}/01.ipynb?download=true"
5 years ago
response = Faraday.get(src_url)
5 years ago
5 years ago
if response.status.to_i != 200
raise("获取文件内容失败:#{response.status}")
5 years ago
end
5 years ago
content = response.body.force_encoding('utf-8')
5 years ago
5 years ago
c = GitService.update_file(repo_path: myshixun.repo_path,
5 years ago
file_path: "01.ipynb",
message: message,
content: content,
author_name: author_name,
author_email: author_email)
return c.size
end
5 years ago
##重置jupyter环境
def jupyter_tpi_reset(myshixun)
jupyter_delete_tpi(myshixun)
url = jupyter_url_with_game(myshixun)
port = jupyter_port_with_game(myshixun)
{url: url, port: port}
end
## 重置tpm环境
def jupyter_tpm_reset(shixun)
jupyter_delete_tpm(shixun)
url = jupyter_url_with_shixun(shixun)
port = jupyter_port_with_shixun(shixun)
{url: url, port: port}
end
# 删除pod
def jupyter_delete_tpi(myshixun)
myshixun_id = myshixun.id
digest = myshixun.identifier + edu_setting('bridge_secret_key')
digest_key = Digest::SHA1.hexdigest("#{digest}")
begin
shixun_tomcat = edu_setting('cloud_bridge')
uri = "#{shixun_tomcat}/bridge/jupyter/delete"
Rails.logger.info("#{current_user} => cloese_jupyter digest is #{digest}")
params = {:tpiID => myshixun_id, :digestKey => digest_key, :identifier => myshixun.identifier}
res = uri_post uri, params
if res && res['code'].to_i != 0
raise("实训云平台繁忙繁忙等级110")
end
end
end
def jupyter_delete_tpm(shixun)
tpiID = "tpm#{shixun.id}"
digest = shixun.identifier + edu_setting('bridge_secret_key')
digest_key = Digest::SHA1.hexdigest("#{digest}")
begin
shixun_tomcat = edu_setting('cloud_bridge')
uri = "#{shixun_tomcat}/bridge/jupyter/delete"
Rails.logger.info("#{current_user} => cloese_jupyter digest is #{digest}")
params = {:tpiID => tpiID, :digestKey => digest_key, :identifier => shixun.identifier}
res = uri_post uri, params
if res && res['code'].to_i != 0
raise("实训云平台繁忙繁忙等级110")
end
end
end
5 years ago
def jupyter_service jupyter_port
edu_setting('jupyter_service').gsub("PORT", jupyter_port)
end
def _jupyter_active(tpiID)
shixun_tomcat = edu_setting('cloud_bridge')
uri = "#{shixun_tomcat}/bridge/jupyter/active"
params = {:tpiID => tpiID}
res = uri_post uri, params
if res && res['code'].to_i != 0
raise("实训云平台繁忙繁忙等级120")
end
end
# tpm 延时
def jupyter_active_tpm(shixun)
tpiID = "tpm#{shixun.id}"
_jupyter_active(tpiID)
end
# tpi 延时
def jupyter_active_tpi(myshixun)
tpiID = myshixun.id
_jupyter_active(tpiID)
end
5 years ago
def _jupyter_timeinfo(tpiID)
shixun_tomcat = edu_setting('cloud_bridge')
uri = "#{shixun_tomcat}/bridge/jupyter/getTimeInfo"
params = {:tpiID => tpiID}
res = uri_post uri, params
if res && res['code'].to_i != 0
raise("实训云平台繁忙繁忙等级130")
end
res['data']
end
# 获取时间参数
def jupyter_timeinfo_tpm(shixun)
tpiID = "tpm#{shixun.id}"
_jupyter_timeinfo(tpiID)
end
# 获取时间参数
def jupyter_timeinfo_tpi(myshixun)
tpiID = myshixun.id
_jupyter_timeinfo(tpiID)
end
5 years ago
end