Summer's Blog
😈酷炫主页
✨运维
🎉安装
👀踩坑
  • k8s
  • shell
  • python
  • redis
  • elasticsearch
  • mysql
  • ceph
  • spark
  • 关于
  • 思维
  • 命令
  • 友链
  • 分类
  • 标签
  • 归档
👨‍👩‍👦‍👦腾讯云社区
🗣GitHub

Summer———夏苏文

💨运维界的前行者
😈酷炫主页
✨运维
🎉安装
👀踩坑
  • k8s
  • shell
  • python
  • redis
  • elasticsearch
  • mysql
  • ceph
  • spark
  • 关于
  • 思维
  • 命令
  • 友链
  • 分类
  • 标签
  • 归档
👨‍👩‍👦‍👦腾讯云社区
🗣GitHub
  • K8s

  • shell

  • spark

  • Python

    • Python实现ssh远程执行
    • Python搭建http共享文件
    • Python合并多个excl表格
    • Python提取log参数生成图表
    • Python算法-冒泡排序
    • Python算法-查找
    • Python算法-汉诺塔
    • Python通过librbd操作ceph
    • Python统计OSD上PG的数量
    • Python预估ceph集群恢复时间
  • Redis

  • ceph

  • Elasticsearch

  • Mysql

  • 学习
  • Python
summer
2022-02-28

Python通过librbd操作ceph

通过Librbd调用ceph,测试rbd创建删除操作

# -*- coding: utf-8 -*-
"""
@Time    : 2022/2/28 10:07
@Author  : summer
@File    : test_librbd.py
@Software: PyCharm
"""

import rados, rbd


def main():
    try:
        cluster = rados.Rados(conffile='/etc/ceph/ceph.conf')
    except TypeError as e:
        print 'Argument validation error: ', e
        raise e
    print "------------------------------"
    print "Created cluster handle."

    try:
        cluster.connect()
    except Exception as e:
        print "Connectoin error: ", e
        raise e
    finally:
        print "------------------------------"
        print "Connected to the cluster."
        print "Start Test LibRbd.\n"
        return cluster


def cluster_shutdown(cluster):
    print "------------------------------"
    print "Shutting down th handle."
    cluster.shutdown()


def cluster_stats(cluster):
    print "------------------------------"
    print "Cluster Status:"
    stats = cluster.get_cluster_stats()
    print stats
    for key, value in stats.iteritems():
        print str(key) + " -> " + str(value)


def pool_list(cluster):
    print "------------------------------"
    print "List Available Pools:"
    pools = cluster.list_pools()
    for pool in pools:
        print pool


def pool_create(cluster, pool_name):
    if cluster.pool_exists(pool_name):
        print "------------------------------"
        print "Pool carete fail. Pool name is exist."
    else:
        print "------------------------------"
        print("Create {name} Pool".format(
            name=pool_name
        ))
        cluster.create_pool(pool_name)


def pool_delete(cluster, pool_name):
    if cluster.pool_exists(pool_name):
        print "------------------------------"
        print("Delete {name} Pool".format(
            name=pool_name
        ))
        cluster.delete_pool(pool_name)
    else:
        print "Pool delete fail. Pool name is not exist."


def image_create(cluster, pool_name, image_name):
    print "------------------------------"
    print("Create {image_name} in {name} Pool".format(
        image_name=image_name,
        name=pool_name
    ))
    ioctx = cluster.open_ioctx(pool_name)
    try:
        rbd_inst = rbd.RBD()
        size = 1024 ** 3
        rbd_inst.create(ioctx, image_name, size)
        image = rbd.Image(ioctx, image_name)
        try:
            data = 'foo' * 200
            image.write(data, 0)
        except Exception as e:
            print "Write error: ", e
            raise e
        finally:
            image.close()
    finally:
        ioctx.close()


def image_list(cluster, pool_name):
    print "------------------------------"
    print("List Available Images in {pool_name} Pool".format(
        pool_name=pool_name,
    ))
    ioctx = cluster.open_ioctx(pool_name)
    try:
        rbd_inst = rbd.RBD()
        rbd_list = rbd_inst.list(ioctx)
        for r in rbd_list:
            print r
    except Exception as e:
        print "list error: ", e
        raise e
    finally:
        ioctx.close()


def image_remove(cluster, pool_name, image_name):
    print "------------------------------"
    print("Remove {image_name} in {pool_name} Pool".format(
        pool_name=pool_name,
        image_name=image_name
    ))
    ioctx = cluster.open_ioctx(pool_name)
    try:
        rbd_inst = rbd.RBD()
        rbd_inst.remove(ioctx, image_name)
    except Exception as e:
        print "remove error: ", e
        raise e
    finally:
        ioctx.close()


if __name__ == '__main__':
    # connect cluster
    cluster = main()
    cluster_stats(cluster)

    # create test pool
    pool_list(cluster)
    pool_create(cluster, 'test_rbd')
    pool_list(cluster)

    # create test image and remove it
    image_create(cluster, 'test_rbd', 'test_image')
    image_list(cluster, 'test_rbd')
    image_remove(cluster, 'test_rbd', 'test_image')
    image_list(cluster, 'test_rbd')

    # delete pool
    # pool_delete(cluster, 'test_rbd')

    # disconnect cluster
    cluster_shutdown(cluster)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ceph#python
上次更新: 3/18/2022, 4:13:31 PM
Python算法-汉诺塔
Python统计OSD上PG的数量

← Python算法-汉诺塔 Python统计OSD上PG的数量→

最近更新
01
ceph块设备使用iscsi
03-29
02
Citrix设置vm开机自启动
02-17
03
ntp与chrony时间同步
12-17
更多文章>
Theme by Vdoing | Copyright © 2019-2023 夏苏文 | MIT License

网站已在灾难中运行:

蜀ICP备2022029853号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式