Skip to content

Instantly share code, notes, and snippets.

@cyrex562
Created August 22, 2020 21:35
Show Gist options
  • Save cyrex562/9a8054256c7efccce26c95fae5834d2b to your computer and use it in GitHub Desktop.
Save cyrex562/9a8054256c7efccce26c95fae5834d2b to your computer and use it in GitHub Desktop.
Deploy glances web ui as a service to Ubuntu host
import sys
import subprocess
from subprocess import CompletedProcess
import logging
from typing import List
import tempfile
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
def run_cmd(cmd: List[str], shell: bool = False) -> str:
result: CompletedProcess = subprocess.run(cmd, capture_output=True, shell=shell)
logging.debug("result: %s", result)
logging.debug("stdout: %s", result.stdout)
if result.returncode != 0:
logging.error("call failed, stderr: %s", result.stderr)
raise RuntimeError("")
return result.stdout.decode()
glances_service = """
[Unit]
Description=Glances
After=network.target
[Service]
ExecStart=/usr/local/bin/glances -w
Restart=on-abort
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
"""
def run() -> int:
# install python package
run_cmd(["sudo", "pip3", "install", "glances", "bottle", "psutil"])
# write service file
# with open("/etc/systemd/system/glances.service", "w") as fd:
# fd.write(glances_service)
ntf = tempfile.NamedTemporaryFile()
tmp_file_path = ntf.name
with open(tmp_file_path, "w") as fd:
fd.write(glances_service)
run_cmd(["sudo", "cp", ntf.name, "/etc/systemd/system/glances.service"])
ntf.close()
# enable service
run_cmd(["sudo", "systemctl", "daemon-reload"])
run_cmd(["sudo", "systemctl", "enable", "glances.service"])
# start service
run_cmd(["sudo", "systemctl", "start", "glances.service"])
# get service status
result = run_cmd(["sudo", "systemctl", "status", "glances.service"])
logging.debug("systemctl status result: \"%s\"", result)
return 0
if __name__ == "__main__":
sys.exit(run())
# END OF FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment