This commit is contained in:
Administrator 2024-06-20 20:03:52 +08:00
parent 179b247f2c
commit 22657b8bd2
7 changed files with 168 additions and 6 deletions

Binary file not shown.

Binary file not shown.

View File

@ -1,5 +1,6 @@
from glance import image
def keystone_main():
from keystone import get_token
def glance_main():
print("====================")
print("选择一个操作:")
print("====================")
@ -9,3 +10,51 @@ def keystone_main():
choice = input("输入选项 (1-3): ")
ip = "172.30.26.171"
token = get_token.get_token(ip,'demo','admin','admin','000000')
if choice == '1':
print("====================")
print("镜像管理")
print("====================")
print("1. 创建镜像")
print("2. 列出镜像")
print("3. 获取镜像")
print("4. 删除镜像")
print("5. 上传镜像文件")
print("6. 更新镜像")
print("====================")
choice = input("输入选项 (1-6): ")
if choice == '1':
image_name = input("输入镜像名称: ")
image_description = input("输入镜像描述: ")
disk_format = input("输入镜像格式: ")
container_format = input("输入镜像容器格式: ")
result=image.create_image(ip, token, image_name, image_description,disk_format, container_format)
print(result)
glance_main()
if choice == '2':
image.get_image(ip, token)
result=image.get_image(ip, token)
print(result)
glance_main()
if choice == '3':
image_name = input("输入镜像名称: ")
result=image.show_image(ip, token, image_name)
print(result)
glance_main()
if choice == '4':
image_name = input("输入镜像名称: ")
result=image.delete_image(ip, token, image_name)
print(result)
glance_main()
if choice == '5':
image_name = input("输入镜像名称: ")
image_path = input("输入镜像路径: ")
result=image.upload_iamge_file(ip,token,image_name,image_path)
print(result)
glance_main()
if choice == '6':
image_name = input("输入镜像名称: ")
new_image_name = input("输入新镜像名称: ")
new_image_description = input("输入新镜像描述: ")
result=image.update_image(ip, token, image_name, new_image_name, new_image_description)
print(result)
glance_main()

View File

@ -0,0 +1,96 @@
import requests
import json
def create_image(ip, token, image_name, image_description,disk_format, container_format):
url = "http://" + ip + ":9292/v2/images"
headers = {
"X-Auth-Token": token,
"Content-Type": "application/json"
}
data = {
"name": image_name,
"description": image_description,
"disk_format": disk_format,
"container_format": container_format,
"visibility": "public"
}
response = requests.post(url, headers=headers, data=json.dumps(data))
return response.json()
def get_image(ip, token):
url = "http://" + ip + ":9292/v2/images"
headers = {
"X-Auth-Token": token,
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers)
return response.json()
def get_image_id(ip, token, image_name):
url = "http://" + ip + ":9292/v2/images"
headers = {
"X-Auth-Token": token,
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers)
image_id = ""
for image in response.json()["images"]:
if image["name"] == image_name:
image_id = image["id"]
return image_id
def show_image(ip, token, image_name):
image_id=get_image_id(ip, token, image_name)
if image_id is None:
return {"error":"镜像不存在"}
url = "http://" + ip + ":9292/v2/images/" + image_id
headers = {
"X-Auth-Token": token,
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers)
return response.json()
def delete_image(ip, token, image_name):
image_id=get_image_id(ip, token, image_name)
if image_id is None:
return {"error":"镜像不存在"}
url = "http://" + ip + ":9292/v2/images/" + image_id
headers = {
"X-Auth-Token": token,
"Content-Type": "application/json"
}
response = requests.delete(url, headers=headers)
if response.status_code == 204:
return {"message":"删除成功"}
else:
return {"error":"删除失败"+str(response.status_code)}
def upload_iamge_file(ip,token,image_name,image_path):
image_id=get_image_id(ip, token, image_name)
if image_id is None:
return {"error":"镜像不存在"}
url = "http://" + ip + ":9292/v2/images/" + image_id + "/file"
with open(image_path, "rb") as e:
image_data = e.read()
headers = {
"X-Auth-Token": token,
"Content-Type": "application/octet-stream"
}
response = requests.patch(url, headers=headers, data=image_data)
if response.status_code == 200:
return {"message":"上传成功"}
else:
return {"error":"上传失败"+str(response.status_code)}
def update_image(ip, token, image_name, new_image_name, new_image_description):
image_id=get_image_id(ip, token, image_name,)
if image_id is None:
return {"error":"镜像不存在"}
url = "http://" + ip + ":9292/v2/images/" + image_id
headers = {
"X-Auth-Token": token,
"Content-Type": "application/octet-stream"
}
data = {
"name": new_image_name,
"description": new_image_description,
}
response = requests.patch(url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
return {"message":"更新成功"}
else:
return {"error":"更新失败"+str(response.status_code)}

View File

@ -2,6 +2,9 @@ from keystone import get_token, group
from keystone import user
from keystone import domain
from keystone import project
ip = ""
token =""
import time
def keystone_main():
print("====================")
@ -15,8 +18,7 @@ def keystone_main():
print("5. 获取token")
print("====================")
choice = input("输入选项 (1-3): ")
ip = "172.30.26.171"
token = get_token.get_token(ip,'demo','admin','admin','000000')
if choice == '1':
print("====================")

19
main.py
View File

@ -1,3 +1,18 @@
from keystone import keystone_main
from keystone import get_token,keystone_main
from glance import glance_main
if __name__ == '__main__':
keystone_main.keystone_main()
ip="172.30.26.171"
token=get_token.get_token(ip, 'demo', 'admin', 'admin', '000000')
print("====================")
print("选择一个操作:")
print("====================")
print("1.Glance操作")
print("2.Keystone操作")
print("====================")
choice=input("输入选项 (1-2): ")
if choice=='1':
glance_main.glance_main()
elif choice=='2':
keystone_main.keystone_main()
else:
print("输入错误")