openstack(liberty): devstack之stack.sh分析

学习openstack,从devstack入手,是个不错的选择。devstack中,首先需要分析stack.sh都做了些什么!

这里面涉及到了很多shell的基础知识。我就做个简单的梳理,方便后续查阅!

1. 参数扩展(ParameterExpansion)

${parameter:-word}    使用默认值

If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted. 

需要说明的是,若word没有写,且parameter也没有设置或者是空,则用空字符串代替parameter。

1 #!/bin/bash
2 
3 val=
4 echo "val: ${val:-WORLD}"
1 [root@CloudGame shelltest]# ./parameterexpansion.sh 
2 val: WORLD

${parameter:=word}   设置默认值

If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way. 

1 #!/bin/bash
2 
3 val=
4 echo "val: ${val:=WORLD}"
1 [root@CloudGame shelltest]# ./parameterexpansion.sh 
2 val: WORLD

${parameter:?word}   设置错误提示信息

If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted. 

1 #!/bin/bash
2 
3 val=
4 echo "val: ${val:?no value setted}"
1 [root@CloudGame shelltest]# ./parameterexpansion.sh 
2 ./parameterexpansion.sh: line 4: val: no value setted

${parameter:+word}   替换已设置的信息

If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted. 

1 #!/bin/bash
2 
3 val=Hello
4 echo "val: ${val:+value setted}"
1 [root@CloudGame shelltest]# ./parameterexpansion.sh 
2 val: value setted

【注意】:若将${parameter:xxxx}中(xxxx表示-word,=word, +word, ?word)的冒号:号去掉,看看有什么不同吧。

这当中,第一个扩展方式,在stack.sh中使用的最多。

2. set设置shell变量

使用set命令可以设置各种shell选项或者列出shell变量.单个选项设置常用的特性. 在某些选项之后-o参数将特殊特性打开.在某些选项之后使用+o参数将关闭某些特性, 不带任何参数的set命令将显示shell的全部变量.除非遇到非法的选项,否则set总是返回ture.

在stack.sh中,-o和+o的使用,也很多。

比如,为了保证bash的健壮性而设置的set -o nounset,这个的含义就是说当有没有设置值的变量被发现,则bash立即退出。set -o nounset还有另外一个相同作用的指令set -u。 与之类似的,防止因为错误造成灾难,建议在bash中设置set -e,即告诉bash一旦遇到返回值非0时,就报错并退出。当然,你若希望脚本出现返回值非0,建议自己做检查。 原则上不建议这么做。

3. bash版本

需要注意的是,在openstack liberty中,要求bash的版本必须是4.2或者更新。

1 [stack@ip-10-121-2-117 devstack]$ bash -version
2 GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
3 Copyright (C) 2011 Free Software Foundation, Inc.
4 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
5 
6 This is free software; you are free to change and redistribute it.
7 There is NO WARRANTY, to the extent permitted by law.

4. devstack执行用户非root

主要是因为horizon不允许执行者是root的身份,所以,在stack.sh脚本执行时,检测当前用户身份。root身份EUID值为0.

1 [root@ip-10-121-2-117 devstack]# echo $EUID
2 0
3 [root@ip-10-121-2-117 devstack]# su stack
4 [stack@ip-10-121-2-117 devstack]$ echo $EUID
5 1001

5. 参数设置

全局的:首先检查devstack目录下是否有localrc文件,若有用之,若没有检查local.conf文件并提取当中的localrc段(section),并生成.localrc.auto文件。然后,将devstack目录下stackrc中的参数source到环境当中。最后做OS检查,开源社区目前只做了几种系统的测试,比如centos7,ubuntu14等,若用户的系统不是列出的系统,则可以在local.conf中设置FORCE=yes去尝试安装devstack,我试过,失败的概率非常大。。。

局部的:主要是通过export将proxy引入到环境,另外,将“-”开头的services从全局打开的服务列表中去除。

6. sudoers设置

因为devstack运行在非root用户下,某些指令需要root权限。所以需要做sudo检查/安装以及相关的sudoers的配置。

7. OS系统依赖设置

8. 配置目标目录

devstack运行的默认DEST路径是/opt/stack。为该目录创建需要的子目录。比如data,logs,status以及一些需要安装的project对应的目录(cinder,nova,neutron。。。)等。并为新建的目录设置属主。

9.设置hostname

主要是因为类似rabbitmq等服务需要解析hostname,所以,必须在/etc/hosts下面设置主机名。

10.设置log及trap

log主要是执行stack.sh的时候的输出信息,trap则涉及到error发生后该如何处理,另外一类trap就是用户执行了exit这类事件devstack该如何响应。

11.配置projects

A。首先clone所有配置了的插件,例如nova-docker等。

B。配置database。

C。配置queue。

D。准备keystone, 如果enabled

E。准备swift, 如果enabled

F。安装需要的依赖工具/程序(比如pip)

G。准备虚拟环境(主要是需要的infra的支持库,安装rabbitmq-server, 安装数据库(默认mysql),以及操作数据库的python包,若neutron enable了,安装其相关agent包)

H。安装openstack的project相关的库(oslo, keystoneauth, glanceclient, cinderclient, novaclient...)

      这一步有很多的project的安装过程,是依赖相关的project service项是否配置进行操作的,比如g-api, n-api等,只有ENABLED_SERVICES里面有,才会进行安装。

I。启动rabbitmq-server,配置数据库,配置screen (devstack中每个service都运行在一个screen window中,关于screen的内容,再起一个博文讲述)

J。init/create服务

 1) netstat

    2) keystone 

    3) horizon

    4) glance (g-reg)

    5) neutron (dabase backends up and q-svc enabled)

    6) nova (n-net, q-dhcp, 此处,强制IP Forwarding打开)

    7) swift (s-proxy)

    8) cinder

    9) compute service (depends on nova and neutron, init nova cell)

K。 额外配置(post-config)

L。 启动服务(Only run the services specified in ``ENABLED_SERVICES``)

M。创建account rc文件(例如admin-openrc.sh,demo-openrc.sh等)

N。后续收尾(比如显示horizon的主页链接及默认用户名和设置的对应密码等信息)

最后补充一点关于输入输出重定向的知识,因为stack.sh中引入了3和6两个文件描述符,除了标注的输入0,输出1和错误2.

重定向指令 >,有下面一些用法:

: > filename
      # The > truncates file "filename" to zero length.
      # If file not present, creates zero-length file (same effect as 'touch').
      # The : serves as a dummy placeholder, producing no output.
> filename    
      # The > truncates file "filename" to zero length.
      # If file not present, creates zero-length file (same effect as 'touch').
      # (Same result as ": >", above, but this does not work with some shells.)
1>filename
      # Redirect stdout to file "filename."
1>>filename
      # Redirect and append stdout to file "filename."
2>filename
      # Redirect stderr to file "filename."
 
 
2>>filename
      # Redirect and append stderr to file "filename."
 
&>filename
      # Redirect both stdout and stderr to file "filename."
      # This operator is now functional, as of Bash 4, final release.
 
M>N
     # "M" is a file descriptor, which defaults to 1, if not explicitly set.
     # "N" is a filename.
     # File descriptor "M" is redirect to file "N."
 
M>&N
     # "M" is a file descriptor, which defaults to 1, if not set.
     # "N" is another file descriptor.
 
2>&1
      # Redirects stderr to stdout.
      # Error messages get sent to same place as standard output.
        >>filename 2>&1
            bad_command >>filename 2>&1
            # Appends both stdout and stderr to the file "filename" ...
        2>&1 | [command(s)]
            bad_command 2>&1 | awk '{print $5}'   # found
            # Sends stderr through a pipe.
            # |& was added to Bash 4 as an abbreviation for 2>&1 |.
 
i>&j
      # Redirects file descriptor i to j.
      # All output of file pointed to by i gets sent to file pointed to by j.
>&j
      # Redirects, by default, file descriptor 1 (stdout) to j.
      # All stdout gets sent to file pointed to by j.

下面看看如何关闭文件描述符:

n<&-

Close input file descriptor n.

0<&-, <&-

Close stdin.

n>&-

Close output file descriptor n.

1>&-, >&-

Close stdout.

原文地址:https://www.cnblogs.com/shihuc/p/5160633.html