前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于Python脚本批量重装轻量应用服务器系统

基于Python脚本批量重装轻量应用服务器系统

原创
作者头像
拖拉飞机
发布2024-02-02 22:48:50
1090
发布2024-02-02 22:48:50
举报
文章被收录于专栏:嘉钰踩坑实录嘉钰踩坑实录

背景:腾讯云轻量应用服务器一直没支持批量重装系统,于是用Python写了个脚本,处理同地域多台实例重装系统的需求。

代码语言:python
复制
import json
import csv
import time
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.lighthouse.v20200324 import lighthouse_client, models

# 设置您的腾讯云SecretID和SecretKey, 获取地址: https://console.cloud.tencent.com/cam/capi
secret_id = "your_secret_id"
secret_key = "your_secret_key"

csv_file = "instance_id.csv"
# 设置CSV文件路径,可以在轻量应用服务器选择导出实例信息,默认为csv文件

try:
    cred = credential.Credential(secret_id, secret_key)
    httpProfile = HttpProfile()
    httpProfile.endpoint = "lighthouse.tencentcloudapi.com"

    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    client = lighthouse_client.LighthouseClient(cred, "ap-guangzhou", clientProfile)
    # 注意地域

    req = models.ResetInstanceRequest()
    params = {
        "InstanceId": "",
        "BlueprintId": "lhbp-i0sjva9u",  
        # 设置要重装的镜像ID,注意镜像的地域要和实例的保持一致,例如:lhbp-hwey1fjw
        # 镜像ID可以通过请求DescribeBlueprints接口来实现
        # 参考:https://cloud.tencent.com/document/product/1207/47689
    }

    with open(csv_file, 'r') as file:
        csv_reader = csv.reader(file)
        next(csv_reader)  # 跳过标题行
        for row in csv_reader:
            instance_id = row[0]
            params["InstanceId"] = instance_id
            req.from_json_string(json.dumps(params))

            resp = client.ResetInstance(req)
            print("Instance %s reset success" % instance_id)
            time.sleep(1)  # 休眠1秒,防止频繁调用达到API限制

except TencentCloudSDKException as err:
    print("%s error: %s" % (instance_id, err))

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档