feat: 新增 ci

This commit is contained in:
fangshunjian
2022-03-31 10:44:28 +08:00
parent 7ea335e945
commit 3f9ff7e918
7 changed files with 264 additions and 0 deletions

62
.gitlab-ci.yml Normal file
View File

@@ -0,0 +1,62 @@
# docker镜像
image: git.mesalab.cn:7443/nezha/nz-build-env:1.1
# 依赖的docker服务
services:
- mariadb
- redis
# 开始执行脚本前所需执行脚本
before_script:
- echo "begin ci"
# 脚本执行完后的钩子,执行所需脚本
after_script:
- echo "end ci"
# 该ci pipeline适合的场景
stages:
- build
- test
cache:
paths:
- .m2/repository
# 定义的任务
build_rpm:
stage: build
# 所需执行的脚本
script:
- env | sort
- chmod +x ./tools/package.sh
artifacts:
name: "$CI_COMMIT_REF_NAME"
when: on_success
paths:
- ./target/*.xjar
- ./target/xjar
expire_in: 1 week
# 在哪个分支上可用
only:
- /^rel-.*$/i
# 指定哪个ci runner跑该工作
tags:
- nezha
# 定义的任务
test:
stage: test
# 所需执行的脚本
script:
- env | sort
- mvn clean test
artifacts:
name: "$CI_COMMIT_REF_NAME"
when: on_success
paths:
- ./target/*.xjar
- ./target/xjar
expire_in: 1 week
# 在哪个分支上可用
only:
- /^dev-.*$/i
# 指定哪个ci runner跑该工作
tags:
- nezha

93
tools/afterinstall.sh Normal file
View File

@@ -0,0 +1,93 @@
#!/bin/sh
# Find Java
if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
JAVA_EXE="$JAVA_HOME/bin/java"
elif type -p java > /dev/null 2>&1; then
JAVA_EXE=$(type -p java)
elif [[ -x "/usr/bin/java" ]]; then
JAVA_EXE="/usr/bin/java"
else
echo "Unable to find Java"
JAVA_EXE="java"
# exit 1
fi
# install
if [ 1 -eq $1 ];then
cat > /usr/lib/systemd/system/nz-talon.service <<EOF
[Unit]
Description=nz-talon
After=network.target
[Service]
WorkingDirectory=/opt/nezha/nz-talon
ExecStart=/opt/nezha/nz-talon/xjar ${JAVA_EXE} -Dnz-agent.dir=/opt/nezha/nz-talon -jar /opt/nezha/nz-talon/nz-talon.xjar
RestartSec=5s
Restart=always
[Install]
WantedBy=multi-user.target
EOF
cat > /usr/lib/systemd/system/promtail.service <<"EOF"
[Unit]
Description=nz-talon
After=network.target
[Service]
WorkingDirectory=/opt/nezha/promtail
EnvironmentFile=/opt/nezha/promtail/config.conf
ExecStart=/opt/nezha/promtail/promtail $OPTION
RestartSec=5s
Restart=always
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable nz-talon && systemctl restart nz-talon
systemctl enable promtail && systemctl restart promtail
echo 'install nz-talon success !'
fi
compareMD5()
{
if [ ! -f $1 ] || [ ! -f $2 ];then
echo 1
return 1
fi
local f1MD5=`md5sum $1|awk '{print $1}'`
local f2MD5=`md5sum $2|awk '{print $1}'`
if [ ${f1MD5} = ${f2MD5} ];then
echo 0
return 0
else
echo 1
return 1
fi
}
# update
if [ 2 -eq $1 ];then
TALON_PATH=/opt/nezha/nz-talon
TMP_PATH=/tmp/nezha/nz-talon
PROMTAIL_PATH=/opt/nezha/promtail
echo 'move backup config file...'
if [ -d $TMP_PATH ];then
# 恢复 nz-talon 配置文件
for i in $(ls $TALON_PATH/config);do
if [ 1 -eq `compareMD5 $WEB_PATH/config/$i $TMP_PATH/config/$i` ];then
echo 'return config file '${i}
cp -f $TMP_PATH/config/$i $TALON_PATH/config/$i
fi
done
# 恢复 promtail 配置文件
for n in $(ls $TMP_PATH/promtail);do
if [ -f "$PROMTAIL_PATH/$n" ];then
echo 'return config file '${n}
cp -f $TMP_PATH/promtail/$n $PROMTAIL_PATH/$n
fi
done
fi
systemctl restart nz-talon
systemctl restart promtail
fi

20
tools/afterremove.sh Normal file
View File

@@ -0,0 +1,20 @@
#!/bin/sh
if [ 0 -eq $1 ];then
echo 'start remove nz-talon module from disk...'
systemctl stop nz-talon.service
systemctl stop promtail.service
rm -rf /opt/nezha/nz-talon
rm -rf /opt/nezha/promtail
systemctl disable nz-talon.service
systemctl disable promtail.service
rm -rf /usr/lib/systemd/system/{nz-talon.service,promtail.service}
systemctl daemon-reload
echo 'uninstall success!'
fi

32
tools/beforeinstall.sh Normal file
View File

@@ -0,0 +1,32 @@
#!/bin/bash
TALON_PATH=/opt/nezha/nz-talon
PROMTAIL_PATH=/opt/nezha/promtail
if [ 2 -eq $1 ];then
if [ ! -d ${TALON_PATH}/config ];then
exit 0
fi
systemctl stop nz-talon
systemctl stop promtail
TMP_PATH=/tmp/nezha/nz-talon
rm -rf $TMP_PATH
mkdir -p $TMP_PATH
mkdir -p $TMP_PATH/promtail
cp -rf $TALON_PATH/config $TMP_PATH
[ -f "$PROMTAIL_PATH/promtail.yaml" ] && cp -rf $PROMTAIL_PATH/promtail.yaml $TMP_PATH/promtail
[ -f "$PROMTAIL_PATH/config.conf" ] && cp -rf $PROMTAIL_PATH/config.conf $TMP_PATH/promtail
echo 'backup config file from '${TALON_PATH}
if [ 0 -lt $(rpm -aq nz-talon|wc -w) ];then
echo 'clean before install...'
rm -rf $TALON_PATH
rm -rf $PROMTAIL_PATH
fi
fi

1
tools/config.conf Normal file
View File

@@ -0,0 +1 @@
OPTION=" -config.file /opt/nezha/promtail/promtail.yaml "

43
tools/package.sh Normal file
View File

@@ -0,0 +1,43 @@
#!/bin/bash
set -evx
pwd
# 初始化 minio
MINIO_HOST=http://192.168.44.36:2020/
MINIO_USER=admin
MINIO_PWD=Nezha@02!
mc alias set nz $MINIO_HOST $MINIO_USER $MINIO_PWD
BUILD_PATH=./build
RPM_TALON_PATH=$BUILD_PATH/opt/nezha/nz-talon
RPM_PROMTAIL_PATH=$BUILD_PATH/opt/nezha/promtail
mkdir -p $RPM_TALON_PATH $RPM_PROMTAIL_PATH
mkdir -p $RPM_TALON_PATH/bin $RPM_TALON_PATH/conf
mkdir -p $RPM_PROMTAIL_PATH/bin $RPM_PROMTAIL_PATH/conf
echo 'packaging nz-talon ...'
# mvn 编译 nz-talon
mvn clean install -Dxjar.password=111111 -Dxjar.excludes=/db/*,/static/**/*
go build ./target/xjar.go
chmod -x ./target/xjar
cp -f ./target/nz-talon.xjar $RPM_TALON_PATH/bin
cp -f ./target/xjar $RPM_TALON_PATH/bin
cp -f ./src/main/resources/{application-prod.yml,application.yml,logback-spring.xml,config/auth.yml,config/token.auth,config/promtail.version} ${RPM_TALON_PATH}/config
sed -i 's/<property name="log.path" value=".*"/<property name="log.path" value="\/var\/log\/nezha\/nz-talon\/"/g' ${RPM_TALON_PATH}/config/logback-spring.xml
# 下载 promtail
mc cp nz/depends/loki/promtail-linux-amd64.zip ./
unzip -o promtail-linux-amd64.zip -d $RPM_PROMTAIL_PATH/bin
cp -f ./tools/config.conf $RPM_TALON_PATH
cp -f ./tools/promtail.yaml $RPM_TALON_PATH
#修改日志地址 2021_08_26
sed -i 's/<property name="log.path" value=".*"/<property name="log.path" value="\/var\/log\/nezha\/nz-talon\/"/g' ${RPM_TALON_PATH}/config/logback-spring.xml
fpm -s dir -t rpm -n ${PACKAGE_NAME} -v ${PACKAGE_VERSION} --iteration ${ITERATION} --rpm-os 'linux' --pre-install ${CUR_PATH}/script/nz-talon/beforeinstall.sh --post-install ${CUR_PATH}/script/nz-talon/afterinstall.sh --post-uninstall ${CUR_PATH}/script/nz-talon/afterremove.sh -C ${CUR_PATH}/build/nz-talon -f -p ${CUR_PATH}/rpm
echo 'package nz-talon finished'

13
tools/promtail.yaml Normal file
View File

@@ -0,0 +1,13 @@
server:
http_listen_address: 127.0.0.1
http_listen_port: 19080
grpc_listen_port: 19081
positions:
filename: /tmp/positions.yaml
clients:
- url: http://127.0.0.1
scrape_configs:
- job_name: init
pipeline_stages:
syslog:
listen_address: 127.0.0.1:56799