ceph-deploy的mon 命令用于管理mon守护进程
其入口函数在E:\ceph-deploy-master\ceph-deploy-master\ceph_deploy\mon.py 中的make函数
@priority(30)
def make(parser):
"""
Ceph MON Daemon management
"""
parser.formatter_class = ToggleRawTextHelpFormatter
mon_parser = parser.add_subparsers(dest='subcommand')
mon_parser.required = True
mon_add = mon_parser.add_parser(
'add',
help=('R|Add a monitor to an existing cluster:\n'
'\tceph-deploy mon add node1\n'
'Or:\n'
'\tceph-deploy mon add --address 192.168.1.10 node1\n'
'If the section for the monitor exists and defines a `mon addr` that\n'
'will be used, otherwise it will fallback by resolving the hostname to an\n'
'IP. If `--address` is used it will override all other options.')
)
mon_destroy = mon_parser.add_parser(
'destroy',
help='Completely remove Ceph MON from remote host(s)'
)
mon_destroy.add_argument(
'mon',
nargs='+',
)
parser.set_defaults(
func=mon,
)
make函数为mon 添加各种参数,并指定默认的处理函数是mon
def mon(args):
if args.subcommand == 'create':
mon_create(args)
elif args.subcommand == 'add':
mon_add(args)
elif args.subcommand == 'destroy':
mon_destroy(args)
elif args.subcommand == 'create-initial':
mon_create_initial(args)
else:
LOG.error('subcommand %s not implemented', args.subcommand)
可以看动作mon 总共支持4个子命令,分别是create/add/destroy/create-initial
首先看看create命令
def mon_create(args):
#获取配置文件
cfg = conf.ceph.load(args)
if not args.mon:
@如果没有指定mon,则从配置文件中mon_initial_members 作为mon
args.mon = get_mon_initial_members(args, error_on_empty=True, _cfg=cfg)
if args.keyrings:
monitor_keyring = concatenate_keyrings(args)
else:
#获取ceph.mon.keyring 文件的路径
keyring_path = '{cluster}.mon.keyring'.format(cluster=args.cluster)
try:
#读取ceph.mon.keyring文件
monitor_keyring = files.read_file(keyring_path)
except IOError:
LOG.warning('keyring (%s) not found, creating a new one' % keyring_path)
new_mon_keyring(args)
monitor_keyring = files.read_file(keyring_path)
LOG.debug(
'Deploying mon, cluster %s hosts %s',
args.cluster,
' '.join(args.mon),
)
errors = 0
for (name, host) in mon_hosts(args.mon):
try:
# TODO add_bootstrap_peer_hint
LOG.debug('detecting platform for host %s ...', name)
#获取是哪个发行版,并通过callbacks 检测发行版是否安装了ceph
distro = hosts.get(
host,
username=args.username,
callbacks=[packages.ceph_is_installed]
)
LOG.info('distro info: %s %s %s', distro.name, distro.release, distro.codename)
rlogger = logging.getLogger(name)
# ensure remote hostname is good to go
hostname_is_compatible(distro.conn, rlogger, name)
rlogger.debug('deploying mon to %s', name)
#调用发行版的mon模块的create函数来新建mon
distro.mon.create(distro, args, monitor_keyring)
# tell me the status of the deployed mon
#等待2s后检查前面新建的mon的状态
time.sleep(2) # give some room to start
mon_status(distro.conn, rlogger, name, args)
#获取错误日志写到rlogger
catch_mon_errors(distro.conn, rlogger, name, cfg, args)
distro.conn.exit()
except RuntimeError as e:
LOG.error(e)
errors += 1
if errors:
raise exc.GenericError('Failed to create %d monitors' % errors)
我们以suse为例看看mon.create。路径为E:\ceph-deploy-master\ceph-deploy-master\ceph_deploy\hosts\common.py
def mon_create(distro, args, monitor_keyring):
hostname = distro.conn.remote_module.shortname()
logger = distro.conn.logger
logger.debug('remote hostname: %s' % hostname)
path = paths.mon.path(args.cluster, hostname)
uid = distro.conn.remote_module.path_getuid(constants.base_path)
gid = distro.conn.remote_module.path_getgid(constants.base_path)
done_path = paths.mon.done(args.cluster, hostname)
init_path = paths.mon.init(args.cluster, hostname, distro.init)
conf_data = conf.ceph.load_raw(args)
# write the configuration file
distro.conn.remote_module.write_conf(
args.cluster,
conf_data,
args.overwrite_conf,
)
# if the mon path does not exist, create it
distro.conn.remote_module.create_mon_path(path, uid, gid)
logger.debug('checking for done path: %s' % done_path)
if not distro.conn.remote_module.path_exists(done_path):
logger.debug('done path does not exist: %s' % done_path)
if not distro.conn.remote_module.path_exists(paths.mon.constants.tmp_path):
logger.info('creating tmp path: %s' % paths.mon.constants.tmp_path)
distro.conn.remote_module.makedir(paths.mon.constants.tmp_path)
keyring = paths.mon.keyring(args.cluster, hostname)
logger.info('creating keyring file: %s' % keyring)
distro.conn.remote_module.write_monitor_keyring(
keyring,
monitor_keyring,
uid, gid,
)
user_args = []
if uid != 0:
user_args = user_args + [ '--setuser', str(uid) ]
if gid != 0:
user_args = user_args + [ '--setgroup', str(gid) ]
# 重点是通过ceph-mon 这个命令来创建ceph mon
remoto.process.run(
distro.conn,
[
'ceph-mon',
'--cluster', args.cluster,
'--mkfs',
'-i', hostname,
'--keyring', keyring,
] + user_args
)
logger.info('unlinking keyring file %s' % keyring)
distro.conn.remote_module.unlink(keyring)
# create the done file
distro.conn.remote_module.create_done_path(done_path, uid, gid)
# create init path
distro.conn.remote_module.create_init_path(init_path, uid, gid)
#启动mon
# start mon service
start_mon_service(distro, args.cluster, hostname)