2023-06-14 21:11:22 +02:00
# exec-notify
2020-12-06 16:59:16 +01:00
2023-06-14 21:36:31 +02:00
* Send email notification if command fails with [exec-notify ](src/exec_notify/main.py ).
2023-06-14 21:16:09 +02:00
* Send unconditional notifications with [do-notify ](src/exec_notify/do_notify.py ).
2020-12-26 15:36:20 +01:00
2023-06-14 19:39:20 +02:00
## TODOs
~~* Send notification about unread local mail only once!~~
2023-06-14 21:11:22 +02:00
* add PKGBUILD
2023-06-14 19:39:20 +02:00
* Rename python module (and git repository?) to `exec-notify`
* Use subprocess_util > execute_print_capture
* This way, the user can see the stdout/stderr while it is executed
* And we can email the stdout/stderr if the process has failed
* New program flow:
* Read configuration from `/etc/exec-notify` -> `work_dir` (default: `~/exec-notify` )
* Create file `$work_dir` / `$start_date-$id.args` with `$args` as content, e.g. `ls foo bar`
* If one or more .exit_code files exists and `$end_date` of at least one .exit_code file is older than 1 minute, then try to send email
* If email was sent:
* delete corresponding .exit_code, .stdout, .stderr, .args
* continue
* Else:
* continue
* Execute `$args` and redirect stdout/stderr to `$work_dir` / `$start_date-$id.{stdout,stderr}`
* If error occurred, create file `$work_dir` / `$start_date-$id-$end-date.exit_code` with content `$exit_code`
* Then, send email and if successful delete .exit_code file
* Delete .args, .stdout, .stderr files
* If error was reported, exit(1), else exit(0)
2022-03-01 16:32:45 +01:00
## Installation
### Installation - global
Requires `pip` (e.g. `pacman -S --needed python-pip` ).
Note: Make sure that pip is up-to-date and matches your python version:
```shell
python --version
#=> Python 3.10.1
pip --version
#=> pip 20.3.4 from /usr/lib/python3.10/site-packages/pip (python 3.10)
```
Then install with one of the following options:
```shell
2023-06-14 21:11:22 +02:00
sudo python3 -m pip install --upgrade --force-reinstall git+https://codeberg.org/privacy1st/exec-notify
2022-05-30 22:49:15 +02:00
sudo python3 -m pip install --upgrade --force-reinstall .
2022-03-01 16:32:45 +01:00
```
### Installation - venv
2020-12-06 16:59:16 +01:00
2021-07-22 16:20:03 +02:00
More detailed instructions about _venv_ can be found at [https://docs.python.org/3/library/venv.html ](https://docs.python.org/3/library/venv.html )
Create a _venv_ :
```shell
2023-06-14 21:11:22 +02:00
python3 -m venv ~/exec-notify/venv
2021-07-22 16:20:03 +02:00
```
Activate the venv:
2020-12-06 16:59:16 +01:00
```shell
2023-06-14 21:11:22 +02:00
source ~/exec-notify/venv/bin/activate
2021-07-22 16:20:03 +02:00
# echo "VIRTUAL_ENV=${VIRTUAL_ENV}"
```
2022-02-28 13:44:53 +01:00
And choose one source of installation:
2021-07-22 16:20:03 +02:00
```shell
2022-02-28 13:44:53 +01:00
# Install from git URL
2023-06-14 21:11:22 +02:00
python3 -m pip install git+https://codeberg.org/privacy1st/exec-notify
2022-02-28 13:44:53 +01:00
# Install from local directory
2023-06-14 21:11:22 +02:00
python3 -m pip install ~/Downloads/git/exec-notify/
2020-12-06 16:59:16 +01:00
```
2022-02-28 13:44:53 +01:00
Note about usage of TestPyPI and `--no-deps` :
2021-07-22 13:01:18 +02:00
2022-05-30 20:07:26 +02:00
> Since TestPyPI doesn't have the same packages as the live PyPI, it's possible that attempting
2021-07-22 13:01:18 +02:00
> to install dependencies may fail or install something unexpected. While this package
2022-05-30 20:07:26 +02:00
> doesn't have any dependencies, it's a good practice to avoid installing dependencies when
2021-07-22 13:01:18 +02:00
> using TestPyPI.
2021-07-22 16:20:03 +02:00
(Optionally) list installed modules:
```shell
pip list
```
2021-07-22 13:01:18 +02:00
## Configuration
2023-06-14 21:11:22 +02:00
Create configuration file `/etc/exec-notify/cfg.ini` :
2021-07-22 13:01:18 +02:00
2021-11-23 19:18:38 +01:00
```shell
2023-06-14 21:11:22 +02:00
install -Dm0600 /etc/exec-notify/cfg.ini < < 'EOF'
2021-11-23 19:18:38 +01:00
[DEFAULT]
[mail]
host = smtp.example.com
port = 465
password = examplePassword
from = noreply@example.com
to = me@example.com
[file]
maildir = /home/exampleUser/mail/
EOF
```
2023-06-14 21:11:22 +02:00
See also: [./etc/exec-notify/cfg.ini.example ](etc/exec-notify/cfg.ini.example )
2021-07-22 13:01:18 +02:00
## Usage
2023-06-14 21:11:22 +02:00
### Usage of exec-notify
2020-12-06 16:59:16 +01:00
2023-06-14 21:11:22 +02:00
Add `exec-notify` in front of your command to execute.
2020-12-06 16:59:16 +01:00
2021-07-22 13:01:18 +02:00
**Example:**
2020-12-06 16:59:16 +01:00
2023-06-14 21:11:22 +02:00
`exec-notify ls /non/existent/file` will mail you the exit code, stdout and stderr of `ls /non/existent/file`
2020-12-26 15:36:20 +01:00
2021-07-22 13:01:18 +02:00
### Usage of notify
2020-12-26 15:36:20 +01:00
Send stdout via mail:
2023-06-14 21:11:22 +02:00
`echo "Hello world!" | do-notify`
2020-12-26 15:36:20 +01:00
2021-07-22 13:01:18 +02:00
Send stdout and stderr via mail:
2020-12-26 15:36:20 +01:00
2023-06-14 21:11:22 +02:00
`echo "Hello world!" 2>&1 | do-notify`
2020-12-26 15:36:20 +01:00
2021-07-22 13:01:18 +02:00
Send stdout and specify an optional email subject:
2020-12-26 15:36:20 +01:00
2023-06-14 21:11:22 +02:00
`echo "Hello world!" | do-notify "someSubject"`
2021-07-22 13:01:18 +02:00
Send message without using a pipe:
2020-12-26 15:36:20 +01:00
2023-06-14 21:11:22 +02:00
`do-notify "someSubject" "Hello World! What's up?"`
2021-07-22 13:01:18 +02:00
## Development
When started with environment variable `DE_P1ST_EXEC_NOTIFY` set,
2023-06-14 21:11:22 +02:00
then the configuration file is read from [./etc/exec-notify/cfg.ini ](etc/exec-notify/cfg.ini ).
2021-07-22 13:01:18 +02:00
2021-07-22 16:20:03 +02:00
### Testing
See heading "Installation" above with steps to locally install this module.
2021-07-22 13:01:18 +02:00
### Uploading to TestPyPI
2021-07-22 16:20:03 +02:00
More detailed instructions can be found at [https://packaging.python.org/tutorials/packaging-projects/ ](https://packaging.python.org/tutorials/packaging-projects/ )
2021-07-22 13:01:18 +02:00
2021-07-22 16:20:03 +02:00
1) Set up a _venv_
2021-07-22 13:01:18 +02:00
2) Increase/Adjust `[metadata][version]` in [setup.cfg ](setup.cfg )
3) Install the `build` and `twine` modules to your _venv_ .
4) Next generate a [distribution archive ](https://packaging.python.org/tutorials/packaging-projects/#generating-distribution-archives ):
```shell
python3 -m build
```
5) Upload the distribution packages with twine. For the username, use `__token__` . For the password, use a
[test.pypi.org API token ](https://test.pypi.org/manage/account/#api-tokens ):
```shell
python3 -m twine upload --repository testpypi dist/*
```
6) Congratulations! You can view the uploaded module under:
2023-06-14 21:11:22 +02:00
* [https://test.pypi.org/project/exec_notify/ ](https://test.pypi.org/project/exec_notify/ )