中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

更加便捷地完成云服務器的釋放以及彈性設置的方法

發布時間:2020-08-28 11:29:38 來源:億速云 閱讀:292 作者:小新 欄目:建站服務器

這篇文章給大家分享的是有關更加便捷地完成云服務器的釋放以及彈性設置的方法的內容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。

彈性釋放 ECS 實例

本文將涉及到幾個重要功能和相關API:

釋放按量付費的云服務器

設置按量付費實例的自動釋放時間

停止服務器

查詢實例列表

釋放后,實例所使用的物理資源將被回收,包括磁盤及快照,相關數據將全部丟失且永久不可恢復。如果您還想繼續使用相關的數據,建議您釋放云服務器之前一定要對磁盤數據做快照,下次創建 ECS 時可以直接通過快照創建資源。

釋放云服務器

釋放服務器,首先要求您的服務器處于停止狀態。當服務器停止后,若影響到應用,您可以將服務器重新啟動。

停止云服務器

停止服務器的指令非常簡單,且對于按量付費和包年包月都是一樣的。停止云服務器的一個參數是 ForceStop,若屬性設置為 true,它將類似于斷電,直接停止服務器,但不承諾數據能寫到磁盤中。如果僅僅為了釋放服務器,這個可以設置為 true。

def stop_instance(instance_id, force_stop=False):
    '''
    stop one ecs instance.
    :param instance_id: instance id of the ecs instance, like 'i-***'.
    :param force_stop: if force stop is true, it will force stop the server and not ensure the data
    write to disk correctly.
    :return:
    '''
    request = StopInstanceRequest()
    request.set_InstanceId(instance_id)
    request.set_ForceStop(force_stop)
    logging.info("Stop %s command submit successfully.", instance_id)
    _send_request(request)

釋放云服務器

如果您沒有停止服務器直接執行釋放,可能會有如下報錯:

{"RequestId":"3C6DEAB4-7207-411F-9A31-6ADE54C268BE","HostId":
"ecs-cn-hangzhou.aliyuncs.com","Code":"IncorrectInstanceStatus","Message":"
The current status of the resource does not support this operation."}

當服務器處于Stopped狀態時,您可以執行釋放服務器。釋放服務器的方法比較簡單,參數如下:

InstanceId: 實例的 ID

force: 如果將這個參數設置為 true,將會執行強制釋放。即使云服務器不是Stopped狀態也可以釋放。執行的時候請務必小心,以防錯誤釋放影響您的業務。

釋放云服務器的Request如下:

def release_instance(instance_id, force=False):
    '''
    delete instance according instance id, only support after pay instance.
    :param instance_id: instance id of the ecs instance, like 'i-***'.
    :param force:
    if force is false, you need to make the ecs instance stopped, you can
    execute the delete action.
    If force is true, you can delete the instance even the instance is running.
    :return:
    '''
    request = DeleteInstanceRequest();
    request.set_InstanceId(instance_id)
    request.set_Force(force)
    _send_request(request)

釋放云服務器成功的 Response 如下:

{"RequestId":"689E5813-D150-4664-AF6F-2A27BB4986A3"}

設置云服務器的自動釋放時間

為了更加簡化對云服務器的管理,您可以自定義云服務器的釋放時間。當定時時間到后,阿里云將自動為您完成服務器的釋放, 無需手動執行釋放。

注意:自動釋放時間按照 ISO8601 標準表示,并需要使用 UTC 時間。 格式為:yyyy-MM-ddTHH:mm:ssZ。 如果秒不是 00,則自動取為當前分鐘開始時。自動釋放的時間范圍:當前時間后 30 分鐘 ~ 當前時間起 3 年。

def set_instance_auto_release_time(instance_id, time_to_release = None):
    '''
    setting instance auto delete time
    :param instance_id: instance id of the ecs instance, like 'i-***'.
    :param time_to_release: if the property is setting, such as '2017-01-30T00:00:00Z'
    it means setting the instance to be release at that time.
    if the property is None, it means cancel the auto delete time.
    :return:
    '''
    request = ModifyInstanceAutoReleaseTimeRequest()
    request.set_InstanceId(instance_id)
    if time_to_release is not None:
        request.set_AutoReleaseTime(time_to_release)
    _send_request(request)

執行 set_instance_auto_release_time(‘i-1111’, ‘2017-01-30T00:00:00Z’) 后完成設置。

執行設置成功后,您可以通過DescribeInstances來查詢自動釋放的時間設置。

def describe_instance_detail(instance_id):
    '''
    describe instance detail
    :param instance_id: instance id of the ecs instance, like 'i-***'.
    :return:
    '''
    request = DescribeInstancesRequest()
    request.set_InstanceIds(json.dumps([instance_id]))
    response = _send_request(request)
    if response is not None:
        instance_list = response.get('Instances').get('Instance')
        if len(instance_list) > 0:
            return instance_list[0]
def check_auto_release_time_ready(instance_id):
    detail = describe_instance_detail(instance_id=instance_id)
    if detail is not None:
        release_time = detail.get('AutoReleaseTime')
        return release_time

取消自動釋放設置

如果您的業務有變化,需要取消自動釋放設置。只需執行命令將自動釋放時間設置為空即可。

set_instance_auto_release_time('i-1111')

完整代碼如下:

注意:釋放云服務器需謹慎。

#  coding=utf-8
# if the python sdk is not install using 'sudo pip install aliyun-python-sdk-ecs'
# if the python sdk is install using 'sudo pip install --upgrade aliyun-python-sdk-ecs'
# make sure the sdk version is 2.1.2, you can use command 'pip show aliyun-python-sdk-ecs' to check
import json
import logging
from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526.DeleteInstanceRequest import DeleteInstanceRequest
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
from aliyunsdkecs.request.v20140526.ModifyInstanceAutoReleaseTimeRequest import \
    ModifyInstanceAutoReleaseTimeRequest
from aliyunsdkecs.request.v20140526.StopInstanceRequest import StopInstanceRequest
# configuration the log output formatter, if you want to save the output to file,
# append ",filename='ecs_invoke.log'" after datefmt.
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S')
clt = client.AcsClient('Your Access Key Id', 'Your Access Key Secrect', 'cn-beijing')
def stop_instance(instance_id, force_stop=False):
    '''
    stop one ecs instance.
    :param instance_id: instance id of the ecs instance, like 'i-***'.
    :param force_stop: if force stop is true, it will force stop the server and not ensure the data
    write to disk correctly.
    :return:
    '''
    request = StopInstanceRequest()
    request.set_InstanceId(instance_id)
    request.set_ForceStop(force_stop)
    logging.info("Stop %s command submit successfully.", instance_id)
    _send_request(request)
def describe_instance_detail(instance_id):
    '''
    describe instance detail
    :param instance_id: instance id of the ecs instance, like 'i-***'.
    :return:
    '''
    request = DescribeInstancesRequest()
    request.set_InstanceIds(json.dumps([instance_id]))
    response = _send_request(request)
    if response is not None:
        instance_list = response.get('Instances').get('Instance')
        if len(instance_list) > 0:
            return instance_list[0]
def check_auto_release_time_ready(instance_id):
    detail = describe_instance_detail(instance_id=instance_id)
    if detail is not None:
        release_time = detail.get('AutoReleaseTime')
        return release_time
def release_instance(instance_id, force=False):
    '''
    delete instance according instance id, only support after pay instance.
    :param instance_id: instance id of the ecs instance, like 'i-***'.
    :param force:
    if force is false, you need to make the ecs instance stopped, you can
    execute the delete action.
    If force is true, you can delete the instance even the instance is running.
    :return:
    '''
    request = DeleteInstanceRequest();
    request.set_InstanceId(instance_id)
    request.set_Force(force)
    _send_request(request)
def set_instance_auto_release_time(instance_id, time_to_release = None):
    '''
    setting instance auto delete time
    :param instance_id: instance id of the ecs instance, like 'i-***'.
    :param time_to_release: if the property is setting, such as '2017-01-30T00:00:00Z'
    it means setting the instance to be release at that time.
    if the property is None, it means cancel the auto delete time.
    :return:
    '''
    request = ModifyInstanceAutoReleaseTimeRequest()
    request.set_InstanceId(instance_id)
    if time_to_release is not None:
        request.set_AutoReleaseTime(time_to_release)
    _send_request(request)
    release_time = check_auto_release_time_ready(instance_id)
    logging.info("Check instance %s auto release time setting is %s. ", instance_id, release_time)
def _send_request(request):
    '''
    send open api request
    :param request:
    :return:
    '''
    request.set_accept_format('json')
    try:
        response_str = clt.do_action(request)
        logging.info(response_str)
        response_detail = json.loads(response_str)
        return response_detail
    except Exception as e:
        logging.error(e)
if __name__ == '__main__':
    logging.info("Release ecs instance by Aliyun OpenApi!")
    set_instance_auto_release_time('i-1111', '2017-01-28T06:00:00Z')
    # set_instance_auto_release_time('i-1111')
    # stop_instance('i-1111')
    # release_instance('i-1111')
    # release_instance('i-1111', True)

感謝各位的閱讀!關于更加便捷地完成云服務器的釋放以及彈性設置的方法就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

任丘市| 遂川县| 贵阳市| 绥化市| 来安县| 溧阳市| 循化| 吉木萨尔县| 平顺县| 怀安县| 会泽县| 宁津县| 饶河县| 赤水市| 昌图县| 洛南县| 吉安县| 博乐市| 清流县| 斗六市| 太湖县| 乐山市| 怀来县| 嫩江县| 周宁县| 汕尾市| 镇康县| 嘉峪关市| 高唐县| 德化县| 文昌市| 海口市| 周至县| 比如县| 林州市| 宝丰县| 兴安盟| 凤翔县| 泰宁县| 于田县| 广昌县|