雲計算

使用OOS自動刪除7天前創建的快照

OOS簡介
Operation Orchestration Service,簡稱OOS,是全面、免費的雲上自動化運維平臺,提供運維任務的管理和執行。典型使用場景包括:事件驅動運維,批量操作運維,定時運維任務,跨地域運維等,OOS為重要運維場景提供審批,通知等功能。OOS幫您實現標準化運維任務,從而實踐運維即代碼(Operations as Code)的先進理念。關於OOS更詳細的介紹請參見什麼是運維編排服務

場景介紹
基於OOS實現自動刪除創建於7天前的快照。

操作步驟
1.登陸OOS控制檯,找到我的模板,點擊創建模板
image.png
2.選擇空白模版
image.png

3.複製下文提供的模板到YAML欄中,輸入模板名稱(Delete-ExpiredSnapshot),點擊創建模板
image.png

模板(Delete-ExpiredSnapshot)內容:

FormatVersion: OOS-2019-06-01
Description: ''
Parameters:
  regionId:
    Type: String
    Description:
      en: The id of region.
      zh-cn: 地域ID。
    AssociationProperty: RegionId
    Default: '{{ ACS::RegionId }}'
  force:
    Description:
      en: Specifies whether to forcibly delete the snapshot that has been used to create disks.
      zh-cn: 是否強制刪除有磁盤關聯的快照。
    Type: Boolean
    Default: false
  expiredDays:
    Description:
      en: The expired days of snapshot.
      zh-cn: 快照過期天數。
    Type: Number
    Default: 7
  rateControl:
    Description:
      en: Concurrency ratio of task execution.
      zh-cn: 任務執行的併發比率。
    Type: Json
    AssociationProperty: RateControl
    Default:
      Mode: Concurrency
      MaxErrors: 100%
      Concurrency: 10
Tasks:
  - Name: describeSnapshots
    Action: 'ACS::ExecuteAPI'
    Description: ''
    Properties:
      Service: ECS
      API: DescribeSnapshots
      Parameters:
        RegionId: '{{ regionId }}'
        Filter2Key: CreationEndTime
        Filter2Value:
          'Fn::FormatUTCTime':
            - 'Fn::AddHour':
                - '{{ ACS::CurrentUTCTime }}'
                - "Fn::Eval":
                    - '-{{ expiredDays }}*24'
            - '%Y-%m-%dT00:00Z'
    Outputs:
      snapshotIds:
        Type: List
        ValueSelector: '.Snapshots.Snapshot[].SnapshotId'
  - Name: deleteSnapshots
    Action: 'ACS::ExecuteAPI'
    Description:
      en: Delete the expired snapshots.
      zh-cn: 刪除過期快照。
    Properties:
      Service: ECS
      API: DeleteSnapshot
      Parameters:
        RegionId: '{{ regionId }}'
        SnapshotId: '{{ ACS::TaskLoopItem }}'
        Force: '{{ force }}'
    Loop:
      Items: '{{ describeSnapshots.snapshotIds }}'
      RateControl: '{{ rateControl }}'
Outputs:
  deletedSnapshotIds:
    Type: List
    Value: '{{ describeSnapshots.snapshotIds }}'

定時執行模版
1.找到定時運維,點擊創建,選擇週期性重複執行,設置定時執行的規則。
截圖所示規則:截止規則結束時間,每天0點0分定時執行制指定模板
image.png

2.模板選擇,選擇上文創建的模板。參數設置,選擇目標地域,點擊確認風險並執行。
image.png

3.在執行詳情頁中可以查看定時執行列表和所操作的資源。
image.png

Leave a Reply

Your email address will not be published. Required fields are marked *