mirror of
https://github.com/CHN-beta/nixos.git
synced 2026-01-12 04:19:22 +08:00
services.hpcstat: split services
This commit is contained in:
@@ -8,10 +8,8 @@ inputs:
|
||||
config = let inherit (inputs.config.nixos.services) hpcstat; in inputs.lib.mkIf (hpcstat != null)
|
||||
{
|
||||
systemd =
|
||||
{
|
||||
services.hpcstat =
|
||||
{
|
||||
script =
|
||||
let
|
||||
scripts =
|
||||
let
|
||||
rsync = "${inputs.pkgs.rsync}/bin/rsync";
|
||||
grep = "${inputs.pkgs.gnugrep}/bin/grep";
|
||||
@@ -27,51 +25,78 @@ inputs:
|
||||
jykang = "${inputs.topInputs.self}/devices/jykang.xmuhpc";
|
||||
ssh-agent = "${inputs.pkgs.openssh}/bin/ssh-agent";
|
||||
in
|
||||
''
|
||||
eval $(${ssh-agent})
|
||||
# check if the file content differ
|
||||
if ${rsync} -e "${ssh}" -acnri ${jykang}/ jykang@hpc.xmu.edu.cn:~/ | ${grep} -E '^[<>]' -q; then
|
||||
${curl} -X POST -H 'Content-Type: application/json' \
|
||||
-d "{\"chat_id\": \"$(${cat} ${chat})\", \"text\": \"File content differ!\"}" \
|
||||
https://api.telegram.org/bot$(${cat} ${token})/sendMessage
|
||||
exit 1
|
||||
fi
|
||||
# check finishjob
|
||||
${ssh} jykang@hpc.xmu.edu.cn hpcstat finishjob
|
||||
${ssh} jykang@hpc.xmu.edu.cn hpcstat push
|
||||
# download database
|
||||
now=$(${date} '+%Y%m%d%H%M%S')
|
||||
${rsync} -e "${ssh}" \
|
||||
jykang@hpc.xmu.edu.cn:~/linwei/chn/software/hpcstat/var/lib/hpcstat/hpcstat.db \
|
||||
/var/lib/hpcstat/hpcstat.db.$now
|
||||
if [ $? -ne 0 ]; then
|
||||
${curl} -X POST -H 'Content-Type: application/json' \
|
||||
-d "{\"chat_id\": \"$(${cat} ${chat})\", \"text\": \"Download database failed!\"}" \
|
||||
https://api.telegram.org/bot$(${cat} ${token})/sendMessage
|
||||
exit 1
|
||||
fi
|
||||
# diff database
|
||||
if [ -f /var/lib/hpcstat/hpcstat.db.last ]; then
|
||||
${hpcstat} verify /var/lib/hpcstat/hpcstat.db.last /var/lib/hpcstat/hpcstat.db.$now
|
||||
fi
|
||||
if [ $? -ne 0 ]; then
|
||||
${curl} -X POST -H 'Content-Type: application/json' \
|
||||
-d "{\"chat_id\": \"$(${cat} ${chat})\", \"text\": \"Database verification failed!\"}" \
|
||||
https://api.telegram.org/bot$(${cat} ${token})/sendMessage
|
||||
exit 1
|
||||
fi
|
||||
# update database
|
||||
ln -sf hpcstat.db.$now /var/lib/hpcstat/hpcstat.db.last
|
||||
'';
|
||||
serviceConfig = { Type = "oneshot"; User = "hpcstat"; Group = "hpcstat"; };
|
||||
};
|
||||
timers.hpcstat =
|
||||
{
|
||||
finishjob =
|
||||
''
|
||||
eval $(${ssh-agent})
|
||||
# check if the file content differ
|
||||
if ${rsync} -e "${ssh}" -acnri ${jykang}/ jykang@hpc.xmu.edu.cn:~/ | ${grep} -E '^[<>]' -q; then
|
||||
${curl} -X POST -H 'Content-Type: application/json' \
|
||||
-d "{\"chat_id\": \"$(${cat} ${chat})\", \"text\": \"File content differ!\"}" \
|
||||
https://api.telegram.org/bot$(${cat} ${token})/sendMessage
|
||||
exit 1
|
||||
fi
|
||||
# check finishjob
|
||||
${ssh} jykang@hpc.xmu.edu.cn hpcstat finishjob
|
||||
${ssh} jykang@hpc.xmu.edu.cn hpcstat push
|
||||
'';
|
||||
backupdb =
|
||||
''
|
||||
eval $(${ssh-agent})
|
||||
# download database
|
||||
now=$(${date} '+%Y%m%d%H%M%S')
|
||||
${rsync} -e "${ssh}" \
|
||||
jykang@hpc.xmu.edu.cn:~/linwei/chn/software/hpcstat/var/lib/hpcstat/hpcstat.db \
|
||||
/var/lib/hpcstat/hpcstat.db.$now
|
||||
if [ $? -ne 0 ]; then
|
||||
${curl} -X POST -H 'Content-Type: application/json' \
|
||||
-d "{\"chat_id\": \"$(${cat} ${chat})\", \"text\": \"Download database failed!\"}" \
|
||||
https://api.telegram.org/bot$(${cat} ${token})/sendMessage
|
||||
exit 1
|
||||
fi
|
||||
# diff database
|
||||
if [ -f /var/lib/hpcstat/hpcstat.db.last ]; then
|
||||
${hpcstat} verify /var/lib/hpcstat/hpcstat.db.last /var/lib/hpcstat/hpcstat.db.$now
|
||||
fi
|
||||
if [ $? -ne 0 ]; then
|
||||
${curl} -X POST -H 'Content-Type: application/json' \
|
||||
-d "{\"chat_id\": \"$(${cat} ${chat})\", \"text\": \"Database verification failed!\"}" \
|
||||
https://api.telegram.org/bot$(${cat} ${token})/sendMessage
|
||||
exit 1
|
||||
fi
|
||||
# update database
|
||||
ln -sf hpcstat.db.$now /var/lib/hpcstat/hpcstat.db.last
|
||||
'';
|
||||
diskstat =
|
||||
''
|
||||
eval $(${ssh-agent})
|
||||
${ssh} jykang@hpc.xmu.edu.cn hpcstat diskstat
|
||||
'';
|
||||
};
|
||||
calenders =
|
||||
{
|
||||
finishjob = "*-*-* *:*:00";
|
||||
backupdb = "*-*-* *:00/10:00";
|
||||
diskstat = "*-*-* 07:00:00";
|
||||
};
|
||||
in
|
||||
{
|
||||
wantedBy = [ "timers.target" ];
|
||||
timerConfig = { OnCalendar = "*-*-* *:00/5:00"; Unit = "hpcstat.service"; };
|
||||
services = builtins.listToAttrs (builtins.map
|
||||
(script: { "hpcstat-${script.name}" =
|
||||
{
|
||||
script = script.value;
|
||||
serviceConfig = { Type = "oneshot"; User = "hpcstat"; Group = "hpcstat"; };
|
||||
};})
|
||||
(inputs.localLib.attrsToList scripts));
|
||||
timers = builtins.listToAttrs (builtins.map
|
||||
(calender: { "hpcstat-${calender.name}" =
|
||||
{
|
||||
wantedBy = [ "timers.target" ];
|
||||
timerConfig = { OnCalendar = calender.value; Unit = "hpcstat-${calender.name}.service"; };
|
||||
};})
|
||||
(inputs.localLib.attrsToList calenders));
|
||||
tmpfiles.rules = [ "d /var/lib/hpcstat 0700 hpcstat hpcstat" ];
|
||||
};
|
||||
tmpfiles.rules = [ "d /var/lib/hpcstat 0700 hpcstat hpcstat" ];
|
||||
};
|
||||
sops.secrets =
|
||||
{
|
||||
"telegram/token" = { group = "telegram"; mode = "0440"; };
|
||||
|
||||
Reference in New Issue
Block a user