Commit b3bd5e2d authored by BAIRE Anthony's avatar BAIRE Anthony
Browse files

rename constants

parent 6a25c44b
......@@ -434,7 +434,7 @@ class SandboxManager(Manager):
"""Narrow a WebappVersion query to select the candidate versions to be committed"""
return (query.
filter_by(webapp_id=webapp_id,
state = int(VersionState.sandbox))
state = int(VersionState.SANDBOX))
)
def _start(self, webapp, version):
......@@ -595,7 +595,7 @@ EOF
ses.execute('''UPDATE webapp_versions
SET changelog=CONCAT(changelog, " [", :changelog, "]"), state=:state
WHERE id IN :ids''', dict(changelog=changelog, ids=recover,
state=int(VersionState.error)))
state=int(VersionState.ERROR)))
# create a recovery version
version = WebappVersion(
......@@ -603,7 +603,7 @@ EOF
number = time.strftime("recovery-%Y%m%d-%H%M%S"),
changelog = changelog,
published = False,
state = int(VersionState.sandbox))
state = int(VersionState.SANDBOX))
ses.add(version)
ses.refresh(version)
ses.expunge(version)
......@@ -619,13 +619,13 @@ EOF
next_state = image_size = None
try:
# stop the container (if stopping or if creating a new sandbox)
if webapp.sandbox_state in (SandboxState.stopping, SandboxState.starting):
if webapp.sandbox_state in (SandboxState.STOPPING, SandboxState.STARTING):
ctrl.sandbox.stop(container)
ctrl.sandbox.wait(container)
# commit
cid = ctrl.sandbox.commit(container, webapp.image_name, version.number)
next_state = VersionState.committed
next_state = VersionState.COMMITTED
image_size = ctrl.sandbox.inspect_image(cid)["Size"]
return version, error
......@@ -633,7 +633,7 @@ EOF
except docker.errors.NotFound:
error = "commit error: container not found %r" % container
log.error("%s", error)
next_state = VersionState.error
next_state = VersionState.ERROR
image_size = 0
ses.execute('''UPDATE webapp_versions
SET changelog=CONCAT(changelog, " [commit error: sandbox is down]")
......@@ -731,11 +731,11 @@ EOF
phase = "inspect"
next_state = fail_state = None
try:
if webapp.sandbox_state == SandboxState.starting:
if webapp.sandbox_state == SandboxState.STARTING:
# start the sandbox
phase = "start"
next_state = SandboxState.running
fail_state = SandboxState.start_error
next_state = SandboxState.RUNNING
fail_state = SandboxState.START_ERROR
# commit (if a sandbox exists)
yield from self._manage_commit(webapp, commit_versions, force=True)
......@@ -752,11 +752,11 @@ EOF
# start sandbox
yield from self.run_in_executor(self._start, webapp, sandbox_version)
elif webapp.sandbox_state == SandboxState.stopping:
elif webapp.sandbox_state == SandboxState.STOPPING:
# stop the sandbox
phase = "stop"
next_state = SandboxState.idle
fail_state = SandboxState.stop_error
next_state = SandboxState.IDLE
fail_state = SandboxState.STOP_ERROR
# commit (if requested)
yield from self._manage_commit(webapp, commit_versions)
......@@ -1078,8 +1078,8 @@ class JobManager(Manager):
webapp_id = job.webapp_id,
number = job.version).filter(
WebappVersion.state.in_((
int(VersionState.committed),
int(VersionState.ready)))
int(VersionState.COMMITTED),
int(VersionState.READY)))
).order_by(
WebappVersion.state.desc(),
WebappVersion.id.desc()).first()
......@@ -1181,11 +1181,11 @@ class PushManager(Manager):
with ses.begin():
# get the version object and check its state
version = ses.query(WebappVersion).filter_by(id=version_id).one()
if version.state != VersionState.committed:
if version.state in (VersionState.ready, VersionState.replaced):
if version.state != VersionState.COMMITTED:
if version.state in (VersionState.READY, VersionState.REPLACED):
# already pushed
return
if version.state == VersionState.sandbox:
if version.state == VersionState.SANDBOX:
raise Error("unable to push (image not yet committed)")
raise Error("unable to push (invalid state: %s)" % version.state)
......@@ -1194,7 +1194,7 @@ class PushManager(Manager):
others = (ses.query(WebappVersion.id)
.filter_by(webapp_id=version.webapp_id, number=version.number)
.filter(WebappVersion.id != version.id)
.filter(WebappVersion.state.in_((int(VersionState.sandbox), int(VersionState.committed)))))
.filter(WebappVersion.state.in_((int(VersionState.SANDBOX), int(VersionState.COMMITTED)))))
if others.count():
raise Error("unable to push (there are other pushable versions with the same number: %s)" % (
" ".join(map(str, itertools.chain(*others)))))
......@@ -1212,19 +1212,19 @@ class PushManager(Manager):
prev = ses.query(WebappVersion).filter_by(
webapp_id = version.webapp_id,
number = version.number,
state = int(VersionState.ready)).scalar()
state = int(VersionState.READY)).scalar()
log.debug("prev version id %r", (prev.id if prev else None))
if prev is None:
# this is a new version
version.state = int(VersionState.ready)
version.state = int(VersionState.READY)
else:
# overwrite an existing version
for key in "updated_at", "changelog", "published":
setattr(prev, key, getattr(version, key))
# mark this version as replaced
version.state = int(VersionState.replaced)
version.state = int(VersionState.REPLACED)
ses.add(version)
class ImageManager:
......@@ -1259,24 +1259,24 @@ class ImageManager:
if swarm:
# pull to the swarm
if version.state == VersionState.committed:
if version.state == VersionState.COMMITTED:
# must be pushed to the registry first
yield from self.push(version_id)
with ses.begin():
version = ses.query(WebappVersion).filter_by(id=version_id).one()
if version.state not in (VersionState.ready, VersionState.replaced):
if version.state not in (VersionState.READY, VersionState.REPLACED):
raise Error("bad version state: %s" % version.state)
yield from self.swarm_pull_manager.process((image, tag))
else:
# pull to the sandbox
if version.state == VersionState.committed:
if version.state == VersionState.COMMITTED:
# do not pull!
return
if version.state not in (VersionState.ready, VersionState.replaced):
if version.state not in (VersionState.READY, VersionState.REPLACED):
raise Error("bad version state: %s" % version.state)
yield from self.sandbox_pull_manager.process((image, tag))
......@@ -1409,17 +1409,17 @@ class DockerController:
with ses.begin():
if startup:
ses.execute("DELETE FROM webapp_versions WHERE state=%d"
% VersionState.replaced)
% VersionState.REPLACED)
for version_id, in ses.execute(
"SELECT id FROM webapp_versions WHERE state=%d"
% VersionState.committed).fetchall():
% VersionState.COMMITTED).fetchall():
self.image_manager.push(version_id)
for webapp_id, in ses.execute("""SELECT webapps.id FROM webapps
LEFT JOIN webapp_versions ON webapps.id=webapp_versions.webapp_id
WHERE sandbox_state IN (%d,%d) OR state=%d
GROUP BY webapps.id""" % (
SandboxState.starting, SandboxState.stopping, VersionState.sandbox)).fetchall():
SandboxState.STARTING, SandboxState.STOPPING, VersionState.SANDBOX)).fetchall():
self.sandbox_manager.process(webapp_id)
......
......@@ -13,19 +13,19 @@ Base = declarative_base()
class SandboxState(enum.IntEnum):
idle = 0
running = 1
starting = 2
start_error = 3
stopping = 4
stop_error = 5
IDLE = 0
RUNNING = 1
STARTING = 2
START_ERROR = 3
STOPPING = 4
STOP_ERROR = 5
class VersionState(enum.IntEnum):
sandbox = 0
committed = 1
ready = 2
error = 3
replaced = 4
SANDBOX = 0
COMMITTED = 1
READY = 2
ERROR = 3
REPLACED = 4
class JobState(enum.IntEnum):
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment