使用 OpenStack 共享文件系统¶
在使用共享文件系统服务之前,您需要通过遵循 连接 用户指南来创建与您的 OpenStack 云的连接。这将为您提供在以下示例中使用的 conn 变量。
列出可用区¶
共享文件系统服务的可用区是您的共享文件系统的故障域。您可以在给定的可用区中创建一个共享文件系统(简称为共享),并在其他可用区中创建共享的副本。
def list_availability_zones(conn):
print("List Shared File System Availability Zones:")
for az in conn.share.availability_zones():
print(az)
共享实例¶
管理员可以列出、显示共享实例的信息、显式设置共享实例的状态以及强制删除共享实例。
def share_instances(conn, **query):
print('List all share instances:')
for si in conn.share.share_instances(**query):
print(si)
获取共享实例¶
显示单个共享实例的详细信息。
def get_share_instance(conn, share_instance_id):
print('Get share instance with given Id:')
share_instance = conn.share.get_share_instance(share_instance_id)
print(share_instance)
重置共享实例状态¶
显式更新共享实例的状态。
def reset_share_instance_status(conn, share_instance_id, status):
print(
'Reset the status of the share instance with the given '
'share_instance_id to the given status'
)
conn.share.reset_share_instance_status(share_instance_id, status)
删除共享实例¶
强制删除共享实例。
def delete_share_instance(conn, share_instance_id):
print('Force-delete the share instance with the given share_instance_id')
conn.share.delete_share_instance(share_instance_id)
调整共享大小¶
共享文件系统共享可以调整大小(扩展或缩小)到给定的尺寸。有关调整共享大小的详细信息,请参阅 Manila 文档。
def resize_share(conn, share_id, share_size):
# Be explicit about not wanting to use force if the share
# will be extended.
use_force = False
print('Resize the share to the given size:')
conn.share.resize_share(share_id, share_size, use_force)
def resize_shares_without_shrink(conn, min_size):
# Sometimes, extending shares without shrinking
# them (effectively setting a min size) is desirable.
# Get list of shares from the connection.
shares = conn.share.shares()
# Loop over the shares:
for share in shares:
# Extend shares smaller than min_size to min_size,
# but don't shrink shares larger than min_size.
conn.share.resize_share(share.id, min_size, no_shrink=True)
列出共享组快照¶
共享组快照是包含在共享组中的数据的某个时间点的只读副本。您可以列出所有共享组快照
def list_share_group_snapshots(conn, **query):
print("List all share group snapshots:")
share_group_snapshots = conn.share.share_group_snapshots(**query)
for share_group_snapshot in share_group_snapshots:
print(share_group_snapshot)
获取共享组快照¶
显示共享组快照详情
def get_share_group_snapshot(conn, group_snapshot_id):
print("Show share group snapshot with given Id:")
share_group_snapshot = conn.share.get_share_group_snapshots(
group_snapshot_id
)
print(share_group_snapshot)
列出共享组快照成员¶
列出所有共享组快照成员。
def share_group_snapshot_members(conn, group_snapshot_id):
print("Show share group snapshot members with given Id:")
members = conn.share.share_group_snapshot_members(group_snapshot_id)
for member in members:
print(member)
创建共享组快照¶
从共享组创建快照。
def create_share_group_snapshot(conn, share_group_id, **attrs):
print("Creating a share group snapshot from given attributes:")
share_group_snapshot = conn.share.create_share_group_snapshot(
share_group_id, **attrs
)
print(share_group_snapshot)
重置共享组快照¶
重置共享组快照状态。
def reset_share_group_snapshot_status(conn, group_snapshot_id, status):
print("Reseting the share group snapshot status:")
conn.share.reset_share_group_snapshot_status(group_snapshot_id, status)
更新共享组快照¶
更新一个共享组快照。
def update_share_group_snapshot(conn, group_snapshot_id, **attrs):
print("Updating a share group snapshot with given Id:")
share_group_snapshot = conn.share.update_share_group_snapshot(
group_snapshot_id, **attrs
)
print(share_group_snapshot)
删除共享组快照¶
删除一个共享组快照。
def delete_share_group_snapshot(conn, group_snapshot_id):
print("Deleting a share group snapshot with given Id:")
conn.share.delete_share_group_snapshot(group_snapshot_id)
列出共享元数据¶
列出给定共享的所有元数据。
def list_share_metadata(conn, share_id):
# Method returns the entire share with the metadata inside it.
returned_share = conn.get_share_metadata(share_id)
# Access metadata of share
metadata = returned_share['metadata']
print("List All Share Metadata:")
for meta_key in metadata:
print(f"{meta_key}={metadata[meta_key]}")
获取共享元数据项¶
通过其键从共享的元数据中检索特定的元数据项。
def get_share_metadata_item(conn, share_id, key):
# Method returns the entire share with the metadata inside it.
returned_share = conn.get_share_metadata_item(share_id, key)
# Access metadata of share
metadata = returned_share['metadata']
print("Get share metadata item given item key and share id:")
print(metadata[key])
创建共享元数据¶
创建共享元数据。
def create_share_metadata(conn, share_id, metadata):
# Method returns the entire share with the metadata inside it.
created_share = conn.create_share_metadata(share_id, metadata)
# Access metadata of share
metadata = created_share['metadata']
print("Metadata created for given share:")
print(metadata)
更新共享元数据¶
更新给定共享的元数据。
def update_share_metadata(conn, share_id, metadata):
# Method returns the entire share with the metadata inside it.
updated_share = conn.update_share_metadata(share_id, metadata, True)
# Access metadata of share
metadata = updated_share['metadata']
print("Updated metadata for given share:")
print(metadata)
删除共享元数据¶
通过其键从共享的元数据中删除特定的元数据项。可以指定多个要删除的键。
def delete_share_metadata(conn, share_id, keys):
# Method doesn't return anything.
conn.delete_share_metadata(share_id, keys)
管理共享¶
使用 Manila 管理共享。
def manage_share(conn, protocol, export_path, service_host, **params):
# Manage a share with the given protocol, export path, service host, and
# optional additional parameters
managed_share = conn.share.manage_share(
protocol, export_path, service_host, **params
)
# Can get the ID of the share, which is now being managed with Manila
managed_share_id = managed_share.id
print("The ID of the share which was managed: %s", managed_share_id)
取消管理共享¶
从 Manila 取消管理共享。
def unmanage_share(conn, share_id):
# Unmanage the share with the given share ID
conn.share.unmanage_share(share_id)
try:
# Getting the share will raise an exception as it has been unmanaged
conn.share.get_share(share_id)
except Exception:
pass