Skip to content

Commit a2d35dd

Browse files
authored
Merge pull request #8 from TINF21CS1/docker_monitoring
Create Grade-Monitoring Docker container
2 parents 3fbf91b + d64fbed commit a2d35dd

File tree

6 files changed

+148
-1
lines changed

6 files changed

+148
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Grade Notifier Docker Publish
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- dev*
8+
9+
jobs:
10+
docker-publish:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v2
15+
- name: Docker meta
16+
id: meta
17+
uses: docker/metadata-action@v3
18+
with:
19+
images: |
20+
ghcr.io/tinf21cs1/campusnet-grade-notifier
21+
# tags: latest, if default branch; name of branch; name of semver tag
22+
tags: |
23+
type=raw,value=latest,enable={{is_default_branch}}
24+
type=ref,event=branch
25+
- name: Login to GitHub Container Registry
26+
uses: docker/login-action@v1
27+
with:
28+
registry: ghcr.io
29+
username: ${{ github.actor }}
30+
password: ${{ github.token }}
31+
- name: Build and Push Docker Image
32+
uses: docker/build-push-action@v2
33+
with:
34+
platforms: linux/amd64
35+
context: ./grades-docker
36+
push: true # will only push if not a pull_request
37+
tags: ${{ steps.meta.outputs.tags }}
38+
labels: ${{ steps.meta.outputs.labels }}

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,24 @@ pip install git+https://github.com/TINF21CS1/python-campusnet@dev
88

99
## Usage Examples
1010

11-
### Getting all exams and grades
11+
### Grade Notifier with Docker
12+
13+
This docker container will check your grade every hour and send a notification mail, if something has changed. Currently this docker container is very tailored for DHBW-Mannheim students. But feel free to broaden its usability with a PR.
14+
15+
3 Environment variables are needed:
16+
17+
* `USERNAME`: Your student mail address with the domain part. That is also used to login to CampusNet (Dualis)
18+
* `VPNUSERNAME`: Your username without the domain part. Used for the login with Anyconnect VPN, to send mails.
19+
* `PASSWORD`: Your account password. Used for VPN, Campusnet and Mail authentication.
20+
* Optional `NOTIFY_RECIPIENT`: Mail address that will receive notifications. By default the same as `USERNAME`.
21+
22+
```
23+
docker run --privileged -e "[email protected]" -e "VPNUSERNAME=s212689" -e "PASSWORD=xxxxxxxxxxxxxxxx" ghcr.io/tinf21cs1/campusnet-grade-notifier
24+
```
25+
26+
[Detailed Readme](./grades-docker/Readme.md)
27+
28+
### Getting Exams and Grades directly
1229

1330
The module can be used directly to get information about all exams
1431

grades-docker/Dockerfile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#get vpn client
2+
FROM ghcr.io/jm-lemmi/anyconnect-simple
3+
4+
# install python and package
5+
RUN apk --update add git python3
6+
RUN python3 -m ensurepip
7+
RUN pip3 install --no-cache --upgrade pip setuptools
8+
RUN pip install git+https://github.com/TINF21CS1/python-campusnet@dev
9+
10+
# Add crontab
11+
COPY grades.sh /app/grades.sh
12+
RUN chmod 777 /app/grades.sh; chmod +x /app/grades.sh
13+
RUN crontab -l /cron
14+
RUN echo "0 7-23 * * * /bin/sh /app/grades.sh" >> /cron
15+
RUN crontab /cron
16+
RUN rm /cron
17+
18+
#ENV USERNAME "s:)@student.dhbw-mannheim.de"
19+
#ENV PASSWORD='***'
20+
#ENV NOTIFY_RECIPIENT="***"
21+
#default values for dhbw mannheim
22+
ENV AUTHGROUP Studenten
23+
ENV HOST drogon.dhbw-mannheim.de
24+
25+
# add sendmail
26+
RUN apk add ssmtp
27+
28+
COPY entrypoint.sh /app/entrypoint.sh
29+
CMD /bin/sh /app/entrypoint.sh

grades-docker/Readme.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Campusnet Grade Notifier
2+
3+
This docker container will check your grade every hour and send a notification mail, if something has changed. Currently this docker container is very tailored for DHBW-Mannheim students. But feel free to broaden its usability with a PR.
4+
5+
## Usage
6+
7+
3 Environment variables are needed:
8+
9+
* `USERNAME`: Your student mail address with the domain part. That is also used to login to CampusNet (Dualis)
10+
* `VPNUSERNAME`: Your username without the domain part. Used for the login with Anyconnect VPN, to send mails.
11+
* `PASSWORD`: Your account password. Used for VPN, Campusnet and Mail authentication.
12+
* Optional `NOTIFY_RECIPIENT`: Mail address that will receive notifications. By default the same as `USERNAME`.
13+
14+
```
15+
docker run --privileged -e "[email protected]" -e "VPNUSERNAME=s212689" -e "PASSWORD=xxxxxxxxxxxxxxxx" ghcr.io/tinf21cs1/campusnet-grade-notifier
16+
```
17+
18+
`--privileged` is needed for the VPN connection.
19+
20+
## Process
21+
22+
On startup a VPN connection to `drogon.dhbw-mannheim.de` is established to allow for sending mails with SMTP.
23+
24+
Every hour a cronjob will connect to CampusNet and download the latest grades. These will be compared to the last known state, that is saved in `/app/grades.txt`
25+
26+
If something changed, sendmail is used to send the diff to the `NOTIFIER_RECIPIENT`, via `studgate.dhbw-mannheim.de:25`, authenticated as `USERNAME`.

grades-docker/entrypoint.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#! /bin/bash
2+
3+
# https://stackoverflow.com/a/39296583
4+
# sets noficy_recipient to username, if not set
5+
if [[ -z "${NOTIFY_RECIPIENT}" ]]; then
6+
NOTIFY_RECIPIENT="${USERNAME}"
7+
fi
8+
9+
echo -e "root=${USERNAME}\nmailhub=studgate.dhbw-mannheim.de:25\nrewriteDomain=student.dhbw-mannheim.de\nhostname=campusnet.docker.local\nAuthUser=${USERNAME}\nAuthPass=${PASSWORD}\nAuthMethod=LOGIN\nFromLineOverride=yes" > /etc/ssmtp/ssmtp.conf
10+
echo -e "root:${USERNAME}:studgate.dhbw-mannheim.de:25" > /etc/ssmtp/revaliases
11+
echo "$(date +"%b %d %H:%M:%S") $HOSTNAME entrypoint.sh[$$]: Updated ssmtp configuration."
12+
13+
echo "$(date +"%b %d %H:%M:%S") $HOSTNAME entrypoint.sh[$$]: Starting crond..."
14+
crond -f

grades-docker/grades.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
echo $PASSWORD | openconnect -b --user=$VPNUSERNAME --authgroup=$AUTHGROUP --passwd-on-stdin $HOST
4+
echo "$(date +"%b %d %H:%M:%S") $HOSTNAME grades.sh[$$]: Started openconnect in background."
5+
6+
echo "$(date +"%b %d %H:%M:%S") $HOSTNAME grades.sh[$$]: Getting grades from Campusnet."
7+
python3 -m campusnet $USERNAME $PASSWORD -o table > /tmp/grades.txt
8+
if [ ! -s grades.txt ]; then
9+
mv /tmp/grades.txt grades.txt
10+
echo "$(date +"%b %d %H:%M:%S") $HOSTNAME grades.sh[$$]: First run, created grades.txt."
11+
exit 0
12+
fi
13+
/usr/bin/diff grades.txt /tmp/grades.txt > /tmp/diff.txt
14+
if [ -s /tmp/diff.txt ]; then
15+
(echo -e "From: Grades <${USERNAME}>\nSubject: Grades changed\n\n"; cat /tmp/diff.txt; echo -e '\n\nAll grades:\n'; cat /tmp/grades.txt) | /usr/sbin/sendmail $NOTIFY_RECIPIENT
16+
echo "$(date +"%b %d %H:%M:%S") $HOSTNAME grades.sh[$$]: Sent mail to ${NOTIFY_RECIPIENT}."
17+
else
18+
echo "$(date +"%b %d %H:%M:%S") $HOSTNAME grades.sh[$$]: No changes detected."
19+
fi
20+
mv /tmp/grades.txt grades.txt
21+
22+
/usr/bin/killall openconnect
23+
echo "$(date +"%b %d %H:%M:%S") $HOSTNAME grades.sh[$$]: Killed openconnect."

0 commit comments

Comments
 (0)