X-Git-Url: https://scripts.mit.edu/gitweb/wizard.git/blobdiff_plain/a9d643d150d1c3a9cdc33b180ff6e17612fd768f..30380c4b5b28df9670ea5952e14bc485d1d34133:/wizard/deploy.py?ds=sidebyside diff --git a/wizard/deploy.py b/wizard/deploy.py index 73f0095..67c4118 100644 --- a/wizard/deploy.py +++ b/wizard/deploy.py @@ -10,6 +10,10 @@ import logging import decorator import datetime import tempfile +import time +import traceback +import shutil +import errno import wizard from wizard import app, git, old_log, scripts, shell, sql, util @@ -147,7 +151,7 @@ class Deployment(object): """ repo = self.application.repository(srv_path) try: - shell.Shell().eval("git", "--git-dir", repo, "rev-parse", self.app_version.scripts_tag, '--') + shell.eval("git", "--git-dir", repo, "rev-parse", self.app_version.scripts_tag, '--') except shell.CallError: raise NoTagError(self.app_version.scripts_tag) @@ -159,13 +163,12 @@ class Deployment(object): corresponds to the one in the remote repository. """ with util.ChangeDirectory(self.location): - sh = shell.Shell() repo = self.application.repository(srv_path) def repo_rev_parse(tag): - return sh.eval("git", "--git-dir", repo, "rev-parse", tag) + return shell.eval("git", "--git-dir", repo, "rev-parse", tag) def self_rev_parse(tag): try: - return sh.safeCall("git", "rev-parse", tag, strip=True) + return shell.safeCall("git", "rev-parse", tag, strip=True) except shell.CallError: raise NoLocalTagError(tag) def compare_tags(tag): @@ -175,7 +178,7 @@ class Deployment(object): if not compare_tags(self.app_version.scripts_tag): raise InconsistentScriptsTagError(self.app_version.scripts_tag) parent = repo_rev_parse(self.app_version.scripts_tag) - merge_base = sh.safeCall("git", "merge-base", parent, "HEAD", strip=True) + merge_base = shell.safeCall("git", "merge-base", parent, "HEAD", strip=True) if merge_base != parent: raise HeadNotDescendantError(self.app_version.scripts_tag) @@ -271,7 +274,7 @@ class Deployment(object): except old_log.ScriptsVersionNoSuchFile: pass if not self._app_version: - appname = shell.Shell().eval("git", "config", "remote.origin.url").rpartition("/")[2].partition(".")[0] + appname = shell.eval("git", "config", "remote.origin.url").rpartition("/")[2].partition(".")[0] self._app_version = app.ApplicationVersion.make(appname, "unknown") return self._app_version @property @@ -347,20 +350,24 @@ class ProductionCopy(Deployment): shutil.rmtree(tmpdir) raise backup = None - while 1: - backup = str(self.version) + "-" + datetime.datetime.today().strftime("%Y-%m-%dT%H%M%S") - outdir = os.path.join(backupdir, backup) - if os.path.exists(outdir): - logging.warning("Backup: A backup occurred in the last second. Trying again in a second...") - time.sleep(1) - continue - try: - os.rename(tmpdir, outdir) - except OSError: - logging.warning("Backup: We lost the race, trying again in a second...") - time.sleep(1) - continue - break + with util.LockDirectory(os.path.join(backupdir, "lock")): + while 1: + backup = str(self.version) + "-" + datetime.datetime.today().strftime("%Y-%m-%dT%H%M%S") + outdir = os.path.join(backupdir, backup) + if os.path.exists(outdir): + logging.warning("Backup: A backup occurred in the last second. Trying again in a second...") + time.sleep(1) + continue + try: + shutil.move(tmpdir, outdir) + except: + # don't leave half-baked stuff lying around + try: + shutil.rmtree(outdir) + except OSError: + pass + raise + break return backup @chdir_to_location def restore(self, backup, options): @@ -389,7 +396,7 @@ class ProductionCopy(Deployment): """ Performs a HTTP request on the website. """ - return util.fetch(self.url.netloc, self.url.path, path, post) + return util.fetch(self.url.netloc, self.url.path, path, post) # pylint: disable-msg=E1103 class WorkingCopy(Deployment): """