Skip to content

Commit f491cc2

Browse files
authored
feat: release for v0.1.15. (#9)
Main feature includes: - Support agent smooth upgrade. - Remove dependency of sudo. - Use #[tokio::test] for some test. - Store ps1 with gbk code.
1 parent 31e02db commit f491cc2

19 files changed

+713
-746
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# Changelog
22

33
All notable changes to this project will be documented in this file.
4+
## [0.1.15] - 2021-12-05
5+
6+
### Changed
7+
8+
- Support agent smooth upgrade.
9+
- Remove sudo dependency.
10+
- Fix powershell script gbk error.
11+
412
## [0.1.14] - 2021-12-02
513

614
### Changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tat_agent"
3-
version = "0.1.14"
3+
version = "0.1.15"
44
edition = "2018"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ AGENT_IMG=$(DOCKER_REGISTRY)/tat-develop/tat_agent
99

1010
# test all case
1111
lib-test:
12-
cargo test --lib -- --nocapture --skip ontime --skip executor::proc::tests::test_shell_cmd_timeout
12+
cargo test --lib -- --nocapture --skip ontime
1313

1414
integration-test:
1515
cargo test --test http_test

install/install.bat

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ if not exist "C:\Program Files\qcloud\tat_agent\workdir" (
99
md "C:\Program Files\qcloud\tat_agent\workdir"
1010
)
1111

12-
1312
sc query tatsvc | find "STATE" >nul && sc stop tatsvc >nul ||^
1413
sc create tatsvc binPath= "C:\Program Files\qcloud\tat_agent\tat_agent.exe" start= auto
1514

install/install.sh

+119-80
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
#!/bin/bash
22
cd `dirname $0`
33

4-
need_restart=true
5-
if [ "only_update" = ""$1 ]; then
6-
need_restart=false
7-
fi
8-
94
PID_FILE="/var/run/tat_agent.pid"
105

116
# install the agent binary
@@ -14,38 +9,6 @@ PATH_DIR="/usr/sbin/"
149
TAT_AGENT="tat_agent"
1510
TAT_AGENT32="tat_agent32"
1611

17-
# if arch is 32bit and 32bit bin exists, rename `tat_agent32` to `tat_agent`
18-
machine=$(uname -m)
19-
if [ "$machine" != "x86_64" ] && [ -f "$TAT_AGENT32" ]; then
20-
mv ${TAT_AGENT} -f ${TAT_AGENT}64
21-
mv ${TAT_AGENT32} -f ${TAT_AGENT}
22-
fi
23-
24-
# check if agent runnable
25-
chmod +x ${TAT_AGENT}
26-
if ! ./${TAT_AGENT} -V; then
27-
echo "tat_agent not runnable, exit."
28-
exit 1
29-
fi
30-
31-
mkdir -p ${SERVICE_DIR}
32-
if [ $? -ne 0 ]; then
33-
# handle special case for CoreOS whose /usr is Read-only
34-
grep -q CoreOS /etc/os-release
35-
if [ $? -eq 0 ]; then
36-
SERVICE_DIR="/var/lib/qcloud/tat_agent/"
37-
mkdir -p ${SERVICE_DIR}
38-
PATH_DIR="/opt/bin/"
39-
sed -i 's/\/usr\/local\/qcloud/\/var\/lib\/qcloud/g' tat_agent.service tat_agent_service.conf tat_agent_service uninstall.sh
40-
sed -i 's/\/usr\/sbin/\/opt\/bin/g' uninstall.sh
41-
else
42-
echo 'Install fail, has no permission, may not root.'
43-
exit 1
44-
fi
45-
fi
46-
cp -f ${TAT_AGENT} ${SERVICE_DIR}
47-
ln -sf ${SERVICE_DIR}${TAT_AGENT} ${PATH_DIR}${TAT_AGENT}
48-
4912
has_systemd() {
5013
[[ `systemctl` =~ -\.mount ]] > /dev/null 2>&1 && return 0
5114
if systemctl 2>/dev/null | grep -e "-\.mount" > /dev/null 2>&1; then
@@ -74,46 +37,122 @@ has_upstart() {
7437
return 1
7538
}
7639

77-
if has_systemd; then
78-
echo "use systemd to manage service"
79-
SYSTEMD_DIR="/etc/systemd/system/"
80-
cp -f tat_agent.service ${SYSTEMD_DIR}
81-
systemctl daemon-reload
82-
systemctl enable tat_agent.service
83-
if test "${need_restart}" = true; then
84-
systemctl restart tat_agent.service
85-
fi
86-
elif has_upstart; then
87-
echo "use upstart(initctl) to manage service"
88-
cp -f tat_agent_service.conf /etc/init/
89-
if test "${need_restart}" = true; then
90-
initctl stop tat_agent_service
91-
initctl start tat_agent_service
92-
fi
93-
elif has_sysvinit; then
94-
cp -f tat_agent_service /etc/init.d/
95-
chmod 755 /etc/init.d/tat_agent_service
96-
/etc/init.d/tat_agent_service restart
97-
which chkconfig > /dev/null 2>&1
98-
if [ $? -eq 0 ]; then
99-
echo "use chkconfig to manage service"
100-
chkconfig --add tat_agent_service
101-
chkconfig tat_agent_service on
102-
else
103-
which update-rc.d > /dev/null 2>&1
104-
if [ $? -eq 0 ]; then
105-
echo "use update-rc.d to manage service"
106-
update-rc.d tat_agent_service defaults
107-
else
108-
echo "no proper daemon manager found, tat_agent can not auto start"
109-
fi
110-
fi
111-
else
112-
echo "no proper daemon manager found, tat_agent can not auto start"
113-
kill `cat ${PID_FILE}` > /dev/null 2>&1
114-
sleep 1
115-
rm -f ${PID_FILE}
116-
cd ${SERVICE_DIR}
117-
./${TAT_AGENT}
118-
echo "tat_agent started"
119-
fi
40+
install() {
41+
need_restart=$1
42+
43+
# if arch is 32bit and 32bit bin exists, rename `tat_agent32` to `tat_agent`
44+
machine=$(uname -m)
45+
if [ "$machine" != "x86_64" ] && [ -f "$TAT_AGENT32" ]; then
46+
mv ${TAT_AGENT} -f ${TAT_AGENT}64
47+
mv ${TAT_AGENT32} -f ${TAT_AGENT}
48+
fi
49+
50+
# check if agent runnable
51+
chmod +x ${TAT_AGENT}
52+
if ! ./${TAT_AGENT} -V; then
53+
echo "tat_agent not runnable, exit."
54+
exit 1
55+
fi
56+
57+
mkdir -p ${SERVICE_DIR}
58+
if [ $? -ne 0 ]; then
59+
# handle special case for CoreOS whose /usr is Read-only
60+
grep -q CoreOS /etc/os-release
61+
if [ $? -eq 0 ]; then
62+
SERVICE_DIR="/var/lib/qcloud/tat_agent/"
63+
mkdir -p ${SERVICE_DIR}
64+
PATH_DIR="/opt/bin/"
65+
sed -i 's/\/usr\/local\/qcloud/\/var\/lib\/qcloud/g' tat_agent.service tat_agent_service.conf tat_agent_service uninstall.sh
66+
sed -i 's/\/usr\/sbin/\/opt\/bin/g' uninstall.sh
67+
else
68+
echo 'Install fail, has no permission, may not root.'
69+
exit 1
70+
fi
71+
fi
72+
cp -f ${TAT_AGENT} ${SERVICE_DIR}
73+
ln -sf ${SERVICE_DIR}${TAT_AGENT} ${PATH_DIR}${TAT_AGENT}
74+
75+
if has_systemd; then
76+
echo "use systemd to manage service"
77+
SYSTEMD_DIR="/etc/systemd/system/"
78+
cp -f tat_agent.service ${SYSTEMD_DIR}
79+
systemctl daemon-reload
80+
systemctl enable tat_agent.service
81+
if test "${need_restart}" = true; then
82+
systemctl restart tat_agent.service
83+
fi
84+
elif has_upstart; then
85+
echo "use upstart(initctl) to manage service"
86+
cp -f tat_agent_service.conf /etc/init/
87+
if test "${need_restart}" = true; then
88+
initctl stop tat_agent_service
89+
initctl start tat_agent_service
90+
fi
91+
elif has_sysvinit; then
92+
cp -f tat_agent_service /etc/init.d/
93+
chmod 755 /etc/init.d/tat_agent_service
94+
# TODO: uncomment following code after 0.1.14 is released.
95+
# if test "${need_restart}" = true; then
96+
/etc/init.d/tat_agent_service restart
97+
# fi
98+
which chkconfig > /dev/null 2>&1
99+
if [ $? -eq 0 ]; then
100+
echo "use chkconfig to manage service"
101+
chkconfig --add tat_agent_service
102+
chkconfig tat_agent_service on
103+
else
104+
which update-rc.d > /dev/null 2>&1
105+
if [ $? -eq 0 ]; then
106+
echo "use update-rc.d to manage service"
107+
update-rc.d tat_agent_service defaults
108+
else
109+
echo "no proper daemon manager found, tat_agent can not auto start"
110+
fi
111+
fi
112+
else
113+
# TODO: uncomment following code after 0.1.14 is released.
114+
# if test "${need_restart}" = true; then
115+
echo "no proper daemon manager found, tat_agent can not auto start"
116+
PID=$(cat ${PID_FILE})
117+
kill ${PID} > /dev/null 2>&1
118+
sleep 0.1 || sleep 1
119+
rm -f ${PID_FILE}
120+
cd ${SERVICE_DIR}
121+
./${TAT_AGENT}
122+
echo "tat_agent started"
123+
# fi
124+
fi
125+
}
126+
127+
restart() {
128+
if has_systemd; then
129+
echo "use systemd to manage service"
130+
systemctl restart tat_agent.service
131+
elif has_upstart; then
132+
echo "use upstart(initctl) to manage service"
133+
initctl stop tat_agent_service
134+
initctl start tat_agent_service
135+
elif has_sysvinit; then
136+
/etc/init.d/tat_agent_service restart
137+
else
138+
PID=$(cat ${PID_FILE})
139+
kill ${PID} > /dev/null 2>&1
140+
sleep 0.1 || sleep 1
141+
rm -f ${PID_FILE}
142+
cd ${SERVICE_DIR}
143+
./${TAT_AGENT}
144+
echo "tat_agent started"
145+
fi
146+
}
147+
148+
case $1 in
149+
only_update)
150+
install false
151+
;;
152+
restart)
153+
restart
154+
;;
155+
*)
156+
install true
157+
;;
158+
esac

install/self_update.bat

-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@ set "temp=%temp:.=%"
55
set newname=temp_%temp%.exe
66
rename "C:\Program Files\qcloud\tat_agent\tat_agent.exe" %newname%
77
copy /Y tat_agent.exe "C:\Program Files\qcloud\tat_agent\"
8-

src/common/consts.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@ cfg_if::cfg_if! {
2424
pub const TASK_LOG_PATH: &str = "/tmp/tat_agent/logs/";
2525
pub const SELF_UPDATE_PATH: &str = "/tmp/tat_agent/self_update/";
2626
pub const SELF_UPDATE_SCRIPT: &str = "self_update.sh";
27+
pub const INSTALL_SCRIPT: &str = "install.sh";
2728
pub const AGENT_DEFAULT_WORK_DIRECTORY: &str = "/root";
28-
29-
// pre_exec fn name for cmd
30-
pub const OWN_PROCESS_GROUP: &str = "OWN_PROCESS_GROUP";
31-
pub const DUP2_1_2: &str = "DUP2_1_2";
32-
3329
pub const FILE_EXECUTE_PERMISSION_MODE: u32 = 0o755;
3430
pub const PIPE_BUF_DEFAULT_SIZE: usize = 64 * 4096;
3531
} else if #[cfg(windows)] {

src/daemonizer/windows.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ fn clean_update_files() {
111111
})
112112
}
113113

114+
fn set_ps1_policy() {
115+
let mut cmd = std::process::Command::new("PowerShell.exe");
116+
cmd.args(&["set-ExecutionPolicy", "RemoteSigned"]);
117+
let mut child = cmd.spawn().unwrap();
118+
child.wait().map_err(|_| error!("set_ps1_policy fail")).ok();
119+
}
120+
121+
114122
fn set_work_dir() {
115123
let exe_path = env::current_exe().unwrap();
116124
let work_dir = exe_path.parent().unwrap();
@@ -150,7 +158,7 @@ where
150158
pub fn daemonize(entry: fn()) {
151159
clean_update_files();
152160
set_work_dir();
153-
161+
set_ps1_policy();
154162
if already_start() {
155163
std::process::exit(183);
156164
}

src/executor/powershell_command.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@ impl PowerShellCommand {
6464
}
6565
}
6666

67-
async fn set_ps1_policy(&self) {
68-
let mut cmd = Command::new("PowerShell.exe");
69-
cmd.args(&["set-ExecutionPolicy", "RemoteSigned"]);
70-
let child = cmd.spawn().unwrap();
71-
child.await.map_err(|_| error!("set_ps1_policy fail")).ok();
72-
}
73-
7467
fn work_dir_check(&self) -> Result<(), String> {
7568
if !wow64_disable_exc(|| Path::new(self.base.work_dir.as_str()).exists()) {
7669
let ret = format!(
@@ -115,6 +108,7 @@ impl MyCommand for PowerShellCommand {
115108
3. support set process group.
116109
*/
117110
async fn run(&mut self) -> Result<(), String> {
111+
info!("=>PowerShellCommand::run()");
118112
// store path check
119113
self.store_path_check()?;
120114

@@ -123,9 +117,6 @@ impl MyCommand for PowerShellCommand {
123117

124118
let log_file = self.open_log_file()?;
125119

126-
// set policy
127-
self.set_ps1_policy().await;
128-
129120
// create pipe
130121
let (our_pipe, their_pipe) = anon_pipe(true)?;
131122

@@ -141,6 +132,7 @@ impl MyCommand for PowerShellCommand {
141132

142133
*self.base.pid.lock().unwrap() = Some(child.id());
143134
let base = self.base.clone();
135+
info!("=>PowerShellCommand::tokio::spawn");
144136
// async read output.
145137
tokio::spawn(async move {
146138
base.add_timeout_timer();

0 commit comments

Comments
 (0)