共計 8365 個字符,預計需要花費 21 分鐘才能閱讀完成。
這篇文章主要介紹 ceph-deploy 中源碼結構與 cli 是怎么樣的,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
ceph-deploy 源碼分析(一)——源碼結構與 cli
ceph-deploy 是部署 Ceph 集群的工具,可以通過 SSH 方式往遠程主機上安裝 Ceph 軟件包、創建集群、增加監視器、收集(或銷毀)密鑰、增加 OSD 和元數據服務器、配置管理主機,甚至拆除集群。
ceph-deploy 使用 Python 開發,GitHub 為 https://github.com/ceph/ceph-deploy。本次源碼分析的 ceph-deploy 版本為 1.5.37。
源碼結構
ceph-deploy-1.5.37 源碼目錄
├── ceph_deploy # ceph-deploy 源碼目錄
├── ceph_deploy.egg-info # egg-info 目錄
├── CONTRIBUTING.rst # 貢獻指南
├── LICENSE # MIT LICENSE
├── MANIFEST.in # 打包規則
├── PKG-INFO # PKG-INFO 文件
├── README.rst # ceph-deploy 介紹
├── scripts # 啟動腳本目錄
├── setup.cfg # setup.py 配置
├── setup.py # ceph-deploy 安裝腳本
├── tox.ini # 標準化測試
└── vendor.py
ceph_deploy 源碼目錄文件
├── admin.py # 子命令 admin 模塊,將 ceph.conf 和 client.admin key push 到遠程主機
├── calamari.py # 子命令 calamari 模塊,連接 calamari master
├── cli.py # CLI 入口
├── cliutil.py # 為裝飾器函數增加 priority
├── conf # ceph.conf 與 cephdeploy.conf 讀取與寫入相關操作目錄
├── config.py # 子命令 config 模塊,push ceph.conf 文件到遠程主機;從遠程主機 pull ceph.conf 文件
├── connection.py # 連接本地主機、遠程主機
├── exc.py # 異常處理 Error
├── forgetkeys.py # 子命令 forgetkeys 模塊,本地移除 authentication keys
├── gatherkeys.py # 子命令 gatherkeys 模塊,從 mon 主機上拉取 authentication keys
├── hosts # ceph-deploy 在不同操作系統(centos、debian、fedora、rhel、suse)的操作
├── __init__.py # 初始化版本號信息,當前版本 1.5.37
├── install.py # 子命令 install 模塊,安裝、卸載 ceph 包,清除數據
├── lib # vendor 類庫
├── mds.py # 子命令 mds 模塊,mds 管理
├── misc.py # 其他工具類,比如:mon host 組裝 tuples
├── mon.py # 子命令 mon 模塊,mon 管理
├── new.py # 子命令 new 模塊,部署集群
├── osd.py # 子命令 osd 模塊,osd 管理
├── pkg.py # 子命令 pkg 模塊,逗號分隔的包安裝、卸載
├── repo.py # 子命令 repo 模塊,添加 yum repo
├── rgw.py # 子命令 rgw 模塊,rgw 管理
├── tests # 測試文件目錄
├── util # util 目錄
└── validate.py # 參數校驗函數
源碼入口
script 目錄下的 ceph-deploy 文件是 ceph-deploy 的入口,安裝之后是 /usr/bin/ceph-deploy。
ceph-deploy 的__main__函數調用 ceph_deploy.cli 的 main 函數
...
from ceph_deploy.cli import main
if __name__ == __main__ :
sys.exit(main())
cli 模塊
cli.py 是命令行操作模塊。
main 函數調用_main 函數
def main(args=None, namespace=None):
try:
_main(args=args, namespace=namespace)
finally:
# This block is crucial to avoid having issues with
# Python spitting non-sense thread exceptions. We have already
# handled what we could, so close stderr and stdout.
if not os.environ.get(CEPH_DEPLOY_TEST):
try:
sys.stdout.close()
except:
pass
try:
sys.stderr.close()
except:
pass
_main 函數
設置日志:Console Logger 與 File Logger 添加到 root_logger
調用 argparse 模塊,解析 cli 參數
調用 conf 目錄下的 ceph-deploy 模塊 set_overrides 函數,從當前目錄的 cephdeploy.conf 或~/.cephdeploy.conf 文件獲取 ceph-deploy-global、ceph-deploy-[subcmd]配置項寫入 args
調用執行 subcmd 相應的模塊
@catches((KeyboardInterrupt, RuntimeError, exc.DeployError,), handle_all=True)
def _main(args=None, namespace=None):
# Set console logging first with some defaults, to prevent having exceptions
# before hitting logging configuration. The defaults can/will get overridden
# later.
# Console Logger
# 命令行控制臺日志
sh = logging.StreamHandler()
# 不同級別的日志,使用不同的顏色區別:DEBUG 藍色;WARNIN 黃色;ERROR 紅色;INFO 白色
sh.setFormatter(log.color_format())
# 設置日志級別為 WARNING
sh.setLevel(logging.WARNING)
# because we re in a module already, __name__ is not the ancestor of
# the rest of the package; use the root as the logger for everyone
# root_logger 日志
root_logger = logging.getLogger()
# allow all levels at root_logger, handlers control individual levels
# 設置 root_logger 日志級別為 DEBUG
root_logger.setLevel(logging.DEBUG)
# 將 sh 添加到 root_logger
root_logger.addHandler(sh)
# 獲取解析 cli 的 argparse,調用 argparse 模塊
parser = get_parser()
if len(sys.argv) 2:
parser.print_help()
sys.exit()
else:
# 解析獲取 sys.argv 中的 ceph-deploy 子命令和參數
args = parser.parse_args(args=args, namespace=namespace)
# 設置日志級別
console_loglevel = logging.DEBUG # start at DEBUG for now
if args.quiet:
console_loglevel = logging.WARNING
if args.verbose:
console_loglevel = logging.DEBUG
# Console Logger
sh.setLevel(console_loglevel)
# File Logger
# 文件日志
fh = logging.FileHandler(ceph-deploy-{cluster}.log .format(cluster=args.cluster))
fh.setLevel(logging.DEBUG)
fh.setFormatter(logging.Formatter(log.FILE_FORMAT))
# 將 fh 添加到 root_logger
root_logger.addHandler(fh)
# Reads from the config file and sets values for the global
# flags and the given sub-command
# the one flag that will never work regardless of the config settings is
# logging because we cannot set it before hand since the logging config is
# not ready yet. This is the earliest we can do.
# 從當前目錄的 cephdeploy.conf 或~/.cephdeploy.conf 文件獲取 ceph-deploy 配置覆蓋命令行參數
args = ceph_deploy.conf.cephdeploy.set_overrides(args)
LOG.info(Invoked (%s): %s % (
ceph_deploy.__version__,
.join(sys.argv))
)
log_flags(args)
# args.func 為 cli 中的 subcmd 子命令,調用相應的模塊
return args.func(args)
get_parser 函數 [ceph_deploy.cli] 以以下方式配置:
模塊名 = 模塊包名: 執行函數
比如:new = ceph_deploy.new:make
new 作為 ceph-deploy 的子命令,執行 ceph-deploy new 命令時,執行 make 函數
其他的模塊也類似:
mon = ceph_deploy.mon:make
osd = ceph_deploy.osd:make
rgw = ceph_deploy.rgw:make
mds = ceph_deploy.mds:make
config = ceph_deploy.config:make
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
def get_parser():
# 調用 argparse 模塊
parser = argparse.ArgumentParser(
prog= ceph-deploy ,
formatter_ >
description= Easy Ceph deployment\n\n%s % __header__,
)
verbosity = parser.add_mutually_exclusive_group(required=False)
verbosity.add_argument(
-v , –verbose ,
action= store_true , dest= verbose , default=False,
help= be more verbose ,
)
verbosity.add_argument(
-q , –quiet ,
action= store_true , dest= quiet ,
help= be less verbose ,
)
parser.add_argument(
–version ,
action= version ,
version= %s % ceph_deploy.__version__,
help= the current installed version of ceph-deploy ,
)
parser.add_argument(
–username ,
help= the username to connect to the remote host ,
)
parser.add_argument(
–overwrite-conf ,
action= store_true ,
help= overwrite an existing conf file on remote host (if present) ,
)
parser.add_argument(
–cluster ,
metavar= NAME ,
help= name of the cluster ,
type=validate.alphanumeric,
)
parser.add_argument(
–ceph-conf ,
dest= ceph_conf ,
help= use (or reuse) a given ceph.conf file ,
)
sub = parser.add_subparsers(
title= commands ,
metavar= COMMAND ,
help= description ,
)
sub.required = True
# 獲取 ceph_deploy.cli 下的 entry_points
entry_points = [
(ep.name, ep.load())
for ep in pkg_resources.iter_entry_points(ceph_deploy.cli)
]
# 根據 priority 排序
entry_points.sort(
key=lambda name_fn: getattr(name_fn[1], priority , 100),
)
# 將模塊加入到子命令
for (name, fn) in entry_points:
p = sub.add_parser(
name,
description=fn.__doc__,
help=fn.__doc__,
)
if not os.environ.get(CEPH_DEPLOY_TEST):
p.set_defaults(cd_conf=ceph_deploy.conf.cephdeploy.load())
# flag if the default release is being used
p.set_defaults(default_release=False)
fn(p)
p.required = True
parser.set_defaults(
cluster= ceph ,
)
return parser
argparse 模塊
cli 命令的解析使用了 argparse.py 模塊。argparse 是 Python 標準庫中命令行選項、參數和子命令的解析器,其是為替代已經過時的 optparse 模塊,argparse 在 Python2.7 中被引入。
argparse 模塊請參考
https://docs.python.org/2.7/library/argparse.html
http://python.usyiyi.cn/translate/python_278/library/argparse.html
set_overrides 函數
conf 目錄下的 ceph-deploy 模塊 set_overrides 函數
調用 load()函數
判斷 ceph-deploy 配置文件 ceph-deploy-global、ceph-deploy-[subcommand]配置項,調用 override_subcommand()函數寫入 args。
def set_overrides(args, _conf=None):
Read the configuration file and look for ceph-deploy sections
to set flags/defaults from the values found. This will alter the
“args“ object that is created by argparse.
# Get the subcommand name to avoid overwritting values from other
# subcommands that are not going to be used
subcommand = args.func.__name__
command_section = ceph-deploy-%s % subcommand
# 加載 ceph-deploy 配置
conf = _conf or load()
for section_name in conf.sections():
if section_name in [ceph-deploy-global , command_section]:
# ceph-deploy-global、ceph-deploy-[subcommand]配置項寫入 args
override_subcommand(
section_name,
conf.items(section_name),
args
)
return args
load 函數,調用 location()函數
1
2
3
4
5
def load():
parser = Conf()
# 讀取解析 ceph-deploy 配置文件
parser.read(location())
return parser
location 函數,調用_locate_or_create()函數
1
2
3
4
5
6
def location():
Find and return the location of the ceph-deploy configuration file. If this
file does not exist, create one in a default location.
return _locate_or_create()
_locate_or_create 函數,判斷當前目錄的 cephdeploy.conf 或~/.cephdeploy.conf 文件是否存在。
如果都不存在則調用 create_stub 函數創建一個~/.cephdeploy.conf 文件。這個文件是根據模板創建的,內容為空。
如果存在(提前創建)cephdeploy.conf 或~/.cephdeploy.conf 文件,可以在文件中配置 public_network、cluster_network、overwrite-conf 等。
def _locate_or_create():
home_config = path.expanduser(~/.cephdeploy.conf)
# With order of importance
locations = [
path.join(os.getcwd(), cephdeploy.conf ),
home_config,
]
for location in locations:
if path.exists(location):
logger.debug(found configuration file at: %s % location)
return location
logger.info(could not find configuration file, will create one in $HOME)
create_stub(home_config)
return home_config
以上是“ceph-deploy 中源碼結構與 cli 是怎么樣的”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注丸趣 TV 行業資訊頻道!