此内容没有您所选择的语言版本。

A.4. Python SDK Example: Volumes


Create Volume
When a volume is created, the volume object needs a Red Hat Storage volume name, volume type and the bricks to be associated with the volume. Before creating a volume, you need to build a corresponding cluster object and associate that cluster with brick objects.
try:
    api = API(url=URL,
              username=USERNAME,
              password=PASSWORD,
              insecure=True)

    brick1 = params.GlusterBrick(
        brick_dir='/exports/music_1',
        server_id='0f5a7016-bb36-4f2b-a226-3b16c3eeca0c')

    brick2 = params.GlusterBrick(
        brick_dir='/exports/music_2',
        server_id='0f5a7016-bb36-4f2b-a226-3b16c3eeca0c')

    bricksParam = params.GlusterBricks()
    bricksParam.add_brick(brick1)
    bricksParam.add_brick(brick2)

    volume1 = params.GlusterVolume(
        name="music",
        cluster=api.clusters.get(name="Gluster"),
        volume_type="distribute",
        bricks=bricksParam)

    api.clusters.get("Gluster").glustervolumes.add(volume1)
    api.disconnect()

except Exception as e:
    print "Unexpected error: %s" % e
Copy to Clipboard Toggle word wrap
You can retrieve the server ID using the api.host collection.
glusterVolume Delete
def glusterVolumeDelete(clusterName, volumeName):
    try:
        API.clusters.get(clusterName).glustervolumes.get(volumeName).delete()
        return True
    except:
        return False
Copy to Clipboard Toggle word wrap
glusterVolumeStart
To start a volume, you need to pass the cluster name with which the Red Hat Storage volume is associated and the corresponding volume name to the glusterVolumeStart() function.
def glusterVolumeStart(clusterName, volumeName):
    try:
        API.clusters.get(clusterName).glustervolumes.get(volumeName).start()
        return True
    except:
        return False
Copy to Clipboard Toggle word wrap
glusterVolumeStop
To stop a volume, you need to pass the cluster name with which the Red Hat Storage volume is associated and the corresponding volume name to the glusterVolumeStart() function.
def glusterVolumeStop(clusterName, volumeName):
    try:
        API.clusters.get(clusterName).glustervolumes.get(volumeName).stop()
        return True
    except:
        return False
Copy to Clipboard Toggle word wrap
Rebalance operations: startRebalance, stopRebalance, and rebalanceStatus
To rebalance data among the servers, you need to pass the cluster name and the volume name to the startRebalance and stopRebalance functions and the jobID to the rebalanceStatus function.
import ovirtsdk
from ovirtsdk.api import API
from ovirtsdk.xml import params

# This function returns a stop rebalance task instance. Using this
# we can reterive the status of the stop rebalance.
# ex. volume = stopRebalance(clusterName, volumeName)
#     print volume.status.state

def stopRebalance(clusterName, volumeName):
    volume = api.clusters.get(clusterName).glustervolumes.get(volumeName)
    try:
        return volume.stoprebalance()
    except ovirtsdk.infrastructure.errors.RequestError, e:
        print "Error"

def startRebalance(clusterName, volumeName):
    volume = api.clusters.get(clusterName).glustervolumes.get(volumeName)
    return volume.rebalance()

def rebalanceStatus(jobId):
    # jobs.get accepts job name and job id as an optional
    # parameters to provide the job details.
    return api.jobs.get(None, jobId)

clusterName="Default"   # name of the cluser
volumeName="music"      # name of the volume which belongs
                        # to the purticular cluster
try:
    api = API (url="https://HOST",
               username="USER",
               password="PASS",
               ca_file="ca.crt")

    status = startRebalance(clusterName, volumeName)
    jobId = status.job.get_id()
    # jobId return by startRebalance can be stored somewhere
    # for later use. With out the jobId it would be very
    # difficult to find out the actual status of this task.
    print jobId
    privStatus = None
    while True:
        status = rebalanceStatus(jobId)
        jobStatus = status.get_status().get_state()
        if jobStatus != privStatus:
            print "Description: ", status.get_description()
            print "Status:      ", jobStatus
        if "FINISHED" == jobStatus:
            break
        privStatus = jobStatus

    api.disconnect()
except Exception as ex:
    print "Unexpected error: %s" % ex
Copy to Clipboard Toggle word wrap
22632%2C+Console+Developer+Guide-322-09-2014+17%3A11%3A35Report a bug
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat