From 179f99ad9dfe32fcbd00ef8157b88c05726e1602 Mon Sep 17 00:00:00 2001 From: Michael Scherer Date: Wed, 28 Sep 2022 14:47:59 +0200 Subject: [PATCH] Simplify sync by cloning in a temporary directory See #72 and #73 --- config.py | 2 -- console | 4 +--- elekto/controllers/webhook.py | 5 +---- elekto/models/meta.py | 12 ++++++------ 4 files changed, 8 insertions(+), 15 deletions(-) diff --git a/config.py b/config.py index 542252d..d73fdcd 100644 --- a/config.py +++ b/config.py @@ -83,12 +83,10 @@ # application via gitops, see the design documentation [/docs/DESIGN.md] for # more detailed information on working. # - REMOTE : Remote repository url -# - PATH : Where the meta repository is cloned (if development is local) # - DEPLOYMENT : mode of deployment (local, sidecar) META = { 'REMOTE': env('META_REPO'), 'ELECDIR': env('ELECTION_DIR'), - 'PATH': env('META_PATH', 'meta'), 'DEPLOYMENT': env('META_DEPLOYMENT', 'local'), 'BRANCH': env('META_BRANCH', 'main'), 'SECRET': env('META_SECRET') diff --git a/console b/console index 92a8f5b..4ca79bc 100755 --- a/console +++ b/console @@ -81,10 +81,8 @@ if __name__ == "__main__": print('# ----------- Syncing the meta with the database ----------- #') - if not os.path.exists(backend.META) or not os.path.isdir(backend.META): - backend.clone() + backend.clone() - backend.pull() print(sync(SESSION, meta.Election.all())) exit() diff --git a/elekto/controllers/webhook.py b/elekto/controllers/webhook.py index 94d9d17..a3e04e3 100644 --- a/elekto/controllers/webhook.py +++ b/elekto/controllers/webhook.py @@ -27,8 +27,5 @@ @csrf.exempt def webhook_sync(): backend = meta.Meta(APP.config['META']) - if not os.path.exists(backend.META) or not os.path.isdir(backend.META): - backend.clone() - else: - backend.pull() + backend.clone() return sync(SESSION, meta.Election.all()) diff --git a/elekto/models/meta.py b/elekto/models/meta.py index 8ce7f8d..db9b1a5 100644 --- a/elekto/models/meta.py +++ b/elekto/models/meta.py @@ -16,6 +16,8 @@ import os import random +import subprocess +import tempfile import flask as F from datetime import datetime @@ -32,20 +34,18 @@ class Meta: """ def __init__(self, config): - self.META = os.path.abspath(config['PATH']) + # keep this, as it ties the directory to the object lifecycle + self.TMPDIR = tempfile.TemporaryDirectory("elekto") + self.META = self.TMPDIR.name self.ELECDIR = config['ELECDIR'] self.REMOTE = config['REMOTE'] self.BRANCH = config['BRANCH'] self.SECRET = config['SECRET'] self.git = '/usr/bin/git' - self.pref = "/usr/bin/git --git-dir={}/.git --work-tree={}\ - ".format(self.META, self.META) def clone(self): - os.system('{} clone -b {} -- {} {}'.format(self.git, self.BRANCH, self.REMOTE, self.META)) + subprocess.check_call([self.git, 'clone', '-b', self.BRANCH, '--', self.REMOTE, self.META]) - def pull(self): - os.system('{} pull --ff-only origin {}'.format(self.pref, self.BRANCH)) class Election(Meta):