运维编排系列场景-----快速生成模版shell命令
应用场景
当通过模版的方式在一台机器上运行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帮助文档的链接
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Android Studio运行Hello World程序
老的神舟本本装上了深度LINUX了。。。应该是基于ubuntu的,安装软件用的apt-get而不是yum 想重装学下android原生开发,官网下载了android studio, 发现不用FQ也能下载了,不错不错,支持支持 下载后解压了直接双击那个Android Studio.sh运行就行了,新建一个hello world项目,结果运行不起来,那个调试那个地方都没有绿色三角形, 想了下,好像没有装JAVA,又上ORACLE上下载了JAVA的安装,结果又总是下不了,晕。。不知道是不是得FQ。。。 然后不知道从哪里搜索出来的。。在命令行里装了。。装的是OPENJDK。。。装了以后再运行android studio ,还是运行不起来 最后看了下项目设置 File -> Project Structure ,发现NDK没有安装,安装以后再apply一下,再刷新下项目,结果就能运行上了 不错不错,支持支持,特此记录一下....(下载NDK 要800多M的,看下载域名是google.com的,想着得FQ下载的。。结果不用,我的南宁电信2M带宽10分钟左右就下载好了...)
- 下一篇
9月最新184道阿里、百度、腾讯、头条Java面试题合集
阿里面试题 如何实现一个高效的单向链表逆序输出? 已知sqrt(2)约等于1.414,要求不用数学库,求sqrt(2)精确到小数点后10位 给定一个二叉搜索树(BST),找到树中第 K 小的节点 LRU缓存机制 关于epoll和select的区别,以下哪些说法是正确的 从innodb的索引结构分析,为什么索引的 key 长度不能太长 MySQL的数据如何恢复到任意时间点? NFS 和 SMB 是最常见的两种 NAS(Network Attached Storage)协议,当把一个文件系统同时通过 NFS 和 SMB 协议共享给多个主机访问时,以下哪些说法是错误的 输入 ping IP 后敲回车,发包前会发生什么? 请解释下为什么鹿晗发布恋情的时候,微博系统会崩溃,如何解决? 现有一批邮件需要发送给订阅顾客,且有一个集群(集群的节点数不定,会动态扩容缩容)来负责具体的邮件发送任务,如何让系统尽快地完成发送? 有一批气象观测站,现需要获取这些站点的观测数据,并存储到 Hive 中。但是气象局只提供了 api 查询,每次只能查询单个观测点。那么如果能够方便快速地获取到所有的观测点的数据? 如...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS关闭SELinux安全模块
- SpringBoot2整合Redis,开启缓存,提高访问速度
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS8安装Docker,最新的服务器搭配容器使用
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果