首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

DevOps 工具-代码调度脚本

2012-06-26 
DevOps 工具-代码部署脚本#!/bin/bash################################################################

DevOps 工具-代码部署脚本

#!/bin/bash###################################################################### Description: Automation Deployment Script # Netkiller series utilities# Author: Neo<openunix@163.com># Homepage: http://netkiller.sourceforge.net/# $Id$#####################################################################if [ -z $DEPLOY_HOME ]; thenecho 'Example: export DEPLOY_HOME=/srv/deploy'exitfiif [ -f $DEPLOY_HOME/conf/default.conf ];then . $DEPLOY_HOME/conf/default.conffi#================================================================================LOGFILE="deploy.$(date -d "today" +"%Y-%m-%d").log"TMPDIR=$(mktemp -d --suffix=.tmp -p /tmp deploy.XXXXXX)#SUBVERSION=SVN=/usr/bin/svnTIMEPOINT=`date '+%Y%m%d-%H%M%S'`BACKUPDIR=$TMPDIR/backup/${TIMEPOINT}RSYNC="rsync"UPLOAD_DIR=$TMPDIRREVISION=''#================================================================================if [ ! -d ${TMPDIR} ]; then mkdir ${TMPDIR}fiif [ ! -d ${BACKUPDIR} ]; then mkdir -p $BACKUPDIRfi#pkgname=${project}-${version}-${datetime}.pkg#tar jcvf ${pkgname} /tmp/${project} --remove-files >> deploy.log#####################################################################function usage(){echo $"Usage: $0 [OPTION] <server-id> <directory/timepoint>"echo -ne "OPTION: deploy <domain> <host> revert <domain> <host> <revision> backup <domain> <host> <directory> list list <domain> <host> import <domain> <host> <dir> export <domain> <host> <dir>clean <domain> <host> log <project> <line> conf list conf new <project> conf remove <project> conf show <project> conf edit <project> cron showcron setupcron edit" exit}function logging(){ local logfile="$LOGDIR/$LOGFILE" local timepoint=$(date -d "today" +"%Y-%m-%d_%H:%M:%S") local status=$1 local message=$2 echo "[$timepoint] [${status}] ${message}" >> $logfile}#logging 'OK' 'This is test msg!!!'function conf(){ local cmd=$2 local prj=$3 case $cmd in list) ls $SYSCONFDIR ;; new) read -p "Project directory: " prjdir if [ -z $prjdir ]; then exit fi if [ -f $SYSCONFDIR/$prjdir.conf ]; then echo "cannot create config $prjdir.conf': File exists" exit 1 fi read -p "subversion url: $REPOSITORIES/: " svnurl if [ -z $svnurl ]; then svnurl=$REPOSITORIES fi read -p "hostname: " host if [ -z $host ]; then host="localhost" echo "default hostname 'localhost'" fi read -p "upload mode ftp/scp/sftp/rsync: " mode if [ -z $mode ]; then mode=ftp else case $mode in ftp) mode="ftpdeploy" ;; scp) mode="scpdeploy" ;; sftp) mode="sftpdeploy" ;; rsync) mode="rsync" ;; esac fi read -p "Create $prjdir config? [y/n]" -n 1 key echo if [ $key = 'y' ]; then echo -ne "REPOSITORIES=$REPOSITORIES/$svnurlCOMMAND=$modeHOSTNAME=$host " >> $SYSCONFDIR/$prjdir.conf fi ;; remove) if [ -f $SYSCONFDIR/$prj ]; then rm -rf $SYSCONFDIR/$prj fi ;; show) cat $SYSCONFDIR/$prj ;; edit) vim $SYSCONFDIR/$prj ;; *) usage ;; esac}#####################################################################function config { local cfg=$1 exclude_from=$PREFIX/exclude/${cfg}.lst include_from=$PREFIX/include/${cfg}.lst if [ -f $SYSCONFDIR/${cfg}.conf ];then . $SYSCONFDIR/${cfg}.conf else echo "Please provide the config($SYSCONFDIR/${cfg}.conf) to deploy!" exit fi if [ -z "$cfg" ]; then echo "Please provide the path for deploy!"exit fi if [ ! -f $exclude_from ]; then echo "Please provide a list of excluded in the $exclude_from." exit fi if [ ! -f $include_from ]; then echo "Please provide a list of included in the $include_from." exit fi}function deploy() { local domain=$2local host=$3local logfile=${LOGDIR}/${host}.${domain}.log if [ -z $domain ]; then usage fi config ${domain}/${host}project=$SRCDIR/${domain}/$host.${domain} SVN_OPTS=${REVISION}if [ -d ${project} ]; then $SVN update ${SVN_OPTS} $project >> $logfilelogging 'update' ${project}else mkdir -p ${project}$SVN checkout ${REPOSITORY} ${project} >> $logfilelogging 'checkout' ${project} fi# $BINDIR/$COMMAND deploy $HOSTNAME $UPLOAD_DIR/$ver/RSYNC_OPTS=" -azvP --backup --backup-dir=${BACKUPDIR} --exclude=.svn --log-file=${logfile} --exclude-from=$exclude_from --include-from=$include_from"if [ -f ${LIBEXECDIR}/${domain}/${host}/before ];then/bin/sh ${LIBEXECDIR}/${domain}/${host}/beforefi find $SRCDIR/* -type f -name "Thumbs.db" -exec rm -rf {} \;for addr in ${REMOTE} docase ${MODE} in FTP)ftpdeploy;; SCP) scp -ar ${project}/* ${addr}:${DESTINATION} ;; SFTP) sftpdeploy ;; RSYNC) $RSYNC $RSYNC_OPTS $OPTION ${project}/* ${addr}::${DESTINATION} ;; "RSYNC+SSH") $RSYNC $RSYNC_OPTS ${project}/* ${addr}:${DESTINATION} ;; esacif [ -d ${SHAREDIR}/current/${domain}/${host}/ ]; thencp -a ${SHAREDIR}/current/${domain}/${host}/* ${project}/fiif [ -z "${REVISION}" ]; thenlogging 'revert' "${project} => ${addr}:${DESTINATION}"elselogging 'deploy' "${project} => ${addr}:${DESTINATION}"fidone if [ -f ${LIBEXECDIR}/${domain}/${host}/after ];then#ssh ${scp} < ${LIBEXECDIR}/${domain}/${host}/afterexitfi}function revert() {local domain=$2local host=$3 local revision=$4 if [ -z $domain ]; then usage fi if [ -z $host ]; then usage fi if [ -z $revision ]; then usage elseREVISION="-r ${revision}" fi#echo $domain , $host, $dir; exitdeploy $@}function import(){ local domain=$2local host=$3 local dir=$4 if [ -z $domain ]; then usage fi if [ -z $host ]; then usage fi if [ -z $dir ]; then usage ficonfig ${domain}/${host} $SVN import $dir ${REPOSITORY} >> $logfile}function export(){ local domain=$2local host=$3 local dir=$4 if [ -z $domain ]; then usage fi if [ -z $host ]; then usage fi if [ -z $dir ]; then usage ficonfig ${domain}/${host}local logfile=${LOGDIR}/${host}.${domain}.log $SVN export --force ${REPOSITORY} ${dir} >> $logfilelogging 'export' "${TRUNK} to ${dir}"}function timepoint { echo $TIMEPOINT >> timepoint.log}function unstable { local edition=$(basename $unstable) svn export ${unstable} ${src}/$edition for ignore in $( cat excluded.lst ); do rm -rf ${src}/$edition/$ignore done $RSYNC ${src}/$edition ${destination} ssh ${remote} < script/unstable}function clean() { local domain=$2 local host=$3 if [ -z $domain ]; then usage fi if [ -z $host ]; then usage fi local project=$SRCDIR/${domain}/$host.${domain} rm -rf ${project}}function list { local domain=$2 local host=$3 local dir=$4 if [ -z $domain ]; then ls $SRCDIR/*exit fi if [ -z $host ]; then usage fi config ${domain}/${host} svn ls ${REPOSITORIES}/$dir #| awk -F '/' '{print $1}'}function backup() { local domain=$2 local host=$3 local dir=$4 if [ -z $domain ]; thenusage fi if [ -z $host ]; then usage fi local logfile=${LOGDIR}/${host}.${domain}.log config ${domain}/${host} if [ -z $dir ]; then dir=$TMPDIR fi for addr in ${REMOTE} dodir=$dir/${addr} if [ ! -d ${dir} ]; then mkdir -p $dir fi $RSYNC -auzvP $OPTION ${addr}::${DESTINATION} $dir >> ${logfile}logging 'backup' "rsync://${addr}::${DESTINATION} to ${dir}" echo 'Backup Directory:' $direxit done}function cron(){ local fun=$2 case ${fun} in show) crontab -l ;; setup) cat $PREFIX/cron.d/crontab | crontab ;; edit) vim $PREFIX/cron.d/crontab cat $PREFIX/cron.d/crontab | crontab ;; *) usage ;; esac}function files { local project_dir=$1 local project=$2 if [ -z "$2" ]; then echo "Please provide the project name for deploy!" else config ${project} exclude_from=exclude/${project}.lst include_from=include/${project}.lst $RSYNC --exclude-from=$exclude_from --include-from=$include_from ${project_dir} ${remote} ssh ${scp} < script/${project} timepoint fi}function help { echo $"Usage: $0 {list|clean}" echo $" $0 subversion <project>"# echo $" $0 files <path> <project>" echo $" $0 revert <project> <time>"}case "$1" in deploy) deploy $@ ;; revert) revert $@ ;; import) import $@ ;; export) export $@ ;; backup) backup $@ ;;# switch)# switch $2# ;; cron) cron $@ ;;# files)# files $2 $3# ;; clean) clean $@ ;; list) list $@ ;; log) ls -1 $LOGDIR/* ;; conf) conf $@ ;; *) usage exit 1 esac?

?

?

?

热点排行