Skip to content

Commit

Permalink
feat: release for v0.1.15. (#9)
Browse files Browse the repository at this point in the history
Main feature includes:
  - Support agent smooth upgrade.
  - Remove dependency of sudo.
  - Use #[tokio::test] for some test.
  - Store ps1 with gbk code.
  • Loading branch information
hustclf authored Dec 16, 2021
1 parent 31e02db commit f491cc2
Show file tree
Hide file tree
Showing 19 changed files with 713 additions and 746 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Changelog

All notable changes to this project will be documented in this file.
## [0.1.15] - 2021-12-05

### Changed

- Support agent smooth upgrade.
- Remove sudo dependency.
- Fix powershell script gbk error.

## [0.1.14] - 2021-12-02

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tat_agent"
version = "0.1.14"
version = "0.1.15"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ AGENT_IMG=$(DOCKER_REGISTRY)/tat-develop/tat_agent

# test all case
lib-test:
cargo test --lib -- --nocapture --skip ontime --skip executor::proc::tests::test_shell_cmd_timeout
cargo test --lib -- --nocapture --skip ontime

integration-test:
cargo test --test http_test
Expand Down
1 change: 0 additions & 1 deletion install/install.bat
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ if not exist "C:\Program Files\qcloud\tat_agent\workdir" (
md "C:\Program Files\qcloud\tat_agent\workdir"
)


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

Expand Down
199 changes: 119 additions & 80 deletions install/install.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#!/bin/bash
cd `dirname $0`

need_restart=true
if [ "only_update" = ""$1 ]; then
need_restart=false
fi

PID_FILE="/var/run/tat_agent.pid"

# install the agent binary
Expand All @@ -14,38 +9,6 @@ PATH_DIR="/usr/sbin/"
TAT_AGENT="tat_agent"
TAT_AGENT32="tat_agent32"

# if arch is 32bit and 32bit bin exists, rename `tat_agent32` to `tat_agent`
machine=$(uname -m)
if [ "$machine" != "x86_64" ] && [ -f "$TAT_AGENT32" ]; then
mv ${TAT_AGENT} -f ${TAT_AGENT}64
mv ${TAT_AGENT32} -f ${TAT_AGENT}
fi

# check if agent runnable
chmod +x ${TAT_AGENT}
if ! ./${TAT_AGENT} -V; then
echo "tat_agent not runnable, exit."
exit 1
fi

mkdir -p ${SERVICE_DIR}
if [ $? -ne 0 ]; then
# handle special case for CoreOS whose /usr is Read-only
grep -q CoreOS /etc/os-release
if [ $? -eq 0 ]; then
SERVICE_DIR="/var/lib/qcloud/tat_agent/"
mkdir -p ${SERVICE_DIR}
PATH_DIR="/opt/bin/"
sed -i 's/\/usr\/local\/qcloud/\/var\/lib\/qcloud/g' tat_agent.service tat_agent_service.conf tat_agent_service uninstall.sh
sed -i 's/\/usr\/sbin/\/opt\/bin/g' uninstall.sh
else
echo 'Install fail, has no permission, may not root.'
exit 1
fi
fi
cp -f ${TAT_AGENT} ${SERVICE_DIR}
ln -sf ${SERVICE_DIR}${TAT_AGENT} ${PATH_DIR}${TAT_AGENT}

has_systemd() {
[[ `systemctl` =~ -\.mount ]] > /dev/null 2>&1 && return 0
if systemctl 2>/dev/null | grep -e "-\.mount" > /dev/null 2>&1; then
Expand Down Expand Up @@ -74,46 +37,122 @@ has_upstart() {
return 1
}

if has_systemd; then
echo "use systemd to manage service"
SYSTEMD_DIR="/etc/systemd/system/"
cp -f tat_agent.service ${SYSTEMD_DIR}
systemctl daemon-reload
systemctl enable tat_agent.service
if test "${need_restart}" = true; then
systemctl restart tat_agent.service
fi
elif has_upstart; then
echo "use upstart(initctl) to manage service"
cp -f tat_agent_service.conf /etc/init/
if test "${need_restart}" = true; then
initctl stop tat_agent_service
initctl start tat_agent_service
fi
elif has_sysvinit; then
cp -f tat_agent_service /etc/init.d/
chmod 755 /etc/init.d/tat_agent_service
/etc/init.d/tat_agent_service restart
which chkconfig > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "use chkconfig to manage service"
chkconfig --add tat_agent_service
chkconfig tat_agent_service on
else
which update-rc.d > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "use update-rc.d to manage service"
update-rc.d tat_agent_service defaults
else
echo "no proper daemon manager found, tat_agent can not auto start"
fi
fi
else
echo "no proper daemon manager found, tat_agent can not auto start"
kill `cat ${PID_FILE}` > /dev/null 2>&1
sleep 1
rm -f ${PID_FILE}
cd ${SERVICE_DIR}
./${TAT_AGENT}
echo "tat_agent started"
fi
install() {
need_restart=$1

# if arch is 32bit and 32bit bin exists, rename `tat_agent32` to `tat_agent`
machine=$(uname -m)
if [ "$machine" != "x86_64" ] && [ -f "$TAT_AGENT32" ]; then
mv ${TAT_AGENT} -f ${TAT_AGENT}64
mv ${TAT_AGENT32} -f ${TAT_AGENT}
fi

# check if agent runnable
chmod +x ${TAT_AGENT}
if ! ./${TAT_AGENT} -V; then
echo "tat_agent not runnable, exit."
exit 1
fi

mkdir -p ${SERVICE_DIR}
if [ $? -ne 0 ]; then
# handle special case for CoreOS whose /usr is Read-only
grep -q CoreOS /etc/os-release
if [ $? -eq 0 ]; then
SERVICE_DIR="/var/lib/qcloud/tat_agent/"
mkdir -p ${SERVICE_DIR}
PATH_DIR="/opt/bin/"
sed -i 's/\/usr\/local\/qcloud/\/var\/lib\/qcloud/g' tat_agent.service tat_agent_service.conf tat_agent_service uninstall.sh
sed -i 's/\/usr\/sbin/\/opt\/bin/g' uninstall.sh
else
echo 'Install fail, has no permission, may not root.'
exit 1
fi
fi
cp -f ${TAT_AGENT} ${SERVICE_DIR}
ln -sf ${SERVICE_DIR}${TAT_AGENT} ${PATH_DIR}${TAT_AGENT}

if has_systemd; then
echo "use systemd to manage service"
SYSTEMD_DIR="/etc/systemd/system/"
cp -f tat_agent.service ${SYSTEMD_DIR}
systemctl daemon-reload
systemctl enable tat_agent.service
if test "${need_restart}" = true; then
systemctl restart tat_agent.service
fi
elif has_upstart; then
echo "use upstart(initctl) to manage service"
cp -f tat_agent_service.conf /etc/init/
if test "${need_restart}" = true; then
initctl stop tat_agent_service
initctl start tat_agent_service
fi
elif has_sysvinit; then
cp -f tat_agent_service /etc/init.d/
chmod 755 /etc/init.d/tat_agent_service
# TODO: uncomment following code after 0.1.14 is released.
# if test "${need_restart}" = true; then
/etc/init.d/tat_agent_service restart
# fi
which chkconfig > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "use chkconfig to manage service"
chkconfig --add tat_agent_service
chkconfig tat_agent_service on
else
which update-rc.d > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "use update-rc.d to manage service"
update-rc.d tat_agent_service defaults
else
echo "no proper daemon manager found, tat_agent can not auto start"
fi
fi
else
# TODO: uncomment following code after 0.1.14 is released.
# if test "${need_restart}" = true; then
echo "no proper daemon manager found, tat_agent can not auto start"
PID=$(cat ${PID_FILE})
kill ${PID} > /dev/null 2>&1
sleep 0.1 || sleep 1
rm -f ${PID_FILE}
cd ${SERVICE_DIR}
./${TAT_AGENT}
echo "tat_agent started"
# fi
fi
}

restart() {
if has_systemd; then
echo "use systemd to manage service"
systemctl restart tat_agent.service
elif has_upstart; then
echo "use upstart(initctl) to manage service"
initctl stop tat_agent_service
initctl start tat_agent_service
elif has_sysvinit; then
/etc/init.d/tat_agent_service restart
else
PID=$(cat ${PID_FILE})
kill ${PID} > /dev/null 2>&1
sleep 0.1 || sleep 1
rm -f ${PID_FILE}
cd ${SERVICE_DIR}
./${TAT_AGENT}
echo "tat_agent started"
fi
}

case $1 in
only_update)
install false
;;
restart)
restart
;;
*)
install true
;;
esac
1 change: 0 additions & 1 deletion install/self_update.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ set "temp=%temp:.=%"
set newname=temp_%temp%.exe
rename "C:\Program Files\qcloud\tat_agent\tat_agent.exe" %newname%
copy /Y tat_agent.exe "C:\Program Files\qcloud\tat_agent\"

6 changes: 1 addition & 5 deletions src/common/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ cfg_if::cfg_if! {
pub const TASK_LOG_PATH: &str = "/tmp/tat_agent/logs/";
pub const SELF_UPDATE_PATH: &str = "/tmp/tat_agent/self_update/";
pub const SELF_UPDATE_SCRIPT: &str = "self_update.sh";
pub const INSTALL_SCRIPT: &str = "install.sh";
pub const AGENT_DEFAULT_WORK_DIRECTORY: &str = "/root";

// pre_exec fn name for cmd
pub const OWN_PROCESS_GROUP: &str = "OWN_PROCESS_GROUP";
pub const DUP2_1_2: &str = "DUP2_1_2";

pub const FILE_EXECUTE_PERMISSION_MODE: u32 = 0o755;
pub const PIPE_BUF_DEFAULT_SIZE: usize = 64 * 4096;
} else if #[cfg(windows)] {
Expand Down
10 changes: 9 additions & 1 deletion src/daemonizer/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ fn clean_update_files() {
})
}

fn set_ps1_policy() {
let mut cmd = std::process::Command::new("PowerShell.exe");
cmd.args(&["set-ExecutionPolicy", "RemoteSigned"]);
let mut child = cmd.spawn().unwrap();
child.wait().map_err(|_| error!("set_ps1_policy fail")).ok();
}


fn set_work_dir() {
let exe_path = env::current_exe().unwrap();
let work_dir = exe_path.parent().unwrap();
Expand Down Expand Up @@ -150,7 +158,7 @@ where
pub fn daemonize(entry: fn()) {
clean_update_files();
set_work_dir();

set_ps1_policy();
if already_start() {
std::process::exit(183);
}
Expand Down
12 changes: 2 additions & 10 deletions src/executor/powershell_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,6 @@ impl PowerShellCommand {
}
}

async fn set_ps1_policy(&self) {
let mut cmd = Command::new("PowerShell.exe");
cmd.args(&["set-ExecutionPolicy", "RemoteSigned"]);
let child = cmd.spawn().unwrap();
child.await.map_err(|_| error!("set_ps1_policy fail")).ok();
}

fn work_dir_check(&self) -> Result<(), String> {
if !wow64_disable_exc(|| Path::new(self.base.work_dir.as_str()).exists()) {
let ret = format!(
Expand Down Expand Up @@ -115,6 +108,7 @@ impl MyCommand for PowerShellCommand {
3. support set process group.
*/
async fn run(&mut self) -> Result<(), String> {
info!("=>PowerShellCommand::run()");
// store path check
self.store_path_check()?;

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

let log_file = self.open_log_file()?;

// set policy
self.set_ps1_policy().await;

// create pipe
let (our_pipe, their_pipe) = anon_pipe(true)?;

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

*self.base.pid.lock().unwrap() = Some(child.id());
let base = self.base.clone();
info!("=>PowerShellCommand::tokio::spawn");
// async read output.
tokio::spawn(async move {
base.add_timeout_timer();
Expand Down
Loading

0 comments on commit f491cc2

Please sign in to comment.