From 1b249c0a042173510857fe8da9ad26035f6c791d Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Oct 11 2023 08:19:54 +0000 Subject: Add a script and a playbook to import IRC cookies to Matrix Signed-off-by: Aurélien Bompard --- diff --git a/files/zodbot/karma-to-cookies-db.py b/files/zodbot/karma-to-cookies-db.py new file mode 100755 index 0000000..a7643b1 --- /dev/null +++ b/files/zodbot/karma-to-cookies-db.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +import os +import shelve +from argparse import ArgumentParser + +import sqlalchemy as sa +from sqlalchemy.ext.declarative import declarative_base + + +Base = declarative_base() + + +class Cookie(Base): + __tablename__ = "cookies" + __table_args__ = ( + sa.Index("idx_cookies_to_user_release", "to_user", "release"), + ) + from_user = sa.Column(sa.String(254), nullable=False, primary_key=True) + to_user = sa.Column(sa.String(254), nullable=False, primary_key=True) + release = sa.Column(sa.String(63), nullable=False, primary_key=True) + value = sa.Column(sa.Integer, nullable=False, default=1) + date = sa.Column(sa.DateTime, nullable=False, server_default=sa.func.current_timestamp()) + + +def get_pg_url(): + with open(os.path.expanduser("~/.pgpass")) as fh: + for line in fh: + hostname, port, database, username, password = line.strip().split(":") + if username == "maubot": + return f"postgresql://{username}:{password}@{hostname}/{database}" + + +def parse_args(): + parser = ArgumentParser() + parser.add_argument("-i", "--instance", required=True, help="The maubot instance name") + parser.add_argument("karma_db", help="The Limnoria Karma DB") + # parser.add_argument("cookies_db", help="The Maubot Cookies DB") + return parser.parse_args() + + +def main(): + args = parse_args() + karma_data = shelve.open(args.karma_db, flag="r", protocol=2) + # engine = sa.create_engine(f"sqlite:///{args.cookies_db}") + engine = sa.create_engine(get_pg_url()) + Cookie.__table__.schema = f"mbp_{args.instance}" + # Base.metadata.create_all(engine) + Session = sa.orm.sessionmaker(bind=engine) + session = Session() + for mode, data in karma_data.items(): + direction, release = mode.split("-") + if direction != "forwards": + continue + if not release.startswith("f"): + continue + print(release, len(data)) + for agent, gifts in data.items(): + for recip, value in gifts.items(): + cookie = Cookie(from_user=agent, to_user=recip, release=release.upper(), value=value) + session.add(cookie) + try: + session.commit() + except sa.exc.IntegrityError: + session.rollback() + continue + + +if __name__ == "__main__": + main() diff --git a/playbooks/manual/import-irc-cookies-to-matrix.yml b/playbooks/manual/import-irc-cookies-to-matrix.yml new file mode 100644 index 0000000..d9fbcec --- /dev/null +++ b/playbooks/manual/import-irc-cookies-to-matrix.yml @@ -0,0 +1,42 @@ +# This playbook imports the cookies given on IRC to the Matrix Zodbot + +- name: Import the cookies + hosts: value02.iad2.fedoraproject.org:value02.stg.iad2.fedoraproject.org + user: root + + vars_files: + - /srv/web/infra/ansible/vars/global.yml + - "/srv/private/ansible/vars.yml" + - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml + handlers: + - import_tasks: "{{ handlers_path }}/restart_services.yml" + + tasks: + + - name: add the pgpass file + copy: + content: | + db01{{ env_suffix }}.iad2.fedoraproject.org:*:maubot:maubot::{{ {{ (env == 'production')|ternary(maubot_prod_db_password, maubot_stg_db_password) }} }} + dest: /root/.pgpass + owner: root + group: root + mode: 0400 + + - name: install the required package + dnf: + state: installed + name: python3-psycopg2 + + - name: install the import script + copy: + src: "{{ files }}/zodbot/karma-to-cookies-db.py" + dest: /usr/local/bin/karma-to-cookies-db + mode: 0755 + + - name: run the import script + command: + argv: + - /usr/local/bin/karma-to-cookies-db + - -i + - zodbot{% if env == "staging" %}-stg{% endif %}-fedora + - /var/lib/zodbot/data/karma.db