首页 文章 精选 留言 我的

精选列表

搜索[AI生成],共10000篇文章
优秀的个人博客,低调大师

运维编排系列场景-----快速生成模版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脚本的运行方式:运行命令:pythonoos_convert.pyxxx.sh (例如:pythonoos_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客户支持钉钉群:23330931OOS管理控制台的链接OOS帮助文档的链接

优秀的个人博客,低调大师

MaxComputer-Pyodps之mysql ddl 批量生成odps ddl

系统环境:windows 64 位python版本:2.7Maxcomputer(原ODPS) ----- 1、安装python2.7(略过,百度一下就可以) ----- 2、安装MySQLdb模块windows下个人习惯pip 安装 whl文件,虽然不是所有都能顺利安装,但是大部分还是可以的。下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python 下载到本地后,cmd进入dos环境,可以直接切换到下载目录,直接使用命令 pip install mysqlclient-1.3.13-cp27-cp27m-win_amd64.whl 或者pip-script.py install mysqlclient-1.3.13-cp27-cp27m-win_amd64.whl 编译方法:1、创建个xxx.py的文件,进入dos,切换到py文件路径:python xxx.py 2、使用编译工具:PyCharm、Maxcomputer Studio等工具执行 #!/usr/bin/python # -*- coding: UTF-8 -*- # author:zhy import MySQLdb ##### mysql数据库配置 def exec_sql(sql): conn=MySQLdb.connect(host='数据库地址',user='用户',passwd='密码',db=TABLE_SCHEMA,charset='utf8') cursor = conn.cursor() cursor.execute(sql) rows = cursor.fetchall() cursor.close() conn.close() return rows #### schema TABLE_SCHEMA='mysql' # mysql数据库元数据库 sys_id='s01' # ods入edw规范:表名+前缀 target_schema='odps_project' #odps对应项目名称 ##### 获取mysql表名 sql=''' select table_name from information_schema.`TABLES` where TABLE_SCHEMA='%s' and table_type='BASE TABLE' '''%TABLE_SCHEMA exec_relusts=exec_sql(sql) # sql执行结果为二维 out_comment='' # 定义输出参数 ###以下并不包含所有mysql字段类型的转换 for i in exec_relusts: TABLE_NAME = i[0] print TABLE_NAME sql=''' select TABLE_NAME ,COLUMN_NAME ,case when DATA_TYPE in ('varchar','char','date','longtext','text') then 'string' when DATA_TYPE in ('float') then 'double' when DATA_TYPE = 'tinyint' then 'int' when DATA_TYPE = 'timestamp' then 'datetime' else DATA_TYPE end as DATA_TYPE ,COLUMN_COMMENT from information_schema.`COLUMNS` where TABLE_SCHEMA = '%s' and TABLE_NAME = '%s' order by TABLE_NAME,ORDINAL_POSITION '''%(TABLE_SCHEMA,TABLE_NAME) rows=exec_sql(sql) sql=''' show create table %s.%s '''%(TABLE_SCHEMA,TABLE_NAME) sql_exec_result= exec_sql(sql) ddl_info0= sql_exec_result[0][1] if ddl_info0.find('COMMENT=')>1: ddl_table_common = ddl_info0[ddl_info0.index("COMMENT="):] ddl_table_common= ddl_table_common.replace('=',' ') else: ddl_table_common="COMMENT ''" ddl_info='CREATE TABLE IF NOT EXISTS %s.%s_%s (\n'%(target_schema,sys_id,TABLE_NAME) for row in rows: row3=row[3].replace('\r\n','\\r\\n') row3=row3.replace('\n','\\n') ddl_info=ddl_info+ str(row[1]).upper()+' '+row[2].upper()+' '+"COMMENT'"+ row3+"'\n," ddl_info=ddl_info+')' ddl_info=ddl_info.replace(')',')') ddl_info=ddl_info+'\n'+ddl_table_common+"\nPARTITIONED BY (PT STRING COMMENT '数据日期')".decode('utf-8') #如不需分区,需修改 out_comment= out_comment+'-- '+TABLE_NAME +'\n'+ddl_info +'\n;\n' # print out_comment # 将DDL语句输出到文件 Output_File=r'D:\data\output'+'\\'+sys_id+'_'+TABLE_SCHEMA+'.sql' file_w=file(Output_File,"w") file_w.write(out_comment.encode("utf-8")) file_w.close()

资源下载

更多资源
Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册