Browse Source

Make update more stable

master
Burathar 3 years ago
parent
commit
630fcb42c7
  1. 34
      biscd/biscd/models/project.py
  2. 4
      biscd/biscd/templates/project.html

34
biscd/biscd/models/project.py

@ -1,9 +1,10 @@ @@ -1,9 +1,10 @@
from pathlib import Path
import re
from shutil import rmtree
import string
import random
from werkzeug.utils import secure_filename
from git import Repo, InvalidGitRepositoryError, GitCommandError
from git import Repo, InvalidGitRepositoryError, GitCommandError, cmd as git_cmd
from flask import current_app as app
from biscd import config
@ -56,36 +57,53 @@ class Project(YamlSerializable): @@ -56,36 +57,53 @@ class Project(YamlSerializable):
repo = Repo(path)
app.logger.info('Already under git control')
except InvalidGitRepositoryError:
app.logger.info(f"Cloning repo from {self.git_repo}")
repo = Repo.clone_from(self.git_repo, path)
app.logger.info('Done!')
repo = self._clone_repo(path)
# If repo is not type Repo it must be an error and thus returned directly
if not isinstance(repo, Repo):
return repo
remotes = repo.remotes
if not any(remotes):
app.logger.error(f"Repo {path} doesn't have any remotes.")
return ["Repo doesn't have any remotes. please fix manually", 'error']
response = remotes[0].pull()
for item in response:
# TODO: is this nessecary?
app.logger.debug(item)
response = self._checkout_branch(repo, self.branch)
return response
def _clone_repo(self, path):
app.logger.info("Cloning repo from %s", self.git_repo)
try:
repo = Repo.clone_from(self.git_repo, path)
except GitCommandError as gce:
app.logger.error("Project %s's git repo: %s is invalid: %s",
self.name, self.git_repo, gce.stderr.strip('\n '))
return ["Project does not have a valid git repository url", "error"]
app.logger.info('Cloning repo (%s) succeeded!', self.git_repo)
return repo
def _checkout_branch(self, repo, branch):
try:
response = repo.git.checkout(self.branch)
repo.git.checkout(branch)
except AttributeError:
return [f"Branch is not set for {self.name}", 'error']
except GitCommandError:
return [
f"Repo '{self.git_repo}' does not have an existing branch called {self.branch}",
f"Repo '{self.git_repo}' does not have an existing branch called {branch}",
'error']
return None
def delete_files(self):
if self._production_path_exists():
if self.production_path_exists():
path = self.absulute_path
app.logger.info(f"Deleting {path}")
rmtree(path)
self.relative_production_path = None
self.save()
def _production_path_exists(self):
def production_path_exists(self):
"""Return True if project's the production directory path exists"""
prod_path_dir = Path(config['production_path'].get())
if hasattr(self, 'relative_production_path') and self.relative_production_path:

4
biscd/biscd/templates/project.html

@ -8,12 +8,16 @@ @@ -8,12 +8,16 @@
<a href="{{ url_for('project_change_settings', project_name=project.name) }}">
<button class="btn btn-primary">Change Settings</button>
</a>
{% if project.git_repo %}
<a href="{{ url_for('project_update', project_name=project.name) }}">
<button class="btn btn-success">Update Repo</button>
</a>
{% endif %}
{% if project.production_path_exists() %}
<a href="{{ url_for('project_delete_files', project_name=project.name) }}">
<button class="btn btn-danger">Delete Local Repo</button>
</a>
{% endif %}
<br><br>
</row>
{% endif %}

Loading…
Cancel
Save