修改环境变量加载逻辑

This commit is contained in:
whyour
2024-07-10 23:29:44 +08:00
parent 5afac3a3ac
commit e191aca41f
18 changed files with 210 additions and 343 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
create_token() {
local token_command="tsx ${dir_root}/back/token.ts"
local token_command="ts-node-transpile-only ${dir_root}/back/token.ts"
local token_file="${dir_root}/static/build/token.js"
if [[ -f $token_file ]]; then
token_command="node ${token_file}"
+1 -1
View File
@@ -73,7 +73,7 @@ check_pm2() {
main() {
echo -e "=====> 开始检测"
npm i -g pnpm@8.3.1 pm2 tsx
npm i -g pnpm@8.3.1 pm2 ts-node
patch_version
reset_env
+73 -45
View File
@@ -91,6 +91,12 @@ env_str_to_array() {
read -ra array <<<"${!env_param}"
}
clear_non_sh_env() {
if [[ $file_param != *.sh ]]; then
clear_env
fi
}
## 正常运行单个脚本,$1:传入参数
run_normal() {
local file_param=$1
@@ -105,37 +111,34 @@ run_normal() {
file_param=${file_param/$relative_path\//}
fi
$timeoutCmd $which_program $file_param "${script_params[@]}"
if [[ $isJsOrPythonFile == 'false' ]]; then
clear_non_sh_env
fi
configDir="${dir_config}" $timeoutCmd $which_program $file_param "${script_params[@]}"
}
handle_env_split() {
if [[ ! $num_param ]]; then
num_param="1-max"
fi
env_str_to_array
local tempArr=$(echo $num_param | sed "s/-max/-${#array[@]}/g" | sed "s/max-/${#array[@]}-/g" | perl -pe "s|(\d+)(-\|~\|_)(\d+)|{\1..\3}|g")
local runArr=($(eval echo $tempArr))
array_run=($(awk -v RS=' ' '!a[$1]++' <<<${runArr[@]}))
}
## 并发执行时,设定的 RandomDelay 不会生效,即所有任务立即执行
run_concurrent() {
local file_param="$1"
local env_param="$2"
local num_param=$(echo "$3" | perl -pe "s|.*$2(.*)|\1|")
local num_param=$(echo "$3" | perl -pe "s|.*$2(.*)|\1|" | awk '{$1=$1};1')
if [[ ! $env_param ]]; then
echo -e "\n 缺少并发运行的环境变量参数"
exit 1
fi
env_str_to_array
local tempArr=$(echo $num_param | sed "s/-max/-${#array[@]}/g" | sed "s/max-/${#array[@]}-/g" | perl -pe "s|(\d+)(-\|~\|_)(\d+)|{\1..\3}|g")
local runArr=($(eval echo $tempArr))
runArr=($(awk -v RS=' ' '!a[$1]++' <<<${runArr[@]}))
local n=0
for i in ${runArr[@]}; do
array_run[n]=${array[$i - 1]}
let n++
done
local cookieStr=$(
IFS="&"
echo "${array_run[*]}"
)
[[ ! -z $cookieStr ]] && export "${env_param}=${cookieStr}"
env_str_to_array
handle_env_split
single_log_time=$(date "+%Y-%m-%d-%H-%M-%S.%3N")
cd $dir_scripts
@@ -144,15 +147,24 @@ run_concurrent() {
cd ${relative_path}
file_param=${file_param/$relative_path\//}
fi
for i in "${!array[@]}"; do
export "${env_param}=${array[i]}"
single_log_path="$dir_log/$log_dir/${single_log_time}_$((i + 1)).log"
eval $timeoutCmd $which_program $file_param "${script_params[@]}" &>$single_log_path &
local j=0
for i in ${array_run[@]}; do
single_log_path="$dir_log/$log_dir/${single_log_time}_$((j + 1)).log"
let j++
if [[ $isJsOrPythonFile == 'false' ]]; then
export "${env_param}=${array[$i - 1]}"
clear_non_sh_env
fi
eval configDir="${dir_config}" envParam="${env_param}" numParam="${i}" $timeoutCmd $which_program $file_param "${script_params[@]}" &>$single_log_path &
done
wait
for i in "${!array[@]}"; do
single_log_path="$dir_log/$log_dir/${single_log_time}_$((i + 1)).log"
local k=0
for i in ${array_run[@]}; do
single_log_path="$dir_log/$log_dir/${single_log_time}_$((k + 1)).log"
let k++
cat $single_log_path
[[ -f $single_log_path ]] && rm -f $single_log_path
done
@@ -161,28 +173,27 @@ run_concurrent() {
run_designated() {
local file_param="$1"
local env_param="$2"
local num_param=$(echo "$3" | perl -pe "s|.*$2(.*)|\1|")
if [[ ! $env_param ]] || [[ ! $num_param ]]; then
echo -e "\n 缺少单独运行的参数 task xxx.js desi Test 1 3"
local num_param=$(echo "$3" | perl -pe "s|.*$2(.*)|\1|" | awk '{$1=$1};1')
if [[ ! $env_param ]]; then
echo -e "\n 缺少单独运行的参数 task xxx.js desi Test"
exit 1
fi
env_str_to_array
local tempArr=$(echo $num_param | sed "s/-max/-${#array[@]}/g" | sed "s/max-/${#array[@]}-/g" | perl -pe "s|(\d+)(-\|~\|_)(\d+)|{\1..\3}|g")
local runArr=($(eval echo $tempArr))
runArr=($(awk -v RS=' ' '!a[$1]++' <<<${runArr[@]}))
handle_env_split
local n=0
for i in ${runArr[@]}; do
array_run[n]=${array[$i - 1]}
let n++
done
local cookieStr=$(
IFS="&"
echo "${array_run[*]}"
)
[[ ! -z $cookieStr ]] && export "${env_param}=${cookieStr}"
if [[ $isJsOrPythonFile == 'false' ]]; then
local n=0
for i in ${array_run[@]}; do
array_str[n]=${array[$i - 1]}
let n++
done
local envStr=$(
IFS="&"
echo "${array_str[*]}"
)
[[ ! -z $envStr ]] && export "${env_param}=${envStr}"
clear_non_sh_env
fi
cd $dir_scripts
local relative_path="${file_param%/*}"
@@ -190,7 +201,8 @@ run_designated() {
cd ${relative_path}
file_param=${file_param/$relative_path\//}
fi
$timeoutCmd $which_program $file_param "${script_params[@]}"
configDir="${dir_config}" envParam="${env_param}" numParam="${num_param}" $timeoutCmd $which_program $file_param "${script_params[@]}"
}
## 运行其他命令
@@ -206,11 +218,26 @@ run_else() {
shift
clear_non_sh_env
$timeoutCmd $which_program $file_param "$@"
}
## 命令检测
main() {
isJsOrPythonFile="false"
if [[ $1 == *.js ]] || [[ $1 == *.py ]] || [[ $1 == *.pyc ]] || [[ $1 == *.ts ]]; then
isJsOrPythonFile="true"
fi
if [[ -f $file_env ]]; then
if [[ $isJsOrPythonFile == 'true' ]]; then
export NODE_OPTIONS="${NODE_OPTIONS} -r ${preload_js_file}"
export PYTHONPATH="${PYTHONPATH}:${dir_preload}"
else
get_env_array
. $file_env
fi
fi
if [[ $1 == *.js ]] || [[ $1 == *.py ]] || [[ $1 == *.pyc ]] || [[ $1 == *.sh ]] || [[ $1 == *.ts ]]; then
case $# in
1)
@@ -245,4 +272,5 @@ handle_task_start "${task_shell_params[@]}"
run_task_before "${task_shell_params[@]}"
main "${task_shell_params[@]}"
run_task_after "${task_shell_params[@]}"
clear_env
handle_task_end "${task_shell_params[@]}"
+30
View File
@@ -0,0 +1,30 @@
require(`./env.js`);
function expandRange(rangeStr, max) {
const tempRangeStr = rangeStr
.trim()
.replace(/-max/g, `-${max}`)
.replace(/max-/g, `${max}-`);
return tempRangeStr.split(' ').flatMap((part) => {
const rangeMatch = part.match(/^(\d+)([-~_])(\d+)$/);
if (rangeMatch) {
const [, start, , end] = rangeMatch.map(Number);
return Array.from({ length: end - start + 1 }, (_, i) => start + i);
}
return Number(part);
});
}
function run() {
if (process.env.envParam && process.env.numParam) {
const { envParam, numParam } = process.env;
const array = (process.env[envParam] || '').split('&');
const runArr = expandRange(numParam, array.length);
const arrayRun = runArr.map((i) => array[i - 1]);
const envStr = arrayRun.join('&');
process.env[envParam] = envStr;
}
}
run();
+44
View File
@@ -0,0 +1,44 @@
import os
import re
import env
def try_parse_int(value):
try:
return int(value)
except ValueError:
return None
def expand_range(range_str, max_value):
temp_range_str = (
range_str.strip()
.replace("-max", f"-{max_value}")
.replace("max-", f"{max_value}-")
)
result = []
for part in temp_range_str.split(" "):
range_match = re.match(r"^(\d+)([-~_])(\d+)$", part)
if range_match:
start, _, end = map(try_parse_int, range_match.groups())
result.extend(range(start, end + 1))
else:
result.append(int(part))
return result
def run():
env_param = os.getenv("envParam")
num_param = os.getenv("numParam")
if env_param and num_param:
array = (os.getenv(env_param) or "").split("&")
run_arr = expand_range(num_param, len(array))
array_run = [array[i - 1] for i in run_arr if i - 1 < len(array) and i > 0]
env_str = "&".join(array_run)
os.environ[env_param] = env_str
run()
+1 -1
View File
@@ -11,7 +11,7 @@ echo -e "提交master代码"
git push
echo -e "更新cdn文件"
tsx sample/tool.ts
ts-node-transpile-only sample/tool.ts
string=$(cat version.yaml | grep "version" | egrep "[^ ]*" -o | egrep "\d\.*")
version="v$string"
+18 -4
View File
@@ -10,6 +10,7 @@ if [[ $QL_DATA_DIR ]]; then
fi
dir_shell=$dir_root/shell
dir_preload=$dir_shell/preload
dir_sample=$dir_root/sample
dir_static=$dir_root/static
dir_config=$dir_data/config
@@ -25,7 +26,10 @@ ql_static_repo=$dir_repo/static
## 文件
file_config_sample=$dir_sample/config.sample.sh
file_env=$dir_config/env.sh
file_env=$dir_preload/env.sh
js_file_env=$dir_preload/env.js
py_file_env=$dir_preload/env.py
preload_js_file=$dir_preload/sitecustomize.js
file_sharecode=$dir_config/sharecode.sh
file_config_user=$dir_config/config.sh
file_auth_sample=$dir_sample/auth.sample.json
@@ -74,9 +78,6 @@ init_env() {
import_config() {
[[ -f $file_config_user ]] && . $file_config_user
if [[ $LOAD_ENV != 'false' ]] && [[ -f $file_env ]]; then
. $file_env
fi
ql_base_url=${QlBaseUrl:-"/"}
ql_port=${QlPort:-"5700"}
@@ -439,6 +440,19 @@ init_nginx() {
sed -i "s,IPV4_CONFIG,${ipv4Str},g" /etc/nginx/conf.d/front.conf
}
get_env_array() {
exported_variables=()
while IFS= read -r line; do
exported_variables+=("$line")
done < <(grep '^export ' $file_env | awk '{print $2}' | cut -d= -f1)
}
clear_env() {
for var in "${exported_variables[@]}"; do
unset "$var"
done
}
handle_task_start() {
[[ $ID ]] && update_cron "\"$ID\"" "0" "$$" "$log_path" "$begin_timestamp"
echo -e "## 开始执行... $begin_time\n"
+2 -6
View File
@@ -19,13 +19,9 @@ define_program() {
elif [[ $file_param == *.py ]] || [[ $file_param == *.pyc ]]; then
which_program="python3"
elif [[ $file_param == *.sh ]]; then
which_program="bash"
which_program="."
elif [[ $file_param == *.ts ]]; then
if ! type tsx &>/dev/null; then
which_program="ts-node-transpile-only"
else
which_program="tsx"
fi
which_program="ts-node-transpile-only"
else
which_program=""
fi