DevOps 工具-代码部署脚本

简介:

Neo Chen (netkiller)

版权 © 2011, 2012 http://netkiller.github.com

摘要

本程序用于收集,防火墙,路由器,交换机等日志

 

 

 

 

Bash代码   收藏代码
  1. #!/bin/bash  
  2. #####################################################################  
  3. # Description: Automation Deployment Script   
  4. #              Netkiller series utilities  
  5. # Author: Neo<openunix@163.com>  
  6. # Homepage: http://netkiller.sourceforge.net/  
  7. # $Id$  
  8. #####################################################################  
  9.   
  10. if [ -z $DEPLOY_HOME ]; then  
  11.     echo 'Example: export DEPLOY_HOME=/srv/deploy'  
  12.     exit  
  13. fi  
  14. if [ -f $DEPLOY_HOME/conf/default.conf ];then  
  15.     . $DEPLOY_HOME/conf/default.conf  
  16. fi  
  17.   
  18. #================================================================================  
  19.   
  20. LOGFILE="deploy.$(date -d "today" +"%Y-%m-%d").log"  
  21. TMPDIR=$(mktemp -d --suffix=.tmp -p /tmp deploy.XXXXXX)  
  22.   
  23. #SUBVERSION=  
  24. SVN=/usr/bin/svn  
  25.   
  26. TIMEPOINT=`date '+%Y%m%d-%H%M%S'`  
  27. BACKUPDIR=$TMPDIR/backup/${TIMEPOINT}  
  28. RSYNC="rsync"  
  29. UPLOAD_DIR=$TMPDIR  
  30. REVISION=''  
  31. #================================================================================  
  32.   
  33. if [ ! -d ${TMPDIR} ]; then  
  34.     mkdir ${TMPDIR}  
  35. fi  
  36.   
  37. if [ ! -d ${BACKUPDIR} ]; then   
  38.     mkdir -p $BACKUPDIR  
  39. fi  
  40.   
  41. #pkgname=${project}-${version}-${datetime}.pkg  
  42. #tar jcvf ${pkgname} /tmp/${project} --remove-files >> deploy.log  
  43. #####################################################################  
  44.   
  45. function usage(){  
  46. echo $"Usage: $0 [OPTION] <server-id> <directory/timepoint>"  
  47. echo -ne "  
  48. OPTION:  
  49.   
  50.         deploy <domain> <host>  
  51.         revert <domain> <host> <revision>  
  52.         backup <domain> <host> <directory>  
  53.   
  54.         list  
  55.         list <domain> <host>  
  56.   
  57.         import <domain> <host> <dir>  
  58.         export <domain> <host> <dir>  
  59.       
  60.     clean <domain> <host>     
  61.     log <project> <line>  
  62.   
  63.         conf list  
  64.         conf new <project>  
  65.         conf remove <project>  
  66.         conf show <project>  
  67.         conf edit <project>        
  68.   
  69.     cron show  
  70.     cron setup  
  71.     cron edit  
  72. "  
  73.   
  74.     exit  
  75. }  
  76.   
  77. function logging(){  
  78.         local logfile="$LOGDIR/$LOGFILE"  
  79.         local timepoint=$(date -d "today" +"%Y-%m-%d_%H:%M:%S")  
  80.         local status=$1  
  81.         local message=$2  
  82.         echo "[$timepoint] [${status}] ${message}" >> $logfile  
  83. }  
  84. #logging 'OK' 'This is test msg!!!'  
  85.   
  86. function conf(){  
  87.     local cmd=$2  
  88.     local prj=$3  
  89.     case $cmd in  
  90.         list)  
  91.             ls $SYSCONFDIR  
  92.             ;;  
  93.         new)  
  94.             read -p "Project directory: " prjdir  
  95.             if [ -z $prjdir ]; then  
  96.                 exit  
  97.             fi  
  98.             if [ -f $SYSCONFDIR/$prjdir.conf ]; then  
  99.                 echo "cannot create config $prjdir.conf': File exists"  
  100.                 exit 1  
  101.             fi  
  102.   
  103.             read -p "subversion url: $REPOSITORIES/: " svnurl  
  104.             if [ -z $svnurl ]; then  
  105.                 svnurl=$REPOSITORIES  
  106.             fi  
  107.             read -p "hostname: " host  
  108.             if [ -z $host ]; then  
  109.                 host="localhost"  
  110.                 echo "default hostname 'localhost'"  
  111.             fi  
  112.             read -p "upload mode ftp/scp/sftp/rsync: " mode  
  113.             if [ -z $mode ]; then  
  114.                 mode=ftp  
  115.             else  
  116.                 case $mode in  
  117.                     ftp)  
  118.                         mode="ftpdeploy"  
  119.                         ;;  
  120.                     scp)  
  121.                         mode="scpdeploy"  
  122.                         ;;  
  123.                     sftp)  
  124.                         mode="sftpdeploy"  
  125.                         ;;  
  126.                     rsync)  
  127.                         mode="rsync"  
  128.                         ;;  
  129.                 esac  
  130.             fi  
  131.             read -p "Create $prjdir config? [y/n]" -n 1 key  
  132.             echo  
  133.             if [ $key = 'y' ]; then  
  134.                 echo -ne "REPOSITORIES=$REPOSITORIES/$svnurl  
  135. COMMAND=$mode  
  136. HOSTNAME=$host  
  137.                 " >> $SYSCONFDIR/$prjdir.conf  
  138.             fi  
  139.             ;;  
  140.         remove)  
  141.             if [ -f $SYSCONFDIR/$prj ]; then  
  142.                 rm -rf $SYSCONFDIR/$prj  
  143.             fi  
  144.             ;;  
  145.         show)  
  146.             cat $SYSCONFDIR/$prj  
  147.             ;;  
  148.         edit)  
  149.             vim $SYSCONFDIR/$prj  
  150.             ;;  
  151.         *)  
  152.         usage  
  153.         ;;  
  154.     esac  
  155.   
  156. }  
  157.   
  158.   
  159. #####################################################################  
  160.   
  161. function config {  
  162.     local cfg=$1  
  163.     exclude_from=$PREFIX/exclude/${cfg}.lst  
  164.     include_from=$PREFIX/include/${cfg}.lst  
  165.   
  166.     if [ -f $SYSCONFDIR/${cfg}.conf ];then  
  167.         . $SYSCONFDIR/${cfg}.conf   
  168.     else  
  169.         echo "Please provide the config($SYSCONFDIR/${cfg}.conf) to deploy!"  
  170.         exit  
  171.     fi  
  172.     if [ -z "$cfg" ]; then  
  173.         echo "Please provide the path for deploy!"  
  174.         exit  
  175.     fi  
  176.   
  177.     if [ ! -f $exclude_from ]; then  
  178.         echo "Please provide a list of excluded in the $exclude_from."  
  179.         exit  
  180.     fi  
  181.     if [ ! -f $include_from ]; then  
  182.         echo "Please provide a list of included in the $include_from."  
  183.         exit  
  184.     fi  
  185. }  
  186.   
  187. function deploy() {  
  188.   
  189.     local domain=$2  
  190.     local host=$3  
  191.     local logfile=${LOGDIR}/${host}.${domain}.log  
  192.       
  193.     if [ -z $domain ]; then  
  194.         usage  
  195.     fi  
  196.       
  197.     config ${domain}/${host}  
  198.   
  199.     project=$SRCDIR/${domain}/$host.${domain}  
  200.    
  201.     SVN_OPTS=${REVISION}      
  202.     if [ -d ${project} ]; then   
  203.         $SVN update ${SVN_OPTS} $project >> $logfile  
  204.         logging 'update' ${project}  
  205.     else  
  206.         mkdir -p ${project}  
  207.         $SVN checkout ${REPOSITORY} ${project} >> $logfile  
  208.         logging 'checkout' ${project}   
  209.     fi  
  210.       
  211. #    $BINDIR/$COMMAND deploy $HOSTNAME $UPLOAD_DIR/$ver/      
  212.   
  213.     RSYNC_OPTS=" -azvP --backup --backup-dir=${BACKUPDIR} --exclude=.svn --log-file=${logfile} --exclude-from=$exclude_from --include-from=$include_from"  
  214.   
  215.     if [ -f ${LIBEXECDIR}/${domain}/${host}/before ];then  
  216.         /bin/sh ${LIBEXECDIR}/${domain}/${host}/before  
  217.     fi  
  218.   
  219.     find $SRCDIR/* -type f -name "Thumbs.db" -exec rm -rf {} \;  
  220.   
  221.     for addr in ${REMOTE}   
  222.     do  
  223.         case ${MODE} in  
  224.             FTP)  
  225.             ftpdeploy  
  226.             ;;  
  227.                     SCP)  
  228.                         scp -ar ${project}/* ${addr}:${DESTINATION}  
  229.                         ;;  
  230.                     SFTP)  
  231.                         sftpdeploy  
  232.                         ;;  
  233.                     RSYNC)  
  234.                         $RSYNC $RSYNC_OPTS $OPTION ${project}/* ${addr}::${DESTINATION}  
  235.                         ;;  
  236.                     "RSYNC+SSH")  
  237.                         $RSYNC $RSYNC_OPTS ${project}/* ${addr}:${DESTINATION}  
  238.                         ;;  
  239.                 esac  
  240.         if [ -d ${SHAREDIR}/current/${domain}/${host}/ ]; then  
  241.             cp -a ${SHAREDIR}/current/${domain}/${host}/* ${project}/  
  242.         fi  
  243.         if [ -z "${REVISION}" ]; then  
  244.             logging 'revert' "${project} => ${addr}:${DESTINATION}"  
  245.         else  
  246.             logging 'deploy' "${project} => ${addr}:${DESTINATION}"  
  247.         fi  
  248.     done  
  249.           
  250.         if [ -f ${LIBEXECDIR}/${domain}/${host}/after ];then  
  251.         #ssh ${scp} < ${LIBEXECDIR}/${domain}/${host}/after  
  252.         exit  
  253.     fi  
  254.       
  255. }  
  256.   
  257. function revert() {  
  258.   
  259.     local domain=$2  
  260.     local host=$3  
  261.     local revision=$4  
  262.       
  263.     if [ -z $domain ]; then  
  264.         usage  
  265.     fi  
  266.       
  267.     if [ -z $host ]; then  
  268.         usage  
  269.     fi  
  270.       
  271.     if [ -z $revision  ]; then  
  272.         usage  
  273.     else  
  274.     REVISION="-r ${revision}"  
  275.     fi  
  276.     #echo $domain , $host, $dir; exit  
  277.   
  278.       
  279.     deploy $@  
  280. }  
  281.   
  282. function import(){  
  283.     local domain=$2  
  284.     local host=$3  
  285.     local dir=$4  
  286.       
  287.     if [ -z $domain ]; then  
  288.         usage  
  289.     fi  
  290.       
  291.     if [ -z $host ]; then  
  292.         usage  
  293.     fi  
  294.       
  295.     if [ -z $dir ]; then  
  296.         usage  
  297.     fi  
  298.       
  299.     config ${domain}/${host}  
  300.   
  301.     $SVN import $dir ${REPOSITORY} >> $logfile  
  302. }  
  303. function export(){  
  304.   
  305.     local domain=$2  
  306.     local host=$3  
  307.     local dir=$4  
  308.       
  309.     if [ -z $domain ]; then  
  310.         usage  
  311.     fi  
  312.       
  313.     if [ -z $host ]; then  
  314.         usage  
  315.     fi  
  316.       
  317.     if [ -z $dir ]; then  
  318.         usage  
  319.     fi  
  320.       
  321.     config ${domain}/${host}  
  322.       
  323.     local logfile=${LOGDIR}/${host}.${domain}.log  
  324.       
  325.     $SVN export --force ${REPOSITORY} ${dir} >> $logfile  
  326.       
  327.     logging 'export' "${TRUNK} to ${dir}"  
  328. }  
  329.   
  330.   
  331. function timepoint {  
  332.     echo $TIMEPOINT >> timepoint.log  
  333. }  
  334.   
  335. function unstable {  
  336.     local edition=$(basename $unstable)  
  337.     svn export ${unstable} ${src}/$edition  
  338.   
  339.     for ignore in $( cat excluded.lst ); do  
  340.         rm -rf ${src}/$edition/$ignore  
  341.     done  
  342.   
  343.     $RSYNC ${src}/$edition ${destination}  
  344.   
  345.     ssh ${remote} < script/unstable  
  346. }  
  347.   
  348. function clean() {  
  349.     local domain=$2  
  350.         local host=$3  
  351.     if [ -z $domain ]; then  
  352.         usage  
  353.     fi  
  354.   
  355.     if [ -z $host ]; then  
  356.         usage  
  357.     fi  
  358.   
  359.     local project=$SRCDIR/${domain}/$host.${domain}  
  360.   
  361.     rm -rf ${project}  
  362. }  
  363.   
  364. function list {  
  365.     local domain=$2  
  366.     local host=$3  
  367.     local dir=$4  
  368.     if [ -z $domain ]; then  
  369.         ls $SRCDIR/*  
  370.     exit  
  371.     fi  
  372.   
  373.     if [ -z $host ]; then  
  374.         usage  
  375.     fi  
  376.   
  377.     config ${domain}/${host}  
  378.   
  379.     svn ls ${REPOSITORIES}/$dir #| awk -F '/' '{print $1}'  
  380.   
  381. }  
  382.   
  383. function backup() {  
  384.     local domain=$2  
  385.     local host=$3  
  386.     local dir=$4  
  387.   
  388.     if [ -z $domain ]; then  
  389.     usage  
  390.     fi  
  391.   
  392.     if [ -z $host ]; then  
  393.         usage  
  394.     fi  
  395.   
  396.     local logfile=${LOGDIR}/${host}.${domain}.log  
  397.   
  398.     config ${domain}/${host}  
  399.   
  400.     if [ -z $dir ]; then  
  401.         dir=$TMPDIR  
  402.     fi  
  403.   
  404.     for addr in ${REMOTE}  
  405.     do  
  406.     dir=$dir/${addr}  
  407.         if [ ! -d ${dir} ]; then  
  408.             mkdir -p $dir  
  409.         fi  
  410.   
  411.         $RSYNC -auzvP $OPTION ${addr}::${DESTINATION} $dir >> ${logfile}  
  412.   
  413.     logging 'backup' "rsync://${addr}::${DESTINATION} to ${dir}"  
  414.   
  415.         echo 'Backup Directory:' $dir  
  416.     exit  
  417.     done  
  418.   
  419. }  
  420. function cron(){  
  421.     local fun=$2      
  422.     case ${fun} in  
  423.         show)  
  424.             crontab -l  
  425.             ;;  
  426.         setup)  
  427.             cat $PREFIX/cron.d/crontab | crontab  
  428.             ;;  
  429.         edit)  
  430.             vim $PREFIX/cron.d/crontab  
  431.             cat $PREFIX/cron.d/crontab | crontab  
  432.             ;;  
  433.         *)  
  434.             usage  
  435.             ;;  
  436.     esac  
  437.   
  438. }  
  439.   
  440. function files {  
  441.     local project_dir=$1  
  442.     local project=$2  
  443.     if [ -z "$2"  ]; then  
  444.         echo "Please provide the project name for deploy!"  
  445.     else  
  446.         config ${project}  
  447.         exclude_from=exclude/${project}.lst  
  448.         include_from=include/${project}.lst  
  449.         $RSYNC --exclude-from=$exclude_from --include-from=$include_from ${project_dir} ${remote}  
  450.         ssh ${scp} < script/${project}   
  451.         timepoint  
  452.     fi  
  453. }  
  454.   
  455. function help {  
  456.     echo $"Usage: $0 {list|clean}"  
  457.     echo $"       $0 subversion <project>"  
  458. #    echo $"       $0 files <path> <project>"  
  459.     echo $"       $0 revert <project> <time>"  
  460. }  
  461. case "$1" in  
  462.     deploy)  
  463.         deploy $@  
  464.         ;;  
  465.     revert)  
  466.         revert $@  
  467.         ;;        
  468.     import)  
  469.         import $@  
  470.         ;;  
  471.     export)  
  472.         export $@  
  473.         ;;    
  474.     backup)  
  475.         backup $@  
  476.         ;;  
  477. #    switch)  
  478. #       switch $2  
  479. #        ;;  
  480.     cron)  
  481.         cron $@  
  482.         ;;        
  483. #    files)  
  484. #        files $2 $3  
  485. #        ;;  
  486.     clean)  
  487.         clean $@  
  488.         ;;  
  489.     list)  
  490.         list $@  
  491.         ;;  
  492.     log)  
  493.         ls -1 $LOGDIR/*  
  494.         ;;  
  495.     conf)  
  496.         conf $@  
  497.         ;;        
  498.     *)  
  499.         usage  
  500.         exit 1  
  501.           
  502. esac  
目录
相关文章
|
4月前
|
运维 监控 Devops
|
5月前
|
Devops 网络安全 Docker
百度搜索:蓝易云【DevOps系列文章之Docker部署web ssh工具sshwifty教程。】
同时,了解DevOps和Docker的基本原理和概念也对你进行部署和管理这样的工具非常有帮助。你可以进一步研究Docker容器化技术和相关的DevOps实践,以更好地理解和应用这些概念。
55 0
|
7月前
|
jenkins Devops 持续交付
【devops】五、Integrate工具——容器部署Jenkins(上)
【devops】五、Integrate工具——容器部署Jenkins(上)
102 1
|
7月前
|
Java Devops jenkins
【devops】三、Build阶段工具——配置maven
【devops】三、Build阶段工具——配置maven
|
7月前
|
Devops 开发工具 git
【devops】二、Code阶段工具——容器部署Gitlab
【devops】二、Code阶段工具——容器部署Gitlab
|
7月前
|
Kubernetes Devops Docker
【devops】十、Kubernetes编排工具(中)
【devops】十、Kubernetes编排工具(中)
147 1
|
7月前
|
存储 Kubernetes Devops
【devops】十、Kubernetes编排工具(上)
【devops】十、Kubernetes编排工具(上)
159 1
|
4天前
|
Devops jenkins 网络安全
【DevOps】(四)jekins服务器ssh部署
【DevOps】(四)jekins服务器ssh部署
12 1
|
16天前
|
jenkins Devops Java
【DevOps】(一)jekins部署
【DevOps】(一)jekins部署
14 1
|
2月前
|
监控 Devops 持续交付
程序员必须了解的 10个免费 Devops 工具
程序员必须了解的 10个免费 Devops 工具

热门文章

最新文章