Skip to content

Commit

Permalink
main: add basic support for repo + worker (bug 1865907) (#5)
Browse files Browse the repository at this point in the history
- add Repo and Worker models
- add basic support commands
- add method to initialize repo in filesystem
  • Loading branch information
zzzeid authored Nov 27, 2023
1 parent 93d1b89 commit d8c9b02
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lando/lando/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@
MEDIA_URL = "media/"
MEDIA_ROOT = "/mediafiles"

REPO_ROOT = f"{MEDIA_ROOT}/repos"


# Default primary key field type
# https://docs.djangoproject.com/en/dev/ref/settings/#default-auto-field
Expand Down
66 changes: 66 additions & 0 deletions src/lando/main/migrations/0002_repo_worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Generated by Django 5.0b1 on 2023-11-23 19:56

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("main", "0001_initial"),
]

operations = [
migrations.CreateModel(
name="Repo",
fields=[
(
"basemodel_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="main.basemodel",
),
),
("name", models.CharField(max_length=255, unique=True)),
("default_branch", models.CharField(default="main", max_length=255)),
("url", models.CharField(max_length=255)),
("push_path", models.CharField(max_length=255)),
("pull_path", models.CharField(max_length=255)),
("is_initialized", models.BooleanField(default=False)),
(
"system_path",
models.FilePathField(
allow_folders=True, max_length=255, path="/mediafiles/repos"
),
),
],
bases=("main.basemodel",),
),
migrations.CreateModel(
name="Worker",
fields=[
(
"basemodel_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="main.basemodel",
),
),
("name", models.CharField(max_length=255, unique=True)),
("is_paused", models.BooleanField(default=False)),
("is_stopped", models.BooleanField(default=False)),
("ssh_private_key", models.TextField(blank=True, null=True)),
("throttle_seconds", models.IntegerField(default=10)),
("sleep_seconds", models.IntegerField(default=10)),
("applicable_repos", models.ManyToManyField(to="main.repo")),
],
bases=("main.basemodel",),
),
]
53 changes: 53 additions & 0 deletions src/lando/main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import datetime
import logging
import os
import subprocess
from pathlib import Path
from typing import (
Any,
Iterable,
Expand All @@ -13,6 +15,7 @@
from django.db.models import QuerySet
from django.utils.translation import gettext_lazy

from lando import settings
from lando.utils import build_patch_for_revision

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -179,3 +182,53 @@ def add_job_with_revisions(revisions: list[Revision], **params: Any) -> LandingJ
for revision in revisions:
job.revisions.add(revision)
return job


class Repo(BaseModel):
name = models.CharField(max_length=255, unique=True)
default_branch = models.CharField(max_length=255, default="main")
url = models.CharField(max_length=255)
push_path = models.CharField(max_length=255)
pull_path = models.CharField(max_length=255)
is_initialized = models.BooleanField(default=False)

system_path = models.FilePathField(
path=settings.REPO_ROOT, max_length=255, allow_folders=True
)

def _run(self, *args, cwd=None):
cwd = cwd or self.system_path
command = ["git"] + list(args)
result = subprocess.run(command, cwd=cwd)
return result

def initialize(self):
if self.is_initialized:
raise

self.system_path = str(Path(settings.REPO_ROOT) / self.name)
self.save()
result = self._run("clone", self.pull_path, self.name, cwd=settings.REPO_ROOT)
if result.returncode == 0:
self.is_initialized = True
else:
raise Exception(result.returncode)
self.save()

def update(self):
self._run("pull", "--all", "--prune")

def reset(self, branch=None):
self._run("reset", "--hard", branch or self.default_branch)
self._run("clean", "--force")


class Worker(BaseModel):
name = models.CharField(max_length=255, unique=True)
is_paused = models.BooleanField(default=False)
is_stopped = models.BooleanField(default=False)
ssh_private_key = models.TextField(null=True, blank=True)
applicable_repos = models.ManyToManyField(Repo)

throttle_seconds = models.IntegerField(default=10)
sleep_seconds = models.IntegerField(default=10)

0 comments on commit d8c9b02

Please sign in to comment.