ZStack - 創建鏡像

/ 前言 /

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

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

       想要創建鏡像我們先需要創建鏡像服務器用來存儲鏡像, 鏡像服務器會在管理節點內部開闢一個目錄存儲鏡像, 而鏡像是創建雲主機所必需的條件

/ API /

添加鏡像倉庫服務器
  • API名稱

    添加鏡像倉庫服務器(AddImageStoreBackupStorage)

  • 請求方式

    POST zstack/v1/backup-storage/image-store

  • curl示例

    curl -H "Content-Type: application/json" \
    -H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
    -X POST -d '{"params":{"hostname":"192.168.1.8","username":"admin","password":"admin% pass","sshPort":22.0,"url":"/data/imagestore","name":"ImageStore","importImages":false}}' \ 
    http://localhost:8080/zstack/v1/backup-storage/image-store
    
  • 返回示例

    // 成功
    {}
    // 失敗
    {"error": {
    	"code": "SYS.1001",
    	"description": "A message or a operation timeout", 
    	"details": "Create VM on KVM timeout after 300s"
    }}
    
掛載鏡像服務器至區域
  • API名稱

    掛載鏡像服務器至區域(AttachBackupStorageToZone)

  • 請求方式

    POST zstack/v1/zones/{zoneUuid}/backup-storage/{backupStorageUuid}

  • curl示例

    curl -H "Content-Type: application/json" \
    -H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
    -X POST 
    http://localhost:8080/zstack/v1/zones/5776b543bc713f6d9abca9cd605c8199/backup- storage/53ef01cbf71d3bd68206908b87e51403
    
  • 返回示例

    {"inventory": {
    	"name": "My Backup Storage", 
    	"description": "Public Backup Storage", 
    	"totalCapacity": 1.073741824E9, 
    	"availableCapacity": 9.68884224E8, 
    	"type": "Ceph",
    	"state": "Enabled",
    	"status": "Connected",
    	"createDate": "Jun 7, 2017 9:20:19 PM", 
    	"lastOpDate": "Jun 7, 2017 9:20:19 PM", 
    	"attachedZoneUuids": [ "73799602dbf64c0d85694de34fc596c3"]
    }}
    
添加鏡像
  • API名稱

    添加鏡像(AddImage)

  • 請求方式

    POST zstack/v1/images

  • curl示例

    curl -H "Content-Type: application/json" \
    -H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
    -X POST -d '{"params":{"name":"TinyLinux","url":"http://192.168.1.20/share/images/tinylinux. qcow2","mediaType":"RootVolumeTemplate","system":false,"format":"qcow2","platform":"Linux ","backupStorageUuids":["b8fc9c1c027438c28d36af24eca06595"]}}' \ 
    http://localhost:8080/zstack/v1/images
    
  • 返回示例

    { "inventory": {
    	"uuid": "8c63603a810a4838a4cd62228e6e13b2", 
    	"name": "TinyLinux",
    	"url": "http://192.168.1.20/share/images/tinylinux.qcow2", 
    	"mediaType": "RootVolumeTemplate",
    	"platform": "Linux",
    	"format": "qcow2",
    	"backupStorageRefs": [
    		{ "id": 0.0,
    		"imageUuid": "8c63603a810a4838a4cd62228e6e13b2", 
    		"backupStorageUuid": "06ccc8e0322a4cd3ae5555d9d88451de", 
    		"installPath": "ceph://zs-images/f0b149e053b34c7eb7fe694b182ebffd",
    		"status": "Ready"
    		}
    	]
    }} 
    

/ 代碼 /

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

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


# 添加鏡像
def create_images(session_uuid):
	# 創建鏡像服務器
	data = json.dumps({
        "params":
            {"hostname": "192.168.0.129",
             "username": "root",
             "password": "password",
             "sshPort": 22,
             "url": "/zs_images",
             "name": "ImageStore",
             "type": "image-store",
             "description": "image_des"
             }})
    url = host + 'zstack/v1/backup-storage/image-store'
    headers = {"Content-Type": "application/json", "Authorization": "OAuth " + session_uuid}
    response = requests.post(url, data, headers=headers)
    store_uuid = deal_response(response, True)
    if store_uuid:
    	# 掛載鏡像服務器到區域
    	url = host + 'zstack/v1/zones/' + zone_uuid + '/backup-storage/' + store_uuid
	    response = requests.post(url, headers=headers)
	    if deal_response(response, False): 
	    	# 向鏡像服務器中添加鏡像
		    data = json.dumps({
		        "params": {
		            "name": "Linux",
		            "url": "http://47.103.76.2/iso/centos7.iso",
		            "mediaType": "RootVolumeTemplate",
		            "system": "false",
		            "format": "qcow2",
		            "platform": "Linux",
		            "backupStorageUuids": [store_uuid]}})
		    url = host + 'zstack/v1/images'
		    response = requests.post(url, data, headers=headers)
		    image_uuid = deal_response(response, True)
		    if image_uuid :
		        return image_uuid


# 處理返回數據
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 - 全流程代碼

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