华为云BMS GO SDK和Python脚本实现裸金属服务器的操作系统切换
【摘要】 使用华为云BMS GO SDK和Python封装API两种方式,实现裸金属服务器的操作系统切换。
0. 背景描述
在满足如下限制条件后, 华为云BMS裸金属服务器在停机状态可以切换操作系统。
(1) 裸金属服务器状态为停止;
(2) 目标操作系统必须是该Region下的IMS公共镜像或者私有共享镜像.
笔者给出使用华为云BMS GO SDK, 以及Python封装API的两种方式实现该功能。
1. BMS Go SDK切换操作系统
package main
import (
"fmt"
"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 := "*****your access key id*********"
sk := "*****your secret access key*******"
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)
}
}
2. 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": "@DevServer",
"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")
3. 裸金属服务器操作系统切换事宜
(1) 切换中, 服务器状态为"切换操作系统中"
(2) 切换后,服务器状态为"运行中"
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)