diff --git a/pkg/server/gateway/pb/gateway-for-agent.proto b/pkg/server/gateway/pb/gateway-for-agent.proto index 97a8004..1c82e1c 100644 --- a/pkg/server/gateway/pb/gateway-for-agent.proto +++ b/pkg/server/gateway/pb/gateway-for-agent.proto @@ -179,7 +179,6 @@ service GatewayService { // 查询控制参数 rpc get_control_configs(GetControlConfigsRequest) returns(GetControlConfigsResponse) {} - // deprecated rpc write_metrics_v1 (WriteMetricsRequestV1) returns (WriteMetricsResponse) {} // deprecated diff --git a/pkg/server/gateway/pb/gateway-for-agent_grpc.pb.go b/pkg/server/gateway/pb/gateway-for-agent_grpc.pb.go index eff431d..2330f8c 100644 --- a/pkg/server/gateway/pb/gateway-for-agent_grpc.pb.go +++ b/pkg/server/gateway/pb/gateway-for-agent_grpc.pb.go @@ -30,7 +30,6 @@ type GatewayServiceClient interface { Ping(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*emptypb.Empty, error) // 查询控制参数 GetControlConfigs(ctx context.Context, in *GetControlConfigsRequest, opts ...grpc.CallOption) (*GetControlConfigsResponse, error) - // deprecated WriteMetricsV1(ctx context.Context, in *WriteMetricsRequestV1, opts ...grpc.CallOption) (*WriteMetricsResponse, error) // deprecated WriteMetricsV2(ctx context.Context, in *WriteMetricsRequestV2, opts ...grpc.CallOption) (*WriteMetricsResponse, error) @@ -108,7 +107,7 @@ type GatewayServiceServer interface { Ping(context.Context, *emptypb.Empty) (*emptypb.Empty, error) // 查询控制参数 GetControlConfigs(context.Context, *GetControlConfigsRequest) (*GetControlConfigsResponse, error) - // deprecated + WriteMetricsV1(context.Context, *WriteMetricsRequestV1) (*WriteMetricsResponse, error) // deprecated WriteMetricsV2(context.Context, *WriteMetricsRequestV2) (*WriteMetricsResponse, error) diff --git a/scripts/vm/README.md b/scripts/vm/README.md new file mode 100644 index 0000000..2a50438 --- /dev/null +++ b/scripts/vm/README.md @@ -0,0 +1,15 @@ +# 介绍 +VM模式下的相关脚本. +该脚本可以在 Mac m1 或 Linux 上执行, 要求机器上装有 docker. Mac m1 特别慢, 建议在 Linux 上执行, 可以通过 rsync 将代码复制到 Linux 上再构建. + +# 使用 +```bash +./scripts/vm/build.sh 1.0.0 +``` + +构建的结果在 `/root/workspace/remote/cloudmonitor-agent/scripts/vm/holoinsight-agent_linux-amd64_1.0.0.tar.gz` + +之后我们把它上传到 OSS, 给用户下载安装即可. +```bash +./scripts/vm/upload.sh 1.0.0 +``` diff --git a/scripts/vm/agent.ini b/scripts/vm/agent.ini new file mode 100644 index 0000000..b1fccc3 --- /dev/null +++ b/scripts/vm/agent.ini @@ -0,0 +1,22 @@ +[program:agent] +command=/usr/local/holoinsight/agent/bin/agent.sh +process_name=%(program_name)s +autostart=true +# 启动后持续3秒处于RUNNING状态则认为启动成功 +#startsecs=3 +# 重试 +startretries=3 +autorestart=unexpected +exitcodes=0,7 +stopsignal=TERM +stopwaitsecs=3 +stopasgroup=true +killasgroup=true +redirect_stderr=true +stdout_logfile=/usr/local/holoinsight/agent/logs/stdout.log +stdout_logfile_maxbytes=50MB +stdout_logfile_backups=10 +stdout_capture_maxbytes=0 +stdout_events_enabled=true +#environment=KEY="val",KEY2="val2" +directory=/usr/local/holoinsight/agent diff --git a/scripts/vm/agent.sh b/scripts/vm/agent.sh new file mode 100755 index 0000000..5eae160 --- /dev/null +++ b/scripts/vm/agent.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +export GOTRACEBACK=all +export GODEBUG=gctrace=1,madvdontneed=1 + +script_dir=`dirname $0` + +# 如果文件存在就从它加载一些环境变量 +if [ -e "$script_dir/env.sh" ]; then + source $script_dir/env.sh +fi + +exec $script_dir/agent + diff --git a/scripts/vm/build-lite.sh b/scripts/vm/build-lite.sh new file mode 100755 index 0000000..1eb9397 --- /dev/null +++ b/scripts/vm/build-lite.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +set -e + +# VM 模式下的打包: 打成 tar 包, 用户下载之后解压启动即可 + +script_dir=`dirname $0 | xargs realpath` +project_root=`realpath $script_dir/../..` + +version=` cat $project_root/VERSION ` + +echo '[build vm lite agent package]' + +# tar 包目录结构: +# /bin +# /agent agent 本体 +# /data +# /agent.yaml 配置文件, install 时生成 + +echo script dir is $script_dir +echo project root is $project_root + +tmpdir=`mktemp -d` +agent_home=$tmpdir/agent + +mkdir -p $agent_home/{bin,data,conf,logs} + +echo temp agent home $agent_home + +$script_dir/../build/build-using-docker.sh + +cp $script_dir/../../build/linux-amd64/bin/agent $agent_home/bin/agent + +echo +echo ls -lh $agent_home +ls -lh $agent_home +echo + +build_target=$project_root/build/linux-amd64/holoinsight-agent-lite_linux-amd64_${version}.tar.gz +echo "build to $build_target" +cd $tmpdir/agent && tar -zcf $build_target * + +echo you should upload $build_target to your OSS diff --git a/scripts/vm/build.sh b/scripts/vm/build.sh new file mode 100755 index 0000000..1f9cb32 --- /dev/null +++ b/scripts/vm/build.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +set -e + +# VM 模式下的打包: 打成 tar 包, 用户下载之后解压启动即可 + +script_dir=`dirname $0 | xargs realpath` +project_root=`realpath $script_dir/../..` + +version=` cat $project_root/VERSION ` + +echo '[build vm agent package]' + +# tar 包目录结构: +# /bin +# /agent agent 本体 +# /data +# /agent.yaml 配置文件, install 时生成 + +echo script dir is $script_dir +echo project root is $project_root + +tmpdir=`mktemp -d` +agent_home=$tmpdir/agent + +mkdir -p $agent_home/{bin,data,conf,logs} + +echo temp agent home $agent_home + +cp $script_dir/{supervisord,supervisord.conf,ctl.sh,agent.ini,agent.sh,uninstall.sh} $agent_home/bin/ + +cp $script_dir/initd_holoinsight-agent.sh $agent_home/bin/initd_holoinsight-agent.sh + +$script_dir/../build/build-using-docker.sh + +cp $script_dir/../../build/linux-amd64/bin/agent $agent_home/bin/agent + +echo +echo ls -lh $agent_home +ls -lh $agent_home +echo + +build_target=$project_root/build/linux-amd64/holoinsight-agent_linux-amd64_${version}.tar.gz +echo "build to $build_target" +cd $tmpdir/agent && tar -zcf $build_target * + +echo you should upload $build_target to your OSS diff --git a/scripts/vm/ctl.sh b/scripts/vm/ctl.sh new file mode 100644 index 0000000..b40a88a --- /dev/null +++ b/scripts/vm/ctl.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +set -e + +bin_dir=`dirname $0` +bin_dir=`cd $bin_dir && echo $PWD` +echo bin_dir is $bin_dir + +SUPERVISORD_BIN=$bin_dir/supervisord +SUPERVISORD_CONF=${SUPERVISORD_BIN}.conf +SUPERVISORD_PID_FILE=$bin_dir/../run/supervisord.pid + + +function start() { + # TODO 防止重复启动 + if [ -e "$SUPERVISORD_PID_FILE" ]; then + pid=`cat $SUPERVISORD_PID_FILE` + if ps -p $pid >/dev/null; then + # PID 存在 + return + else + rm -f $SUPERVISORD_PID_FILE + # 文件存在但pid不存在, 启动 + $SUPERVISORD_BIN -c $SUPERVISORD_CONF -d + fi + else + # pid文件不存在, 启动 + $SUPERVISORD_BIN -c $SUPERVISORD_CONF -d + fi + + $SUPERVISORD_BIN -c $SUPERVISORD_CONF ctl start agent +} + +function stop() { + $SUPERVISORD_BIN -c $SUPERVISORD_CONF ctl stop agent + + out=`$SUPERVISORD_BIN -c $SUPERVISORD_CONF ctl shutdown` + if [ "Shut Down" != "$out" ] && [ "Hmmm! Something gone wrong?!" != "$out" ]; then + echo "stop supervisord: $out" + fi + + if [ -e "$SUPERVISORD_PID_FILE" ]; then + pid=`cat $SUPERVISORD_PID_FILE` + if ps -p $pid >/dev/null 2>&1; then + kill $pid || true + fi + rm $SUPERVISORD_PID_FILE + fi + + pid=`ps aux | grep $SUPERVISORD_BIN | grep -v grep | awk '{ print $2 }'` + if [ -n "$pid" ]; then + kill $pid || true + fi +} + +function restart() { + $SUPERVISORD_BIN -c $SUPERVISORD_CONF ctl restart agent +} + +function status() { + $SUPERVISORD_BIN -c $SUPERVISORD_CONF ctl status agent +} + +case "$1" in +start) + start +;; +stop) + stop +;; +restart) + restart +;; +status) + status +;; +*) + echo 'usage: ctl.sh ' + exit 1 +;; +esac + diff --git a/scripts/vm/initd_holoinsight-agent.sh b/scripts/vm/initd_holoinsight-agent.sh new file mode 100644 index 0000000..8322293 --- /dev/null +++ b/scripts/vm/initd_holoinsight-agent.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -e + +# 开机自启动时会调用这个脚本 +exec /usr/local/holoinsight/agent/bin/ctl.sh "$@" diff --git a/scripts/vm/install.sh b/scripts/vm/install.sh new file mode 100755 index 0000000..74540b3 --- /dev/null +++ b/scripts/vm/install.sh @@ -0,0 +1,201 @@ +#!/usr/bin/env bash +set -e + +# 这个脚本是安装agent的脚本, 假设当前目录下有 +# - install +# - holoinsight-agent_linux-amd64_latest.tar.gz + +# usage: bash install -a ${应用名} -i ${apikey} -r ${Registry服务端地址} -g ${Gateway服务端地址} -t ${holoinsight-agent tar.gz 包的本地路径} -h ${指定一个agent的安装目录} -d ${如果安装时agent的安装目录已经存在, 是否先删除已存在的agent的安装目录} +# 参数解释: +# 参数 必填 解释 +# -a 否 应用名 +# -i 是 apikey +# -r 是 registry服务端地址 +# -g 是 gateway服务端地址 +# -t 是 agent安装包地址 +# -h 否 用于指定agent的安装目录(即 agent_home ) 如果不填, 默认会安装在 ./agent,即当前目录下 +# -d 否 如果 agent_home 已经存在, 是否先删除它 + +# 对于每个给定的环境而言, -r -g -t 是固定的 +# 安装和运行 agent 不需要root权限, 除非你需要采集 root 权限的数据 ( 比如 /root 目录下的日志 ). +# agent 总是以当前用户(执行启动脚本的用户)执行. + +agent_tar_url= +hi_app= +hi_apikey= +hi_registry_addr= +hi_gateway_addr= +hi_home= +# 如果为true则表示删除旧目录 +hi_delete_old= +debug_grpc_secure=true + +while getopts "a:i:r:g:t:h:ds:" OPT; do + case "$OPT" in + a) + hi_app=$OPTARG + ;; + i) + hi_apikey=$OPTARG + ;; + r) + hi_registry_addr=$OPTARG + ;; + g) + hi_gateway_addr=$OPTARG + ;; + t) + agent_tar_url=$OPTARG + ;; + h) + hi_home=$OPTARG + ;; + d) + hi_delete_old=1 + ;; + s) + debug_grpc_secure=$OPTARG + ;; + *) + echo "unknown opt $OPT" + exit 1 + ;; + esac +done + +echo "install params:" +echo "hi_app=$hi_app" +echo "hi_apikey=$hi_apikey" +echo "hi_registry_addr=$hi_registry_addr" +echo "hi_gateway_addr=$hi_gateway_addr" +echo "hi_home=$hi_home" +echo "hi_delete_old=$hi_delete_old" +echo "debug_grpc_secure=$debug_grpc_secure" + +echo + +if [ -z "$hi_apikey" ]; then + echo 'apikey is empty' + exit 1 +fi + +if [ -z "$hi_registry_addr" ]; then + echo 'registry addr is empty' + exit 1 +fi + +if [ -z "$hi_gateway_addr" ]; then + echo 'gateway addr is empty' + exit 1 +fi + + +if [ -z "$agent_tar_url" ]; then + echo 'agent tar url is empty' + exit 1 +fi + +# 以当前目录作为 $agent_home + +cwd=$PWD +echo "current working directory $cwd" + +if [ -n "$hi_home" ]; then + agent_home=$hi_home +else + agent_home=$PWD/agent + echo "hi_home is empty, use $agent_home as agent home" +fi + +echo "use agent home $agent_home" + +# 先停止 (如果存在的话) +if [ -e "$agent_home/bin/ctl.sh" ]; then + echo "find $agent_home/bin/ctl.sh, stop agent first" + $agent_home/bin/ctl.sh stop +fi + +if ps aux | grep $agent_home/bin | grep -v grep >/dev/null; then + echo "kill processes related to $agent_home/bin" + for pid in `ps aux | grep $agent_home/bin | grep -v grep | awk '{ print $2 }'`; do + echo "$$ $pid" + if [ "$$" != "$pid" ]; then + echo kill $pid `cat /proc/$pid/cmdline | tr '\0' ' '` + kill $pid + fi + done +fi + +# 卸载 (如果存在的话) +if [ "$hi_delete_old" = "1" ] && [ -e "$agent_home" ]; then + echo "remove directory $agent_home" + rm -rf $agent_home +fi + +# 开始安装 + +echo "holoinsight agent will be install on $agent_home" + +# 入参会有一个url, 下载 tar.gz 包到临时目录 +tmpdir=`mktemp -d` +echo "make temp dir $tmpdir" + +echo "download agent tar from $agent_tar_url to $tmpdir" + +if [ -e "$agent_tar_url" ]; then + echo "use local agent tar /$agent_tar_url" +else + if command -v wget >/dev/null; then + wget -O $tmpdir/holoinsight-agent_linux-amd64.tar.gz $agent_tar_url + elif command -v curl >/dev/null; then + curl -o $tmpdir/holoinsight-agent_linux-amd64.tar.gz $agent_tar_url + else + echo "there is no 'wget' or 'curl' in PATH" + exit 1 + fi +fi + +# 解压到标准目录, 需要sudo (如何检查自己能否无密码sudo?) +mkdir -p $agent_home + +echo "unarchive agent tar to $agent_home/.." +tar -zxf $tmpdir/holoinsight-agent_linux-amd64.tar.gz -C $agent_home + +# 此时的目录结构 /usr/local/holoinsight/agent/... + +# 修复权限 +chmod a+x $agent_home/bin/* + +# 初始化 agent.yaml +echo "apikey: \"$hi_apikey\" +app: \"$hi_app\" +registry: + addr: $hi_registry_addr + secure: $debug_grpc_secure +gateway: + addr: $hi_gateway_addr + secure: $debug_grpc_secure +" > ${agent_home}/conf/agent.yaml + +echo +echo "conf/agent.yaml:" +cat ${agent_home}/conf/agent.yaml +echo + +# 建立相关目录 +mkdir -p $agent_home/{logs,run} + +chmod a+x $agent_home/bin/initd_holoinsight-agent.sh + +# 更新 supervisor 地址 +sed -i s@/usr/local/holoinsight/agent@$agent_home@g $agent_home/bin/supervisord.conf +sed -i s@/usr/local/holoinsight/agent@$agent_home@g $agent_home/bin/agent.ini + +# 启动agent +echo "start agent" +$agent_home/bin/ctl.sh start + +sleep 1 + +echo "agent status" +$agent_home/bin/ctl.sh status diff --git a/scripts/vm/supervisord b/scripts/vm/supervisord new file mode 100755 index 0000000..2746ae6 Binary files /dev/null and b/scripts/vm/supervisord differ diff --git a/scripts/vm/supervisord.conf b/scripts/vm/supervisord.conf new file mode 100644 index 0000000..4911454 --- /dev/null +++ b/scripts/vm/supervisord.conf @@ -0,0 +1,32 @@ +#[unix_http_server] +#file=/var/run/holoinsightsupervisord.sock +#chmod=not support +#chown=not support + +[inet_http_server] +port=127.0.0.1:9001 + +[supervisord] +logfile=/usr/local/holoinsight/agent/logs/supervisord.log +logfileMaxbytes=50MB +logfileBackups=10 +loglevel=info +pidfile=/usr/local/holoinsight/agent/run/supervisord.pid +#umask=not support +#nodaemon=not support +#minfds=not support +#minprocs=not support +#nocleanup=not support +#childlogdir=not support +#user=not support +#directory=not support +#strip_ansi=not support +#environment=not support +identifier=supervisor + +[include] +files=/usr/local/holoinsight/agent/bin/agent.ini + +[supervisorctl] +serverurl=http://127.0.0.1:9001 +#serverurl=unix:///tmp/supervisord.sock diff --git a/scripts/vm/uninstall.sh b/scripts/vm/uninstall.sh new file mode 100644 index 0000000..221a018 --- /dev/null +++ b/scripts/vm/uninstall.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +set -e + +bin_dir=`dirname $0 | xargs realpath` +echo bin dir $bin_dir + +agent_home=` realpath $bin_dir/.. ` +echo agent home $agent_home + +if [ -e "$bin_dir/ctl.sh" ]; then + echo "find $bin_dir/ctl.sh, $bin_dir/ctl.sh stop first" + $bin_dir/ctl.sh stop +fi + +rm -rf $agent_home diff --git a/scripts/vm/upload-release.sh b/scripts/vm/upload-release.sh new file mode 100755 index 0000000..8035f11 --- /dev/null +++ b/scripts/vm/upload-release.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +set -e + +# 这个脚本发版本到线上, 必须非常小心 + +version="$1" +if [ -z "$version" ]; then + echo 'usage: upload.sh ' + exit 1 +fi + +script_dir=`dirname $0 | xargs realpath` +project_root=`realpath $script_dir/../..` + +echo "version is $version" + +file="$project_root/build/linux-amd64/holoinsight-agent_linux-amd64_${version}.tar.gz" +tar=`basename $file` + +# 运行手动export +#ak=`` +#sk=`` +#ossUrl=`` +#ossBucket=`` + +# 上传安装脚本 +echo '[upload install.sh and package to OSS]' +ossutil -e $ossUrl -i $ak -k $sk cp -f $script_dir/install.sh oss://$ossBucket/agent/install +echo "upload $file to oss" + +oss_target="oss://$ossBucket/agent/$tar" + +# 检查一下文件是否存在, 如果存在别随便覆盖!!! +echo stat $oss_target + +# 如果为true则表示要强制覆盖, 这个比较危险, 对于已经打包的, 我们最好别覆盖了 +OVERWRITE="0" + +if ! ossutil -e $ossUrl -i $ak -k $sk stat $oss_target; then + ossutil -e $ossUrl -i $ak -k $sk cp -f $file $oss_target +elif [ "$OVERWRITE" = "1" ]; then + echo "warning: overwrite $oss_target already exists" + ossutil -e $ossUrl -i $ak -k $sk cp -f $file $oss_target +else + # 多打印几行 引起注意 + for i in `seq 10`; do + echo "warning: $oss_target already exists" + done + exit 1 +fi + +ossutil -e $ossUrl -i $ak -k $sk cp -f $file oss://$ossBucket/agent/$tar +ossutil -e $ossUrl -i $ak -k $sk cp -f $file oss://$ossBucket/agent/holoinsight-agent_linux-amd64_latest.tar.gz +echo "you can download from oss" diff --git a/scripts/vm/upload.sh b/scripts/vm/upload.sh new file mode 100755 index 0000000..1cd6709 --- /dev/null +++ b/scripts/vm/upload.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +set -e + +# 普通的upload 仅 upload 到 snapshot 版本, 防止开发人员意外操作覆盖线上版本 + +script_dir=`dirname $0 | xargs realpath` +project_root=`realpath $script_dir/../..` + +version=` cat $project_root/VERSION ` + +echo "version is $version" + +file="$project_root/build/linux-amd64/holoinsight-agent_linux-amd64_${version}.tar.gz" +tar=`basename $file` + +# 运行手动export +#ak=`` +#sk=`` +#ossHost=`` +#ossBucket=`` + +# 上传安装脚本 +echo '[snapshot][upload install.sh and package to OSS]' +ossutil -e $ossHost -i $ak -k $sk cp -f $script_dir/install.sh oss://$ossBucket/agent/install-snapshot +echo "[snapshot]upload $file to oss" + +ossutil -e $ossHost -i $ak -k $sk cp -f $file oss://$ossBucket/agent/holoinsight-agent_linux-amd64_snapshot.tar.gz +echo "[snapshot]you can download from oss"