切换Lite Server服务器操作系统
场景描述
Lite Server为一台弹性裸金属服务器,您可以使用BMS服务提供的切换操作系统功能,对Lite Server资源操作系统进行切换。本文介绍以下三种切换操作系统的方式:
- 在BMS控制台切换操作系统
- 使用BMS Go SDK的方式切换操作系统
- 使用Python封装API的方式切换操作系统
切换操作系统需满足以下条件:
- 当前裸金属服务器状态为停止状态。
- 目标操作系统必须是该Region下的IMS公共镜像或者私有共享镜像。
在BMS控制台切换操作系统
- 获取操作系统镜像。
由华为云官方提供给客户操作系统镜像,在IMS镜像服务的共享镜像处进行接收即可,参考如下图操作。图1 共享镜像
- 切换操作系统。
对Lite Server资源对应的裸金属服务器,对其进行关机操作,完成关机后,才可以执行切换操作系统动作。
在裸金属服务的更多选项中,单击切换操作系统,如下图所示。
图2 切换操作系统
在切换操作系统界面,选择上一步接收到的共享镜像即可。
使用BMS Go SDK的方式切换操作系统
以下为BMS使用Go语言通过SDK方式切换操作系统的示例代码。
package main import ( "fmt" "os" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" bms "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/bms/v1" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/bms/v1/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/bms/v1/region" ) func main() { // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 ak := os.Getenv("HUAWEICLOUD_SDK_AK") sk := os.Getenv("HUAWEICLOUD_SDK_SK") auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). Build() client := bms.NewBmsClient( bms.BmsClientBuilder(). WithRegion(region.ValueOf("cn-north-4")). WithCredential(auth). Build()) keyname := "KeyPair-name" userdata := "aGVsbG8gd29ybGQsIHdlbGNvbWUgdG8gam9pbiB0aGUgY29uZmVyZW5jZQ==" request := &model.ChangeBaremetalServerOsRequest{ ServerId: "****input your bms instance id****", Body: &model.OsChangeReq{ OsChange: &model.OsChange{ Keyname: &keyname, Imageid: "****input your ims image id****", Metadata: &model.MetadataInstall{ UserData: &userdata, }, }, }, } response, err := client.ChangeBaremetalServerOs(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
Python封装API方式切换操作系统
以下为BMS使用Python语言通过API方式切换操作系统的示例代码。
# -*- coding: UTF-8 -*- import requests import json import time import requests.packages.urllib3.exceptions from urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) class ServerOperation(object): ################################ IAM认证API################################################# def __init__(self, account, password, region_name, username=None, project_id=None): """ :param username: if IAM user,here is small user, else big user :param account: account big big user :param password: account :param region_name: """ self.account = account self.username = username self.password = password self.region_name = region_name self.project_id = project_id self.ma_endpoint = "https://modelarts.{}.myhuaweicloud.com".format(region_name) self.service_endpoint = "https://bms.{}.myhuaweicloud.com".format(region_name) self.iam_endpoint = "https://iam.{}.myhuaweicloud.com".format(region_name) self.headers = {"Content-Type": "application/json", "X-Auth-Token": self.get_project_token_by_account(self.iam_endpoint)} def get_project_token_by_account(self, iam_endpoint): body = { "auth": { "identity": { "methods": [ "password" ], "password": { "user": { "name": self.username if self.username else self.account, "password": self.password, "domain": { "name": self.account } } } }, "scope": { "project": { "name": self.region_name } } } } headers = { "Content-Type": "application/json" } import json url = iam_endpoint + "/v3/auth/tokens" response = requests.post(url, headers=headers, data=json.dumps(body), verify=True) token = (response.headers['X-Subject-Token']) return token def change_os(self, server_id): url = "{}/v1/{}/baremetalservers/{}/changeos".format(self.service_endpoint, self.project_id, server_id) print(url) body = { "os-change": { "adminpass": "@Server", "imageid": "40d88eea-6e41-418a-ad6c-c177fe1876b8" } } response = requests.post(url, headers=self.headers, data=json.dumps(body), verify=False) print(json.dumps(response.json(), indent=1)) return response.json() if __name__ == '__main__': # 调用API前置准备,初始化认证鉴权信息 server = ServerOperation(username="xxx", account="xxx", password="xxx", project_id="xxx", region_name="cn-north-4") server.change_os(server_id="0c84bb62-35bd-4e1c-ba08-a3a686bc5097")