salt实现自动化部署项目

1.在salt-master上设置配置文件

[root@zabbix project]# cat /etc/salt/master |grep -Ev '^#|^$'
interface: 0.0.0.0
file_roots:
  base:
    - /srv/salt/base
pillar_roots:
  base:
    - /srv/salt/pillar

[root@zabbix project]# pwd
/srv/salt/base/project

2.project目录是我们需要创建自动部署项目所需的文件目录,如下

[root@zabbix project]# ll
总用量 16676
drwxr-xr-x 3 root root     159 4月  24 17:11 file
-rw-r--r-- 1 root root      32 3月  21 2019 init.sls
-rw-r--r-- 1 root root    1906 5月  24 2019 project.sls
drwxr-xr-x 3 root root      34 4月  24 17:11 test
-rw-r--r-- 1 root root 8528017 3月  21 2019 tomcat-9.0.11.zip
-rw-r--r-- 1 root root 8528017 4月  24 17:04 tomcat-9.0.11.zip.20200424
-rw-r--r-- 1 root root     401 5月  24 2019 user.sls
  1. 我们都知道pillar设置是需要一个总入口文件,这个项目的总入口文件是:init.sls
[root@zabbix project]# cat init.sls
include:
  - .user
  - .project

既然有总入口这个init.sls文件,如上的配置文件可以看到还应该有user和project的子配置文件,具体内容如下:

[root@zabbix project]# cat user.sls
#项目用户
create_user:
  user.present:
    - name: {{ pillar['user'] }}
    - home: /home/{{ pillar['user'] }}
    - hash_password: True
    - password: "123456"
#环境变量
bashrc:
  file.managed:
    - name: /home/{{ pillar['user'] }}/.bashrc
    - source: salt://project/file/bashrc  
    - template: jinja
    - user: {{ pillar['user'] }}
    - group: {{ pillar['user'] }}
    - mode: 644




[root@zabbix project]# cat project.sls
#项目配置

{% if pillar['type'] == 'tomcat'  %}
#安装tomcat
  install_timcat:
    archive.extracted:
      - name: /home/{{ pillar['user'] }}/
      - source: salt://project/file/tomcat-9.0.11.zip
      - use_cmd_unzip: True
#配置tomcat
  config_tomcat:
    file.managed:
      - name: /home/{{ pillar['user'] }}/apache-tomcat-9.0.11/conf/server.xml
      - source: salt://project/file/server.xml
      - template: jinja
#软链接
  symlink_tomcat:
    file.symlink:
      - name: /home/{{ pillar['user'] }}/tomcat
      - target: /home/{{ pillar['user'] }}/apache-tomcat-9.0.11
      - user: {{ pillar['user'] }}
      - group: {{ pillar['user'] }}
#创建webapps
  create_webapps:
      file.directory:
        - name: /home/{{ pillar['user'] }}/webapps
        - user: {{ pillar['user'] }}
        - group:  {{ pillar['user'] }}
        - mode: 755
        - makedirs: True
#启动脚本
  start_shell:
    file.managed:
      - name: /home/{{ pillar['user'] }}/publish.sh
      - source: salt://project/file/publish.sh
      - template: jinja
      - user: {{ pillar['user'] }}
      - group: {{ pillar['user'] }}
      - mode: 755

{% elif pillar['type'] == 'spring' %}

#启动脚本
  spring_shell:
    file.managed:
      - name: /home/{{ pillar['user'] }}/publish.sh
      - source: salt://project/file/start.sh
      - template: jinja
      - user: {{ pillar['user'] }}
      - group: {{ pillar['user'] }}
      - mode: 755
#日志目录
  log_dir:
    file.directory:
      - name: /home/{{ pillar['user'] }}/logs
      - user: {{ pillar['user'] }}
      - group: {{ pillar['user'] }}
      - mode: 755
      - makedirs: True

{% endif %}
#权限属组
  chown_and_chmod:
    file.directory:
      - name: /home/{{ pillar['user'] }}
      - user: {{ pillar['user'] }}
      - group: {{ pillar['user'] }}
      - mode: 755
      - recurse:
        - user
        - group
     #   - mode

  1. 上面的子配中设计的文件(bashrc、server.xml、publish.sh、start.sh)配置如下
[root@zabbix file]# cat bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

export JAVA_HOME=/usr/java/jdk1.8.0_181
export MVN_HOME=/opt/apache-maven-3.5.2
export PATH=$JAVA_HOME/bin:$PATH:$MVN_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tool.jar
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=/opt/mongodb/mongodb-linux-x86_64-3.2.3/bin:$PATH

TZ='Asia/Shanghai'

[root@zabbix file]# cat server.xml
<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="-1" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->

  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->

    <Connector port="{{ pillar['port'] }}" address="0.0.0.0" protocol="HTTP/1.1" URIEncoding="UTF-8" relaxedQueryChars="[,]"/>
    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the BIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->

    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->
        <Context path="" docBase="/home/{{ pillar['user'] }}/webapps/{{ pillar['user'] }}"></Context>
        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>
[root@zabbix file]# cat publish.sh
#!/bin/bash
NAME={{ pillar['user'] }}
WAR_NAME={{ pillar['pkg'] }}
rm -rf /home/$NAME/webapps/$NAME/*
unzip -d /home/$NAME/webapps/$NAME /home/$NAME/$WAR_NAME
ps -ef | grep  $NAME  |grep java | grep -v grep | awk '{print $2}' |xargs kill -9
echo "/home/$NAME/tomcat/bin/startup.sh"
sleep 3s
/home/$NAME/tomcat/bin/startup.sh

[root@zabbix file]# cat start.sh
#!/bin/bash
PROJECT_HOME=/home/{{ pillar['user'] }}
PORT={{ pillar['port'] }}
JAR_NAME={{ pillar['pkg'] }}
ps -ef|grep $JAR_NAME|grep java|grep -v grep|awk '{print $2}'|xargs kill -9
nohup java -Dserver.port=$PORT -Xms256m -Xmx256m -verbose:gc -XX:+PrintGCDetails -XX:NativeMemoryTracking=detail -XX:+PrintGCTimeStamps -XX:+UseParallelGC -XX:+AggressiveOpts -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+AlwaysPreTouch -Dlog4j.shutdownHookEnabled=false -XX:+DisableExplicitGC -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=256m -Dfile.encoding=UTF-8 -jar $PROJECT_HOME/$JAR_NAME > nohup.out 2>&1 &

  1. 自动创建项目命令如下:

v1代表是服务器,pillar后面的参数是属于key-value形式的参数,user代表项目启动用户,type指定是激spring项目还是tomcat项目,port代表端口,pkg代表包名

salt 'v1' state.sls project pillar='{"user": "elastic-job", "type":"spring","port":8086,"pkg":"elastic-job.jar"}'

salt 'v5' state.sls project pillar='{"user": "gateway-kunlai", "type":"tomcat","port":8065,"pkg":"gateway-kunlai.war"}
  1. 整体目录结构如下:
[root@zabbix base]# tree project/
project/
├── file
│   ├── apache-tomcat-9.0.11
│   │   ├── bin
│   │   │   ├── bootstrap.jar
│   │   │   ├── catalina.sh
│   │   │   ├── catalina-tasks.xml
│   │   │   ├── ciphers.sh
│   │   │   ├── commons-daemon.jar
│   │   │   ├── commons-daemon-native.tar.gz
│   │   │   ├── configtest.sh
│   │   │   ├── daemon.sh
│   │   │   ├── digest.sh
│   │   │   ├── makebase.sh
│   │   │   ├── setclasspath.sh
│   │   │   ├── shutdown.sh
│   │   │   ├── startup.sh
│   │   │   ├── tomcat-juli.jar
│   │   │   ├── tomcat-native.tar.gz
│   │   │   ├── tool-wrapper.sh
│   │   │   └── version.sh
│   │   ├── BUILDING.txt
│   │   ├── conf
│   │   │   ├── Catalina
│   │   │   │   └── localhost
│   │   │   ├── catalina.policy
│   │   │   ├── catalina.properties
│   │   │   ├── context.xml
│   │   │   ├── jaspic-providers.xml
│   │   │   ├── jaspic-providers.xsd
│   │   │   ├── logging.properties
│   │   │   ├── server.xml
│   │   │   ├── tomcat-users.xml
│   │   │   ├── tomcat-users.xsd
│   │   │   └── web.xml
│   │   ├── CONTRIBUTING.md
│   │   ├── lib
│   │   │   ├── annotations-api.jar
│   │   │   ├── catalina-ant.jar
│   │   │   ├── catalina-ha.jar
│   │   │   ├── catalina.jar
│   │   │   ├── catalina-storeconfig.jar
│   │   │   ├── catalina-tribes.jar
│   │   │   ├── ecj-4.7.3a.jar
│   │   │   ├── el-api.jar
│   │   │   ├── jasper-el.jar
│   │   │   ├── jasper.jar
│   │   │   ├── jaspic-api.jar
│   │   │   ├── jsp-api.jar
│   │   │   ├── servlet-api.jar
│   │   │   ├── tomcat-api.jar
│   │   │   ├── tomcat-coyote.jar
│   │   │   ├── tomcat-dbcp.jar
│   │   │   ├── tomcat-i18n-es.jar
│   │   │   ├── tomcat-i18n-fr.jar
│   │   │   ├── tomcat-i18n-ja.jar
│   │   │   ├── tomcat-i18n-ru.jar
│   │   │   ├── tomcat-jdbc.jar
│   │   │   ├── tomcat-jni.jar
│   │   │   ├── tomcat-util.jar
│   │   │   ├── tomcat-util-scan.jar
│   │   │   ├── tomcat-websocket.jar
│   │   │   └── websocket-api.jar
│   │   ├── LICENSE
│   │   ├── logs
│   │   ├── NOTICE
│   │   ├── README.md
│   │   ├── RELEASE-NOTES
│   │   ├── RUNNING.txt
│   │   ├── temp
│   │   ├── webapps
│   │   └── work
│   ├── bashrc
│   ├── publish.sh
│   ├── server.xml
│   ├── start.sh
│   ├── tomcat-9.0.11.zip
│   └── tomcat-9.0.11.zip.20200424
├── init.sls
├── project.sls
├── test
│   └── apache-tomcat-9.0.11
│       ├── bin
│       │   ├── bootstrap.jar
│       │   ├── catalina.sh
│       │   ├── catalina-tasks.xml
│       │   ├── ciphers.sh
│       │   ├── commons-daemon.jar
│       │   ├── commons-daemon-native.tar.gz
│       │   ├── configtest.sh
│       │   ├── daemon.sh
│       │   ├── digest.sh
│       │   ├── makebase.sh
│       │   ├── setclasspath.sh
│       │   ├── shutdown.sh
│       │   ├── startup.sh
│       │   ├── tomcat-juli.jar
│       │   ├── tomcat-native.tar.gz
│       │   ├── tool-wrapper.sh
│       │   └── version.sh
│       ├── BUILDING.txt
│       ├── conf
│       │   ├── Catalina
│       │   │   └── localhost
│       │   ├── catalina.policy
│       │   ├── catalina.properties
│       │   ├── context.xml
│       │   ├── jaspic-providers.xml
│       │   ├── jaspic-providers.xsd
│       │   ├── logging.properties
│       │   ├── server.xml
│       │   ├── tomcat-users.xml
│       │   ├── tomcat-users.xsd
│       │   └── web.xml
│       ├── CONTRIBUTING.md
│       ├── lib
│       │   ├── annotations-api.jar
│       │   ├── catalina-ant.jar
│       │   ├── catalina-ha.jar
│       │   ├── catalina.jar
│       │   ├── catalina-storeconfig.jar
│       │   ├── catalina-tribes.jar
│       │   ├── ecj-4.7.3a.jar
│       │   ├── el-api.jar
│       │   ├── jasper-el.jar
│       │   ├── jasper.jar
│       │   ├── jaspic-api.jar
│       │   ├── jsp-api.jar
│       │   ├── servlet-api.jar
│       │   ├── tomcat-api.jar
│       │   ├── tomcat-coyote.jar
│       │   ├── tomcat-dbcp.jar
│       │   ├── tomcat-i18n-es.jar
│       │   ├── tomcat-i18n-fr.jar
│       │   ├── tomcat-i18n-ja.jar
│       │   ├── tomcat-i18n-ru.jar
│       │   ├── tomcat-jdbc.jar
│       │   ├── tomcat-jni.jar
│       │   ├── tomcat-util.jar
│       │   ├── tomcat-util-scan.jar
│       │   ├── tomcat-websocket.jar
│       │   └── websocket-api.jar
│       ├── LICENSE
│       ├── logs
│       ├── NOTICE
│       ├── README.md
│       ├── RELEASE-NOTES
│       ├── RUNNING.txt
│       ├── temp
│       ├── webapps
│       └── work
├── tomcat-9.0.11.zip
├── tomcat-9.0.11.zip.20200424
└── user.sls

22 directories, 131 files
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章