mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
fixed issue #801 , support canal docker
This commit is contained in:
117
docker/image/admin/app.sh
Executable file
117
docker/image/admin/app.sh
Executable file
@@ -0,0 +1,117 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
source /etc/profile
|
||||
export JAVA_HOME=/usr/java/latest
|
||||
export PATH=$JAVA_HOME/bin:$PATH
|
||||
touch /tmp/start.log
|
||||
chown admin: /tmp/start.log
|
||||
chown -R admin: /home/admin/canal-server
|
||||
host=`hostname -i`
|
||||
|
||||
# waitterm
|
||||
# wait TERM/INT signal.
|
||||
# see: http://veithen.github.io/2014/11/16/sigterm-propagation.html
|
||||
waitterm() {
|
||||
local PID
|
||||
# any process to block
|
||||
tail -f /dev/null &
|
||||
PID="$!"
|
||||
# setup trap, could do nothing, or just kill the blocker
|
||||
trap "kill -TERM ${PID}" TERM INT
|
||||
# wait for signal, ignore wait exit code
|
||||
wait "${PID}" || true
|
||||
# clear trap
|
||||
trap - TERM INT
|
||||
# wait blocker, ignore blocker exit code
|
||||
wait "${PID}" 2>/dev/null || true
|
||||
}
|
||||
|
||||
# waittermpid "${PIDFILE}".
|
||||
# monitor process by pidfile && wait TERM/INT signal.
|
||||
# if the process disappeared, return 1, means exit with ERROR.
|
||||
# if TERM or INT signal received, return 0, means OK to exit.
|
||||
waittermpid() {
|
||||
local PIDFILE PID do_run error
|
||||
PIDFILE="${1?}"
|
||||
do_run=true
|
||||
error=0
|
||||
trap "do_run=false" TERM INT
|
||||
while "${do_run}" ; do
|
||||
PID="$(cat "${PIDFILE}")"
|
||||
if ! ps -p "${PID}" >/dev/null 2>&1 ; then
|
||||
do_run=false
|
||||
error=1
|
||||
else
|
||||
sleep 1
|
||||
fi
|
||||
done
|
||||
trap - TERM INT
|
||||
return "${error}"
|
||||
}
|
||||
|
||||
|
||||
function checkStart() {
|
||||
local name=$1
|
||||
local cmd=$2
|
||||
local timeout=$3
|
||||
cost=5
|
||||
while [ $timeout -gt 0 ]; do
|
||||
ST=`eval $cmd`
|
||||
if [ "$ST" == "0" ]; then
|
||||
sleep 1
|
||||
let timeout=timeout-1
|
||||
let cost=cost+1
|
||||
elif [ "$ST" == "" ]; then
|
||||
sleep 1
|
||||
let timeout=timeout-1
|
||||
let cost=cost+1
|
||||
else
|
||||
break
|
||||
fi
|
||||
done
|
||||
echo "start $name successful"
|
||||
}
|
||||
|
||||
|
||||
function start_canal() {
|
||||
echo "start canal ..."
|
||||
serverPort=`perl -le 'print $ENV{"canal.port"}'`
|
||||
if [ -z "$serverPort" ] ; then
|
||||
serverPort=11111
|
||||
fi
|
||||
|
||||
destination=`perl -le 'print $ENV{"canal.destinations"}'`
|
||||
if [[ "$destination" =~ ',' ]]; then
|
||||
echo "multi destination:$destination is not support"
|
||||
exit 1;
|
||||
else
|
||||
mv /home/admin/canal-server/conf/example /home/admin/canal-server/conf/$destination
|
||||
fi
|
||||
su admin -c 'cd /home/admin/canal-server/bin/ && sh restart.sh 1>>/tmp/start.log 2>&1'
|
||||
sleep 5
|
||||
#check start
|
||||
checkStart "canal" "nc 127.0.0.1 $serverPort -w 1 -z | wc -l" 30
|
||||
}
|
||||
|
||||
function stop_canal() {
|
||||
echo "stop canal"
|
||||
su admin -c 'cd /home/admin/canal-server/bin/ && sh stop.sh 1>>/tmp/start.log 2>&1'
|
||||
echo "stop canal successful ..."
|
||||
}
|
||||
|
||||
echo "==> START ..."
|
||||
|
||||
start_canal
|
||||
|
||||
echo "==> START SUCCESSFUL ..."
|
||||
|
||||
tail -f /dev/null &
|
||||
# wait TERM signal
|
||||
waitterm
|
||||
|
||||
echo "==> STOP"
|
||||
|
||||
stop_canal
|
||||
|
||||
echo "==> STOP SUCCESSFUL ..."
|
||||
2
docker/image/admin/bin/clean_log
Normal file
2
docker/image/admin/bin/clean_log
Normal file
@@ -0,0 +1,2 @@
|
||||
# cron clean log once per minute
|
||||
*/2 * * * * admin /home/admin/bin/clean_log.sh >>/tmp/clean_log.log 2>&1
|
||||
45
docker/image/admin/bin/clean_log.sh
Executable file
45
docker/image/admin/bin/clean_log.sh
Executable file
@@ -0,0 +1,45 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Global Settings
|
||||
PATH="$HOME/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin"
|
||||
export PATH
|
||||
|
||||
CUTOFF="85"
|
||||
#获取磁盘使用率最高的分区
|
||||
USAGE=$(df -h|awk 'NR>1 {gsub(/%$/,"",$5);print $5 }'|sort -nr|head -1)
|
||||
before=$USAGE
|
||||
|
||||
baseClean(){
|
||||
#删除tmp目录15天前的文件。
|
||||
#更新文档时间戳
|
||||
if [ -d /tmp/hsperfdata_admin ]
|
||||
then
|
||||
touch /tmp/hsperfdata_admin
|
||||
touch /tmp/hsperfdata_admin/*
|
||||
fi
|
||||
|
||||
find /tmp/ -type f -mtime +15 | xargs -t rm -rf >/dev/null 2>&1
|
||||
|
||||
|
||||
now=$(df -h|awk 'NR>1 {gsub(/%$/,"",$5);print $5 }'|sort -nr|head -1)
|
||||
echo "before:$before; now:$now"
|
||||
}
|
||||
|
||||
CANAL_DIR="/home/admin/canal-server/logs"
|
||||
if [[ -d $CANAL_DIR ]]; then
|
||||
USAGE=$(df -h|awk 'NR>1 {gsub(/%$/,"",$5);print $5 }'|sort -nr|head -1)
|
||||
if [[ $USAGE -ge 90 ]]; then
|
||||
find $CANAL_DIR -type f -mtime +7 | xargs rm -rf {}
|
||||
fi
|
||||
USAGE=$(df -h|awk 'NR>1 {gsub(/%$/,"",$5);print $5 }'|sort -nr|head -1)
|
||||
if [[ $USAGE -ge 80 ]]; then
|
||||
find $CANAL_DIR -type f -mtime +3 | xargs rm -rf {}
|
||||
fi
|
||||
USAGE=$(df -h|awk 'NR>1 {gsub(/%$/,"",$5);print $5 }'|sort -nr|head -1)
|
||||
if [[ $USAGE -ge 80 ]]; then
|
||||
find $CANAL_DIR -type d -empty -mtime +3 | grep -v canal | xargs rm -rf {}
|
||||
find $CANAL_DIR -type f -iname '*.tmp' | xargs rm -rf {}
|
||||
fi
|
||||
baseClean
|
||||
exit 0
|
||||
fi
|
||||
13
docker/image/admin/health.sh
Executable file
13
docker/image/admin/health.sh
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
CHECK_URL="http://127.0.0.1:8080/metrics"
|
||||
CHECK_POINT="success"
|
||||
CHECK_COUNT=`curl -s --connect-timeout 7 --max-time 7 $CHECK_URL | grep -c $CHECK_POINT`
|
||||
if [ $CHECK_COUNT -eq 0 ]; then
|
||||
echo "[FAILED]"
|
||||
status=0
|
||||
error=1
|
||||
else
|
||||
echo "[ OK ]"
|
||||
status=1
|
||||
error=0
|
||||
fi
|
||||
11
docker/image/alidata/bin/exec_rc_local.sh
Executable file
11
docker/image/alidata/bin/exec_rc_local.sh
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/bin/bash
|
||||
|
||||
if [ "${SKIP_EXEC_RC_LOCAL}" = "YES" ] ; then
|
||||
echo "skip /etc/rc.local: SKIP_EXEC_RC_LOCAL=${SKIP_EXEC_RC_LOCAL}"
|
||||
exit
|
||||
fi
|
||||
|
||||
if [ "${DOCKER_DEPLOY_TYPE}" = "HOST" ] ; then
|
||||
echo "skip /etc/rc.local: DOCKER_DEPLOY_TYPE=${DOCKER_DEPLOY_TYPE}"
|
||||
exit
|
||||
fi
|
||||
6
docker/image/alidata/bin/lark-wait
Executable file
6
docker/image/alidata/bin/lark-wait
Executable file
@@ -0,0 +1,6 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
chown admin: -R /home/admin/
|
||||
source /alidata/lib/proc.sh
|
||||
waitterm
|
||||
27
docker/image/alidata/bin/main.sh
Executable file
27
docker/image/alidata/bin/main.sh
Executable file
@@ -0,0 +1,27 @@
|
||||
#!/bin/bash
|
||||
|
||||
[ -n "${DOCKER_DEPLOY_TYPE}" ] || DOCKER_DEPLOY_TYPE="VM"
|
||||
echo "DOCKER_DEPLOY_TYPE=${DOCKER_DEPLOY_TYPE}"
|
||||
|
||||
# run init scripts
|
||||
for e in $(ls /alidata/init/*) ; do
|
||||
[ -x "${e}" ] || continue
|
||||
echo "==> INIT $e"
|
||||
$e
|
||||
echo "==> EXIT CODE: $?"
|
||||
done
|
||||
|
||||
echo "==> INIT DEFAULT"
|
||||
service sshd start
|
||||
service crond start
|
||||
|
||||
#echo "check hostname -i: `hostname -i`"
|
||||
#hti_num=`hostname -i|awk '{print NF}'`
|
||||
#if [ $hti_num -gt 1 ];then
|
||||
# echo "hostname -i result error:`hostname -i`"
|
||||
# exit 120
|
||||
#fi
|
||||
|
||||
echo "==> INIT DONE"
|
||||
echo "==> RUN ${*}"
|
||||
exec "${@}"
|
||||
19
docker/image/alidata/init/02init-sshd.sh
Executable file
19
docker/image/alidata/init/02init-sshd.sh
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/bin/bash
|
||||
|
||||
# set port
|
||||
if [ -z "${SSHD_PORT}" ] ; then
|
||||
SSHD_PORT=22
|
||||
[ "${DOCKER_DEPLOY_TYPE}" = "HOST" ] && SSHD_PORT=2222
|
||||
fi
|
||||
|
||||
sed -r -i '/^OPTIONS=/ d' /etc/sysconfig/sshd
|
||||
echo 'OPTIONS="-p '"${SSHD_PORT}"'"' >> /etc/sysconfig/sshd
|
||||
|
||||
# set admin ssh pulic key
|
||||
if [ "${USE_ADMIN_PASSAGE}" = "YES" ] ; then
|
||||
echo "set admin passage"
|
||||
mkdir -p /home/admin/.ssh
|
||||
chown admin:admin /home/admin/.ssh
|
||||
chown admin:admin /home/admin/.ssh/authorized_keys
|
||||
chmod 644 /home/admin/.ssh/authorized_keys
|
||||
fi
|
||||
66
docker/image/alidata/init/fix-hosts.py
Executable file
66
docker/image/alidata/init/fix-hosts.py
Executable file
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
#****************************************************************#
|
||||
# Create Date: 2017-01-06 17:58
|
||||
#***************************************************************#
|
||||
|
||||
import socket
|
||||
import shutil
|
||||
from time import gmtime, strftime
|
||||
|
||||
# get host_name
|
||||
host_name = socket.gethostname()
|
||||
tmp_file = "/tmp/.lark-fix-host.hosts"
|
||||
host_file = "/etc/hosts"
|
||||
bak_file_name = "/tmp/hosts-fix-bak.%s" % ( strftime("%Y-%m-%d_%H-%M-%S", gmtime()) )
|
||||
|
||||
# load /etc/hosts file context
|
||||
FH = open(host_file,"r")
|
||||
file_lines = [ i.rstrip() for i in FH.readlines()]
|
||||
FH.close()
|
||||
file_lines_reverse = file_lines[::-1]
|
||||
new_lines = []
|
||||
bad_lines = []
|
||||
last_match_line = ""
|
||||
|
||||
for line in file_lines_reverse:
|
||||
if line.find(host_name) < 0: # 不匹配的行直接跳过
|
||||
new_lines.append(line + "\n")
|
||||
continue
|
||||
|
||||
cols = line.split()
|
||||
new_cols = []
|
||||
if cols[0].startswith("#"): # 跳过已经注释掉的行
|
||||
new_lines.append(line + "\n")
|
||||
continue
|
||||
for col in cols:
|
||||
if not col == host_name: # 跳过不匹配的列
|
||||
new_cols.append(col)
|
||||
continue
|
||||
|
||||
if cols[0] == "127.0.0.1": # 如果第一列是 127.0.0.1 就跳过匹配的列, 防止 hostname -i 返回 127.0.0.1
|
||||
continue
|
||||
|
||||
# 如果已经发现过匹配的列, 就丢掉重复的列
|
||||
if not len(last_match_line) == 0:
|
||||
continue
|
||||
|
||||
new_cols.append(col)
|
||||
last_match_line = line
|
||||
|
||||
# 跳过 xx.xx.xx.xx hostname 这样的重复列
|
||||
if len(new_cols) == 1:
|
||||
continue
|
||||
|
||||
new_l = "%s\n" % " ".join(new_cols)
|
||||
new_lines.append(new_l)
|
||||
|
||||
# save tmp hosts
|
||||
|
||||
FH2=file(tmp_file,"w+")
|
||||
FH2.writelines( new_lines[::-1])
|
||||
FH2.close()
|
||||
|
||||
# mv to /etc/hosts
|
||||
shutil.copy(host_file, bak_file_name)
|
||||
shutil.move(tmp_file, host_file)
|
||||
40
docker/image/alidata/lib/proc.sh
Normal file
40
docker/image/alidata/lib/proc.sh
Normal file
@@ -0,0 +1,40 @@
|
||||
# waitterm
|
||||
# wait TERM/INT signal.
|
||||
# see: http://veithen.github.io/2014/11/16/sigterm-propagation.html
|
||||
waitterm() {
|
||||
local PID
|
||||
# any process to block
|
||||
tail -f /dev/null &
|
||||
PID="$!"
|
||||
# setup trap, could do nothing, or just kill the blocker
|
||||
trap "kill -TERM ${PID}" TERM INT
|
||||
# wait for signal, ignore wait exit code
|
||||
wait "${PID}" || true
|
||||
# clear trap
|
||||
trap - TERM INT
|
||||
# wait blocker, ignore blocker exit code
|
||||
wait "${PID}" 2>/dev/null || true
|
||||
}
|
||||
|
||||
# waittermpid "${PIDFILE}".
|
||||
# monitor process by pidfile && wait TERM/INT signal.
|
||||
# if the process disappeared, return 1, means exit with ERROR.
|
||||
# if TERM or INT signal received, return 0, means OK to exit.
|
||||
waittermpid() {
|
||||
local PIDFILE PID do_run error
|
||||
PIDFILE="${1?}"
|
||||
do_run=true
|
||||
error=0
|
||||
trap "do_run=false" TERM INT
|
||||
while "${do_run}" ; do
|
||||
PID="$(cat "${PIDFILE}")"
|
||||
if ! ps -p "${PID}" >/dev/null 2>&1 ; then
|
||||
do_run=false
|
||||
error=1
|
||||
else
|
||||
sleep 1
|
||||
fi
|
||||
done
|
||||
trap - TERM INT
|
||||
return "${error}"
|
||||
}
|
||||
Reference in New Issue
Block a user