学习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。

 #!/bin/bash

 val=
echo "val: ${val:-WORLD}"
 [root@CloudGame shelltest]# ./parameterexpansion.sh
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.

 #!/bin/bash

 val=
echo "val: ${val:=WORLD}"
 [root@CloudGame shelltest]# ./parameterexpansion.sh
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.

 #!/bin/bash

 val=
echo "val: ${val:?no value setted}"
 [root@CloudGame shelltest]# ./parameterexpansion.sh
./parameterexpansion.sh: line : val: no value setted

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

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

 #!/bin/bash

 val=Hello
echo "val: ${val:+value setted}"
 [root@CloudGame shelltest]# ./parameterexpansion.sh
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或者更新。

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

4. devstack执行用户非root

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

 [root@ip---- devstack]# echo $EUID

 [root@ip---- devstack]# su stack
[stack@ip---- devstack]$ echo $EUID

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.

openstack(liberty): devstack之stack.sh分析的更多相关文章

  1. openstack(liberty): devstack之screen

    在devstack的stack.sh文件中,可以看到所有配置的service启动方式有两种,根据是否USE_SCREEN进行是在screen window中启动,还是直接起. 默认情况,USE_SCR ...

  2. openstack(liberty): devstack中的iniset/iniget函数分析

    这个ini开头的函数在devstack的启动配置中用的非常多,他主要负责.ini文件的配置,这个过程包括对相关ini文件的添加,注释,删除,获取信息,多行信息获取等. 这里主要说的iniset和ini ...

  3. [原]在使用ubuntu14.04,安装devstack的时候报错./stack.sh: line 463: generate-subunit: command not found

    =======在使用ubuntu14.04,安装devstack的时候报错./stack.sh: line 463: generate-subunit: command not found 2016- ...

  4. [Openstack]使用devstack自己主动化安装

    os环境为: ubuntu14.04 安装步骤: 更新系统软件包: sudo apt-get dist-upgrade #出现无法訪问到ubuntu官网的错误. 安装git: sudo apt-get ...

  5. CentOS7.4安装部署openstack [Liberty版] (二)

    继上一篇博客CentOS7.4安装部署openstack [Liberty版] (一),本篇继续讲述后续部分的内容 一.添加块设备存储服务 1.服务简述: OpenStack块存储服务为实例提供块存储 ...

  6. stack.sh failing giving error "g-api did not start"

    same issue i faced , tried with ./unstack.sh and ./clean.sh also but couldn't fix the issue.Followin ...

  7. CentOS7.4安装部署openstack [Liberty版] (一)

    一.OpenStack简介 OpenStack是一个由NASA(美国国家航空航天局)和Rackspace合作研发并发起的,以Apache许可证授权的自由软件和开放源代码项目. OpenStack是一个 ...

  8. [OpenStack] [Liberty] Neutron单网卡桥接模式访问外网

    环境配置: * Exsi一台 * Exsi创建的单网卡虚拟机一台 * Ubuntu 14LTS 64位操作系统 * OpenStack Liberty版本 * 使用Neutron网络而非Nova网络 ...

  9. [译] OpenStack Liberty 版本中的53个新变化

    一个新的秋季,一个新的OpenStack 版本.OpenStack 的第12个版本,Liberty,在10月15日如期交付,而且目前发行版本已经备好了.那么我们期望能从过去六个月时间的开发中获得些什么 ...

随机推荐

  1. Magento产品批量导入方法?

    从事外贸的我们在工作中,经常需要添加成千上万个的产品,如果一个一个的去上传,要花费很多时间,有是很让人头痛,那么应该如何实现产品批量上传?如果使用的是Magento系统的话,那么你现在有福利了,因为M ...

  2. 第一个Sprint冲刺第十天

    讨论成员:邵家文.李新.朱浩龙.陈俊金 工作:第一个计时功能完成,还有一些复杂的公式已完成.          关于github其实我们团队,还没有搞清楚github的真正用途,我们尚要花时间去学习如 ...

  3. Qt之JSON生成与解析

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - December ...

  4. JavaScript数据结构——树

    树:非顺序数据结构,对于存储需要快速查找的数据非常有用. 二叉树:二叉树中的节点最多只能有两个子节点(左侧子节点和右侧子节点).这些定义有助于我们写出更高效的向/从树中插入.查找和删除节点的算法. 二 ...

  5. Java字符串null相加

    Java字符串null相加 最近和同事讨论了下面的一段代码: String a = null; a += a; System.out.println(a); 运行结果: nullnull 本着学习的态 ...

  6. java NIO-我们到底能走多远系列(39)

    献给各位: Satisfied MindRed Hayes and Jack RhodesHow many times have you heard someone say,"If I ha ...

  7. editplus查找替换的正则表达式应用

    表达式        说明\t        制表符.\n        新行..        匹配任意字符.|        匹配表达式左边和右边的字符. 例如, "ab|bc" ...

  8. sql server导入mdf 报操作系统错误 5:“5(拒绝访问。)”

    错误一:拒绝访问 在安装示例库时出现以下的错误 消息 5120,级别 16,状态 101,第 1 行无法打开物理文件"D:\Download\AdventureWorks2012_Data. ...

  9. Codeforces Round #301 (Div. 2) B. School Marks

    其实是很水的一道bfs题,昨晚比赛的时候没看清题意,漏了一个条件. #include<cstdio> #include<cstring> #include<iostrea ...

  10. 根据域名获取IP地址,并探测是否可达

    /* Author :decwang@2014.09.01 Mail :deworks@sina.com*/#define PRINTLOG printf//返回0表示成功,其他为失败. int ge ...