全部產品
Search
文件中心

CloudOps Orchestration Service:ACS-ECS-BulkyInstallARMSAgent

更新時間:Sep 06, 2024

模板名稱

ACS-ECS-BulkyInstallARMSAgent 大量安裝ARMS外掛程式

立即執行

模板描述

使用這個模板批量的在ECS安裝ARMS外掛程式

模板類型

自動化

所有者

Alibaba Cloud

輸入參數

參數名稱

描述

類型

是否必填

預設值

約束

appName

要監控的應用程式名稱

String

licenseKey

應用監控功能的License Key

String

targets

目標執行個體

Json

regionId

地區ID

String

{{ ACS::RegionId }}

action

操作類型

String

install

rateControl

任務執行的並發比率

Json

{‘Mode’: ‘Concurrency’, ‘MaxErrors’: 0, ‘Concurrency’: 10}

OOSAssumeRole

OOS扮演的RAM角色

String

“”

輸出參數

參數名稱

描述

類型

commandOutput

String

執行此模板需要的權限原則

{
    "Version": "1",
    "Statement": [
        {
            "Action": [
                "ecs:DescribeInstances",
                "ecs:DescribeInvocationResults",
                "ecs:DescribeInvocations",
                "ecs:RunCommand"
            ],
            "Resource": "*",
            "Effect": "Allow"
        }
    ]
}

詳情

ACS-ECS-BulkyInstallARMSAgent詳情

模板內容

FormatVersion: OOS-2019-06-01
Description:
  en: Use this template to install ARMS agent on ecs
  zh-cn: 使用這個模板批量的在ECS安裝ARMS外掛程式
  name-en: ACS-ECS-BulkyInstallARMSAgent
  name-zh-cn: 大量安裝ARMS外掛程式
  categories:
    - run_command
Parameters:
  regionId:
    Type: String
    Label:
      en: RegionId
      zh-cn: 地區ID
    AssociationProperty: RegionId
    Default: '{{ ACS::RegionId }}'
  action:
    Type: String
    Label:
      en: Action
      zh-cn: 操作類型
    AllowedValues:
      - install
      - upgrade
      - uninstall
    Default: install
  appName:
    Type: String
    Label:
      en: AppName
      zh-cn: 要監控的應用程式名稱
  licenseKey:
    Type: String
    Label:
      en: LicenseKey
      zh-cn: 應用監控功能的License Key
  targets:
    Type: Json
    Label:
      en: TargetInstance
      zh-cn: 目標執行個體
    AssociationProperty: Targets
    AssociationPropertyMetadata:
      ResourceType: 'ALIYUN::ECS::Instance'
      RegionId: regionId
  rateControl:
    Label:
      en: RateControl
      zh-cn: 任務執行的並發比率
    Type: Json
    AssociationProperty: RateControl
    Default:
      Mode: Concurrency
      MaxErrors: 0
      Concurrency: 10
  OOSAssumeRole:
    Label:
      en: OOSAssumeRole
      zh-cn: OOS扮演的RAM角色
    Type: String
    Default: ''
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: getInstance
    Description:
      en: Views the ECS instances
      zh-cn: 擷取ECS執行個體
    Action: 'ACS::SelectTargets'
    Properties:
      ResourceType: 'ALIYUN::ECS::Instance'
      RegionId: '{{ regionId }}'
      Filters:
        - '{{ targets }}'
    Outputs:
      instanceIds:
        Type: List
        ValueSelector: 'Instances.Instance[].InstanceId'
      instanceInfos:
        Type: List
        ValueSelector: '.Instances.Instance[] | {"osType":.OSType, "instanceId":.InstanceId}'
  - Name: runCommand
    Action: 'ACS::ECS::RunCommand'
    When:
      'Fn::Equals':
        - 'Fn::Jq':
            - First
            - '.|map(select(.instanceId == "{{ ACS::TaskLoopItem }}").osType)[]'
            - '{{ getInstance.instanceInfos }}'
        - linux
    Description:
      en: Execute cloud assistant command
      zh-cn: 執行雲助手命令
    Properties:
      commandContent: |-
        #!/usr/bin/env bash
        regionId="{{ regionId }}"
        region=`echo $regionId|awk -F "-" '{print $2}'`
        action="{{action}}"
        licenseKey="{{licenseKey}}"
        agentBaseDir="$HOME/.arms"
        pidFileName="supervisor.pid"
        pidFile="${agentBaseDir}/${pidFileName}"
        appName="{{appName}}"
        if [[ "$regionId" =~ "cn-"  ]];then
          DOWNLOAD_URL="http://arms-apm-${region}.oss-${regionId}.aliyuncs.com/"
        elif [[ "$regionId" =~ "ap-southeast-1"  ]];then
          DOWNLOAD_URL="http://arms-apm-ap-southeast.oss-ap-southeast-1-internal.aliyuncs.com/cloud_ap-southeast-1/"
        elif [[ "$regionId" =~ "ap-northeast-1"  ]];then
          DOWNLOAD_URL="http://arms-apm-japan.oss-ap-northeast-1-internal.aliyuncs.com/"
        elif [[ "$regionId" =~ "us-west-1"  ]];then
          DOWNLOAD_URL="http://arms-apm-usw.oss-us-west-1-internal.aliyuncs.com/"
        else
          echo "Current ${regionId} is not supported."
          exit 1
        fi

        function operateJavaAgent() {
        appEnv="java"
        function installAgent() {
          wget -O- "${DOWNLOAD_URL}/install.sh" -q|sh
          currentProcess=`ps -ef |grep java|grep -v "grep --color" `
          if echo $currentProcess|grep "$appName" >/dev/null 2>&1; then
              processIndex=`echo null |${agentBaseDir}/supervisor/cli.sh "$licenseKey" $appName |grep "$appName"  |awk -F ' ' '{print $1}'`
              (echo $processIndex |${agentBaseDir}/supervisor/cli.sh "$licenseKey" $appName )  && echo "Automatically input application index $processIndex for attach $appName."
          else
            echo "It seems that no running $appName application."
          fi
        }

        function uninstallAgent() {
        if [ -f $pidFile ];
        then
            kill -9 `cat $pidFile` > /dev/null 2>&1
            echo "Finished to stop $pidFile."
        fi
        rm -rf $agentBaseDir
        }
        if [ $action = "install" ]; then
            if [ -d ${agentBaseDir}/agent ];
            then
                  uninstallAgent || exit 1
                  installAgent || exit 1
                  echo "Finished to install $appEnv arms agent."
            else
              installAgent || exit 1
              echo "Finished to install $appEnv arms agent."
            fi
        elif [ $action = "upgrade" ]; then
          installAgent || exit 1
          echo "Finished to upgrade $appEnv arms agent."
        else
          uninstallAgent || exit 1
          echo "Finished to uninstall $appEnv arms agent."
        fi
        }
        operateJavaAgent

      instanceId: '{{ ACS::TaskLoopItem }}'
      regionId: '{{ regionId }}'
      commandType: RunShellScript
    Loop:
      Items: '{{ getInstance.instanceIds }}'
      RateControl: '{{ rateControl }}'
      Outputs:
        commandOutputs:
          AggregateType: 'Fn::ListJoin'
          AggregateField: commandOutput
    Outputs:
      commandOutput:
        Type: String
        ValueSelector: invocationOutput
Outputs:
  commandOutput:
    Type: String
    Value: '{{ runCommand.commandOutputs }}'
Metadata:
  ALIYUN::OOS::Interface:
    ParameterGroups:
      - Parameters:
          - action
          - appName
          - licenseKey
        Label:
          default:
            zh-cn: 設定參數
            en: Configure Parameters
      - Parameters:
          - regionId
          - targets
        Label:
          default:
            zh-cn: 選擇執行個體
            en: Select Ecs Instances
      - Parameters:
          - rateControl
          - OOSAssumeRole
        Label:
          default:
            zh-cn: 進階選項
            en: Control Options