Case XX, automated operation and maintenance - the code on line

In the case of sixteen has been introduced using expect scripts, you can remotely log machine and execute commands, in fact, expect script can also transfer files. This case needs is to synchronize files expect the script to achieve the code on line. Production environment, a business usually run on multiple servers, that is, load balancing, so the code running on these machines must be consistent. How consistent? There are two options.

1, by way of share

If a small amount of machine, NFS implementations may be used, if desired stability is best to use a professional storage device (NAS, SAN, etc.), such a way architecture is as follows:

1.png

The advantage of this architecture is easy to maintain, such as when there are code updates only need to update the code on a machine, the other machines will follow the update. The disadvantage is that large, then the machine, shared memory can become a bottleneck, even as competition for files cause performance problems. Another point, where shared storage is a big risk of a single point, no fault everything is OK, once fails, the entire business hang up, the impact is very large.

2, distributed

Since there are many drawbacks by sharing manner, then choose another way, the code that is stored on a local disk of each web server, as shown:

f966abb6f4d9b650a68cba8e02ec4449.png

The advantage of this is that no storage performance problem, there is no competition for resources, conflict, and no single point of failure risks. The disadvantage is that, every time the code needs to update the line updates to all web servers, more complicated. Although the process was clumsy, but most companies will choose this way. This case is the second way of background, the specific requirements are as follows:

1) provide a list of IP ip.list all web servers.

2) Assuming there is a regular user user01 on all web servers, password SGs2ox6uj, the user is a user synchronization code.

3) every time the code on line will provide a list of file.list (ie the list of files to change) a file.


A knowledge point: rsync synchronize files by file list

The rsync file synchronization tool, although there have been cases in front, but through a list of files to synchronize files is not common, look at an example:

# cat 1.txt 
/tmp/123/1.sh
/root/test/a.txt
/etc/passwd

# rsync -av --files-from=./1.txt  /  [email protected]:/

说明:1.txt为一个文件列表,即要同步的文件列表;rsync的--files-from选项指定要同步文件的列表文件(1.txt的路径,可以是绝对路径,也可以是相对路径),这个文件列表内容其实就是一堆文件路径,这个路径建议用绝对路径,不然会出错;如果文件列表中的文件路径为绝对路径,则rsync的源目录必须为/,目标目录也必须为/。


知识点二:expect脚本同步文件

跟远程执行命令类似,expect脚本要想同步文件,spawn后面的shell命令不再是ssh,而是rsync,示例:

#!/usr/bin/expect
set passwd "SGs2ox6uj"
set host "192.168.93.130"
spawn rsync -a  user01@$host:/tmp/test.txt /tmp/
expect {
    "yes/no" {send "yes\r"}
    "password:" {send "$passwd\r"}
}
expect eof

也可以传递参数给expect脚本,示例:
#!/usr/bin/expect
set passwd "SGs2ox6uj"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -a --files-from=$file  / user01@$host:/
expect {
    "yes/no" {send "yes\r"}
    "password:" {send "$passwd\r"}
}
expect eof


本案例参考脚本

#/bin/bash
##代码上线
##作者:
##日期:

#提醒用户,是否更新了要上线的代码列表文件
read -p "你是否已经更新了文件列表./file.list?确认请输入y或者Y,否则按其他任意键退出脚本。" c

#如果直接按回车,也会退出脚本
if [ -z "$c" ]
then
    exit 1
fi

if [ $c == "y" -o $c == "Y" ]
then
    echo "脚本将在2秒后,继续执行。"
    #每秒输出一个.共输出两个.
    for i in 1 2
    do
        echo -n "."
        sleep 1
    done
    echo
else
    exit 1
fi

#判断有无./rsync.exp文件
[ -f ./rsync.exp ] && rm -f ./rsync.exp

#定义rsync.exp
cat >./rsync.exp <

Note: To use this script in a production environment, you need to create a user user01 in advance, and ensure user01 user has write access to the directory on the web server code.


Guess you like

Origin blog.51cto.com/13576245/2432586