openstack(liberty): devstack之screen

时间:2023-03-09 22:10:15
openstack(liberty): devstack之screen

在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中。红色部分就是启动的第一级函数调用。

 # start_nova_api() - Start the API process ahead of other things
function start_nova_api {
# Get right service port for testing
local service_port=$NOVA_SERVICE_PORT
local service_protocol=$NOVA_SERVICE_PROTOCOL
if is_service_enabled tls-proxy; then
service_port=$NOVA_SERVICE_PORT_INT
service_protocol="http"
fi # Hack to set the path for rootwrap
local old_path=$PATH
export PATH=$NOVA_BIN_DIR:$PATH # If the site is not enabled then we are in a grenade scenario
local enabled_site_file=$(apache_site_config_for nova-api)
if [ -f ${enabled_site_file} ] && [ "$NOVA_USE_MOD_WSGI" == "True" ]; then
enable_apache_site nova-api
enable_apache_site nova-ec2-api
restart_apache_server
tail_log nova-api /var/log/$APACHE_NAME/nova-api.log
tail_log nova-ec2-api /var/log/$APACHE_NAME/nova-ec2-api.log
else
run_process n-api "$NOVA_BIN_DIR/nova-api"
fi echo "Waiting for nova-api to start..."
if ! wait_for_service $SERVICE_TIMEOUT $service_protocol://$SERVICE_HOST:$service_port; then
die $LINENO "nova-api did not start"
fi # Start proxies if enabled
if is_service_enabled tls-proxy; then
start_tls_proxy '*' $NOVA_SERVICE_PORT $NOVA_SERVICE_HOST $NOVA_SERVICE_PORT_INT &
start_tls_proxy '*' $EC2_SERVICE_PORT $NOVA_SERVICE_HOST $EC2_SERVICE_PORT_INT &
fi export PATH=$old_path
}

2. run_process函数,devstack/functions-common.

 # Run a single service under screen or directly
# If the command includes shell metachatacters (;<>*) it must be run using a shell
# If an optional group is provided sg will be used to run the
# command as that group.
# run_process service "command-line" [group]
function run_process {
local service=$1            #此处的service就是n-api
local command="$2"           #此处的command就是/usr/bin/nova-api,其他的nova相关的指令都在/usr/bin目录下。
local group=$ if is_service_enabled $service; then
if [[ "$USE_SCREEN" = "True" ]]; then    #由于前面分析了USE_SCREEN的值为True,所以,进入到screen_process处理
screen_process "$service" "$command" "$group"
else
# Spawn directly without screen
_run_process "$service" "$command" "$group" &
fi
fi
}

3. screen_process函数,在devstack/functions-common文件中

 # Helper to launch a process in a named screen
# Uses globals ``CURRENT_LOG_TIME``, ```LOGDIR``, ``SCREEN_LOGDIR``, `SCREEN_NAME``,
# ``SERVICE_DIR``, ``USE_SCREEN``
# screen_process name "command-line" [group]
# Run a command in a shell in a screen window, if an optional group
# is provided, use sg to set the group of the command.
function screen_process {
local name=$1      #要启动的service的名字,此处为n-api
local command="$2"   #要执行的命令,此处为nova-api
local group=$ SCREEN_NAME=${SCREEN_NAME:-stack}
SERVICE_DIR=${SERVICE_DIR:-${DEST}/status}
USE_SCREEN=$(trueorfalse True USE_SCREEN) screen -S $SCREEN_NAME -X screen -t $name  #启动一个名为stack的session,在这个session中执行screen命令(给当前screen窗口起名为n-api)

local real_logfile="${LOGDIR}/${name}.log.${CURRENT_LOG_TIME}"
echo "LOGDIR: $LOGDIR"
echo "SCREEN_LOGDIR: $SCREEN_LOGDIR"
echo "log: $real_logfile"
if [[ -n ${LOGDIR} ]]; then
screen -S $SCREEN_NAME -p $name -X logfile "$real_logfile"
screen -S $SCREEN_NAME -p $name -X log on
ln -sf "$real_logfile" ${LOGDIR}/${name}.log
if [[ -n ${SCREEN_LOGDIR} ]]; then
# Drop the backward-compat symlink
ln -sf "$real_logfile" ${SCREEN_LOGDIR}/screen-${}.log
fi
fi # sleep to allow bash to be ready to be send the command - we are
# creating a new window in screen and then sends characters, so if
# bash isn't running by the time we send the command, nothing
# happens. This sleep was added originally to handle gate runs
# where we needed this to be at least seconds to pass
# consistently on slow clouds. Now this is configurable so that we
# can determine a reasonable value for the local case which should
# be much smaller.
sleep ${SCREEN_SLEEP:-} NL=`echo -ne '\015'`
# This fun command does the following:
# - the passed server command is backgrounded
# - the pid of the background process is saved in the usual place
# - the server process is brought back to the foreground
# - if the server process exits prematurely the fg command errors
# and a message is written to stdout and the process failure file
#
# The pid saved can be used in stop_process() as a process group
# id to kill off all child processes
if [[ -n "$group" ]]; then
command="sg $group '$command'"
fi # Append the process to the screen rc file
screen_rc "$name" "$command" screen -S $SCREEN_NAME -p $name -X stuff "$command & echo \$! >$SERVICE_DIR/$SCREEN_NAME/${name}.pid; fg || echo \"$name failed to start\" | tee \"$SERVICE_DIR/$SCREEN_NAME/${name}.failure\"$NL" #在指定name为nova-api的screen窗口里面执行启动nova-api的服务,并将pid写入对应文件。如果出错,将错误信息tee到指定格式的文件
}

其中,上面标识红色的部分,是关键指令。通过上面的3个函数的分析,是不是对于devstack的各个service的启动比较熟悉了?或者说有个比较直观感性的认识了?

下面,介绍一下,screen相关的操作。首先看看screen都有些什么命令选项,或者说screen的帮助信息:

 [root@CloudGame shelltest]# screen -help
Use: screen [-opts] [cmd [args]]
or: screen -r [host.tty] Options:
- Use IPv4.
- Use IPv6.
-a Force all capabilities into each window's termcap.
-A -[r|R] Adapt all windows to the new display width & height.
-c file Read configuration file instead of '.screenrc'.
-d (-r) Detach the elsewhere running screen (and reattach here).
-dmS name Start as daemon: Screen session in detached mode.
-D (-r) Detach and logout remote (and reattach here).
-D -RR Do whatever is needed to get a screen session.
-e xy Change command characters.
-f Flow control on, -fn = off, -fa = auto.
-h lines Set the size of the scrollback history buffer.
-i Interrupt output sooner when flow control is on.
-l Login mode on (update /var/run/utmp), -ln = off.
-list or -ls. Do nothing, just list our SockDir.
-L Turn on output logging.
-m ignore $STY variable, do create a new screen session.
-O Choose optimal output rather than exact vt100 emulation.
-p window Preselect the named window if it exists.
-q Quiet startup. Exits with non-zero return code if unsuccessful.
-r Reattach to a detached screen process.
-R Reattach if possible, otherwise start a new session.
-s shell Shell to execute rather than $SHELL.
-S sockname Name this session <pid>.sockname instead of <pid>.<tty>.<host>.
-t title Set title. (window's name).
-T term Use term as $TERM for windows, rather than "screen".
-U Tell screen to use UTF- encoding.
-v Print "Screen version 4.00.03 (FAU) 23-Oct-06".
-wipe Do nothing, just clean up SockDir.
-x Attach to a not detached screen. (Multi display mode).
-X Execute <cmd> as a screen command in the specified session.

在devstack环境下,相关的操作指令主要有:

screen -ls:  列出当前sockdir。

 [stack@ip---- devstack]$ screen -ls
There is a screen on:
.stack (Detached)
Socket in /var/run/screen/S-stack.

attach screen:

 [stack@ip---- devstack]$ screen -r .stack

进入了screen后,通过执行ctrl+a + n或者p键,可以实现screen窗口的滚动切换。当前的服务窗口对应于服务名字上有个*号:

 。。。。。。。。。。。。。。。
--03T00::00Z", "name": "CloudpipeUpdate", "links": [], "namespace": "http://docs.openstack.org/compute/ext/fake_xml", "alias": "os-cloudpipe-update", "description": ""}, {"updated": "2014-12-03T00:00:00Z", "name": "ConfigDrive", "links": [], "namespace": "http://docs.openstack.org/compute/ext/fake_xml", "alias": "os-config-drive", "description": "Config Drive Extension."}, {"updated": "2014-12-03T00:00:00Z", "name": "ConsoleAuthTokens", "links": [], "namespace": "http://docs.openstack.org/compute/ext/fake_xml", "alias": "os-console-auth-tokens", "description": "Console token authentication support."}, {"updated": "2014-12-03T00:00:00Z", "name": "ConsoleOutput", "links": [], "namespace": "http://docs.openstack.org/compute/ext/fake_xml", "alias": "os-console-output", "description": "Console log output support, with tailing ability."}, {"updated": "2014-12-03T00:00:00Z", "name": "Consoles", "links": [], "namespace": "http://docs.openstack.org/compute/ext/fake_xml", "alias": "os-consoles", "description": "Interactive Console support."}, {"updated": "2014-12-03T00:00:00Z", "name": "CreateBackup", "links": [], "namespace": "http://docs.openstack.org/compute/ext/fake_xml", "alias": "os-create-backup", "description": "Create a backup of a server."}, {"updated": "2014-12-03T00:00:00Z", "name": "Createserverext", "links": [], "namespace": "http://docs.openstack.org/compute/ext/fake_xml", "alias": "os-create-server-ext", "description": ""}, {"updated": "2014-12-03T00:00:00Z", "name": "DeferredDelete", "links": [], "namespace": "http://docs.openstack.org/compute/ext/fake_xml", "alias": "os-deferred-delete", "description": "Instance deferred delete."}, {"updated": "2014-12-03T00:00:0
-- ::05.346975
:q! -$ stack@ip---- $(L) dstat $(L) key $(L) key-access 4$(L) horizon* (ip----/0.29 0.30 0.74)

在当前窗口执行ctrl+c就是杀掉当前的process。

deatch screen(ctrl+a + d):

注意,是ctrl+a指令输入后等一会,再输入d键。

进入服务列表模式(ctrl+a+双引号键,双引号键shift+,就是Enter键左边那个):

openstack(liberty): devstack之screen

在这种模式下,当前所在的process对应的服务名字高亮显示,例如上图中的horizon,此时,点击enter,会进入到常规模式。

其他一些相关的指令,自己以后慢慢摸索吧!