Skip to content

Commit

Permalink
[distgit] build docker image on dist_git_importer startup
Browse files Browse the repository at this point in the history
  • Loading branch information
FrostyX committed Sep 15, 2016
1 parent a69c178 commit 25c7d91
Showing 1 changed file with 42 additions and 23 deletions.
65 changes: 42 additions & 23 deletions dist-git/dist_git/dist_git_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def build(self):
git_subdir = "{}/{}".format(self.git_dir, self.task.tito_git_dir)

log.debug(' '.join(cmd))
run_in_vm(cmd, dst_dir=self.tmp_dest, src_dir=git_subdir, cwd=git_subdir)
VM.run(cmd, dst_dir=self.tmp_dest, src_dir=git_subdir, cwd=git_subdir)


class MockScmProvider(SrpmBuilderProvider):
Expand All @@ -309,7 +309,7 @@ def get_srpm(self):
"--buildsrpm", "--resultdir={}".format(self.tmp_dest)]
log.debug(' '.join(cmd))

run_in_vm(cmd, dst_dir=self.tmp_dest)
VM.run(cmd, dst_dir=self.tmp_dest)
self.copy()

def scm_option_get(self, package_name, branch):
Expand Down Expand Up @@ -339,7 +339,7 @@ def get_srpm(self):

log.debug(' '.join(cmd))

output, error = run_in_vm(cmd, dst_dir=self.tmp_dest)
output, error = VM.run(cmd, dst_dir=self.tmp_dest)

log.info(output)
log.info(error)
Expand All @@ -357,7 +357,7 @@ def get_srpm(self):
# https://github.com/fedora-ruby/gem2rpm/issues/60
cmd = ["gem2rpm", self.task.rubygems_gem_name.strip(), "--fetch", "--srpm"]
log.info(' '.join(cmd))
output, error = run_in_vm(cmd, dst_dir=self.tmp_dest, cwd=self.tmp_dest)
output, error = VM.run(cmd, dst_dir=self.tmp_dest, cwd=self.tmp_dest)

if "Empty tag: License" in error:
raise SrpmBuilderException("{}\n{}\n{}".format(
Expand Down Expand Up @@ -544,6 +544,7 @@ def teardown_per_task_logging(self, handler):
def run(self):
log.info("DistGitImported initialized")

VM.build_image()
pool = Pool(workers=3)
self.is_running = True
while self.is_running:
Expand Down Expand Up @@ -630,25 +631,43 @@ def get_multiple(cls, builds, limit):
return results


def run_in_vm(cmd, dst_dir, src_dir="/tmp", cwd="/"):
"""
Run command in Virtual Machine (Docker)
:param cmd: list
:return: tuple output, error
"""
try:
docker_cmd = ["docker", "run",
"--privileged",
"-v", "{}:{}".format(dst_dir, dst_dir),
"-v", "{}:{}".format(src_dir, src_dir),
"-w", cwd,
"srpm-producer"] + cmd
proc = Popen(docker_cmd, stdout=PIPE, stderr=PIPE)
class VM(object):
hash = None

@staticmethod
def run(cmd, dst_dir, src_dir="/tmp", cwd="/"):
"""
Run command in Virtual Machine (Docker)
:param cmd: list
:return: tuple output, error
"""
try:
docker_cmd = ["docker", "run",
"--privileged",
"-v", "{}:{}".format(dst_dir, dst_dir),
"-v", "{}:{}".format(src_dir, src_dir),
"-w", cwd,
VM.hash] + cmd
log.debug("Running: {}".format(" ".join(docker_cmd)))
proc = Popen(docker_cmd, stdout=PIPE, stderr=PIPE)
output, error = proc.communicate()
except OSError as e:
raise SrpmBuilderException(str(e))

if proc.returncode != 0:
raise SrpmBuilderException(error)

return output, error

@staticmethod
def build_image():
cmd = ["docker", "build", "-q", os.path.join(os.path.dirname(__file__), "docker")]
log.debug("Building VM image: {}".format(" ".join(cmd)))
proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
output, error = proc.communicate()
except OSError as e:
raise SrpmBuilderException(str(e))

if proc.returncode != 0:
raise SrpmBuilderException(error)
if proc.returncode != 0 or error:
raise CoprDistGitException(error)

return output, error
VM.hash = output.split(":")[1][:12]
return output

0 comments on commit 25c7d91

Please sign in to comment.