aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRahiel Kasim <rahielkasim@gmail.com>2016-06-04 15:29:34 +0200
committerRahiel Kasim <rahielkasim@gmail.com>2016-06-04 15:29:34 +0200
commitfed303fc98579b8dd027da837dffab9cf630e149 (patch)
treee7425e7e71664b60ccecc815bb2f45ba4225d6e1
parent910494b21fe1f7027c64c6d25f3c657ab836d5f0 (diff)
add easy set up with a default configuration
-rw-r--r--README.md11
-rw-r--r--supervisor_alert.py41
2 files changed, 48 insertions, 4 deletions
diff --git a/README.md b/README.md
index 5fc1c7d..31236c3 100644
--- a/README.md
+++ b/README.md
@@ -18,7 +18,16 @@ Install supervisor-alert on your system:
sudo pip install supervisor-alert
```
-Then create the file `/etc/supervisor/conf.d/supervisor_alert.conf` as root:
+Then run:
+``` shell
+sudo supervisor-alert --configure
+```
+for the default configuration. This will send notifications over Telegram. Read
+the next section to customize or if you dislike automatic configurations.
+
+# Manual Configuration
+
+Create the file `/etc/supervisor/conf.d/supervisor_alert.conf` as root:
``` shell
[eventlistener:supervisor_alert]
command=supervisor-alert --telegram
diff --git a/supervisor_alert.py b/supervisor_alert.py
index 4b008f7..fc3e8db 100644
--- a/supervisor_alert.py
+++ b/supervisor_alert.py
@@ -15,13 +15,15 @@
# limitations under the License.
import argparse
from functools import partial
+import os
from os.path import expanduser
+from pwd import getpwnam
import shlex
from subprocess import check_call, CalledProcessError
from supervisor.childutils import listener, get_headers
-__version__ = "0.2.1"
+__version__ = "0.3"
def main():
@@ -29,9 +31,13 @@ def main():
epilog="Homepage: https://github.com/rahiel/supervisor-alert")
parser.add_argument("-c", "--command", help="Specify the command to process the event messages.")
parser.add_argument("--telegram", help="Use telegram-send to send event messages.", action="store_true")
+ parser.add_argument("--configure", help="configure %(prog)s", action="store_true")
parser.add_argument("--version", action="version", version="%(prog)s {}".format(__version__))
args = parser.parse_args()
+ if args.configure:
+ return configure()
+
s = "PROCESS_STATE_"
if args.telegram:
@@ -78,8 +84,37 @@ def send(command, message):
def configure():
- """Guide user to set up supervisor-alert."""
- pass
+ """Automatically set up supervisor-alert."""
+ conf = "/etc/supervisor/conf.d/supervisor_alert.conf"
+
+ config = """[eventlistener:supervisor_alert]
+command=supervisor-alert --telegram
+events=PROCESS_STATE_RUNNING,PROCESS_STATE_EXITED,PROCESS_STATE_FATAL
+autostart=true
+autorestart=true
+user=supervisor_alert
+"""
+
+ try:
+ with open(conf, 'w') as f:
+ f.write(config)
+ except IOError:
+ raise Exception("Can't save config, please execute as root: sudo supervisor-alert --configure")
+
+ try:
+ try:
+ getpwnam("supervisor_alert")
+ except KeyError:
+ check_call("adduser supervisor_alert --system --no-create-home".split())
+
+ # restart supervisor
+ check_call("supervisorctl reread".split())
+ check_call("supervisorctl update".split())
+ except CalledProcessError:
+ raise Exception("Please retry as root or configure manually: "
+ "https://github.com/rahiel/supervisor-alert#manual-configuration")
+
+ print("Supervisor-alert has been set up successfully!")
if __name__ == "__main__":