Skip to content

Commit

Permalink
Merge pull request #1612 from hLinx/3.6.x
Browse files Browse the repository at this point in the history
3.6.x
  • Loading branch information
hLinx authored Dec 13, 2022
2 parents b0bc42b + 198b184 commit 64cff95
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Magic Variable is a special variable that built-in specific to the JOB platform,

```bash
# job_import {{host_type_var_name}}

echo ${host_type_var_name}
```

Expand All @@ -36,63 +36,64 @@ Magic Variable is a special variable that built-in specific to the JOB platform,
```bash
# job_import {{JOB_LAST_ALL}}
# Get all host list of last step
# job_import {{JOB_LAST_SUCCESS}}
# Get success host list of last step
# job_import {{JOB_LAST_FAIL}}
# Get failure host list of last step
```

The result output format is "Cloud Area + : + inner IP addr", multiple IPs are separated by commas(,)

- To get value of other host's namespace variable
```bash
# job_import {{JOB_NAMESPACE_ALL}}
# Get value of all namespace variable
echo ${JOB_NAMESPACE_ALL}
# job_import {{JOB_NAMESPACE_varname}}
# Get value of specific namespace variable
echo ${JOB_NAMESPACE_varname}
```
Result (Example):
```bash
### echo ${JOB_NAMESPACE_ALL} (If there have two namespace variables: ns_var1 and ns_var2):
{"ns_var1":{"0:10.10.10.1":"xxxx","0:10.10.10.2":"yyyy","0:10.10.10.3":"zzzz"},"ns_var2":{"0:20.20.20.1":"aaaa","0:20.20.20.2":"bbbb","0:20.20.20.3":"cccc","0:20.20.20.4":"dddd"}}
### echo ${JOB_NAMESPACE_ns_var1}:
{"0:10.10.10.1":"xxxx","0:10.10.10.2":"yyyy","0:10.10.10.3":"zzzz"}
```
# Suggestions
- Two ways to processing JSON string with Shell (For reference only, it's recommended to be on-line after debug):

1. Using sed & awk :
```bash
#!/bin/bash
str='{"ns1":{"ip1":"value1","ip2":"value2"},"ns2":{"ip3":"value3","ip4":"value4"}}'
function json_parse {
local _json=$1
local _key=$2
temp=`echo $_json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $_key`
echo ${temp##*|}
}
json_parse $str ip1 ## Value of ip1 in $str variable, it will be "value1"
json_parse $str ns1 ## Value of ns1, it will be {"ip1":"value1","ip2":"value2"}
```

2. Using Python's json module :
```bash
#!/bin/bash
str='{"ns1":{"ip1":"value1","ip2":"value2"},"ns2":{"ip3":"value3","ip4":"value4"}}'
### To get value of ip2 in $str variable, it will be "value2"
echo ${str} | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["ns1"]["ip2"]'
```
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ JOB平台执行引擎提供的特有的变量能力

```bash
# job_import {{主机列表的全局变量名}}

echo ${主机列表的全局变量名}
```

Expand All @@ -35,62 +35,64 @@ JOB平台执行引擎提供的特有的变量能力
```bash
# job_import {{JOB_LAST_ALL}}
# 获取上一个步骤的所有执行主机IP列表
# job_import {{JOB_LAST_SUCCESS}}
# 获取上一个步骤执行成功的主机IP列表
# job_import {{JOB_LAST_FAIL}}
# 获取上一个步骤执行失败的主机IP列表
```

输出的格式同上: `云区域ID + 冒号 + 内网IP`,多个IP地址以逗号分隔

- 获取其他主机的命名空间变量值

```bash
# job_import {{JOB_NAMESPACE_ALL}}
# 获取所有命名空间变量的汇聚值
echo ${JOB_NAMESPACE_ALL}
# job_import {{JOB_NAMESPACE_命名空间变量名}}
# 获取某个命名空间变量的汇聚值
echo ${JOB_NAMESPACE_命名空间变量名}
```

输出结果(示例):

```bash
### echo ${JOB_NAMESPACE_ALL} 的输出(假定有 ns_var1 和 ns_var2 两个命名空间类型全局变量):
{"ns_var1":{"0:10.10.10.1":"xxxx","0:10.10.10.2":"yyyy","0:10.10.10.3":"zzzz"},"ns_var2":{"0:20.20.20.1":"aaaa","0:20.20.20.2":"bbbb","0:20.20.20.3":"cccc","0:20.20.20.4":"dddd"}}
### echo ${JOB_NAMESPACE_命名空间变量名} 的输出:
{"0:10.10.10.1":"xxxx","0:10.10.10.2":"yyyy","0:10.10.10.3":"zzzz"}
```

# 处理建议
- Shell 脚本处理 JSON字符串的两种方式(仅供参考,正式投入使用建议调试确认后再上线):

方式一:(纯 sed + awk 实现)
```bash
#!/bin/bash
str='{"ns1":{"ip1":"value1","ip2":"value2"},"ns2":{"ip3":"value3","ip4":"value4"}}'
function json_parse {
local _json=$1
local _key=$2
temp=`echo $_json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $_key`
echo ${temp##*|}
}
json_parse $str ip1 ## 获取 str 的 json字符串中 ip1 的值,即:value1
json_parse $str ns1 ## 获取 str 的 json字符串中 ns1 的值,即:{"ip1":"value1","ip2":"value2"}
```

方式二:(借助 Python 的 json 模块实现)
```bash
#!/bin/bash
str='{"ns1":{"ip1":"value1","ip2":"value2"},"ns2":{"ip3":"value3","ip4":"value4"}}'
### 获取 str 的 json字符串中 ip2 的值,即:value2
echo ${str} | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["ns1"]["ip2"]'
```
2 changes: 1 addition & 1 deletion src/frontend/src/views/task-manage/debug-plan/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
class="detail-item-title"
style="margin-bottom: 8px;">
<span>{{ $t('template.全局变量.label') }}</span>
<span>( {{ variableList.length }} / {{ selectedVariable.length }} )</span>
<span>( {{ selectedVariable.length }} / {{ variableList.length }} )</span>
</div>
<render-global-var
:list="variableList"
Expand Down

0 comments on commit 64cff95

Please sign in to comment.