首页 > 代码库 > saltstack实战实例

saltstack实战实例

1.批量将命令alias

alias_ps_java_mappice:

  cmd.run:

    - name: echo -e ‘\nalias hpsm="ps -ef | grep MappIce | egrep -v grep"\nalias hpsj="ps -ef | grep java | egrep -v grep"‘ >> /etc/profile 

2.file.append,增加删除日志的命令

/home/shell/cron_clear_file.sh:

  file.append:

    - text: 

      - find /home -path ‘/home/nfsdata/*‘ -prune -o -type d \( -name "logs" -o -name "mylogs" \) -exec find {} -type f -mtime +1 -name "*log*" ! -name "*.gz" \;| xargs gzip

      - find /home -path ‘/home/nfsdata/*‘ -prune -o -type d \( -name "logs" -o -name "mylogs" \) -exec find {} -type f -mtime +180 -name "*.gz" \;| xargs rm -f

      - find /var/log/ -type f -name "*log.*" -mtime +1 ! -name "*.gz" | xargs gzip

      - find /var/log/ -type f -name "*.gz" -mtime +180 | xargs rm -f

      - cd /var/log/nginx/ && for i in `ls -l /var/log/nginx/*.log | grep -v total | awk ‘{print $NF}‘ | awk -F‘/‘ ‘{print $NF}‘`;do /bin/tar czvf ${i}.$(date +%F).tar.gz $i ; echo "" > ${i} ;done

      - find /var/log/nginx -type f -name "*.gz" -mtime +90 | xargs rm -f

3.更新jar包、目录、配置文件,重启java进程

/home/www/pl-service/lib:

 file.recurse:

    -source: salt://shangxian/lib.liyan

    -include_empty: True

 

/home/www/pl-service/conf/hessian.properties:

 file.managed:

    -source: salt://shangxian/hessian.properties.liyan

    -user: nginx

    -group: nginx

    -mode: 644

/home/www/pl-service/lib/pl-service-0.0.2-release.jar:

 file.managed:

    -source: salt://shangxian/pl-service-0.0.2-release.jar

    -user: nginx

    -group: nginx

    -mode: 644

start-pl-service:

 cmd.run:

    -name: cp /home/www/pl-service/lib/pl-service-0.0.2-release.jar/home/nginx/backfile/pl-service-0.0.2-release.jar.$(date +%F%T) && su -nginx -c "kill -9 `ps -ef | grep pl-service | grep -v grep | awk ‘{print$2}‘` && sleep 7 && sh /home/www/pl-service/bin/start.sh&& ps -ef | grep pl-service | grep -v grep"

    -require:

     - file: /home/www/pl-service/lib/pl-service-0.0.2-release.jar

4.部署java、tomcat、maven

web-dir:

 cmd.run:

    -name: mkdir -p /home/web/

    -unless: test -d /home/web/

 

 

/usr/local/apache-maven-3.0.5.tar.gz:

 file.managed:

  -source: salt://jdk_mav_tom/apache-maven-3.0.5.tar.gz

  -user: root

  -group: root

  -mode: 644

 

/home/web/apache-tomcat-7.0.64.tar.gz:

 file.managed:

  -source: salt://jdk_mav_tom/apache-tomcat-7.0.64.tar.gz

  -user: root

  -group: root

  -mode: 644

  -require:

    -cmd: web-dir

 

 

/usr/local/java.tar.gz:

 file.managed:

  -source: salt://jdk_mav_tom/java.tar.gz

  -user: root

  -group: root

  -mode: 644

 

 

tar-xf:

 cmd.run:

    -name: tar xf /usr/local/apache-maven-3.0.5.tar.gz -C /usr/local/ && tarxf /usr/local/java.tar.gz -C /usr/local/ && tar xf/home/web/apache-tomcat-7.0.64.tar.gz -C /home/web/ && chown -Rnginx.nginx /usr/local/apache-maven-3.0.5 && chown -R nginx.nginx/usr/local/java && chown -R nginx.nginx /home/web/

    -require:

     - file: /usr/local/apache-maven-3.0.5.tar.gz

     - file: /home/web/apache-tomcat-7.0.64.tar.gz

     - file: /usr/local/java.tar.gz

 

 

avg-profile:

 cmd.run:

    -name: echo -e ‘export JAVA_HOME=/usr/local/java\nexportPATH=$JAVA_HOME/bin:$PATH\nexportCLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar\nexportPATH=/usr/local/apache-maven-3.0.5/bin:$PATH\n‘ >> /etc/profile

    -unless: egrep ‘JAVA_HOME|maven‘ /etc/profile

5.部署删除日志通用脚本

###创建文件夹

/home/shell:

 cmd.run:

    -name: mkdir -p /home/shell

    -unless: test -d /home/shell

- order: 1

###将脚本同步到minion

/home/shell/cron_file_union.sh:

 file.managed:

    -source: salt://shangxian/cron_file_union.sh

    -user: root

    -group: root

    -mode: 644

    -require:

     - cmd: /home/shell

###写定时任务

/bin/sh /home/shell/cron_file_union.sh>/dev/null 2>&1:

 cron.present:

    -user: root

    -minute: 00

- hour: 02

 

cron_file_union.sh的内容见下:

#!/bin/bash

 

###delete /home/..logs or /home/..mylogs delog

find /home -path ‘/home/nfsdata/*‘ -prune-o -type d \( -name "logs" -o -name "mylogs" \) -exec find{} -type f -mtime +1 -name "*log.*" ! -name "*.gz" \;|xargs gzip

find /home -path ‘/home/nfsdata/*‘ -prune-o -type d \( -name "logs" -o -name "mylogs" \) -exec find{} -type f -mtime +180 -name "*.gz" \;| xargs rm -f

 

 

###delete /var/log de log   ,eg:/var/log/nginx/gogo.log.fso

find /var/log/ -type f -name"*log.*" -mtime +1 ! -name "*.gz" | xargs gzip

find /var/log/ -type f -name "*.gz"-mtime +180 | xargs rm -f

 

###delete /home/../logs/stdout.log or/home/.../logs/catalina.out

for i in `find /home -path‘/home/nfsdata/*‘ -prune -o -type d \( -name "logs" -o -name"mylogs" \) -exec find {} -type f \( -name "*.log" -o -name"catalina.out" \) -size +100M \; | xargs ls`

do

       /bin/tar czf ${i}.$(date +%F).ThisIsATag.tar.gz ${i}

       echo "" > ${i}

done

find /home -path ‘/home/nfsdata/*‘ -prune-o -type d \( -name "logs" -o -name "mylogs" \) -exec find{} -type f -mtime +7 -name "*.ThisIsATag.tar.gz" \;| xargs rm -f

 

### delete ,like /var/log/nginx/www.log

cd /var/log/nginx/ && for i in `ls-l /var/log/nginx/*.log | grep -v total | awk ‘{print $NF}‘ | awk -F‘/‘ ‘{print$NF}‘`;do /bin/tar czf ${i}.$(date +%F).tar.gz $i ; echo "" > ${i};done

find /var/log/nginx -type f -name"*.gz" -mtime +180 | xargs rm -f

6.清除听云日志脚本

/home/shell:

 file.directory:

    -user: nginx

    -group: nginx

    -mode: 755

    -makedirs: True

tingyun_crontab:

 file.managed:

    -name: /home/shell/cron_clear_tingyun_log.sh

    -source: salt://tingyun/cron_clear_tingyun_log.sh

 

 

/bin/sh/home/shell/cron_clear_tingyun_log.sh >/dev/null 2>&1:

 cron.present:

    -user: root

    -minute: 00

- hour: 02

 

cron_clear_tingyun_log.sh内容:

#!/bin/bash

cd /home && find `ls /home | grep-v ‘.*nfs.*‘` -type d -name ‘axis2-tmp-*.tmp‘ | xargs rm -rf


本文出自 “10536390” 博客,请务必保留此出处http://10546390.blog.51cto.com/10536390/1870180

saltstack实战实例