-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cic-monitor service to monitor shutdown events.
Change-Id: Ice57f6a87441468a01f9ea1ad58b8ab191ec71c4 Tracked-On: OAM-89821 Signed-off-by: bxu10x <[email protected]>
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/bin/sh | ||
|
||
sudo touch /lib/systemd/system/cic-monitor.service | ||
|
||
echo "Adding CIC Monitor Service" | ||
cat << EOF | sudo tee /lib/systemd/system/cic-monitor.service | ||
[Unit] | ||
Description="Start CIC Monitor service" | ||
[Service] | ||
WorkingDirectory=/AIC_WORK_DIR/ | ||
Type=oneshot | ||
ExecStart=/usr/bin/python3 /lib/systemd/system/docker-events.py | ||
RemainAfterExit=yes | ||
[Install] | ||
WantedBy=multi-user.target | ||
EOF | ||
|
||
sudo sed -i "s|/AIC_WORK_DIR|$AIC_WORK_DIR|g" /lib/systemd/system/cic-monitor.service | ||
|
||
sudo touch /lib/systemd/system/docker-events.py | ||
echo "Adding event handling file" | ||
cat << EOF | sudo tee /lib/systemd/system/docker-events.py | ||
import subprocess as sp | ||
import json | ||
import os | ||
def handle(event): | ||
kind, action = event["Type"], event["Action"] | ||
if kind == "container" and action == "die": | ||
name, code = [event["Actor"]["Attributes"][x] | ||
for x in ["name", "exitCode"]] | ||
if code == '129': | ||
print("container {name} exit for reboot") | ||
#os.system('./aic stop') | ||
#on demand | ||
#os.system('reboot') | ||
elif code == '130': | ||
print("container {name} exit for shutdown") | ||
os.system('./aic stop') | ||
#on demand | ||
#os.system('shutdown -h now') | ||
else: | ||
print("container {name} exit {code}") | ||
else: | ||
pass # print(str(event)) | ||
def main(): | ||
cmd = ["docker", "events", "--format", r"{{json .}}"] | ||
p = sp.Popen(cmd, stdout=sp.PIPE, universal_newlines=True) | ||
for line in p.stdout: | ||
handle(json.loads(line)) | ||
if __name__ == "__main__": | ||
try: | ||
main() | ||
except KeyboardInterrupt: | ||
exit(0) | ||
EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters