From 6503e2b91626510e2cc557727597bbe09846007e Mon Sep 17 00:00:00 2001 From: missytake Date: Sun, 8 Oct 2023 11:30:39 +0200 Subject: [PATCH 01/10] pyinfra: started pyinfra deploy.py script --- setup.cfg | 1 + src/teams_bot/pyinfra-deploy.py | 73 +++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/teams_bot/pyinfra-deploy.py diff --git a/setup.cfg b/setup.cfg index aa6c24d..33dafbd 100644 --- a/setup.cfg +++ b/setup.cfg @@ -21,6 +21,7 @@ packages = find: python_requires = >=3.8 install_requires = click + pyinfra pickleDB qrcode deltachat diff --git a/src/teams_bot/pyinfra-deploy.py b/src/teams_bot/pyinfra-deploy.py new file mode 100644 index 0000000..b2ab33b --- /dev/null +++ b/src/teams_bot/pyinfra-deploy.py @@ -0,0 +1,73 @@ +from io import StringIO +import importlib.resources + +from pyinfra.operations import git, server, files, systemd + + +def deploy_teams_bot(unix_user: str, bot_email: str, bot_passwd: str, dbdir: str = None): + """Deploy TeamsBot to a UNIX user""" + + clone_xdcget = git.repo( + name="Pull the teams-bot repository", + src="https://git.0x90.space/missytake/teams-bot", + dest=f"/home/{unix_user}/teams-bot", + _su_user=unix_user, + _use_su_login=True, + ) + + if clone_xdcget.changed: + server.script( + name="Setup virtual environment for teams-bot", + src=importlib.resources.files(__package__).joinpath("setup-venv.sh"), + _su_user=unix_user, + _use_su_login=True, + ) + + server.shell( + name="Compile teams-bot", + commands=[ + f". .venv/bin/activate && cd /home/{unix_user}/teams-bot && pip install ." + ], + _su_user=unix_user, + _use_su_login=True, + ) + + if not dbdir: + dbdir = f"/home/{unix_user}/.config/teams_bot/{bot_email}/" + secrets = [ + f"addr={bot_email}", + f"mail_pw={bot_passwd}", + f'TEAMS_DBDIR={dbdir}', + ] + env = "\n".join(secrets) + files.put( + name="upload secrets", + src=StringIO(env), + dest=f"/home/{unix_user}/.env", + mode="0600", + user=unix_user, + ) + + files.directory( + name="chown database directory", + path=dbdir, + mode="0700", + recursive=True, + user=unix_user, + ) + + files.template( + name="upload teams-bot systemd unit", + src=importlib.resources.files(__package__).joinpath("teams-bot.service.j2"), + dest=f"/home/{unix_user}/.config/systemd/user/teams-bot.service", + user=unix_user, + unix_user=unix_user, + bot_email=bot_email, + ) + systemd.daemon_reload( + name=f"{unix_user}: load teams-bot systemd service", + user_name=unix_user, + user_mode=True, + _su_user=unix_user, + _use_su_login=True, + ) -- 2.43.4 From aa23227486563846931b835576e48f5fce553696 Mon Sep 17 00:00:00 2001 From: missytake Date: Sun, 8 Oct 2023 11:36:44 +0200 Subject: [PATCH 02/10] pyinfra: renamed pyinfra.py --- src/teams_bot/pyinfra-deploy.py | 73 --------------------------------- 1 file changed, 73 deletions(-) delete mode 100644 src/teams_bot/pyinfra-deploy.py diff --git a/src/teams_bot/pyinfra-deploy.py b/src/teams_bot/pyinfra-deploy.py deleted file mode 100644 index b2ab33b..0000000 --- a/src/teams_bot/pyinfra-deploy.py +++ /dev/null @@ -1,73 +0,0 @@ -from io import StringIO -import importlib.resources - -from pyinfra.operations import git, server, files, systemd - - -def deploy_teams_bot(unix_user: str, bot_email: str, bot_passwd: str, dbdir: str = None): - """Deploy TeamsBot to a UNIX user""" - - clone_xdcget = git.repo( - name="Pull the teams-bot repository", - src="https://git.0x90.space/missytake/teams-bot", - dest=f"/home/{unix_user}/teams-bot", - _su_user=unix_user, - _use_su_login=True, - ) - - if clone_xdcget.changed: - server.script( - name="Setup virtual environment for teams-bot", - src=importlib.resources.files(__package__).joinpath("setup-venv.sh"), - _su_user=unix_user, - _use_su_login=True, - ) - - server.shell( - name="Compile teams-bot", - commands=[ - f". .venv/bin/activate && cd /home/{unix_user}/teams-bot && pip install ." - ], - _su_user=unix_user, - _use_su_login=True, - ) - - if not dbdir: - dbdir = f"/home/{unix_user}/.config/teams_bot/{bot_email}/" - secrets = [ - f"addr={bot_email}", - f"mail_pw={bot_passwd}", - f'TEAMS_DBDIR={dbdir}', - ] - env = "\n".join(secrets) - files.put( - name="upload secrets", - src=StringIO(env), - dest=f"/home/{unix_user}/.env", - mode="0600", - user=unix_user, - ) - - files.directory( - name="chown database directory", - path=dbdir, - mode="0700", - recursive=True, - user=unix_user, - ) - - files.template( - name="upload teams-bot systemd unit", - src=importlib.resources.files(__package__).joinpath("teams-bot.service.j2"), - dest=f"/home/{unix_user}/.config/systemd/user/teams-bot.service", - user=unix_user, - unix_user=unix_user, - bot_email=bot_email, - ) - systemd.daemon_reload( - name=f"{unix_user}: load teams-bot systemd service", - user_name=unix_user, - user_mode=True, - _su_user=unix_user, - _use_su_login=True, - ) -- 2.43.4 From beddedf85d34eab363821a98a43f33944036d3b8 Mon Sep 17 00:00:00 2001 From: missytake Date: Sun, 8 Oct 2023 11:39:06 +0200 Subject: [PATCH 03/10] pyinfra: added systemd service --- src/teams_bot/teams-bot.service.j2 | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/teams_bot/teams-bot.service.j2 diff --git a/src/teams_bot/teams-bot.service.j2 b/src/teams_bot/teams-bot.service.j2 new file mode 100644 index 0000000..929421a --- /dev/null +++ b/src/teams_bot/teams-bot.service.j2 @@ -0,0 +1,11 @@ +[Unit] +Description=run deltachat teams-bot: {{ bot_email }} + +[Service] +ExecStart=/home/{{ unix_user }}/.venv/bin/teams-bot run -v +EnvironmentFile=/home/{{ unix_user }}/.env +Restart=on-failure +RestartSec=5s + +[Install] +WantedBy=multi-user.target -- 2.43.4 From c25349383474256d828b7040512e62eedaaeaeed Mon Sep 17 00:00:00 2001 From: missytake Date: Sun, 8 Oct 2023 11:44:18 +0200 Subject: [PATCH 04/10] pyinfra: added virtual environment --- src/teams_bot/setup-venv.sh | 6 ++++++ src/teams_bot/teams-bot.profile | 7 +++++++ src/teams_bot/teams-bot.service.j2 | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 src/teams_bot/setup-venv.sh create mode 100644 src/teams_bot/teams-bot.profile diff --git a/src/teams_bot/setup-venv.sh b/src/teams_bot/setup-venv.sh new file mode 100644 index 0000000..4c4a65b --- /dev/null +++ b/src/teams_bot/setup-venv.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +python3 -m venv ~/.local/lib/teams-bot.venv +source ~/.local/lib/teams-bot.venv/bin/activate +pip install -U pip wheel + diff --git a/src/teams_bot/teams-bot.profile b/src/teams_bot/teams-bot.profile new file mode 100644 index 0000000..4ed2638 --- /dev/null +++ b/src/teams_bot/teams-bot.profile @@ -0,0 +1,7 @@ +export XDG_RUNTIME_DIR="/run/user/$UID" +export DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus" +export NVM_DIR="$HOME/.nvm" +[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm if it exists +[ -s "$HOME/.cargo/env" ] && . "$HOME/.cargo/env" # This loads the cargo environment if it exists +export PATH=$PATH:$HOME/.local/bin + diff --git a/src/teams_bot/teams-bot.service.j2 b/src/teams_bot/teams-bot.service.j2 index 929421a..08ac6e6 100644 --- a/src/teams_bot/teams-bot.service.j2 +++ b/src/teams_bot/teams-bot.service.j2 @@ -2,7 +2,7 @@ Description=run deltachat teams-bot: {{ bot_email }} [Service] -ExecStart=/home/{{ unix_user }}/.venv/bin/teams-bot run -v +ExecStart=/home/{{ unix_user }}/.local/lib/teams-bot.venv/bin/teams-bot run -v EnvironmentFile=/home/{{ unix_user }}/.env Restart=on-failure RestartSec=5s -- 2.43.4 From e8d5a31f0a45afda0daa6a2ea107d74b84b16ae7 Mon Sep 17 00:00:00 2001 From: missytake Date: Sun, 8 Oct 2023 11:48:30 +0200 Subject: [PATCH 05/10] pyinfra: moved assets to separate folder --- src/teams_bot/{ => pyinfra_assets}/setup-venv.sh | 0 src/teams_bot/{ => pyinfra_assets}/teams-bot.profile | 0 src/teams_bot/{ => pyinfra_assets}/teams-bot.service.j2 | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename src/teams_bot/{ => pyinfra_assets}/setup-venv.sh (100%) rename src/teams_bot/{ => pyinfra_assets}/teams-bot.profile (100%) rename src/teams_bot/{ => pyinfra_assets}/teams-bot.service.j2 (100%) diff --git a/src/teams_bot/setup-venv.sh b/src/teams_bot/pyinfra_assets/setup-venv.sh similarity index 100% rename from src/teams_bot/setup-venv.sh rename to src/teams_bot/pyinfra_assets/setup-venv.sh diff --git a/src/teams_bot/teams-bot.profile b/src/teams_bot/pyinfra_assets/teams-bot.profile similarity index 100% rename from src/teams_bot/teams-bot.profile rename to src/teams_bot/pyinfra_assets/teams-bot.profile diff --git a/src/teams_bot/teams-bot.service.j2 b/src/teams_bot/pyinfra_assets/teams-bot.service.j2 similarity index 100% rename from src/teams_bot/teams-bot.service.j2 rename to src/teams_bot/pyinfra_assets/teams-bot.service.j2 -- 2.43.4 From 1ea9125763710981cf8210489486afe114cbe241 Mon Sep 17 00:00:00 2001 From: missytake Date: Sun, 8 Oct 2023 11:50:43 +0200 Subject: [PATCH 06/10] pyinfra: fix paths --- src/teams_bot/pyinfra_assets/teams-bot.profile | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 src/teams_bot/pyinfra_assets/teams-bot.profile diff --git a/src/teams_bot/pyinfra_assets/teams-bot.profile b/src/teams_bot/pyinfra_assets/teams-bot.profile deleted file mode 100644 index 4ed2638..0000000 --- a/src/teams_bot/pyinfra_assets/teams-bot.profile +++ /dev/null @@ -1,7 +0,0 @@ -export XDG_RUNTIME_DIR="/run/user/$UID" -export DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus" -export NVM_DIR="$HOME/.nvm" -[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm if it exists -[ -s "$HOME/.cargo/env" ] && . "$HOME/.cargo/env" # This loads the cargo environment if it exists -export PATH=$PATH:$HOME/.local/bin - -- 2.43.4 From 47a48137d0450eeb4330a624bf7f5f4a9b045072 Mon Sep 17 00:00:00 2001 From: missytake Date: Sun, 8 Oct 2023 13:46:26 +0200 Subject: [PATCH 07/10] pyinfra: dbdir needs to be specified differently --- src/teams_bot/pyinfra.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/teams_bot/pyinfra.py b/src/teams_bot/pyinfra.py index 8c6fc09..69eda69 100644 --- a/src/teams_bot/pyinfra.py +++ b/src/teams_bot/pyinfra.py @@ -43,7 +43,8 @@ def deploy_teams_bot(unix_user: str, bot_email: str, bot_passwd: str, dbdir: str secrets = [ f"TEAMS_INIT_EMAIL={bot_email}", f"TEAMS_INIT_PASSWORD={bot_passwd}", - f'TEAMS_DBDIR={dbdir}', + f'TEAMS_INIT_DBDIR={dbdir}', + f'TEAMS_RUN_DBDIR={dbdir}', ] env = "\n".join(secrets) files.put( -- 2.43.4 From fa9128f65fcb67d01b52bf95c16372c7e964c61a Mon Sep 17 00:00:00 2001 From: missytake Date: Sun, 8 Oct 2023 14:15:42 +0200 Subject: [PATCH 08/10] documented pyinfra deployment --- README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/README.md b/README.md index ec497cc..b3b3350 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,43 @@ The bot only works as long as this command is running. Read more about [running bots on bots.delta.chat](https://bots.delta.chat/howto.html). + +### Deploy with pyinfra + +If you use [pyinfra](https://pyinfra.com/) to manage a server, +you can deploy this bot with it. +Just import it into your [deploy.py file](https://docs.pyinfra.com/en/2.x/getting-started.html#create-a-deploy) like this: + +``` +from teams_bot.pyinfra import deploy_teams_bot + +deploy_teams_bot( + unix_user='root', # an existing UNIX user (doesn't need root or sudo privileges) + bot_email='team@example.org', # the email address your team wants to use + bot_passwd='p4ssw0rd', # the password to the email account +) +``` + +After you deployed it, +you need to do two steps manually: + +First, +login to the user with ssh +and run +`export $(cat ~/.env | xargs) && ~/.local/lib/teams-bot.venv/bin/teams-bot init` +to initialize the bot, +create the crew, +and join the crew. + +Then run +`systemctl --user start teams-bot` +to start the bot +and keep it running in the background. + +You can view the log output +with `journalctl --user -fu teams-bot` +to confirm that it works. + ## Development Environment To get started with developing, -- 2.43.4 From 4fcc99a89120dc8924d30fe6ae7922c877dfb005 Mon Sep 17 00:00:00 2001 From: missytake Date: Sun, 8 Oct 2023 16:54:08 +0200 Subject: [PATCH 09/10] consistent example email address --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b3b3350..998d2c6 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ with an email address you want to use as a team: ``` -teams-bot init --email das_synthikat@systemli.org --password p455w0rD +teams-bot init --email helpdesk@example.org --password p455w0rD ``` This command will show a QR code; @@ -44,7 +44,7 @@ Now to run it, simply execute: ``` -teams-bot init --email das_synthikat@systemli.org --password p455w0rD +teams-bot init --email helpdesk@example.org --password p455w0rD ``` The bot only works as long as this command is running. @@ -62,9 +62,9 @@ Just import it into your [deploy.py file](https://docs.pyinfra.com/en/2.x/gettin from teams_bot.pyinfra import deploy_teams_bot deploy_teams_bot( - unix_user='root', # an existing UNIX user (doesn't need root or sudo privileges) - bot_email='team@example.org', # the email address your team wants to use - bot_passwd='p4ssw0rd', # the password to the email account + unix_user='root', # an existing UNIX user (doesn't need root or sudo privileges) + bot_email='helpdesk@example.org', # the email address your team wants to use + bot_passwd='p4ssw0rd', # the password to the email account ) ``` -- 2.43.4 From 82b1fd8c01d13ca9c55668e08fd8c07a80e3957c Mon Sep 17 00:00:00 2001 From: missytake Date: Mon, 9 Oct 2023 07:49:06 +0200 Subject: [PATCH 10/10] pyinfra: specify environment variable --- src/teams_bot/pyinfra.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/teams_bot/pyinfra.py b/src/teams_bot/pyinfra.py index 69eda69..8c6fc09 100644 --- a/src/teams_bot/pyinfra.py +++ b/src/teams_bot/pyinfra.py @@ -43,8 +43,7 @@ def deploy_teams_bot(unix_user: str, bot_email: str, bot_passwd: str, dbdir: str secrets = [ f"TEAMS_INIT_EMAIL={bot_email}", f"TEAMS_INIT_PASSWORD={bot_passwd}", - f'TEAMS_INIT_DBDIR={dbdir}', - f'TEAMS_RUN_DBDIR={dbdir}', + f'TEAMS_DBDIR={dbdir}', ] env = "\n".join(secrets) files.put( -- 2.43.4