- python启动脚本 bin/nova-compute
python启动脚本 bin/nova-compute
- utils.default_flagfile() #设置flag文件路径.
- flags.FLAGS(sys.argv) #把flag文件中的参数放到args中。
- logging.setup() #设置日志
- utils.monkey_patch()
- server = service.Service.create(binary='nova-compute') #创建服务
- service.serve(server) #启动服务
- service.wait() #等待请求
utils.default_flagfile() #设置flag文件路径.
flags.FLAGS(sys.argv) #把flag文件中的参数放到args中。
logging.setup() #设置日志
utils.monkey_patch()
server = service.Service.create(binary='nova-compute') #创建服务
service.serve(server) #启动服务
service.wait() #等待请求
下面我讲按6大步进行讲述:
第一步:utils.default_flagfile()
- if args is None:
- args = sys.argv
- for arg in args:
- if arg.find('flagfile') != -1:
- break
- else:
- if not os.path.isabs(filename):
- # turn relative filename into an absolute path
- script_dir = os.path.dirname(inspect.stack()[-1][1])
- filename = os.path.abspath(os.path.join(script_dir, filename))
- if not os.path.exists(filename):
- filename = "./nova.conf"
- if not os.path.exists(filename):
- filename = '/etc/nova/nova.conf'
- flagfile = '--flagfile=%s' % filename
- args.insert(1, flagfile)
- 其实所完成的功能也就是把--flagfile=/etc/nova/nova.conf加到入参数列表中。
if args is None:
args = sys.argv
for arg in args:
if arg.find('flagfile') != -1:
break
else:
if not os.path.isabs(filename):
# turn relative filename into an absolute path
script_dir = os.path.dirname(inspect.stack()[-1][1])
filename = os.path.abspath(os.path.join(script_dir, filename))
if not os.path.exists(filename):
filename = "./nova.conf"
if not os.path.exists(filename):
filename = '/etc/nova/nova.conf'
flagfile = '--flagfile=%s' % filename
args.insert(1, flagfile)
其实所完成的功能也就是把--flagfile=/etc/nova/nova.conf加到入参数列表中。
第二步:flags.FLAGS(sys.argv)
flags.py中有一句
FLAGS = FlagValues(),那么直接查看FlagValues()这个类,这个类是继承于gflags.FlagValues.
第三步:logging.setup()
这个是对这个服务开启日志功能。
第四步:server = service.Service.create(binary='nova-compute')
这个函数位于/nova/service.py: 类Service中
- class Service(object):
- """Service object for binaries running on hosts.
- A service takes a manager and enables rpc by listening to queues based
- on topic. It also periodically runs tasks on the manager and reports
- it state to the database services table."""
- 函数定义如下:
- @classmethod
- def create(cls, host=None,binary=None,topic=None,manager=None,
- report_interval=None,periodic_interval=None):
- """Instantiates class and passes back application object.
- :param host: defaults to FLAGS.host
- :param binary: defaults to basename of executable
- :param topic: defaults to bin_name - 'nova-' part
- :param manager: defaults to FLAGS.<topic>_manager