應用場景
當通過模版的方式在一台機器上運作shell檔案時,需要在模版中把目前的所有shell指令都需要手動操作寫進模版中,并添加需要寫入的shell檔案,尤其是遇到一些需要轉譯的特殊字元時,還需要手改,操作較為浪費時間。
解決方案
把目前需要修改的shell指令寫入一個本地shell檔案,通過python腳本的方式來實作把此檔案内的所有指令轉化為某一種特定的形式,及解決轉化後的腳本特殊字元寫進模版中轉譯的問題,轉化的腳本可以直接輸入到模版中運作,并保留格式。
一、轉化shell腳本
下面為用python實作的轉化腳本,并将腳本命名為:oos_convert
import re
import sys
commands = sys.argv
# 要翻譯的shell 腳本
file_path = '' or commands[1]
def translate():
with open(file_path, 'r+', encoding='utf-8') as f:
lines = f.readlines()
for index, line in enumerate(lines):
if index == 0:
continue
# print()
new_line = repr(line).replace('\\t', ' ').replace('\\n', '').strip("'")
if new_line.startswith('"'):
print(new_line + ',')
else:
rep_line = new_line.replace('"', '\\"')
print('"' + rep_line + '",')
translate()
Python腳本的運作方式:
運作指令:python oos_convert.py xxx.sh (例如:python oos_convert.py ~/command.sh)或者在pycharm等編輯工具中直接運作,在編輯工具中需要将file_path根據實際需求來補充。
如下所示為一個shell檔案内的指令
将以上python代碼寫入到一個自定義命名的py的檔案中,在指令行中用python運作此檔案,其運作結果如下所示,并将運作出來的結果複制到JSON格式的模版中。
二、打開
控制台,找到運維編排
三、建立模版
按如下所示編輯模版,并将python腳本轉化的内容,複制到下面的模版中。注意:此腳本轉化的内容僅支援JSON格式。
{
"FormatVersion": "OOS-2019-06-01",
"Description": "Creates a cloud assistant command and triggers it on one ECS instance.",
"Parameters": {
"instanceId": {
"Description": "The ID of ECS instance that will invoke command.",
"Type": "String",
"AllowedPattern": "i-[A-Za-z0-9]*",
"MinLength": 1,
"MaxLength": 30
},
"regionId": {
"Type": "String"
},
"OOSAssumeRole": {
"Description": "The RAM role to be assumed by OOS.",
"Type": "String",
"Default": "OOSServiceRole"
}
},
"RamRole": "{{ OOSAssumeRole }}",
"Tasks": [
{
"Name": "createCommand",
"Action": "ACS::ExecuteAPI",
"Description": "Creates a cloud assistant command.",
"Properties": {
"Service": "ECS",
"API": "CreateCommand",
"Parameters": {
"CommandContent": {
"Fn::Base64Encode": {
"Fn::Join": [
"\n",
[
"echo hello world",
"echo hello world",
"",
"echo \\$hello,this is aliyun",
"echo $hello,this is aliyun",
"",
"if [[ \"a\" == \"a\" ]]; then",
" echo hello",
"else",
" echo word",
"fi",
"",
"echo 'hi judy'"
]
]
}
},
"RegionId": "{{ regionId }}",
"Name": "{{ ACS::ExecutionId }}",
"Type": "RunShellScript",
"WorkingDir": "/root",
"Timeout": 30
}
},
"Outputs": {
"CommandId": {
"Type": "String",
"ValueSelector": "CommandId"
}
}
},
{
"Name": "invokeCommand",
"Action": "ACS::ExecuteAPI",
"Description": "Triggers a cloud assistant command on one ECS instances.",
"Properties": {
"Service": "ECS",
"API": "InvokeCommand",
"Parameters": {
"CommandId": "{{ createCommand.CommandId }}",
"InstanceIds": [
"{{ instanceId }}"
],
"RegionId": "{{regionId}}"
}
},
"Outputs": {
"InvokeId": {
"Type": "String",
"ValueSelector": "InvokeId"
}
}
},
{
"Name": "untilInvocationReady",
"Action": "ACS::WaitFor",
"Description": "Waits for the command to be completed.",
"Delay": 20,
"Retries": 30,
"DelayType": "Constant",
"Properties": {
"Service": "ECS",
"API": "DescribeInvocations",
"Parameters": {
"RegionId": "{{regionId}}",
"InvokeId": "{{ invokeCommand.InvokeId }}"
},
"DesiredValues": [
"Finished"
],
"StopRetryValues": [
"Failed"
],
"PropertySelector": "Invocations.Invocation[].InvokeStatus"
},
"OnError": "deleteCommand"
},
{
"Name": "describeInvocationResults",
"Action": "ACS::ExecuteAPI",
"Description": "Views the command output of a cloud assistant command in the specified ECS instance.",
"Properties": {
"Service": "ECS",
"API": "DescribeInvocationResults",
"Parameters": {
"RegionId": "{{regionId}}",
"InvokeId": "{{ invokeCommand.InvokeId }}"
}
},
"Outputs": {
"InvocationResult": {
"Type": "String",
"ValueSelector": "Invocation.InvocationResults.InvocationResult[].Output"
}
}
},
{
"Name": "checkInvocationResult",
"Action": "ACS::CheckFor",
"Description": "Views the command output of a cloud assistant command in the specified ECS instance.",
"Properties": {
"Service": "ECS",
"API": "DescribeInvocationResults",
"Parameters": {
"RegionId": "{{regionId}}",
"InvokeId": "{{ invokeCommand.InvokeId }}"
},
"PropertySelector": "Invocation.InvocationResults.InvocationResult[].ExitCode",
"DesiredValues": [
0
]
}
},
{
"Name": "deleteCommand",
"Action": "ACS::ExecuteAPI",
"Description": "Deletes a cloud assistant command.",
"Properties": {
"Service": "ECS",
"API": "DeleteCommand",
"Parameters": {
"RegionId": "{{ regionId}}",
"CommandId": "{{ createCommand.CommandId }}"
}
}
}
],
"Outputs": {
"InvocationOutput": {
"Type": "String",
"Value": {
"Fn::Base64Decode": "{{ describeInvocationResults.InvocationResult }}"
}
}
}
}
四、校驗模版,并格式化模版
腳本轉化完的模版格式如下所示,轉化的腳本,如果格式沒有對齊,點選滑鼠右鍵,選擇Format Doucument,來使模版格式化。注意:需要手動删除腳本最後一句的逗号。
五、建立執行
找到建立好的模版,點選建立執行
六、點選建立執行
模版開始正式執行,在輸入的執行個體上執行想要運作的shell指令。
總結
由以上舉例可見,此腳本的作用為手動操作節省了時間,并把在模版中解決了特殊字元轉譯的問題。此腳本還有很多不完善的地方,歡迎提出意見。
歡迎使用OOS
OOS客戶支援釘釘群:23330931
OOS管理控制台的連結 OOS幫助文檔的連結