在devstack的stack.sh文件中,可以看到所有配置的service启动方式有两种,根据是否USE_SCREEN进行是在screen window中启动,还是直接起。
默认情况,USE_SCREEN是true的,这个可以在devstack/stackrc中找到,这个文件默认将USE_SCREEN设置为True。 即,所有的service将在screen window中启动。即每一个service对应一个screen window。下面,我就拿nova api这个service的启动代码进行分析这个是如何在SCREEN窗口中启动的。
1. 下面的代码在devstack/lib/nova中。红色部分就是启动的第一级函数调用。
1 # start_nova_api() - Start the API process ahead of other things 2 function start_nova_api { 3 # Get right service port for testing 4 local service_port=$NOVA_SERVICE_PORT 5 local service_protocol=$NOVA_SERVICE_PROTOCOL 6 if is_service_enabled tls-proxy; then 7 service_port=$NOVA_SERVICE_PORT_INT 8 service_protocol="http" 9 fi 10 11 # Hack to set the path for rootwrap 12 local old_path=$PATH 13 export PATH=$NOVA_BIN_DIR:$PATH 14 15 # If the site is not enabled then we are in a grenade scenario 16 local enabled_site_file=$(apache_site_config_for nova-api) 17 if [ -f ${enabled_site_file} ] && [ "$NOVA_USE_MOD_WSGI" == "True" ]; then 18 enable_apache_site nova-api 19 enable_apache_site nova-ec2-api 20 restart_apache_server 21 tail_log nova-api /var/log/$APACHE_NAME/nova-api.log 22 tail_log nova-ec2-api /var/log/$APACHE_NAME/nova-ec2-api.log 23 else 24 run_process n-api "$NOVA_BIN_DIR/nova-api" 25 fi 26 27 echo "Waiting for nova-api to start..." 28 if ! wait_for_service $SERVICE_TIMEOUT $service_protocol://$SERVICE_HOST:$service_port; then 29 die $LINENO "nova-api did not start" 30 fi 31 32 # Start proxies if enabled 33 if is_service_enabled tls-proxy; then 34 start_tls_proxy '*' $NOVA_SERVICE_PORT $NOVA_SERVICE_HOST $NOVA_SERVICE_PORT_INT & 35 start_tls_proxy '*' $EC2_SERVICE_PORT $NOVA_SERVICE_HOST $EC2_SERVICE_PORT_INT & 36 fi 37 38 export PATH=$old_path 39 }
2. run_process函数,devstack/functions-common.
1 # Run a single service under screen or directly 2 # If the command includes shell metachatacters (;<>*) it must be run using a shell 3 # If an optional group is provided sg will be used to run the 4 # command as that group. 5 # run_process service "command-line" [group] 6 function run_process { 7 local service=$1 #此处的service就是n-api 8 local command="$2" #此处的command就是/usr/bin/nova-api,其他的nova相关的指令都在/usr/bin目录下。 9 local group=$3 10 11 if is_service_enabled $service; then 12 if [[ "$USE_SCREEN" = "True" ]]; then #由于前面分析了USE_SCREEN的值为True,所以,进入到screen_process处理 13 screen_process "$service" "$command" "$group" 14 else 15 # Spawn directly without screen 16 _run_process "$service" "$command" "$group" & 17 fi 18 fi 19 }
3. screen_process函数,在devstack/functions-common文件中
1 # Helper to launch a process in a named screen 2 # Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``, 3 # ``SERVICE_DIR``, ``USE_SCREEN`` 4 # screen_process name "command-line" [group] 5 # Run a command in a shell in a screen window, if an optional group 6 # is provided, use sg to set the group of the command. 7 function screen_process { 8 local name=$1 #要启动的service的名字,此处为n-api 9 local command="$2" #要执行的命令,此处为nova-api 10 local group=$3 11 12 SCREEN_NAME=${SCREEN_NAME:-stack} 13 SERVICE_DIR=${SERVICE_DIR:-${DEST}/status} 14 USE_SCREEN=$(trueorfalse True USE_SCREEN) 15 16 screen -S $SCREEN_NAME -X screen -t $name #启动一个名为stack的session,在这个session中执行screen命令(给当前screen窗口起名为n-api) 17 18 local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}" 19 echo "LOGDIR: $LOGDIR" 20 echo "SCREEN_LOGDIR: $SCREEN_LOGDIR" 21 echo "log: $real_logfile" 22 if [[ -n ${LOGDIR} ]]; then 23 screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile" 24 screen -S $SCREEN_NAME -p $name -X log on 25 ln -sf "$real_logfile" ${LOGDIR}/${name}.log 26 if [[ -n ${SCREEN_LOGDIR} ]]; then 27 # Drop the backward-compat symlink 28 ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${1}.log 29 fi 30 fi 31 32 # sleep to allow bash to be ready to be send the command - we are 33 # creating a new window in screen and then sends characters, so if 34 # bash isn't running by the time we send the command, nothing 35 # happens. This sleep was added originally to handle gate runs 36 # where we needed this to be at least 3 seconds to pass 37 # consistently on slow clouds. Now this is configurable so that we 38 # can determine a reasonable value for the local case which should 39 # be much smaller. 40 sleep ${SCREEN_SLEEP:-3} 41 42 NL=`echo -ne '