Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify sync by cloning in a temporary directory #75

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 1 addition & 3 deletions console
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 1 addition & 4 deletions elekto/controllers/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
12 changes: 6 additions & 6 deletions elekto/models/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import os
import random
import subprocess
import tempfile
import flask as F

from datetime import datetime
Expand All @@ -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):
Expand Down