├── README.md ├── hosts ├── roles ├── deploy-war │ └── tasks │ │ └── main.yml ├── jdk │ └── tasks │ │ └── main.yml ├── supervisor │ ├── handlers │ │ └── main.yml │ ├── tasks │ │ └── main.yml │ └── templates │ │ ├── supervisor-redhat.sh │ │ ├── supervisor-ubuntu.sh │ │ └── supervisord.conf └── tomcat │ ├── tasks │ └── main.yml │ └── templates │ ├── server.xml │ ├── setenv.sh │ ├── tomcat-control.sh │ ├── tomcat-supervisor.ini │ └── tomcat-users.xml └── site.yml /README.md: -------------------------------------------------------------------------------- 1 | # jenkins-ansible-supervisor-deploy 2 | Use jenkins, ansible, supervisor to deploy java application. 3 | 4 | http://hengyunabc.github.io/deploy-system-build-with-jenkins-ansible-supervisor/ 5 | 6 | This playbook do these things: 7 | - install python-pip, supervisor 8 | - install jdk 9 | - download tomcat and config 10 | - tomcat control by supervisor 11 | - deploy war to remote host 12 | - wait for tomcat started 13 | 14 | ## Docker image showcase 15 | 16 | ```bash 17 | docker run -it -p 8080:8080 -p 8101:8101 -p 9001:9001 --name='jenkins' hengyunabc/jenkins-ansible-supervisor 18 | ``` 19 | 20 | - jenkins: 21 | [http://localhost:8080/](http://localhost:8080/) 22 | - spring-mvc-showcase: 23 | [http://localhost:8101/](http://localhost:8101/) 24 | - supervisor: 25 | [http://localhost:9001/](http://localhost:9001/) admin/admin 26 | -------------------------------------------------------------------------------- /hosts: -------------------------------------------------------------------------------- 1 | 2 | [localhost] 3 | 127.0.0.1 ansible_ssh_user=root ansible_ssh_pass=12345 4 | -------------------------------------------------------------------------------- /roles/deploy-war/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Stop Tomcat 3 | supervisorctl: name=tomcat-{{app}} state=stopped 4 | 5 | - name: Delete app ROOT, ROOT.war 6 | shell: rm -rf {{deploy_path}}/tomcat-{{app}}/webapps/ROOT {{deploy_path}}/tomcat-{{app}}/webapps/ROOT.war 7 | sudo: True 8 | 9 | - name: copy app war to tomcat/webapps 10 | copy: src={{ item }} dest={{deploy_path}}/tomcat-{{app}}/webapps/ROOT.war owner={{tomcat_user}} 11 | with_fileglob: 12 | - "{{deploy_war_path}}/*.war" 13 | 14 | - name: Change ownership of Tomcat webapps/ROOT.war 15 | file: path={{deploy_path}}/tomcat-{{app}}/webapps/ROOT.war owner={{tomcat_user}} group={{tomcat_group}} 16 | 17 | - name: supervisor add tomcat service 18 | supervisorctl: name=tomcat-{{app}} state=present 19 | 20 | - name: supervisor start tomcat service 21 | supervisorctl: name=tomcat-{{app}} state=started 22 | 23 | - name: wait for tomcat to start 24 | wait_for: port={{http_port}} timeout=15 25 | -------------------------------------------------------------------------------- /roles/jdk/tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: Install jdk (Debian based) 2 | apt: name={{ jdk_version }} state=present 3 | when: ansible_pkg_mgr == "apt" 4 | sudo: yes 5 | 6 | - name: Install jdk (RedHat based) 7 | yum: name={{ jdk_version }} state=present 8 | when: ansible_pkg_mgr == "yum" 9 | sudo: yes 10 | -------------------------------------------------------------------------------- /roles/supervisor/handlers/main.yml: -------------------------------------------------------------------------------- 1 | - name: restart supervisor 2 | service: name={{ supervisor_service_name }} state=restarted 3 | -------------------------------------------------------------------------------- /roles/supervisor/tasks/main.yml: -------------------------------------------------------------------------------- 1 | 2 | --- 3 | #- name: Display all variables/facts known for a host 4 | # debug: var=hostvars[inventory_hostname] 5 | 6 | - name: Install python-pip (Debian based) 7 | apt: name={{ item }} state=present 8 | with_items: 9 | # - build-essential 10 | # - python-dev 11 | - python-pip 12 | when: ansible_pkg_mgr == "apt" 13 | sudo: yes 14 | 15 | - name: Update ca-certificates (RedHat based) 16 | yum: name=ca-certificates disablerepo=epel 17 | when: ansible_pkg_mgr == "yum" 18 | sudo: yes 19 | 20 | - name: Install python-pip (RedHat based) 21 | yum: name={{ item }} state=present 22 | with_items: 23 | - epel-release 24 | - python-pip 25 | when: ansible_pkg_mgr == "yum" 26 | sudo: yes 27 | 28 | - name: pip install supervisor 29 | command: pip install supervisor 30 | 31 | - name: add supervisor service 32 | template: src=supervisor-ubuntu.sh dest=/etc/init.d/{{ supervisor_service_name }} mode=0755 33 | sudo: yes 34 | when: ansible_os_family == "Debian" 35 | 36 | - name: add supervisor service 37 | template: src=supervisor-redhat.sh dest=/etc/init.d/{{ supervisor_service_name }} mode=0755 38 | sudo: yes 39 | when: ansible_os_family == "RedHat" 40 | 41 | - name: create supervisor conf directory 42 | file: path={{supervisor_conf_root}} state=directory 43 | 44 | - name: create supervisor conf directory 45 | file: path={{supervisor_conf_root}}/conf.d state=directory 46 | 47 | - name: add supervisor conf file 48 | template: src=supervisord.conf dest={{ supervisor_conf_root }} 49 | notify: restart supervisor 50 | sudo: yes 51 | 52 | - name: enable supervisor service, 53 | service: name="{{ supervisor_service_name }}" enabled=yes 54 | 55 | - name: start service supervisor, if not running 56 | service: name="{{ supervisor_service_name }}" state=started 57 | 58 | -------------------------------------------------------------------------------- /roles/supervisor/templates/supervisor-redhat.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # supervisord This scripts turns supervisord on 4 | # 5 | # Author: Mike McGrath (based off yumupdatesd) 6 | # 7 | # chkconfig: - 95 04 8 | # 9 | # description: supervisor is a process control utility. It has a web based 10 | # xmlrpc interface as well as a few other nifty features. 11 | # processname: supervisord 12 | # config: {{ supervisor_conf_root }}/supervisord.conf 13 | # pidfile: /var/run/supervisord.pid 14 | # 15 | 16 | # source function library 17 | . /etc/rc.d/init.d/functions 18 | 19 | RETVAL=0 20 | 21 | start() { 22 | echo -n $"Starting supervisord: " 23 | daemon "supervisord -c {{ supervisor_conf_root }}/supervisord.conf " 24 | RETVAL=$? 25 | echo 26 | [ $RETVAL -eq 0 ] && touch /var/lock/subsys/supervisord 27 | } 28 | 29 | stop() { 30 | echo -n $"Stopping supervisord: " 31 | killproc supervisord 32 | echo 33 | [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/supervisord 34 | } 35 | 36 | restart() { 37 | stop 38 | start 39 | } 40 | 41 | case "$1" in 42 | start) 43 | start 44 | ;; 45 | stop) 46 | stop 47 | ;; 48 | restart|force-reload|reload) 49 | restart 50 | ;; 51 | condrestart) 52 | [ -f /var/lock/subsys/supervisord ] && restart 53 | ;; 54 | status) 55 | status supervisord 56 | RETVAL=$? 57 | ;; 58 | *) 59 | echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}" 60 | exit 1 61 | esac 62 | 63 | exit $RETVAL 64 | -------------------------------------------------------------------------------- /roles/supervisor/templates/supervisor-ubuntu.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # 3 | # skeleton example file to build /etc/init.d/ scripts. 4 | # This file should be used to construct scripts for /etc/init.d. 5 | # 6 | # Written by Miquel van Smoorenburg . 7 | # Modified for Debian 8 | # by Ian Murdock . 9 | # Further changes by Javier Fernandez-Sanguino 10 | # 11 | # Version: @(#)skeleton 1.9 26-Feb-2001 miquels@cistron.nl 12 | # 13 | ### BEGIN INIT INFO 14 | # Provides: supervisor 15 | # Required-Start: $remote_fs $network $named 16 | # Required-Stop: $remote_fs $network $named 17 | # Default-Start: 2 3 4 5 18 | # Default-Stop: 0 1 6 19 | # Short-Description: Start/stop supervisor 20 | # Description: Start/stop supervisor daemon and its configured 21 | # subprocesses. 22 | ### END INIT INFO 23 | 24 | . /lib/lsb/init-functions 25 | 26 | PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 27 | DAEMON=/usr/local/bin/supervisord 28 | NAME=supervisord 29 | DESC=supervisor 30 | 31 | test -x $DAEMON || exit 0 32 | 33 | LOGDIR=/var/log/supervisor 34 | PIDFILE=/tmp/supervisord.pid 35 | DODTIME=5 # Time to wait for the server to die, in seconds 36 | # If this value is set too low you might not 37 | # let some servers to die gracefully and 38 | # 'restart' will not work 39 | 40 | # Include supervisor defaults if available 41 | if [ -f /etc/default/supervisor ] ; then 42 | . /etc/default/supervisor 43 | fi 44 | DAEMON_OPTS="-c {{ supervisor_conf_root }}/supervisord.conf $DAEMON_OPTS" 45 | 46 | set -e 47 | 48 | running_pid() 49 | { 50 | # Check if a given process pid's cmdline matches a given name 51 | pid=$1 52 | name=$2 53 | [ -z "$pid" ] && return 1 54 | [ ! -d /proc/$pid ] && return 1 55 | (cat /proc/$pid/cmdline | tr "\000" "\n"|grep -q $name) || return 1 56 | return 0 57 | } 58 | 59 | running() 60 | { 61 | # Check if the process is running looking at /proc 62 | # (works for all users) 63 | 64 | # No pidfile, probably no daemon present 65 | [ ! -f "$PIDFILE" ] && return 1 66 | # Obtain the pid and check it against the binary name 67 | pid=`cat $PIDFILE` 68 | running_pid $pid $DAEMON || return 1 69 | return 0 70 | } 71 | 72 | force_stop() { 73 | # Forcefully kill the process 74 | [ ! -f "$PIDFILE" ] && return 75 | if running ; then 76 | kill -15 $pid 77 | # Is it really dead? 78 | [ -n "$DODTIME" ] && sleep "$DODTIME"s 79 | if running ; then 80 | kill -9 $pid 81 | [ -n "$DODTIME" ] && sleep "$DODTIME"s 82 | if running ; then 83 | echo "Cannot kill $LABEL (pid=$pid)!" 84 | exit 1 85 | fi 86 | fi 87 | fi 88 | rm -f $PIDFILE 89 | return 0 90 | } 91 | 92 | case "$1" in 93 | start) 94 | echo -n "Starting $DESC: " 95 | start-stop-daemon --start --quiet --pidfile $PIDFILE \ 96 | --startas $DAEMON -- $DAEMON_OPTS 97 | test -f $PIDFILE || sleep 1 98 | if running ; then 99 | echo "$NAME." 100 | else 101 | echo " ERROR." 102 | fi 103 | ;; 104 | stop) 105 | echo -n "Stopping $DESC: " 106 | start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE 107 | echo "$NAME." 108 | ;; 109 | force-stop) 110 | echo -n "Forcefully stopping $DESC: " 111 | force_stop 112 | if ! running ; then 113 | echo "$NAME." 114 | else 115 | echo " ERROR." 116 | fi 117 | ;; 118 | #reload) 119 | # 120 | # If the daemon can reload its config files on the fly 121 | # for example by sending it SIGHUP, do it here. 122 | # 123 | # If the daemon responds to changes in its config file 124 | # directly anyway, make this a do-nothing entry. 125 | # 126 | # echo "Reloading $DESC configuration files." 127 | # start-stop-daemon --stop --signal 1 --quiet --pidfile \ 128 | # /var/run/$NAME.pid --exec $DAEMON 129 | #;; 130 | force-reload) 131 | # 132 | # If the "reload" option is implemented, move the "force-reload" 133 | # option to the "reload" entry above. If not, "force-reload" is 134 | # just the same as "restart" except that it does nothing if the 135 | # daemon isn't already running. 136 | # check wether $DAEMON is running. If so, restart 137 | start-stop-daemon --stop --test --quiet --pidfile $PIDFILE \ 138 | --startas $DAEMON \ 139 | && $0 restart \ 140 | || exit 0 141 | ;; 142 | restart) 143 | echo -n "Restarting $DESC: " 144 | start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE 145 | [ -n "$DODTIME" ] && sleep $DODTIME 146 | start-stop-daemon --start --quiet --pidfile $PIDFILE \ 147 | --startas $DAEMON -- $DAEMON_OPTS 148 | echo "$NAME." 149 | ;; 150 | status) 151 | echo -n "$LABEL is " 152 | if running ; then 153 | echo "running" 154 | else 155 | echo " not running." 156 | exit 1 157 | fi 158 | ;; 159 | *) 160 | N=/etc/init.d/$NAME 161 | # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2 162 | echo "Usage: $N {start|stop|restart|force-reload|status|force-stop}" >&2 163 | exit 1 164 | ;; 165 | esac 166 | 167 | exit 0 168 | -------------------------------------------------------------------------------- /roles/supervisor/templates/supervisord.conf: -------------------------------------------------------------------------------- 1 | ; Sample supervisor config file. 2 | ; 3 | ; For more information on the config file, please see: 4 | ; http://supervisord.org/configuration.html 5 | ; 6 | ; Note: shell expansion ("~" or "$HOME") is not supported. Environment 7 | ; variables can be expanded using this syntax: "%(ENV_HOME)s". 8 | 9 | [unix_http_server] 10 | file=/tmp/supervisor.sock ; (the path to the socket file) 11 | ;chmod=0700 ; socket file mode (default 0700) 12 | ;chown=nobody:nogroup ; socket file uid:gid owner 13 | ;username=user ; (default is no username (open server)) 14 | ;password=123 ; (default is no password (open server)) 15 | 16 | [inet_http_server] ; inet (TCP) server disabled by default 17 | port={{ supervisor_http_host }}:{{ supervisor_http_port }} ; (ip_address:port specifier, *:port for all iface) 18 | username={{ supervisor_http_user_name }} ; (default is no username (open server)) 19 | password={{ supervisor_http_user_password }} ; (default is no password (open server)) 20 | 21 | [supervisord] 22 | logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log) 23 | logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) 24 | logfile_backups=10 ; (num of main logfile rotation backups;default 10) 25 | loglevel=info ; (log level;default info; others: debug,warn,trace) 26 | pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid) 27 | nodaemon=false ; (start in foreground if true;default false) 28 | minfds=1024 ; (min. avail startup file descriptors;default 1024) 29 | minprocs=200 ; (min. avail process descriptors;default 200) 30 | ;umask=022 ; (process file creation umask;default 022) 31 | ;user=chrism ; (default is current user, required if root) 32 | ;identifier=supervisor ; (supervisord identifier, default is 'supervisor') 33 | ;directory=/tmp ; (default is not to cd during start) 34 | ;nocleanup=true ; (don't clean up tempfiles at start;default false) 35 | ;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP) 36 | ;environment=KEY=value ; (key value pairs to add to environment) 37 | ;strip_ansi=false ; (strip ansi escape codes in logs; def. false) 38 | 39 | ; the below section must remain in the config file for RPC 40 | ; (supervisorctl/web interface) to work, additional interfaces may be 41 | ; added by defining them in separate rpcinterface: sections 42 | [rpcinterface:supervisor] 43 | supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface 44 | 45 | [supervisorctl] 46 | serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket 47 | ;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket 48 | ;username=chris ; should be same as http_username if set 49 | ;password=123 ; should be same as http_password if set 50 | ;prompt=mysupervisor ; cmd line prompt (default "supervisor") 51 | ;history_file=~/.sc_history ; use readline history if available 52 | 53 | ; The below sample program section shows all possible program subsection values, 54 | ; create one or more 'real' program: sections to be able to control them under 55 | ; supervisor. 56 | 57 | ;[program:theprogramname] 58 | ;command=/bin/cat ; the program (relative uses PATH, can take args) 59 | ;process_name=%(program_name)s ; process_name expr (default %(program_name)s) 60 | ;numprocs=1 ; number of processes copies to start (def 1) 61 | ;directory=/tmp ; directory to cwd to before exec (def no cwd) 62 | ;umask=022 ; umask for process (default None) 63 | ;priority=999 ; the relative start priority (default 999) 64 | ;autostart=true ; start at supervisord start (default: true) 65 | ;autorestart=unexpected ; whether/when to restart (default: unexpected) 66 | ;startsecs=1 ; number of secs prog must stay running (def. 1) 67 | ;startretries=3 ; max # of serial start failures (default 3) 68 | ;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2) 69 | ;stopsignal=QUIT ; signal used to kill process (default TERM) 70 | ;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) 71 | ;stopasgroup=false ; send stop signal to the UNIX process group (default false) 72 | ;killasgroup=false ; SIGKILL the UNIX process group (def false) 73 | ;user=chrism ; setuid to this UNIX account to run the program 74 | ;redirect_stderr=true ; redirect proc stderr to stdout (default false) 75 | ;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO 76 | ;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) 77 | ;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10) 78 | ;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) 79 | ;stdout_events_enabled=false ; emit events on stdout writes (default false) 80 | ;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO 81 | ;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) 82 | ;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10) 83 | ;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) 84 | ;stderr_events_enabled=false ; emit events on stderr writes (default false) 85 | ;environment=A=1,B=2 ; process environment additions (def no adds) 86 | ;serverurl=AUTO ; override serverurl computation (childutils) 87 | 88 | ; The below sample eventlistener section shows all possible 89 | ; eventlistener subsection values, create one or more 'real' 90 | ; eventlistener: sections to be able to handle event notifications 91 | ; sent by supervisor. 92 | 93 | ;[eventlistener:theeventlistenername] 94 | ;command=/bin/eventlistener ; the program (relative uses PATH, can take args) 95 | ;process_name=%(program_name)s ; process_name expr (default %(program_name)s) 96 | ;numprocs=1 ; number of processes copies to start (def 1) 97 | ;events=EVENT ; event notif. types to subscribe to (req'd) 98 | ;buffer_size=10 ; event buffer queue size (default 10) 99 | ;directory=/tmp ; directory to cwd to before exec (def no cwd) 100 | ;umask=022 ; umask for process (default None) 101 | ;priority=-1 ; the relative start priority (default -1) 102 | ;autostart=true ; start at supervisord start (default: true) 103 | ;autorestart=unexpected ; whether/when to restart (default: unexpected) 104 | ;startsecs=1 ; number of secs prog must stay running (def. 1) 105 | ;startretries=3 ; max # of serial start failures (default 3) 106 | ;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2) 107 | ;stopsignal=QUIT ; signal used to kill process (default TERM) 108 | ;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) 109 | ;stopasgroup=false ; send stop signal to the UNIX process group (default false) 110 | ;killasgroup=false ; SIGKILL the UNIX process group (def false) 111 | ;user=chrism ; setuid to this UNIX account to run the program 112 | ;redirect_stderr=true ; redirect proc stderr to stdout (default false) 113 | ;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO 114 | ;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) 115 | ;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10) 116 | ;stdout_events_enabled=false ; emit events on stdout writes (default false) 117 | ;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO 118 | ;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) 119 | ;stderr_logfile_backups ; # of stderr logfile backups (default 10) 120 | ;stderr_events_enabled=false ; emit events on stderr writes (default false) 121 | ;environment=A=1,B=2 ; process environment additions 122 | ;serverurl=AUTO ; override serverurl computation (childutils) 123 | 124 | ; The below sample group section shows all possible group values, 125 | ; create one or more 'real' group: sections to create "heterogeneous" 126 | ; process groups. 127 | 128 | ;[group:thegroupname] 129 | ;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions 130 | ;priority=999 ; the relative start priority (default 999) 131 | 132 | ; The [include] section can just contain the "files" setting. This 133 | ; setting can list multiple files (separated by whitespace or 134 | ; newlines). It can also contain wildcards. The filenames are 135 | ; interpreted as relative to this file. Included files *cannot* 136 | ; include files themselves. 137 | 138 | [include] 139 | files = {{ supervisor_conf_root }}/conf.d/*.ini 140 | -------------------------------------------------------------------------------- /roles/tomcat/tasks/main.yml: -------------------------------------------------------------------------------- 1 | 2 | - name: add group '{{ tomcat_user }}' 3 | group: name={{tomcat_group}} 4 | sudo: True 5 | 6 | - name: add user '{{ tomcat_user }}' 7 | user: name={{tomcat_user}} group={{tomcat_group}} 8 | sudo: True 9 | 10 | - name: create tomcat directory ' {{ deploy_path }} ' 11 | file: path={{deploy_path}} state=directory owner={{tomcat_user}} group={{tomcat_group}} 12 | 13 | - name: Download Tomcat 14 | get_url: url={{tomcat_mirrors}}/tomcat/{{tomcat_major_version}}/v{{tomcat_version}}/bin/apache-tomcat-{{tomcat_version}}.tar.gz dest={{deploy_path}}/apache-tomcat-{{tomcat_version}}.tar.gz 15 | 16 | - stat: path={{deploy_path}}/tomcat-{{app}} 17 | register: tomcat_path_register 18 | 19 | - name: Extract archive 20 | command: chdir=/usr/share /bin/tar xvf {{deploy_path}}/apache-tomcat-{{tomcat_version}}.tar.gz -C {{deploy_path}} creates={{deploy_path}}/apache-tomcat-{{tomcat_version}} 21 | when: tomcat_path_register.stat.exists == False 22 | 23 | - name: change tomcat directory name 24 | command: chdir=/usr/share /bin/mv {{deploy_path}}/apache-tomcat-{{tomcat_version}} {{deploy_path}}/tomcat-{{app}} creates={{deploy_path}}/tomcat-{{app}} 25 | 26 | - name: Change ownership of Tomcat installation 27 | file: path={{deploy_path}}/tomcat-{{app}} owner={{tomcat_user}} group={{tomcat_group}} state=directory recurse=yes 28 | 29 | - name: Configure Tomcat server 30 | template: src=server.xml dest={{deploy_path}}/tomcat-{{app}}/conf/ 31 | # notify: restart tomcat 32 | 33 | - name: Configure Tomcat setenv.sh 34 | template: src=setenv.sh dest={{deploy_path}}/tomcat-{{app}}/bin/ 35 | # notify: restart tomcat 36 | 37 | - name: Configure tomcat-control.sh 38 | template: src=tomcat-control.sh dest={{deploy_path}}/tomcat-{{app}}/bin/ 39 | 40 | - name: add supervisor config 41 | template: src=tomcat-supervisor.ini dest={{ supervisor_conf_root }}/conf.d/tomcat-{{app}}.ini -------------------------------------------------------------------------------- /roles/tomcat/templates/server.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 21 | 25 | 26 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 41 | 42 | 45 | 50 | 51 | 52 | 57 | 58 | 59 | 60 | 64 | 65 | 66 | 73 | 76 | 77 | 83 | 87 | 92 | 93 | 94 | 95 | 96 | 97 | 102 | 103 | 106 | 107 | 108 | 111 | 114 | 115 | 117 | 118 | 122 | 124 | 125 | 126 | 128 | 129 | 131 | 134 | 135 | 138 | 141 | 142 | 143 | 144 | 145 | 146 | -------------------------------------------------------------------------------- /roles/tomcat/templates/setenv.sh: -------------------------------------------------------------------------------- 1 | 2 | JAVA_OPTS="$JAVA_OPTS {{ JAVA_OPTS }} " 3 | -------------------------------------------------------------------------------- /roles/tomcat/templates/tomcat-control.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # chkconfig: 345 99 28 4 | # description: Starts/Stops Apache Tomcat 5 | # 6 | # Tomcat 7 start/stop/status script 7 | # Forked from: https://gist.github.com/valotas/1000094 8 | # @author: Miglen Evlogiev 9 | # 10 | # Release updates: 11 | # Updated method for gathering pid of the current proccess 12 | # Added usage of CATALINA_BASE 13 | # Added coloring and additional status 14 | # Added check for existence of the tomcat user 15 | # 16 | 17 | #Location of JAVA_HOME (bin files) 18 | export JAVA_HOME={{JAVA_HOME}} 19 | 20 | #Add Java binary files to PATH 21 | export PATH=$JAVA_HOME/bin:$PATH 22 | 23 | #CATALINA_HOME is the location of the bin files of Tomcat 24 | export CATALINA_HOME={{deploy_path}}/tomcat-{{app}} 25 | 26 | #CATALINA_BASE is the location of the configuration files of this instance of Tomcat 27 | export CATALINA_BASE={{deploy_path}}/tomcat-{{app}} 28 | 29 | #TOMCAT_USER is the default user of tomcat 30 | export TOMCAT_USER={{tomcat_user}} 31 | 32 | #TOMCAT_USAGE is the message if this script is called without any options 33 | TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}" 34 | 35 | #SHUTDOWN_WAIT is wait time in seconds for java proccess to stop 36 | SHUTDOWN_WAIT=20 37 | 38 | tomcat_pid() { 39 | echo `ps -fe | grep $CATALINA_BASE/bin | grep -v grep | tr -s " "|cut -d" " -f2` 40 | } 41 | 42 | start() { 43 | pid=$(tomcat_pid) 44 | if [ -n "$pid" ] 45 | then 46 | echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m" 47 | else 48 | # Start tomcat 49 | echo -e "\e[00;32mStarting tomcat\e[00m" 50 | #ulimit -n 100000 51 | #umask 007 52 | #/bin/su -p -s /bin/sh tomcat 53 | if [ `user_exists $TOMCAT_USER` = "1" ] 54 | then 55 | su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh 56 | else 57 | sh $CATALINA_HOME/bin/startup.sh 58 | fi 59 | status 60 | fi 61 | return 0 62 | } 63 | 64 | status(){ 65 | pid=$(tomcat_pid) 66 | if [ -n "$pid" ]; then echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m" 67 | else echo -e "\e[00;31mTomcat is not running\e[00m" 68 | fi 69 | } 70 | 71 | stop() { 72 | pid=$(tomcat_pid) 73 | echo 'pid' 74 | echo $pid 75 | if [ -n "$pid" ] 76 | then 77 | echo -e "\e[00;31mStoping Tomcat\e[00m" 78 | #/bin/su -p -s /bin/sh tomcat 79 | sh $CATALINA_HOME/bin/shutdown.sh 80 | 81 | let kwait=$SHUTDOWN_WAIT 82 | count=0; 83 | until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ] 84 | do 85 | echo -n -e "\n\e[00;31mwaiting for processes to exit\e[00m"; 86 | sleep 1 87 | let count=$count+1; 88 | done 89 | 90 | if [ $count -gt $kwait ]; then 91 | echo -n -e "\n\e[00;31mkilling processes which didn't stop after $SHUTDOWN_WAIT seconds\e[00m" 92 | kill -9 $pid 93 | fi 94 | else 95 | echo -e "\e[00;31mTomcat is not running\e[00m" 96 | fi 97 | 98 | return 0 99 | } 100 | 101 | user_exists(){ 102 | if id -u $1 >/dev/null 2>&1; then 103 | echo "1" 104 | else 105 | echo "0" 106 | fi 107 | } 108 | 109 | case $1 in 110 | 111 | start) 112 | start 113 | ;; 114 | 115 | stop) 116 | stop 117 | ;; 118 | 119 | restart) 120 | stop 121 | start 122 | ;; 123 | 124 | status) 125 | status 126 | 127 | ;; 128 | 129 | *) 130 | echo -e $TOMCAT_USAGE 131 | ;; 132 | esac 133 | exit 0 134 | -------------------------------------------------------------------------------- /roles/tomcat/templates/tomcat-supervisor.ini: -------------------------------------------------------------------------------- 1 | 2 | [program:tomcat-{{app}}] 3 | command = {{ deploy_path }}/%(program_name)s/bin/catalina.sh run 4 | autorestart=true 5 | redirect_stderr = true 6 | stdout_logfile = {{ deploy_path }}/%(program_name)s/logs/catalina.out 7 | logfile_maxbytes = 10MB 8 | user = {{ tomcat_user }} 9 | -------------------------------------------------------------------------------- /roles/tomcat/templates/tomcat-users.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 21 | 22 | 27 | 32 | 33 | 34 | 35 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /site.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # This playbook deploys a simple standalone Tomcat 7 server. 3 | 4 | - hosts: '{{target_host}}' 5 | user: root 6 | vars: 7 | jdk_version: openjdk-7-jdk 8 | tomcat_mirrors: http://mirrors.hust.edu.cn/apache 9 | tomcat_major_version: tomcat-7 10 | tomcat_version: 7.0.59 11 | #supervisord_conf_path: /opt/python27env/supervisor/conf.d 12 | #ansible_python_interpreter: /opt/python27env/bin/python 13 | tomcat_group: www 14 | tomcat_user: www 15 | supervisor_service_name: supervisor 16 | supervisor_conf_root: '/etc/supervisor' 17 | supervisor_http_host: 0.0.0.0 18 | supervisor_http_port: 9001 19 | supervisor_http_user_name: admin 20 | supervisor_http_user_password: admin 21 | roles: 22 | # - selinux 23 | - jdk 24 | - supervisor 25 | - tomcat 26 | - deploy-war 27 | 28 | --------------------------------------------------------------------------------