在实际工作中,我们把树莓派作为一个agent放在用户那边,在里面启动一些服务,进行用户业务数据dicom文件的存储,并转发到阿里云oss上面,起到一个gateway的作用。
但是,放出去的盒子,就如同魔术师手中的飞镖,发出去,怎么还能继续掌控,这很重要。
在实际应用场景中,对于这种agent,我们还需要继续维护,升级,并定期进行一些业务数据的更新。
那么如何进行远程操控以及管理呢?
我们目前采用下面的方式处理,虽然我认为其实有更好的方式,但目前暂先就如此,毕竟我们都知道,稳定大于一切。
1.在每个agent 树莓派盒子上面,通过 sshkey-gen生成自己的公钥私钥;
2.将每个agent的私钥存储在ssh在管控/调度机器(相当于跳板机)的~/.ssh/authorized_keys中,并保存;
3.在自己的应用程序中,通过MQ下发消息,在树莓派盒子上面,用 ssh -o "StrictHostKeyChecking no" -CNfR 10000:127.0.0.1:22 boxagent@smith.lifeccp.com 驱动盒子发起ssh请求;或者
4.在跳板机上面,使用 ssh -p 10140 lifeccp@localhost 命令来连接树莓派盒子;
5.由于ssh连接存在时效性,所以,退出时候记得exit一下,否则可能下次连接时候存在问题导致连接不上。
6.如果连接不上,可以通过如下命令kill旧的进程,然后重新发起连接请求;
kill -9 `ps -ef |grep ssh |grep StrictHostKeyChecking | awk '{print $2}'`
或者 kill -9 `pgrep -f StrictHostKeyChecking`
我这边成功的结果,如下图:
另外,应用更新其实很简单了,在agent盒子那边写个脚本,执行一下就行了。update.sh脚本内容见下面:
#!/bin/sh
export JAVA_HOME=/usr/lib/jdk1.8.0_45
export CALSSPATH=$JAVA_HOME/lib/*.*
export PATH=$PATH:$JAVA_HOME/bin
#config
APP_NAME=osprey
USER_HOME=/home/lifeccp/webapp
RELEASE_SRC="boxagent@smith.lifeccp.com:/home/boxagent/boxrelease/osprey/*.zip"
#function
say() {
echo -e "[Osprey Deploy] $0: $1"
}
fetch_artifacts() {
DEST="$USER_HOME/incoming/${APP_NAME}/"
[ -d ${DEST} ] || mkdir -p ${DEST}
say "Fetching artifacts from $ARTIFACTS_SRC to ${DEST}"
scp ${RELEASE_SRC} ${DEST}
say "Unzip the new war"
cd ${DEST} && unzip -qo *.zip
if [ "$?" = "0" ]; then
say "\033[32m -=Fetch artifacts finished=- \033[0m"
else
say "\033[31m -=Fetch artifacts failed=- \033[0m"
exit 1
fi
}
stop_app() {
say "Stopping [${APP_NAME}]"
pkill -f ${APP_NAME}
osprey_pid=$(pgrep -f ${APP_NAME})
if [ "${osprey_pid}" = "" ]; then
say "\033[32m -=Stop ${APP_NAME} finished=- \033[0m"
else
say "\033[32m -=Stop ${APP_NAME} failed=- \033[0m"
kill -9 "${osprey_pid}"
say "\033[32m -=Kill -9 ${APP_NAME}=- \033[0m"
fi
}
backup_app() {
DEST="$USER_HOME/archive/${APP_NAME}$(date '+%Y%m%d%H%M')/"
say "make backup dir ${DEST}"
mkdir -p "${DEST}"
say "Copying backup app and config files."
cp -r ${USER_HOME}/${APP_NAME}/* ${DEST}
say "\033[32m -=Backup ${APP_NAME} finished=- \033[0m"
}
deploy_app() {
DEST="$USER_HOME/incoming/${APP_NAME}/"
say "Deploy ${APP_NAME}"
say "Remove old app"
rm -rf $USER_HOME/$APP_NAME/osprey*.release.jar
rm -rf $USER_HOME/$APP_NAME/osprey*.release.jar.original
say "Copying new app"
# cp $USER_HOME/incoming/$APP_NAME/*.zip $USER_HOME/$APP_NAME/
cp ${DEST}/osprey*.release.jar $USER_HOME/$APP_NAME/osprey.release.jar
cp ${DEST}/osprey*.release.jar.original $USER_HOME/$APP_NAME/
if [ "$?" = "0" ]; then
say "\033[32m -=Deploy ${APP_NAME} finished=- \033[0m"
else
say "\033[31m -=Deploy ${APP_NAME} failed=- \033[0m"
exit 1
fi
}
clear_app() {
say "clear $USER_HOME/incoming/${APP_NAME}/"
rm -rf $USER_HOME/incoming/${APP_NAME}/*
}
start_app() {
say "Start [${APP_NAME}]"
sudo reboot
if [ "$?" = "0" ]; then
say "\033[32m -=Start reboot finished=- \033[0m"
else
say "\033[31m -=Start reboot failed=- \033[0m"
exit 1
fi
}
publish() {
fetch_artifacts
stop_app
backup_app
deploy_app
clear_app
start_app
}
#### do it ###
publish
简单说明一下上面的脚本:
1.获取新的安装包;
2.停止当前应用;
3.备份当前应用;
4.部署当前应用;
5.清理安装包文件;
6.启动服务应用;
7.发布部署;
新更新:
在生产环境测的时候,却发现不同环境还稍微有点小的问题。
因为旧的不同版本略有小的差异导致;之后经过优化,重新测试OK。
优化点主要是这两点,另外有点其他的小调整:
1.使用ln软连接方式,而不是mv版本号;
2.修改stop服务步骤,kill进程有效性;
在这里,重新补充一下最新的Scripts,如下:
boxagent@iZ2zeed4f6hbydt2078ddcZ:~$ cat boxrelease/script/upgrade_osprey.sh
#!/bin/sh
#config
APP_NAME=osprey
USER_HOME=/home/lifeccp/webapp
RELEASE_SRC="boxagent@smith.lifeccp.com:/home/boxagent/boxrelease/osprey/*.zip"
#function
say() {
echo -e "[Osprey Deploy] $0: $1"
}
fetch_artifacts() {
DEST="$USER_HOME/incoming/${APP_NAME}/"
[ -d ${DEST} ] || mkdir -p ${DEST}
say "Fetching artifacts from $ARTIFACTS_SRC to ${DEST}"
scp ${RELEASE_SRC} ${DEST}
say "Unzip the new war"
cd ${DEST} && unzip -qo *.zip
say "Change startup started"
sed -i 8d ${USER_HOME}/${APP_NAME}/startup
sed -i '8i MAIN_JAR=osprey.release.jar' ${USER_HOME}/${APP_NAME}/startup
say "Change startup finished"
if [ "$?" = "0" ]; then
say "\033[32m -=Fetch artifacts finished=- \033[0m"
else
say "\033[31m -=Fetch artifacts failed=- \033[0m"
exit 1
fi
}
stop_app() {
say "Stopping [${APP_NAME}]"
pkill -f ${APP_NAME}
osprey_pid=$(pgrep -f ${APP_NAME})
if [ "${osprey_pid}" = "" ]; then
say "\033[32m -=Stop ${APP_NAME} finished=- \033[0m"
else
say "\033[32m -=Stop ${APP_NAME} failed=- \033[0m"
kill -9 "${osprey_pid}"
kill -9 `ps aux | grep osprey | awk '{print $2}'`
say "\033[32m -=Kill -9 ${APP_NAME}=- \033[0m"
fi
say "Stopping [${APP_NAME}] finished"
}
backup_app() {
DEST="$USER_HOME/archive/${APP_NAME}$(date '+%Y%m%d%H%M')/"
say "make backup dir ${DEST}"
mkdir -p "${DEST}"
cp -r ${USER_HOME}/${APP_NAME}/* ${DEST}
say "\033[32m -=Backup ${APP_NAME} finished=- \033[0m"
}
deploy_app() {
DEST="$USER_HOME/incoming/${APP_NAME}"
say "Deploy ${APP_NAME}"
say "Remove old app"
rm -rf $USER_HOME/$APP_NAME/osprey*.release.jar
rm -rf $USER_HOME/$APP_NAME/osprey*.release.jar.original
say "Copying new app start"
cp ${DEST}/osprey*.release.jar $USER_HOME/$APP_NAME/
cp ${DEST}/osprey*.release.jar.original $USER_HOME/$APP_NAME/
say "Copying new app finished"
say "Make app link start"
ln -s /home/lifeccp/webapp/osprey/osprey-*.release.jar /home/lifeccp/webapp/osprey/osprey.release.jar
say "Make app link finished"
if [ "$?" = "0" ]; then
say "\033[32m -=Deploy ${APP_NAME} finished=- \033[0m"
else
say "\033[31m -=Deploy ${APP_NAME} failed=- \033[0m"
exit 1
fi
}
clear() {
say "clear $USER_HOME/incoming/${APP_NAME}/"
rm -rf $USER_HOME/incoming/${APP_NAME}/*
}
start_app() {
say "Start [${APP_NAME}]"
sudo reboot
if [ "$?" = "0" ]; then
say "\033[32m -=Start reboot finished=- \033[0m"
else
say "\033[31m -=Start reboot failed=- \033[0m"
exit 1
fi
}
publish() {
say "********Step1 start..."
fetch_artifacts
say "********Step1 finished...\n\n********Step2 start..."
stop_app
say "********Step2 finished...\n\n********Step3 start..."
backup_app
say "********Step3 finished...\n\n********Step4 start..."
deploy_app
say "********Step4 finished...\n\n********Step5 start..."
clear
say "********Step5 finished...\n\n********Step6 start..."
start_app
say "********Step6 finished...Upgrade finished!"
}
#### do it ###
publish
脚本使用方法:
1.使用ssh方式登录agent被控机;
2.执行如下命令;
scp boxagent@smith.lifeccp.com:/home/boxagent/boxrelease/script/upgrade_osprey.sh .
3.输入密码;
4.等待reboot;
升级完成之后,可以自己查看服务是否正常,一般情况下,没什么问题。