首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Softlayer Python API重启虚拟机

如何使用Softlayer Python API重启虚拟机
EN

Stack Overflow用户
提问于 2016-04-22 17:29:16
回答 2查看 800关注 0票数 1

我找不到在何处使用SoftLayer Python API VSManager重启或power-off/on虚拟机实例。

操作在XMLRPC API中进行了描述,网址为:

http://developer.softlayer.com/reference/services/SoftLayer_Virtual_Guest

但我在以下位置找不到对应的:

http://softlayer-python.readthedocs.org/en/latest/api/managers/vs.html

EN

回答 2

Stack Overflow用户

发布于 2016-04-25 22:18:44

实际上,管理器没有这样的实现,您必须为此进行api调用,下面是一些示例:

代码语言:javascript
复制
"""
Power off Guest

The scripts will look for a VSI which has an specific
hostname and the it powers off the VSI by making a single call
to the SoftLayer_Virtual_Guest::powerOff method.

Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Acount/
http://sldn.softlayer.com/reference/services/SoftLayer_Acount/getVirtualGuests
http://sldn.softlayer.com/reference/services/SoftLayer_Virtual_Guest/setTags

License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
import SoftLayer

"""
# Your SoftLayer API username and key.
#
# Generate an API key at the SoftLayer Customer Portal:
# https://manage.softlayer.com/Administrative/apiKeychain
"""
username = 'set me'
key = 'set me'

# The name of the machine you wish to power off
virtualGuestName = 'rctest'

# Declare a new API service object
client = SoftLayer.Client(username=username, api_key=key)


try:
    # Getting all virtual guest that the account has:
    virtualGuests = client['SoftLayer_Account'].getVirtualGuests()
except SoftLayer.SoftLayerAPIError as e:
    """
    If there was an error returned from the SoftLayer API then bomb out with the
    error message.
    """
    print("Unable to retrieve hardware. "
          % (e.faultCode, e.faultString))

# Looking for the virtual guest
virtualGuestId = ''
for virtualGuest in virtualGuests:
    if virtualGuest['hostname'] == virtualGuestName:
        virtualGuestId = virtualGuest['id']

try:
    # Power off the virtual guest
    virtualMachines = client['SoftLayer_Virtual_Guest'].powerOff(id=virtualGuestId)
    print ("powered off")
except SoftLayer.SoftLayerAPIError as e:
    """
    If there was an error returned from the SoftLayer API then bomb out with the
    error message.
    """
    print("Unable to power off the virtual guest"
          % (e.faultCode, e.faultString))

--

代码语言:javascript
复制
"""
Reboot Virtual Guest.
It reboots a SoftLayer Virtual Guest


Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Virtual_Guest/rebootDefault

License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
# So we can talk to the SoftLayer API:
import SoftLayer

# From pprint import pprint as pp
# For nice debug output
from pprint import pprint as pp

# Your SoftLayer API username and key.
API_USERNAME = 'set me'
API_KEY = 'set me'

# If you don't know your server id you can call getVirtualGuests() in the
# SoftLayer_Account API service to get a list of Virtual Guests
serverId = 10403817

# Create a connection to API service.
client = SoftLayer.Client(
        username=API_USERNAME,
        api_key=API_KEY
)

# Reboot the Virtual Guest
try:

    result = client['Virtual_Guest'].rebootDefault(id=serverId)
    pp(result)

except SoftLayer.SoftLayerAPIError as e:
        pp('Unable to reboot the server faultCode=%s, faultString=%s'
            % (e.faultCode, e.faultString))

问候

票数 3
EN

Stack Overflow用户

发布于 2016-04-22 17:49:29

slcli vs power_offslcli vs power_on应该可以做到这一点。Working with Virtual Servers

也许这也有帮助。Source of the reboot functionality from the cli

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36790047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档