ZStack - 創建雲主機

/ 前言 /

       在ZStack的API中, 大多數的API返回的是一個任務結果查詢地址, 此時我們就需要根據這個地址輪訓去查詢任務狀態及結果

{ 
	"location": "http://localhost:8080/v1/api-jobs/967a26b7431c49c0b1d50d709ef1aef3" 
}

       創建雲主機之前我們需要先創建雲主機計算規格鏡像三層網絡物理機主存儲雲盤

/ API /

創建雲盤規格
  • API名稱

    創建雲盤規格(CreateDiskOffering)

  • 請求方式

    POST zstack/v1/disk-offerings

  • curl示例

    curl -H "Content-Type: application/json" \
    -H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
    -X POST -d '{"params":{"name":"diskOffering1","diskSize":100.0,"sortKey":0.0}}' \ 
    http://localhost:8080/zstack/v1/disk-offerings
    
  • 返回示例

    {"inventory": {
    	"uuid": "e7a865815cd34dd29812d335dc1019bf",
    	"name": "diskOffering1",
    	"diskSize": 100.0,
    	"state": "Enabled",
    	"type": "DefaultDiskOfferingType",
    	"allocatorStrategy": "DefaultPrimaryStorageAllocationStrategy"
    }}
    
創建雲主機
  • API名稱

    創建雲主機(CreateVmInstance)

  • 請求方式

    POST zstack/v1/vm-instances

  • curl示例

    curl -H "Content-Type: application/json" \
    -H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
    -X POST -d '{"params":{"name":"vm1","instanceOfferingUuid":"ae97ced44efc3314b8f7 798972b4ba1a","imageUuid":"da119f7906513eccabf271991c35a65e","l3NetworkUuids":["cc0e4c5e77df3af68e59668e7f9e06c5"],"dataDiskOfferingUuids":["19d22d051b063d379a28 16daaf431838","905d94a6abb5398fa1995f6398e3f6fc"],"clusterUuid":"a0468dc645223f67bd0f 2ab95276bbae","description":"this is a vm","strategy":"InstantStart"}}' \ 
    http://localhost:8080/zstack/v1/vm-instances
    
  • 返回示例

    { "inventory": {
    	"uuid": "6efab7159c6932429c6b0e7e0b64b660",
    	"name": "Test-VM",
    	"description": "web server VM",
    	"zoneUuid": "d10bd7a9565138efaf6171db8e18fa7d", 
    	"clusterUuid": "692c71639d873bf6a356cd8e285d0d9c", 
    	"imageUuid": "28061e65e48037eda71b53a0732bf087", 
    	"hostUuid": "f877177de7bd377e811bc6313878b2b1", 
    	"lastHostUuid": "a3ccdc90a600334f994c69aee4270ac5", 
    	"instanceOfferingUuid": "9a874241f867333ca0bf0ab2134f8515", 
    	"rootVolumeUuid": "f52d7c94d69d3bbb97d36a270fe910c3", 
    	"platform": "Linux",
    	"defaultL3NetworkUuid": "2ac1b70fdb0f38769a29c6ebd0262cca", 
    	"type": "UserVm",
    	"hypervisorType": "KVM",
    	"memorySize": 8.589934592E9,
    	"cpuNum": 1.0,
    	"allocatorStrategy": "LastHostPreferredAllocatorStrategy", 
    	"createDate": "Nov 14, 2017 10:20:57 PM",
    	"lastOpDate": "Nov 14, 2017 10:20:57 PM",
    	"state": "Running",
    	"vmNics": [
    	{ "uuid": "0dfbb8a297e9349ea8799c4ce48c6d10", 
    		"vmInstanceUuid": "6efab7159c6932429c6b0e7e0b64b660", 
    		"usedIpUuid": "39b57596a8ba35289bd9f40efceeae03", 
    		"l3NetworkUuid": "2ac1b70fdb0f38769a29c6ebd0262cca", 
    		"ip": "192.168.1.10",
    		"mac": "00:0c:29:bd:99:fc",
    		"netmask": "255.255.255.0",
    		"gateway": "192.168.1.1",
    		"deviceId": 0.0,
    		"createDate": "Nov 14, 2017 10:20:57 PM",
    	],} 
    	"lastOpDate": "Nov 14, 2017 10:20:57 PM"
    	}],
    	"allVolumes": [
    	{ "uuid": "f52d7c94d69d3bbb97d36a270fe910c3",
    		"name": "Root-Volume-For-VM-6efab7159c6932429c6b0e7e0b64b660", 
    		"primaryStorageUuid": "4ed5bbb150a034b5917e7e6c9c4b8170", 
    		"vmInstanceUuid": "6efab7159c6932429c6b0e7e0b64b660", 
    		"diskOfferingUuid": "0521187274d73f5b936c22b145384c74", 
    		"rootImageUuid": "28061e65e48037eda71b53a0732bf087",
    		"installPath": "/zstack_ps/rootVolumes/acct-36c27e8ff05c4780bf6d2fa65700f22e/vol- f52d7c94d69d3bbb97d36a270fe910c3/f52d7c94d69d3bbb97d36a270fe910c3.qcow2",
    		"type": "Root",
    		"format": "qcow2",
    		"size": 1.073741824E11,
    		"actualSize": 2.147483648E10,
    		"deviceId": 0.0,
    		"state": "Enabled",
    		"status": "Ready",
    		"createDate": "Nov 14, 2017 10:20:57 PM",
    		"lastOpDate": "Nov 14, 2017 10:20:57 PM"
    	}]
    } } 
    
創建雲盤
  • API名稱

    創建雲盤(CreateDataVolume)

  • 請求方式

    POST zstack/v1/volumes/data

  • curl示例

    curl -H "Content-Type: application/json" \
    -H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
    -X POST -d '{"params":{"name":"test-volume","description":"test-data-volume","diskOfferingUuid ":"d88594833a393e119c6fc5a8aa2ad08f","primaryStorageUuid":"5fe18eef72483c9cbde9 84a3176fab78","resourceUuid":"52f6075e4d6839fc8a40f569fb7d991e"}}' \ 
    http://localhost:8080/zstack/v1/volumes/data
    
  • 返回示例

    { "inventory": {
    	"uuid": "b9d6460b286441099fa9cb3fe5bce9ec",
    	"name": "test-volume",
    	"primaryStorageUuid": "d7d84bb097da41918ee27eeb3b92d4f4",
    	"vmInstanceUuid": "8732b8e254ad4db18a5cad5d04315948",
    	"diskOfferingUuid": "7e6d828db0b84d8cb62a44e3d4badc7d",
    	"rootImageUuid": "8384b89b03914655b888f9b4f864ab07",
    	"installPath": "/zstack_ps/rootVolumes/acct-36c27e8ff05c4780bf6d2fa65700f22e/vol-
    	b9d6460b286441099fa9cb3fe5bce9ec/b9d6460b286441099fa9cb3fe5bce9ec.qcow2", 
    	"type": "Root",
    	"format": "qcow2",
    	"size": 1.073741824E11,
    	"actualSize": 2.147483648E10, "deviceId": 0.0,
    	"state": "Enabled",
    	"status": "Ready",
    	"createDate": "May 11, 2017 1:22:51 PM",
    	"lastOpDate": "May 11, 2017 1:22:51 PM"
    } } 
    
掛載雲盤到雲主機上
  • API名稱

    掛載雲盤到雲主機上(AttachDataVolumeToVm)

  • 請求方式

    POST zstack/v1/volumes/{volumeUuid}/vm-instances/{vmInstanceUuid}

  • curl示例

    	curl -H "Content-Type: application/json" \
    -H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
    -X POST 
    http://localhost:8080/zstack/v1/volumes/6fdcff5868fe376a98359eba8b3d0efe/vm- instances/f6abc3b26ffe337ba61dd7298fb0b9df
    
  • 返回示例

    {"inventory": {
    	"uuid": "4c118c4e51e54f839e6e0af9e17527f2",
    	"name": "test-volume",
    	"primaryStorageUuid": "9cd9e87803864fa1ae6cf4001fb5d4c8",
    	"vmInstanceUuid": "8a6f516d0da04addb062c903dd000830",
    	"diskOfferingUuid": "bdb5f71d810c49a08ff0e91f81936009",
    	"rootImageUuid": "a12e3048aab340c1b2c6bad2e4187a3a",
    	"installPath": "/zstack_ps/rootVolumes/acct-36c27e8ff05c4780bf6d2fa65700f22e/vol- 4c118c4e51e54f839e6e0af9e17527f2/4c118c4e51e54f839e6e0af9e17527f2.qcow2", 
    	"type": "Root",
    	"format": "qcow2",
    	"size": 1.073741824E11,
    	"actualSize": 2.147483648E10,
    	"deviceId": 0.0,
    	"state": "Enabled",
    	"status": "Ready",
    	"createDate": "Jun 7, 2017 9:20:35 PM",
    	"lastOpDate": "Jun 7, 2017 9:20:35 PM"
    }}
    

/ 代碼 /

ZStack中, 大多數的API在調用後返回的是

user_name = 'admin'
user_password='password'
host = 'http://localhost:8080/'


# 創建雲主機
def create_vm(session_uuid, instance_uuid, image_uuid, l3_uuid_arr, host_uuid, storage_uuid):
    # 創建雲盤規格
    data = json.dumps({
        "params": {
            "name": "diskOffering",
            "diskSize": '1073741824'}})
    url = host + 'zstack/v1/disk-offerings'
    headers = {"Content-Type": "application/json", "Authorization": "OAuth " + session_uuid}
    response = requests.post(url, data, headers=headers)
    offer_uuid = deal_response(response, True)
    if offer_uuid:
        print('CreateDiskOffering successfully!')
        # 創建雲主機
        data = json.dumps({
            "params": {
                "name": "vm",
                "description": "this is a vm",
                "instanceOfferingUuid": instance_uuid,
                "imageUuid": image_uuid,
                "l3NetworkUuids": l3_uuid_arr,
                "dataVolumeSystemTags": [],
                "rootVolumeSystemTags": [],
                "rootDiskOfferingUuid": offer_uuid,
                "hostUuid": host_uuid
            }})
        print(data)
        url = host + 'zstack/v1/vm-instances'
        response = requests.post(url, data, headers=headers)
        vm_uuid = deal_response(response, True)
        if vm_uuid:
            print('CreateVmInstance successfully!')
            # 創建雲盤CreateDataVolume
            data = json.dumps({
                "params": {
                    "name": "vm",
                    "description": "this is a vm",
                    "diskOfferingUuid": offer_uuid,
                    "primaryStorageUuid": storage_uuid,
                },
                "systemTags": [
                    "localStorage::hostUuid::" + host_uuid
                ],
            })
            print(data)
            url = host + 'zstack/v1/volumes/data'
            response = requests.post(url, data, headers=headers)
            data_uuid = deal_response(response, True)
            if data_uuid:
                print('CreateDataVolume successfully!')
                # 掛載雲盤到雲主機
                print(data)
                url = host + 'zstack/v1/volumes/' + data_uuid + '/vm-instances/' + vm_uuid
                response = requests.post(url, None, headers=headers)
                deal_response(response, False)
                print('Virtual machine created successfully!')


# 處理返回數據
def deal_response(response, is_return):
    if response:
        rsp = json.loads(response.text)
        if rsp:
            print('rsp : {%s}' % rsp)
            json_str = query_until_done(rsp)
            if json_str:
                if not json_str.has_key('error'):
                    if is_return:
                        return json_str['inventory']['uuid']
                    else:
                        return True
    return False


# 輪詢查詢API結果
def query_until_done(rsp):
	# 截取任務id, 替換請求地址
        if rsp.has_key('location'):
        location = rsp['location']
        job_uuid = location.split('/')[-1]
        if job_uuid:
            while True:
                url = host + "zstack/v1/api-jobs/" + location.split('/')[-1]
                response = requests.get(url)
                text = response.text
                print(text)
                if text != '{}':
                    print('url : {%s}' % url)
                    return json.loads(text)


if __name__ == '__main__':
	session_uuid = login()
	if session_uuid:
		# 創建計算規格
        image_uuid = create_images(session_uuid)

/ ZStack全流程相關博文鏈接 /

ZStack - 登錄

ZStack - 創建區域、集羣

ZStack - 創建物理機

ZStack - 創建主存儲

ZStack - 創建2層3層網絡

ZStack - 創建雲主機計算規格

ZStack - 創建鏡像

ZStack - 創建雲主機計算規格

ZStack - 創建雲主機

ZStack - 全流程代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章