Skip to content

Commit

Permalink
perf: 更新shell脚本模板,规避使用eval带来的不可预期的风险 TencentBlueKing#2992
Browse files Browse the repository at this point in the history
# Reviewed, transaction id: 8896
  • Loading branch information
hLinx committed May 29, 2024
1 parent 933b6e2 commit f5b5c62
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/frontend/src/components/ace-editor/default-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,44 @@ export default {
shell.push(
'#!/bin/bash',
'',
'anynowtime="date +\'%Y-%m-%d %H:%M:%S\'"',
'NOW="echo [\\`$anynowtime\\`][PID:$$]"',
'',
lang === 'zh-CN' ? '##### 可在脚本开始运行时调用,打印当时的时间戳及PID。' : '##### It\'s usually called when the script starts to run, prints the timestamp and PID.',
lang === 'zh-CN' ? '# 定义获取当前时间和PID的函数' : '# Define function to get current time and PID',
'function job_get_now',
'{',
' echo "[`date +\'%Y-%m-%d %H:%M:%S\'`][PID:$$]"',
'}',
'',
lang === 'zh-CN' ? '# 在脚本开始运行时调用,打印当前的时间戳及PID' : '# Call this function at the beginning of the script to print the current time and PID',
'function job_start',
'{',
' echo "`eval $NOW` job_start"',
' echo "$(job_get_now) job_start"',
'}',
'',
lang === 'zh-CN' ? '##### 可在脚本执行成功的逻辑分支处调用,打印当时的时间戳及PID。 ' : '##### Use this func. to combine the execution results of multiple hosts and display by group.',
lang === 'zh-CN' ? '# 在脚本执行成功的逻辑分支处调用,打印当前的时间戳及PID' : '# Call this function when the script execution succeeds to print the current time and PID with a success message',
'function job_success',
'{',
' MSG="$*"',
' echo "`eval $NOW` job_success:[$MSG]"',
' local msg="$*"',
' echo "$(job_get_now) job_success: [$msg]"',
' exit 0',
'}',
'',
// '#####可在脚本执行失败的逻辑分支处调用,打印当时的时间戳及PID。',
lang === 'zh-CN' ? '##### 可在脚本执行失败的逻辑分支处调用,打印当时的时间戳及PID。' : '##### Use this func. to combine the execution results of multiple hosts and display by group.',
lang === 'zh-CN' ? '# 在脚本执行失败的逻辑分支处调用,打印当前的时间戳及PID' : '# Call this function when the script execution fails to print the current time and PID with a failure message',
'function job_fail',
'{',
' MSG="$*"',
' echo "`eval $NOW` job_fail:[$MSG]"',
' local msg="$*"',
' echo "$(job_get_now) job_fail: [$msg]"',
' exit 1',
'}',
'',
lang === 'zh-CN' ? '# 在当前脚本执行时,第一行输出当前时间和进程ID,详见上面函数:job_get_now' : '# Call the job_start function to print the current time and PID',
'job_start',
'',
lang === 'zh-CN' ? '###### 作业平台中执行脚本成功和失败的标准只取决于脚本最后一条执行语句的返回值' : '###### The script execution result is depends on the value of return/exit code, weather it success or failed.',
lang === 'zh-CN' ? '###### 如果返回值为0,则认为此脚本执行成功,如果非0,则认为脚本执行失败' : '###### If return code is 0, means success, otherwise failed.',
lang === 'zh-CN' ? '###### 可在此处开始编写您的脚本逻辑代码' : '###### Start to writing your script code below this line',
'',
'',
'',
);
return shell.join('\n');
}()),
Expand Down Expand Up @@ -125,6 +130,7 @@ export default {
' exit 1',
'',
'',
'',
);
return batch.join('\n');
}()),
Expand Down Expand Up @@ -165,6 +171,7 @@ export default {
lang === 'zh-CN' ? '###### 可在此处开始编写您的脚本逻辑代码' : '###### You can start writing your script logic code here',
'',
'',
'',
);
return perl.join('\n');
}()),
Expand Down Expand Up @@ -207,6 +214,7 @@ export default {
lang === 'zh-CN' ? '###### 可在此处开始编写您的脚本逻辑代码' : '###### Start to writing your script code below this line',
'',
'',
'',
);
return python.join('\n');
}()),
Expand Down Expand Up @@ -259,6 +267,7 @@ export default {
lang === 'zh-CN' ? '###### 可在此处开始编写您的脚本逻辑代码' : '###### Start to writing your script code below this line',
'',
'',
'',
);
return powershell.join('\n');
}()),
Expand Down

0 comments on commit f5b5c62

Please sign in to comment.