运维编排场景系列-----流水线操作函数计算,和运维


函数计算(FC)是事件驱动的全托管计算服务。使用函数计算,您无需采购与管理服务器等基础设施,只需编写并上传代码。在函数计算的基础上,为实现自动创建与运行函数计算,以及输出函数计算的运行结果。为简单执行上述的流水先操作,此时可以使用OOS服务来实现一系列的自动化动作。

前提条件

  1. 编写业务需求的脚本代码

模版说明:

本模版主要功能为:

  1. 创建资源栈

    1. 创建FC服务
    2. 创建FC函数
    3. 运行FC函数
  2. 等待资源栈至创建成功状态
  3. 删除资源栈
  4. 输出FC函数运行的结果
Description:
  en: Run function (FC) and output the result.
  zh-cn: 运行函数计算(FC)并输出结果。
FormatVersion: OOS-2019-06-01
Parameters:
  Handler:
    Type: String
    Description:
      en: 'Function execution entry, specific format and language related.'
      zh-cn: function执行的入口,具体格式和语言相关
  Runtime:
    Type: String
    Description:
      en: Function runtime environment.
      zh-cn: function的运行环境。
    AllowedValues:
      - nodejs6
      - nodejs8
      - python2.7
      - python3
      - java8
  Timeout:
    Type: Number
    Description:
      en: Function timeout.
      zh-cn: function运行的超时时间。
    MinValue: 1
    MaxValue: 300
    Default: 3
  SourceCode:
    Type: String
    Description:
      en: Function code.
      zh-cn: function的运行代码脚本。
  ParameterValue:
    Type: String
    Description:
      en: The value of the script code.
      zh-cn: 脚本代码的传参。
  OOSAssumeRole:
    Description: The RAM role to be assumed by OOS.
    Type: String
    Default: OOSServiceRole
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: createStack
    Action: 'ACS::ExecuteAPI'
    Description:
      en: Create a resource stack.
      zh-cn: 创建资源栈。
    Properties:
      Service: ROS
      API: CreateStack
      Parameters:
        StackName: 'OOS-{{ACS::ExecutionId}}'
        TimeoutInMinutes: 10
        DisableRollback: false
        Parameters:
          - ParameterKey: ParameterKeys
            ParameterValue: '{{ ParameterValue }}'
          - ParameterKey: FunctionName
            ParameterValue: 'OOS-{{ACS::ExecutionId}}'
          - ParameterKey: ServiceName
            ParameterValue: 'OOS-{{ACS::ExecutionId}}'
        TemplateBody: |-
          {
              "ROSTemplateFormatVersion": "2015-09-01",
              "Parameters": {
                  "ParameterKeys": {
                    "Type": "CommaDelimitedList"
                  },
                  "ServiceName": {
                      "Type": "String",
                      "Default": "test-fc-service"
                  },
                  "FunctionName": {
                      "Type": "String",
                      "Default": "test-fc-function"
                  },
                  "ExecuteVersion": {
                      "Type": "Number",
                      "Default": 0
                  }
              },
              "Resources": {
                  "Service": {
                      "Type": "ALIYUN::FC::Service",
                      "Properties": {
                          "ServiceName": {
                          "Ref": "ServiceName"
                          }
                      }
                  },
                  "Function": {
                      "Type": "ALIYUN::FC::Function",
                      "DependsOn": "Service",
                      "Properties": {
                              "ServiceName": {
                              "Ref": "ServiceName"
                          },
                              "FunctionName": {
                              "Ref": "FunctionName"
                          },
                          "Code": {
                              "SourceCode": "{{ SourceCode }}"
                          },
                          "Handler": "{{ Handler }}",
                          "Runtime": "{{ Runtime }}",
                          "Timeout": "{{ Timeout }}"
                          }
                      },
                      "FunctionInvoker": {
                          "Type": "ALIYUN::FC::FunctionInvoker",
                          "DependsOn": "Function",
                          "Properties": {
                              "ServiceName": {
                                  "Ref": "ServiceName"
                              },
                              "FunctionName": {
                                  "Ref": "FunctionName"
                              },
                              "Async": false,
                              "Event": {
                                  "Fn::Join": [",", {"Ref": "ParameterKeys"}]
                              },
                              "ExecuteVersion": {
                                  "Ref": "ExecuteVersion"
                              }
                          }
                      }
                  },
                  "Outputs": {
                      "Result": {
                          "Value": {
                              "Fn::Replace" : [
                              {
                                  "FcResult": {
                                  "Fn::GetAtt": [
                                      "FunctionInvoker",
                                      "Result"
                                  ]
                                  }
                              },
                              "FcResult"
                              ]
                          }
                      }
                  }
              }
    Outputs:
      StackId:
        Type: String
        ValueSelector: StackId
  - Name: untilStackReady
    Action: 'ACS::WaitFor'
    OnError: deleteStack
    Description:
      en: Wait the stack status util CREATE_COMPLETE.
      zh-cn: 等待资源栈至创建成功状态。
    Properties:
      Service: ROS
      API: GetStack
      Parameters:
        StackId: '{{createStack.StackId}}'
      DesiredValues:
        - CREATE_COMPLETE
      StopRetryValues:
        - CREATE_FAILED
        - CHECK_FAILED
        - ROLLBACK_FAILED
        - ROLLBACK_COMPLETE
      PropertySelector: Status
    Outputs:
      Result:
        Type: String
        ValueSelector: 'Outputs[].OutputValue'
  - Name: deleteStack
    Action: 'ACS::ExecuteApi'
    Description:
      en: Delete the stack.
      zh-cn: 删除资源栈。
    Properties:
      Service: ROS
      API: DeleteStack
      Parameters:
        StackId: '{{createStack.StackId}}'
Outputs:
  Result:
    Type: String
    Value: '{{ untilStackReady.Result }}'

模版参数说明:

参数 说明 示例
Handler function执行的入口,具体格式和语言相关(注:前部分为脚本文件名,后部分为脚本内函数名) index.lottery
Runtime function的运行环境,目前支持:
- nodejs6
- nodejs8
- python2.7
- python3
- java8
python3
Timeout function运行的超时时间,单位为秒,最小1秒,最长5分钟,默认3秒。 3
SourceCode 脚本代码。(注:需要将换行转化为\n) import randomndef lottery(event, context):\n    return '恭喜%s中奖' % random.choice([name.strip() for name in event.decode('utf-8').split(',')])
ParameterValue 用于脚本的参数值(注:在脚本中使用参数时,传入到脚本的参数为Bytes类型,需要在脚本代码中转译成需要的编码类型。) a,b,c,d
OOSAssumeRole 可选参数。
- (默认设置)当前账号的已有权限:执行您使用的账号的权限动作。请确保您拥有创建自定义镜像涉及的所有ECS API调用权限。
- 指定RAM角色,使用该角色的权限:如果指定了RAM角色名称,OOS扮演该RAM角色执行运维任务。
当前账号的已有权限

操作步骤

  1. 请访问运维编排OOS的控制台点击立即开通按钮开通服务。这是一个免费服务,请放心开通。1571987708792_20e8a66f_f200_4778_b144_a2bf5e4ac371
  2. 单击自定义模板,单击创建模板按钮,选取空白模板1571987744797_70bd927c_db33_4957_b817_31d49868e6ba
  3. 将上述提供的子模板粘贴到yaml栏,输入名称RunFcForLottery或自定义名称,点击创建模板1574221563415_e22cb4b8_d772_41d6_9d13_792d2bda7c54
  4. 单击创建执行1574221665034_7f24188e_954b_4da3_a3fc_1d74f3b8b542
  5. 保持基本信息的默认设置,单击下一步:设置参数
  6. 完成参数设置填写,单击下一步:确认1574221682262_259ad4c2_eca0_418d_8091_eefbf559b621
  7. 查看参数设置是否无误,确认无误后单击创建执行1574221712155_17c11e41_86c3_47ad_9bef_0d4d9c12f30d
  8. 执行管理中查看创建的运维任务。1574221824022_3e0e6a0f_876a_473a_8dfe_81b8cf3c73ab
  9. 执行结果如下所示,结果输出栏显示的是根据脚本中运行的结果。
    1574221936326_888dcf95_4083_491d_8da7_9f5141ac9a7d

附录:

此模版是使用的资源栈的形式运行的函数计算。使用函数计算可以将模版产生的数据在脚本内处理成业务需求的的格式或使用于特殊的需求。当以上述的OOS模版的方式来运行函数计算只需要根据实际情况补充参数,及编写的脚本代码即可实现自动创建函数计算,并输出函数计算的结果。

系列文章

主题文章

和通数据库重磅发布云上自动化利器——运维编排OOS

最佳实践

玩转运维编排服务的权限:Assume Role+Pass Role
和通数据库运维编排新功能:一键批量克隆ECS
批量在多台ECS内执行命令

场景系列

运维编排场景系列----更新ECS镜像
运维编排场景系列----给ECS实例自动打TAG
运维编排场景系列----从实例中拷贝文件到OSS
运维编排场景系列----给实例加到SLS机器组
运维编排场景系列----检测MFA功能状态
运维编排场景系列----每日统计多Region实例的运行状态
运维编排场景系列----如何使用jq
运维编排场景系列----分批到机器上运行命令
运维编排场景系列----更新镜像后自动更新伸缩配置镜像
运维编排场景系列----向Linux实例上传文件
运维编排场景系列----运行远端shell脚本
运维编排场景系列----在ECS实例上运行Ansible-playbook
运维编排场景系列----下载JVM堆栈到OSS
运维编排系列场景----将实例的固定公网IP转换为其它新EIP
运维编排场景系列----自动定时升级临时带宽
运维编排场景系列----批量更新云助手客户端
运维编排系列场景----批量释放实例
运维编排场景系列----ECS实例系统快照下载到本地
运维编排场景系列----批量更换系统盘
运维编排场景系列----自动创建多个区域同类型存储空间

相关内容