您好,登錄后才能下訂單哦!
這篇文章主要介紹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)
[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_class=argparse.RawDescriptionHelpFormatter, 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 |
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
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是怎么樣的”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。