Automating code deployment through shell scripting

Source: Internet
Author: User
Tags svn svn update

I. Traditional deployment methods and advantages and disadvantages
1. Traditional deployment methods(1) purely manual SCP(2) manual login git pull, svn update(3) Pure handmade xftp pull up(4) develop to hit a compressed package, RZ up; unzip2. Disadvantages(1) Participate in the whole operation, occupy a lot of time(2) Slow speed on line(3) More human error, management confusion(4) rollback slow, not timelySecond, environmental planning

1. Development environment--the developer has its own environment locally.

Operation and maintenance needs to set up the development environment, everyone shared services.

2. Test environment: Functional test environment and performance test environment.

3. Pre-production environment: one node in the production environment cluster.

4, production environment: directly to the user to provide services to the environment.

When the test environment is inconsistent with the database of the production environment, it may lead to incomplete testing, no problem in test environment, and may cause problems on the line.

Iii. Demand Analysis I. Demand for functional requirements

A cluster has 10 nodes

1. Implement one-click deployment of 10 nodes

2. One-click Rollback to any version

3. One-click Rollback to the previous version

II. Deployment Requirements

Deployment:

1. Where is the code: SVN, git

2. What version codes are available?

Svn/git: Pull directly to a branch SVN: Specify version number git: Specify tag

3. Differential resolution:

(1) The direct difference of each node: the configuration file may not be consistent (crontab.xml). Pre-production nodes.

(2) Code warehouse and actual differences. Whether the configuration file is placed in the Code warehouse.

4. How to update

The update will need to consider whether to restart. For example Java code, you need to consider restarting Tomcat. During the restart process, the user cannot access the.

5. Testing

Deploying multiple nodes, a node that is not successfully deployed due to configuration problems. How to test.

6. Serial and parallel

Deploy multiple nodes, serial or parallel, depending on your business needs.

7. How to Perform

1.shell script, directly execute 2.web interface
III. deployment Process
1. Get the code (direct pull)----"2. Compile (optional)----" 3. configuration file put in----"4. Packaging----" 5.SCP to the target server----"6. Remove the target server from the cluster----7. Unzip----8. Place to Webroot--- -"9.SCP diff file----" 10. Restart (optional)----"11. Test----" 12. Join a cluster
Iv. code Implementation 1, set no interactive access
The public key of the deployment machine is sent to the application server via Ssh-keygen. Note that this is usually used to log on to the deployment machine with a normal user, generate the public key, and then send the public key to the application Server Ssh-keygen -t rsa switch to the. SSH directory [[email protected] ~/.ssh]$  LLTOTAL 16-RWX------ 1 www www  397 Jul 31 22:45  AUTHORIZED_KEYS-RWX------ 1 WWW WWW 1679 JUL 31 22:44 ID_RSA-RWX------  1 www www  397 jul 31 22:44 id_rsa.pub will Id_ The content in Rsa.pub is copied and pasted into the. SSH directory of the application server's WWW user, and the file name is called authorized_keys[[email protected] .ssh]$ cat  authorized_keys ssh-rsa aaaab3nzac1yc2eaaaadaqabaaabaqcqt3vwy9wo7tksxa4ce1zxglt/ iygy30tdbknv4hw4g5bdus48urtvyljl9cwj/hwvoqbtj5mc7pmmhdmoajih1crztgxkekqfb/xp5clease7ih+wfknqavfhd75+ Yum2mbnbvisdxo+/pj/qfbmywwj6cw6ulpqkpitdjwrlpqdjgqv5h3av0khkzdoa+twdxm0lmqcwwjt7zrupq19caxg5b93ktdgyt/1x4bfct5 /+pcaed9suywenei2io8cx9otae3mryrptln0szt89qp/q+q4sktvjc1nkxhhdp2mahqeiblugulfkgubtejagafswb+ejfv0frdhk6bsj  [email protected] Note, modify Authorized_keyS permissions Chmod 600 authorized_keys In addition, set the permissions of the. SSH directory to 700chmod 700 .ssh 
2. Detailed Code
#!/bin/bash#node listpre_list= "192.168.56.11" group1_list= "192.168.56.12" rollback_list= "192.168.56.11  192.168.56.12 "#Date/time variablelog_date= ' date " +%y-%m-%d "' log_time= ' date " +%H-%M-%S "' cdate=$ (date  "+%y-%m-%d") ctime=$ (date  "+%h-%m-%s") #Shell  envshell_name= "/deploy1.sh" SHELL_DIR= " /home/www/"shell_log=" ${shell_dir}/${shell_name}.log "#Code  envpro_name=" Web-demo "CODE_DIR="/deploy/code/ Web-demo "config_dir="/deploy/config/web-demo "tmp_dir="/deploy/tmp "tar_dir="/deploy/tar "LOCK_FILE="/tmp/ Deploy.lock "usage () {echo $" usage: $0 {deploy | rollback [ list |  version ]}  "}writelog () {loginfo=$1echo " ${cdate}${ctime}: ${shell_name}: ${loginfo}   " >> ${shell_log}}shell_lock () {Touch ${lock_file}}shell_unlock () {rm -f ${ Lock_file}}code_get () {writelog  "Code_get";cd  $CODE _dir && echo  "Git pull ";     &Nbsp;   cp -r ${code_dir} ${tmp_dir}/api_ver= "456"  }code_build () {echo  Code_build}code_config () {writelog  "Code_config"/bin/cp -r ${config_dir}/base/* ${tmp_dir}/ "${pro_name}" Pkg_name= "${pro_name}" _ "$API _ver" _ "${cdate}-${ctime}" cd ${tmp_dir} && mv  ${pro_name} ${pkg_name}}code_tar () {writelog  "Code_tar" cd ${tmp_dir} &&  tar czf ${pkg_name}.tar.gz  $PKG _namewritelog  "${pkg_name}.tar.gz"}CODE_SCP () {Writelog   "CODE_SCP" for node in  $PRE _list;doscp ${tmp_dir}/${pkg_name}.tar.gz  $node:/opt/ webrootdonefor node in  $GROUP 1_list;doscp ${tmp_dir}/${pkg_name}.tar.gz  $node:/opt/ Webrootdone}cluster_node_remove () {writelog   "Cluster_node_remove"}pre_deploy () {writelog  "remove  from cluster "        ssh  $PRE _list " cd /opt/ webroot &&Amp; tar zxf ${pkg_name}.tar.gz "        ssh  $PRE _ list  "Rm -rf /webroot/web-demo && ln -s /opt/webroot/${pkg_name}  /webroot/web-demo "}url_test () {url=$1curl -s --head  $URL |grep " 200 OK "if [  $? -ne 0 ];thenshell_unlock;writelog  "Test error"  && exit;fi} Pre_test () {url_test  "http://${pre_list}/index.html" echo  "Add to cluster"}group1_deploy () { writelog  "Remove from cluster" for node in  $GROUP 1_list;do                 ssh  $node   "cd / Opt/webroot && tar zxf ${pkg_name}.tar.gz "         ssh  $node   "rm -rf /webroot/web-demo && ln -s /opt/ webroot/${pkg_name} /wEbroot/web-demo "        donescp ${config_dir}/other/ 192.168.56.12.crontab.xml 192.168.56.12:/webroot/web-demo/crontab.xml}group1_test () {url_test  "http:/ /192.168.56.12/index.html "echo " Add to cluster "}rollback_fun () {for node in $ rollback_list;do                 ssh  $node   "rm -rf /webroot/web-demo && ln -s /opt/ Webroot/$1 /webroot/web-demo "        done}rollback () {if [  -z $1 ];thenshell_unlock; echo  "Please input rollback version"   && exit;ficase $1 in    list) ls -l /opt/webroot/*.tar.gz;;     *) Rollback_fun $1esac}main () {    if [ -f  $LOCK _file ];thenecho  "deploy Is running " && exit;    fi    deploy_methon=$1     ROLLBACK_VER=$2    case  $DEPLOY _methon in         deploy)     shell_lock;    code_get ;    code_build;    code_config;    code_tar;     code_scp;    pre_deploy;    pre_test;     group1_deploy;    group1_test;    shell_unlock;     ;; Rollback)     shell_lock;    rollback  $ROLLBACK _ver;     shell_unlock;    ;; *)     usage;esac    }main $1 $2
Test mode [[email protected] ~]$ Curl--head http://192.168.56.11/index.htmlHTTP/1.1 Okdate:mon, 09:42:23 Gmtse rver:apache/2.4.6 (CentOS) openssl/1.0.1e-fips php/5.4.16 mod_wsgi/3.4 Python/2.7.5last-modified:mon, 2016 09:39 : Gmtetag: "17-538ff61ca0a00" accept-ranges:bytescontent-length:23content-type:text/html; Charset=utf-8[[email protected] ~]$ curl-s--head http://192.168.56.11/index.html|grep "$ OK" http/1.1 OK
3. Rollback
1. List rollback version 2. The destination server removes the cluster 3. Perform rollback 4. Restart and test 5. Join the cluster =========== if you are experiencing significant bug1. List rollback version 2. Perform rollback (restart) ========== very urgent 1. Rollback to previous version (restart)


The core of automated deployment is the creation of soft links, which can also be rolled back in seconds.


How to pull code from Git, listen to tell

This article is from "Jacky Xin" blog, please be sure to keep this source http://jackyxin.blog.51cto.com/1976631/1839177

Automating code deployment through shell scripting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.