Locations of visitors to this page

谢太数据库

Tuesday, March 18, 2014

Ansible 简介

草稿

简介

配置管理、应用部署、临时任务等功能的开源软件工具。开发语言Python。协调器配置文件Playbook使用YAML格式。默认使用SSH管理远端服务器,因此不需要额外安装代理。

对比

和puppet,saltstack的简单对比

简单对比
ansiblesaltstackpuppet
历史 始于2012,由Puppet前雇员、Cobbler的作者、Func的联合作者米高迪翰(Michael Dehaan)开发 始于2011,作者是汤玛士S赫奇(Thomas S Hatch)。新型项目,活跃,不稳定,BUG多 始于2005,历史悠久,成熟,稳定
主要功能 Configuration, State Management, Software Installation, Orchestration,Command Execution 配置管理,状态管理,软件安装,协调,远程执行 配置管理。MCollective模块负责Orchestration和远程执行
语言 Python Python Ruby
拓扑结构 CS结构(control machine, managed node)
不需要安装客户端代理
CS结构(master, minion, syndic)
必须安装代理(0.17.0+版本增加了salt-ssh,可以通过SSH通信,无需部署minion)
CS结构()
通信层协议 SSH2 (Python paramiko模块)
1.3之后旧的Fireball模式已不再使用,新增新的加速模式
1.5之后支持SSH pipeline模式
ZeroMQ (PUB/SUB模式)。速度快,有时执行丢机器 REST
安全 通信、认证基于SSH 支持外部认证、授权,如PAM, LDAP。通信不加密?
输出格式 json 格式有raw, text, yaml, json 等
结果数据还可以通过Returner导入其它外部存储,如Redis, MongoDB, MySQL等
配置文件 Playbook剧本(YAML, jinja2模板引擎)
按剧本顺序执行
没有依赖关系?
sls状态文件
默认Renderer是YAML+jinja2模板引擎
执行顺序不以sls中定义的为准。0.17.0增加state_auto_order选项,按照sls中定义的顺序执行
执行任务、顺序受依赖关系影响
pp(DSL)
开发 有API REST API,目前是个独立的项目
客户 EverNote, Rackspace, Fedora, Care.com, ... LinkedIn, Hulu, Rackspace, Mulesoft, Affirm, Comcast, Clemson University, ...

环境要求

管理端(Control Machine):

  • Python 2.6
  • Python模块:python-pip python-simplejson python-paramiko PyYAML python-jinja2 python-httplib2
被管理端(Managed Node):
  • Python 2.4+
  • Python 2.4版本需要安装Python模块:python-simplejson
注意:
  • 不支持Python 3.x
  • Solaris系统默认不带Python,默认不能支持ansible。需要手工安装Python2.4+,并指定Python解释器(设置变量ansible_python_interpreter)
  • RHEL4.x系统自带Python版本是2.3,因此默认也不能支持ansible。操作同Solaris
  • #script模块和#raw模块不需要被管理端安装Python解释器和python-simplejson模块

模块

几种command类型模块之间对比
模块被管理端是否安装Python环境控制端显示格式是否支持SHELL变量、重定向等
command 原始
raw 原始
script json
shell 原始

command模块

执行一条命令,后面跟着以空格分开的参数
不通过SHELL解释器执行命令,所以不支持SHELL变量和重定向符号等,比如$HOME变量,"<"、">"、"|"、和"&"等操作符(如有此需求,请使用#shell模块)
举例:

$ ansible bai -m command -a "hostname"
10.11.12.1 | success | rc=0 >>
sns1.cp.sohu.com

10.11.12.2 | success | rc=0 >>
sns2.cp.sohu.com

...
被管理端必须安装Python解释器和python-simplejson模块

raw模块

raw模式下,不经过其它模块子系统,执行原始的SSH命令
被管理端服务器无需安装任何Python解释器及Python模块
使用raw模式通常有2个目的:1.初始化(Bootstrap)被管理端的Python环境,2.管理一些无法安装Python的特殊设备(如路由器)。其它情况建议使用#script模块或#command模块
举例:

$ ansible bai -m raw -a "hostname"
10.11.12.1 | success | rc=0 >>
sns1.cp.sohu.com


10.11.12.1 | success | rc=0 >>
sns2.cp.sohu.com


...

script模块

将一个本地脚本传输到远程服务器上,执行此脚本,脚本后面可以带一系列参数
和#raw模块一样,被管理端服务器无需安装Python环境
举例:

$ echo 'hostname; echo $1' >/tmp/z
$ ansible bai -m script -a "/tmp/z haha"
10.11.12.1 | success >> {
    "rc": 0,
    "stderr": "",
    "stdout": "sns1.cp.sohu.com\nhaha\n"
}

10.11.12.2 | success >> {
    "rc": 0,
    "stderr": "",
    "stdout": "sns2.cp.sohu.com\nhaha\n"
}

...

shell模块

同#command模块,只不过通过/bin/sh解释器执行命令
举例:

$ ansible bai -m shell -a "echo \$HOME"
10.11.12.1 | success | rc=0 >>
/home/bai

10.11.12.2 | success | rc=0 >>
/home/bai

...

临时任务

临时任务(Ad hoc task)通常是一次性执行、比较简单的命令
剧本通常用于配置管理和发布
不写-m参数,默认以#command模块执行临时命令

剧本

剧本(Playbook)

目录

目录(Inventory)

图形用户界面

Ansible Tower是收费的(10台服务器以下免费),价格参考Ansible Tower Pricing

Tuesday, May 22, 2012

Cloud Platform SSH

Cloud Platform SSH

Thursday, May 10, 2012

Parallel Command Execution

Parallel Command Execution

比较

基本信息
语言版本日期许可网站
#pdsh C 2.28 2012-02-11 GNU GPL v2 pdsh
#clusterssh Perl 4.01_01 2011-12-09 GNU GPL ClusterSSH
#mussh Bourne-Again Shell 1.0 2011-11-03 GNU GPL MUltihost SSH Wrapper
#pssh Python 2.3.1 2012-02-02 New BSD License PSSH
#sshpt Python 2.5+ 1.2.0 2011-08-14 GNU GPL v3 SSH Power Tool
#tentakel Python 2.2 2005-03-20 unknown tentakel
#massh Bourne-Again Shell 2.0-58 2011-07-29 GNU GPL Massh
#PyDSH Python 2.7 0.6 2011-09-20 GNU GPL The Python Distributed Shell
#ClusterShell Python 2.4+ 1.6 2012-04-08 CeCILL-C ClusterShell
#xargs C n/a n/a GNU GPL n/a
#GNU Parallel Perl 20120422 2012-04-22 GNU GPL GNU Parallel
#PPSS Bourne-Again Shell 2.97 2011-12-30 GNU GPL v3 Parallel Processing Shell Script

基本功能
分布命令执行 分布文件复制 主机范围声明 主机分组 支持其它连接协议 远程执行本地脚本 SSH Key管理 输出格式化 支持密码登录
#pdsh Yes Yes(1)(2) Yes(3) Yes(3) Yes(4) No No Yes(5) No
#clusterssh - - - - - - - - -
#mussh Yes No No Yes(6) No Yes Yes(7) Yes No
#pssh Yes Yes(8) No Yes(6) No No No Yes(9) No
#sshpt Yes Yes(12) No Yes(6) No Yes(11) No Yes(10) Yes
#tentakel - - - - - - - - -
#massh Yes Yes(15) Yes(13) Yes(13)(6) No Yes No Yes(14) No
#PyDSH Yes Yes Yes Yes(6) Yes(16) No Yes(17) No No
#ClusterShell(18) Yes Yes Yes(20) Yes(20) No No No Yes(19) No
#xargs Yes
#GNU Parallel Yes
#PPSS Yes

1. 用pdcp复制文件要求两端服务器都要安装pdcp
2. rpdcp用于反向复制文件
3. 加载misc类型的模块,生成主机列表和过滤
4. 加载rcmd类型的模块,实现不同方式的远程连接
5. 用dshbak命令将输出内容按主机分组
6. 加载主机列表文件
7. 使用ssh-agent
8. prsync 同步文件, pscp 复制文件, pslurp 下载文件
9. 默认只显示一个最终的执行结果,没有屏幕输出
10. 生成cvs报告
11. 远程执行本地脚本其实是通过sftp复制过去然后执行
12. 只能复制到目标服务器,不能下载文件
13. 依赖该作者自己开发的ambit脚本,生成主机名列表和分组。也可加载主机列表文件
14. terse 只显示执行结果,worked 只显示成功的主机名,bombed 只显示失败的主机名
15. 能上传、下载一个文件
16. 支持RSH, Telnet
17. 可以生成、安装、卸载SSH key
18. 指clush,clubak等命令
19. 用clubak命令将输出内容按主机分组
20. 用nodeset工具


pdsh

介绍:

Pdsh is a high-performance, parallel remote shell utility. It uses a sliding window of threads to execute remote commands, conserving socket resources while allowing some connections to timeout if needed. It was originally written as a replacement for IBM's DSH on clusters at LLNL.

The core functionality of pdsh is supplemented by many available dynamically loadable modules. The modules may implement a new connection protocol, target host filtering (e.g. removing hosts that are "down" from the target host list) and/or other host selection options (e.g. -a selects all hosts from a config file).

The pdsh distribution also contains a parallel remote copy utility (pdcp - copy from local host to a group of remote hosts in parallel), reverse parallel remote copy (rpdcp, copy from a group of hosts to localhost in parallel), and a script dshbak for formatting and demultiplexing pdsh output.

文档:

程序:

  • pdsh 并行执行
  • pdcp 复制文件
  • rpdcp 反向复制文件
  • dshbak Perl脚本

安装:

make clean
./configure --with-ssh --with-netgroup --with-readline
make
make check
make install

举例:

并行执行命令:

# pdsh -f 2 -R exec -w 10.10.100.193,10.10.100.194,10.10.100.195 ssh -x -l %u %h "date;hostname;sleep 3;date"
10.10.100.193: Tue May  1 15:22:44 CST 2012
10.10.100.193: vm-10-10-100-193.company.com
10.10.100.194: Tue May  1 15:22:44 CST 2012
10.10.100.194: vm-10-10-100-194.company.com
10.10.100.193: Tue May  1 15:22:47 CST 2012
10.10.100.194: Tue May  1 15:22:47 CST 2012
10.10.100.195: Tue May  1 15:22:47 CST 2012
10.10.100.195: vm-10-10-100-195.company.com
10.10.100.195: Tue May  1 15:22:50 CST 2012

用dshbak格式化屏幕输出:

# pdsh -f 2 -R exec -w 10.10.100.193,10.10.100.194,10.10.100.195 ssh -x -l %u %h "date;hostname;sleep 3;date" | dshbak
----------------
10.10.100.193
----------------
Tue May  1 15:23:34 CST 2012
vm-10-10-100-193.company.com
Tue May  1 15:23:37 CST 2012
----------------
10.10.100.194
----------------
Tue May  1 15:23:34 CST 2012
vm-10-10-100-194.company.com
Tue May  1 15:23:37 CST 2012
----------------
10.10.100.195
----------------
Tue May  1 15:23:37 CST 2012
vm-10-10-100-195.company.com
Tue May  1 15:23:40 CST 2012

并行复制文件:

# pdcp -R ssh -w 10.10.100.193 -p /u01/oracle/deploy/bin/halvs-upgrade.sh /tmp/halvs-upgrade.sh
10.10.100.193: bash: /usr/local/bin/pdcp: No such file or directory
pdcp@xpt01: 10.10.100.193: ssh exited with exit code 127

目标服务器需要pdcp可执行命令

用netgroup给主机分组:

cat >/etc/netgroup <<'EOF'
cp-test (10.10.100.193,,) (10.10.100.194,,)
cp-all @cp-test
EOF

vi /etc/nsswitch.conf
netgroup:   files nisplus

getent netgroup cp-test

pdsh -g cp-test -q
pdsh -R ssh -g cp-test hostname


clusterssh

介绍:

ClusterSSH controls a number of xterm windows via a single graphical console window to allow commands to be interactively run on multiple servers over an ssh connection.

功能:

  • 图形界面的
  • 依赖一堆Perl模块

安装:

perl Build.PL
./Build
./Build test
./Build install

依赖的Perl模块

# perl Build.PL
Checking prerequisites...
  requires:
    !  Exception::Class is not installed
    !  Tk is not installed
    !  X11::Protocol is not installed
  build_requires:
    !  File::Which is not installed
    !  Readonly is not installed
    !  Test::DistManifest is not installed
    !  Test::Pod is not installed
    !  Test::Pod::Coverage is not installed
    !  Test::Trap is not installed

因cpan install Tk执行失败,就没再试


mussh

介绍:

Mussh is a shell script that allows you to execute a command or script over ssh on multiple hosts with one command. When possible mussh will use ssh-agent and RSA/DSA keys to minimize the need to enter your password more than once.

程序

  • mussh 并行执行

功能:

  • 设置并行度(-m)

安装:

cp -p mussh /usr/local/bin/
cp -p mussh.1 /usr/local/man/man1/

举例:

并行执行命令:

# mussh -A -m 2 -h 10.10.100.193 10.10.100.194 10.10.100.195 -c "date;hostname;sleep 3;date"
10.10.100.193: Tue May  1 15:21:33 CST 2012
10.10.100.193: vm-10-10-100-193.company.com
10.10.100.194: Tue May  1 15:21:34 CST 2012
10.10.100.194: vm-10-10-100-194.company.com
10.10.100.193: Tue May  1 15:21:36 CST 2012
10.10.100.195: Tue May  1 15:21:36 CST 2012
10.10.100.195: vm-10-10-100-195.company.com
10.10.100.194: Tue May  1 15:21:37 CST 2012
10.10.100.195: Tue May  1 15:21:39 CST 2012

格式化输出:

# mussh -b -A -m 2 -h 10.10.100.193 10.10.100.194 10.10.100.195 -c "date;hostname;sleep 3;date"
10.10.100.193: Tue May  1 15:25:04 CST 2012
10.10.100.193: vm-10-10-100-193.company.com
10.10.100.193: Tue May  1 15:25:07 CST 2012
10.10.100.194: Tue May  1 15:25:05 CST 2012
10.10.100.194: vm-10-10-100-194.company.com
10.10.100.194: Tue May  1 15:25:08 CST 2012
10.10.100.195: Tue May  1 15:25:07 CST 2012
10.10.100.195: vm-10-10-100-195.company.com
10.10.100.195: Tue May  1 15:25:10 CST 2012

远程执行本地脚本:

# cat >/tmp/hello.sh <<EOF
> echo 'hello world!'
> EOF
# mussh -A -m 2 -h 10.10.100.193 10.10.100.194 10.10.100.195 -C /tmp/hello.sh
10.10.100.193: hello world!
10.10.100.194: hello world!
10.10.100.195: hello world!


pssh

介绍:

PSSH provides parallel versions of OpenSSH and related tools. Included are pssh, pscp, prsync, pnuke, and pslurp. The project includes psshlib which can be used within custom applications.

程序:

  • pnuke 杀进程
  • prsync 同步文件
  • pscp 复制文件
  • pslurp 下载文件
  • pssh 并行执行
  • pssh-askpass

安装:

python setup.py install

举例:

执行命令:

# pssh -P -p 2 -H 10.10.100.193 -H 10.10.100.194 -H 10.10.100.195 "date;hostname;sleep 3;date"
10.10.100.193: Tue May  1 15:48:42 CST 2012
10.10.100.194: Tue May  1 15:48:42 CST 2012
10.10.100.193: vm-10-10-100-193.company.com
10.10.100.194: vm-10-10-100-194.company.com
10.10.100.193: Tue May  1 15:48:45 CST 2012
[1] 15:48:45 [SUCCESS] 10.10.100.193
10.10.100.194: Tue May  1 15:48:45 CST 2012
[2] 15:48:45 [SUCCESS] 10.10.100.194
10.10.100.195: Tue May  1 15:48:46 CST 2012
10.10.100.195: vm-10-10-100-195.company.com
10.10.100.195: Tue May  1 15:48:49 CST 2012
[3] 15:48:49 [SUCCESS] 10.10.100.195

格式化输出:

# pssh -i -p 2 -H 10.10.100.193 -H 10.10.100.194 -H 10.10.100.195 "date;hostname;sleep 3;date"
[1] 15:51:14 [SUCCESS] 10.10.100.193
Tue May  1 15:51:11 CST 2012
vm-10-10-100-193.company.com
Tue May  1 15:51:14 CST 2012
[2] 15:51:14 [SUCCESS] 10.10.100.194
Tue May  1 15:51:11 CST 2012
vm-10-10-100-194.company.com
Tue May  1 15:51:14 CST 2012
[3] 15:51:17 [SUCCESS] 10.10.100.195
Tue May  1 15:51:14 CST 2012
vm-10-10-100-195.company.com
Tue May  1 15:51:17 CST 2012


sshpt

介绍:

The SSH Power Tool (sshpt) enables you to execute commands and upload files to many servers simultaneously via SSH without using pre-shared keys. Uploaded files and commands can be executed directly or via sudo. Connection and command execution results are output in standard CSV format for easy importing into spreadsheets, databases, or data mining applications.

程序:

  • sshpt.py 并行执行

功能:

  • 无需ssh key
  • 依赖Paramiko和pycrypto这两个Python模块
  • Python 2.5+ (not 3.0+ yet)

安装:

rpm -Uvh python-crypto-2.0.1-1.el5.rf.x86_64.rpm
rpm -Uvh python-paramiko-1.7.6-1.el5.rf.noarch.rpm
cp -p sshpt.py /usr/local/bin/

举例:

并行执行命令:

echo "10.10.100.193
10.10.100.194
10.10.100.195" | sshpt.py -S -t 2 -u someone -P "secret"  "date;hostname;sleep 3;date"

RHEL5系统自带Python版本较低,是2.4版本,因此没有继续再试


tentakel

As of 2011-04-07, this project is no longer under active development.

版本太老了,就不试了


massh

介绍:

Massh makes it possible to perform the following on hundreds, or even thousands of hosts in a parallelized fashion:

  • Run Remote Commands
  • Push and Execute Pre-Written Scripts
  • Push Files
  • Pull Files

Massh uses Ambit to enumerate a list of targets for which it will execute one of the aforementioned operations. Hosts can be logically grouped and dynamically managed allowing for consistent operational changes and overall management of identically purposed hosts.

Massh can display the full output resulting from its various operations or it can be set to display an operation's success or failure for individual target hosts. The full ouput produced by Massh is completely organized and labeled by host, regardless of what order output is returned from target hosts.

Starting with version 2.x Massh combines flexible option control management and a simplified command + subcommand sytax. This reduces the complexity of typical Massh commands and makes it much easier to learn how to perform specific operations with specific options with Massh.

程序:

  • /etc/massh/massh.conf
  • /usr/bin/massh
  • /usr/bin/masshfilesupdate
  • /usr/lib/massh/libmassh.sh
  • /usr/share/man/man1/massh.1.gz
  • --以下是ambit的程序--
  • /etc/ambit/ambit.conf
  • /usr/bin/ambit
  • /usr/lib/ambit/libambit.sh
  • /usr/share/man/man1/ambit.1.gz
  • /var/ambit/hosts/down

功能:

  • 设置连接超时
  • 设置并行度。脚本有问题,${wc}应该是wc,还有${dirname}

安装:

rpm -Uvh ambit-2.0-40.noarch.rpm
rpm -Uvh massh-2.0-58.noarch.rpm

举例:

修改massh配置:

# massh list options

System Default Options
----------------------
Debug="no"
ForcePseudoTTY="no"
SyslogMassh="yes"
SSHLogLevel="quiet"
TimeOut="30"
Parallel="30"
Random="$RANDOM"
ControlPath="$HOME/.ssh/masshter-%r@%h:%p"
ControlMaster="no"
ControlPersist="300"
HostsFrom="ambit"
Separator="no"
SubCommand=""

User Specified Options
----------------------
NONE

# echo 2 | massh ed opt Parallel
Updating Option : Parallel
Current Value   : 30
Parallel="2"

并行执行命令:

# massh 10.10.100.19[3..5] verbose "date;hostname;sleep 3;date"
10.10.100.193 : Tue May  1 19:16:58 CST 2012
10.10.100.193 : vm-10-10-100-193.company.com
10.10.100.193 : Tue May  1 19:17:01 CST 2012
10.10.100.194 : Tue May  1 19:16:58 CST 2012
10.10.100.194 : vm-10-10-100-194.company.com
10.10.100.194 : Tue May  1 19:17:01 CST 2012
10.10.100.195 : Tue May  1 19:17:01 CST 2012
10.10.100.195 : vm-10-10-100-195.company.com
10.10.100.195 : Tue May  1 19:17:04 CST 2012

执行脚本:

# cat >/tmp/hello.sh <<EOF
> echo 'hello world!'
> EOF
# massh 10.10.100.19[3..5] execute /tmp/hello.sh
10.10.100.193 : hello world!
10.10.100.194 : hello world!
10.10.100.195 : hello world!


PyDSH

介绍:

PyDSH is a remote administration tool, consisting of pydsh and pydcp. Pydsh allows you to run a command on multiple hosts in parallel over RSH, SSH or Telnet, OR manage your SSH public keys. The pydcp command allows copying files to/from multiple hosts.

文档:

功能:

  • 设置连接超时
  • 设置并行度
  • 加载主机列表文件
  • 依赖Pexpect 0.999
  • 远程执行本地脚本
  • 能上传、下载一个文件

安装:

cd pexpect-2.3
python ./setup.py install
cd ..
cd pydsh-0.6
./install.sh --install

需要Python 2.7,系统自带版本是2.4的,就没再试


ClusterShell

介绍:

ClusterShell is an event-driven open source Python library, designed to run local or distant commands in parallel on server farms or on large Linux clusters. It will take care of common issues encountered on HPC clusters, such as operating on groups of nodes, running distributed commands using optimized execution algorithms, as well as gathering results and merging identical outputs, or retrieving return codes. ClusterShell takes advantage of existing remote shell facilities already installed on your systems, like SSH.

ClusterShell's primary goal is to improve the administration of high-performance clusters by providing a lightweight but scalable Python API for developers. It also provides clush, clubak and nodeset, three convenient command-line tools that allow traditional shell scripts to benefit from some of the library features.

文档:

程序:

  • /etc/clustershell/clush.conf
  • /etc/clustershell/groups
  • /etc/clustershell/groups.conf
  • /etc/clustershell/groups.conf.d/
  • /usr/bin/clubak
  • /usr/bin/clush
  • /usr/bin/nodeset

安装:

rpm -Uvh clustershell-1.6-1.el5.noarch.rpm
rpm -Uvh vim-clustershell-1.6-1.el5.noarch.rpm

举例:


xargs

系统自带

-P参数表示并行度

举例:

# echo 10.10.100.19{3..5}|tr ' ' '\n'|xargs -n1 -P2 -I{} ssh root@{} "sh -c 'date;hostname;sleep 3;date'|sed 's/^/{}: /'"
10.10.100.194: Tue May  1 23:05:59 CST 2012
10.10.100.194: vm-10-10-100-194.company.com
10.10.100.194: Tue May  1 23:06:02 CST 2012
10.10.100.193: Tue May  1 23:05:59 CST 2012
10.10.100.193: vm-10-10-100-193.company.com
10.10.100.193: Tue May  1 23:06:02 CST 2012
10.10.100.195: Tue May  1 23:06:03 CST 2012
10.10.100.195: vm-10-10-100-195.company.com
10.10.100.195: Tue May  1 23:06:06 CST 2012


GNU Parallel

介绍:

GNU parallel is a shell tool for executing jobs in parallel using one or more computers. A job is can be a single command or a small script that has to be run for each of the lines in the input. The typical input is a list of files, a list of hosts, a list of users, a list of URLs, or a list of tables. A job can also be a command that reads from a pipe. GNU parallel can then split the input and pipe it into commands in parallel.

If you use xargs and tee today you will find GNU parallel very easy to use as GNU parallel is written to have the same options as xargs. If you write loops in shell, you will find GNU parallel may be able to replace most of the loops and make them run faster by running several jobs in parallel.

GNU parallel makes sure output from the commands is the same output as you would get had you run the commands sequentially. This makes it possible to use output from GNU parallel as input for other programs.

For each line of input GNU parallel will execute command with the line as arguments. If no command is given, the line of input is executed. Several lines will be run in parallel. GNU parallel can often be used as a substitute for xargs or cat | bash.

文档:


PPSS

介绍:

PPSS is a Bash shell script that executes commands, scripts or programs in parallel. It is designed to make full use of current multi-core CPUs. It will detect the number of available CPUs and start a separate job for each CPU core. It will also use hyper threading by default.

PPSS can be run on multiple hosts, processing a single group of items, like a cluster.

文档:


参考

Wednesday, November 16, 2011

Linkedin/Glu Deployment Automation Platform

Glu自动化部署平台

What is glu?
glu is a free/open source deployment and monitoring automation platform.

What problems does glu solve?
glu is solving the following problems:
deploy (and monitor) applications to an arbitrary large set of nodes:
efficiently
with minimum/no human interaction
securely
in a reproducible manner
ensure consistency over time (prevent drifting)
detect and troubleshoot quickly when problems arise

How does it work?
glu takes a very declarative approach, in which you describe/model what you want, and glu can then:
compute the set of actions to deploy/upgrade your applications
ensure that it remains consistent over time
detect and alert you when there is a mismatch


Glu has 3 major components. Agents, Orchestration Engine, and ZooKeeper.


glu vs puppet
puppet is very good at configuring the infrastructure of a machine (users, groups, packages, etc...)
  • => static/stable does not change often
glu is very good at provisioning dynamic applications on an ensemble of machines (the system)
  • => changes often, real-time failure detection (monitoring), "bounce", etc...



glu
Glu Tech Talk 201107
Glu-ing the Last Mile by Ken Sipe.
Continuous Deployment at outbrain
The future of glu - glu in the cloud
Monitoring a Wild Beast

Thursday, September 1, 2011

bash merge sort

bash merge sort


bash归并排序

#!/bin/bash
rest() { shift; echo $@; }
length() { echo $#; }
first() { echo $1; }
merge()
{
  declare left=$1 right=$2 result=
  declare length_left=$(length $left)
  declare length_right=$(length $right)
  while (( length_left>0 || length_right>0 )); do
    if (( length_left>0 && length_right>0 )); then
      if [ $(first $left) -le $(first $right) ]; then
        result="$result $(first $left)"
        left=$(rest $left)
      else
        result="$result $(first $right)"
        right=$(rest $right)
      fi
    elif [ $(length $left) -gt 0 ]; then
      result="$result $(first $left)"
      left=$(rest $left)
    elif [ $(length $right) -gt 0 ]; then
      result="$result $(first $right)"
      right=$(rest $right)
    fi
    length_left=$(length $left)
    length_right=$(length $right)
  done
  echo $result
}
merge_sort()
{
  [ $# -le 1 ] && { echo $@; return; }
  declare -i middle=$(($#/2+1))
  declare left= right= result=
  for ((i=1;i<middle;i++)); do
    left="$left ${!i}"
  done
  for ((i=middle;i<=$#;i++)); do
    right="$right ${!i}"
  done
  left=$(merge_sort $left)
  right=$(merge_sort $right)
  result=$(merge "$left" "$right")
  echo $result
}
merge_sort $@

执行:
# ./merge_sort.sh $(for in in {1..50}; do echo $((RANDOM%200-100)); done)
-99 -89 -88 -84 -83 -83 -81 -76 -75 -70 -70 -70 -54 -53 -51 -51 -49 -49 -40 -40 -30 -27 -24 -19 -15 -13 -12 -12 -8 -2 -2 -2 2 4 10 17 20 24 28 34 39 39 40 42 45 48 48 49 62 83

算法参考:Merge sort


-fin-

Tuesday, August 30, 2011

bash quicksort

bash quicksort

用bash实现快速排序
参考Quicksort算法实现,如下:
function swap()
{
  declare -r _array_name=$1
  declare -r _i1=$2
  declare -r _i2=$3
  eval declare -r _tmp=\${${_array_name}[$_i1]}
  eval ${_array_name}[$_i1]=\${${_array_name}[$_i2]}
  eval ${_array_name}[$_i2]=\$_tmp
}
function partition()
{
  declare -r _array_name=$1
  declare -r _left=$2
  declare -r _right=$3
  declare -r _pivot_index=${4:-$_left}
  eval declare -r _pivot_value=\${${_array_name}[$_pivot_index]}
  swap $_array_name $_pivot_index $_right
  declare _store_index=$_left
  for ((_i=_left; _i<_right; _i++)); do
    eval declare _v=\${${_array_name}[$_i]}
    if (( _v < _pivot_value )); then
      swap $_array_name $_i $_store_index
      ((_store_index++))
    fi
  done
  swap $_array_name $_right $_store_index
  #echo "store_index=$_store_index"
  return $_store_index
}
function quick_sort()
{  
  declare -r _array_name=$1
  declare -r _left=$2
  declare -r _right=$3
  #declare -r _tmpfile=${TMPDIR:-/tmp}/qs_tmpfile.$$
  #((_right-_left>256)) && { echo "do not support more than 256 elements in array." >&2; return 1; }
  if ((_left<_right)); then
    declare _pivot_index=$_left
    #partition $_array_name $_left $_right $_pivot_index >$_tmpfile
    #declare _pivot_new_index=$(sed -n '/^store_index=/s/.*=//p' $_tmpfile)
    partition $_array_name $_left $_right $_pivot_index
    declare _pivot_new_index=$?
    quick_sort $_array_name $_left $((_pivot_new_index-1))
    quick_sort $_array_name $((_pivot_new_index+1)) $_right
  fi
  #[ -f $_tmpfile ] && /bin/rm -f $_tmpfile
}

稍微简化一下,改成:
function quick_sort2()
{  
  swap()
  {
    eval declare -r _tmp=\${${_array_name}[$1]}
    eval ${_array_name}[$1]=\${${_array_name}[$2]}
    eval ${_array_name}[$2]=\$_tmp
  }
  declare -r _array_name=$1
  declare -r _left=$2
  declare -r _right=$3
  if ((_left<_right)); then
    declare -r _pivot_index=$_left
    #declare -r _pivot_index=$(( ($_right+$_left)/2 ))
    eval declare _pivot_value=\${${_array_name}[$_pivot_index]}
    swap $_pivot_index $_right
    declare _store_index=$_left
    for ((_i=_left; _i<_right; _i++)); do
      eval declare _v=\${${_array_name}[$_i]}
      if (( _v < _pivot_value )); then
        swap $_i $_store_index
        ((_store_index++))
      fi
    done
    swap $_right $_store_index
    quick_sort2 $_array_name $_left $((_store_index-1))
    quick_sort2 $_array_name $((_store_index+1)) $_right
  fi
}

运行结果
# declare -a a=($(for in in {1..50}; do echo $((RANDOM%200-100)); done))
# quick_sort2 a 0 $((${#a[@]}-1))
# echo "${a[@]}"
-99 -97 -96 -94 -90 -82 -77 -76 -72 -70 -66 -66 -66 -63 -59 -53 -51 -50 -50 -48 -46 -45 -38 -34 -33 -30 -22 -21 -21 -20 -18 -10 0 2 2 3 9 24 26 26 44 46 47 47 62 66 80 95 99 99
#

其实网上有个更简单的办法,Tweetable Quicksort by john,一行脚本就可搞定了。
q(){ l=;g=;[ $# -lt 2 ]&&echo $@||(for n in ${@:2};do [ $n -gt $1 ]&&g="$g$n "||l="$l$n ";done;echo `q $l` $1 `q $g`;)}
运行
# q $(for in in {1..50}; do echo $((RANDOM%200-100)); done)
-100 -96 -92 -91 -80 -75 -71 -66 -54 -52 -44 -43 -40 -33 -32 -29 -28 -23 -20 -12 -12 1 16 22 22 27 27 33 33 35 38 39 41 44 45 48 50 55 58 61 62 66 69 72 83 83 85 94 97 98

外部链接:
一个Quicksort究竟可以写到多么短
快速排序(Quicksort)的Javascript实现
数学之美番外篇:快排为什么那样快


又看到一个SLEEP排序,很有趣
Genius sorting algorithm: Sleep sort
#!/bin/bash
function f() {
    sleep "$1"
    echo "$1"
}
while [ -n "$1" ]
do
    f "$1" &
    shift
done
wait

example usage:
./sleepsort.bash 5 3 6 3 6 3 1 4 7


-fin-

Wednesday, August 17, 2011

Getting MySQL process list with GDB

用GDB调试MySQL,显示线程列表

define helper functions
cat >mysql.gdb <<'EOF'
#set interactive-mode off
set confirm off
dont-repeat
define show_processlist
  #argc is not available in older version gdb
  #if $argc==1
  #  set $p_thread_id=(long long)$arg0
  #else
    set $p_thread_id=(long long)-1
  #end
  set $current=((base_ilist)threads).first
  set $last=&(((base_ilist)threads).last)
  printf "Id\tUser\tHost\tDb\tCommand\tTime\tState\tInfo\n"
  while $current != $last
    set $tmp=(class THD *)$current
    set $tmp_sctx=$tmp->security_ctx
    if ( $p_thread_id == -1 || $p_thread_id == $tmp->thread_id )
      set $id=$tmp->thread_id
      set $user=(char*)($tmp_sctx->user?$tmp_sctx->user:($tmp->system_thread?"system user":"unauthenticated user"))
      set $host=$tmp_sctx->host_or_ip
      set $db=($tmp->db)?$tmp->db:"NULL"
      set $command=(int)($tmp->command)
      #set $proc_info=(char*)(($tmp->killed==THD::KILL_CONNECTION)?"Killed":0) #5.1
      set $proc_info=(char*)($tmp->killed?"Killed":0)
      set $state_info=(char*) ($tmp->locked ? "Locked" : $tmp->net.reading_or_writing ? ($tmp->net.reading_or_writing == 2 ? "Writing to net" : $command == COM_SLEEP ? "" : "Reading from net") : $tmp->proc_info ? $tmp->proc_info : ($tmp->mysys_var && $tmp->mysys_var->current_cond) ? "Waiting on cond" : "NULL")
      set $start_time=$tmp->start_time
      #set $now=my_time(0) #5.1
      set $now=time((time_t*) 0)
      set $query=(char*)($tmp->query_string.str?$tmp->query_string.str:"")
      printf "%u\t%s\t%s\t%s\t%s\t%u\t%s\t%.100s\n",$id,$user,$host,$db,command_name[$command].str,$now-$start_time,$state_info,$query
    end
    set $current=$current->next
  end
end
document show_processlist
  Prints MySQL process list.
  Syntax: show_processlist [thread id]
  Examples:
    show_processlist
    show_processlist 123
    show_processlist -1
end
define kill_thread
  #if $argc >= 1
    set $p_thread_id=(unsigned long long)$arg0
    #set $p_only_kill_query=(char *)(($argc >= 2)?$arg1:"connection")
    set $p_only_kill_query=(char *)$arg1
    set $only_kill_query=(strcmp($p_only_kill_query,(char *)"query")==0)?1:0
    set $current=((base_ilist)threads).first
    set $last=&(((base_ilist)threads).last)
    while $current != $last
      set $tmp=(class THD *)$current
      if $p_thread_id == $tmp->thread_id
        set $id=$tmp->thread_id
        printf "kill thread id %d %s\n", $id, $only_kill_query?"only query":""
        #call kill_one_thread($tmp, $id, $only_kill_query)
        call sql_kill($tmp, $id, $only_kill_query)
        loop_break
      end
      set $current=$current->next
    end
  #else
  #  help kill_thread
  #end
end
document kill_thread
  Kills MySQL thread.
  Syntax: kill_thread <thread id> <query|connection>
  Examples:
    kill_thread 123
    kill_thread 123 "connection"
    kill_thread 321 "query"
end
EOF

show processlist


mysql_port=8217
# older version gdb does not support '-ex' option
#gdb -batch -silent -p $(pgrep -f "/mysqld .*$mysql_port"|head -n1) -x mysql.gdb -ex 'show_processlist'
gdb -silent -p $(pgrep -f "/mysqld .*$mysql_port"|head -n1) -x mysql.gdb <<'EOF' |\
sed -n '/^=====begin=====$/,/=====end=====/p'|sed '1d;$d'
set prompt
set pagination off
printf "\n\n"
printf "=====begin=====\n"
show_processlist
printf "=====end=====\n"
EOF

kill thread

thread_id=223
gdb -silent -p $(pgrep -f "/mysqld .*$mysql_port"|head -n1) -x mysql.gdb <<EOF
kill_thread $thread_id "connect"
EOF
Website Analytics

Followers