首页 文章

如何设置slcli Softlayer api

提问于
浏览
0

寻求帮助开始使用slcli以便对Softlayer机器运行命令以关闭,启动和重启vms . 我在ubuntu下安装的软件包14.04 sudo易于得到安装python-SOFTLAYER,现在试图运行slcli setup命令,但有麻烦定位从何处运行它,它不是在bash shell中的路径,也没有在蟒蛇后,我导入SoftLayer,我错过了什么才能继续前进?

2 回答

  • 0

    如果正确安装了软件包,您可以从任何位置运行 slcli config setup 命令 . 我通常建议使用pip来安装SoftLayer软件包 .

    sudo apt-get purge python-softlayer
    
    sudo apt-get install python-setuptools python-pip
    
    sudo pip install softlayer
    
  • 0

    确保正确安装了python和pip,然后运行:

    sudo apt-get install python-softlayer

    然而,如果这不起作用,那么尝试使用pip进行安装:

    sudo pip install softlayer
    

    一旦正确安装了slcli,运行 slcli 而不带参数应显示选项菜单,您可以使用 slcli [command] --help 获取额外信息

    使用 slcli setup 设置默认值,使用 slcli config show 显示默认值 .

    为了使用slcli管理vs,请使用以下命令:

    slcli vs list
    slcli vs power-on 1234567
    slcli vs power-off 1234567
    slcli vs reboot 1234567
    

    1234567 将是使用 slcli vs list 获取的虚拟访客ID

    也可以使用标准的python脚本完成上述操作,这里有一些例子:

    """
    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))
    

    重启

    """
        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))
    

相关问题