- if collection:
- if not args.image:
- docker_image = None
- while not docker_image and os.path.exists(os.path.join(mountdir, "docker_image")):
- try:
- with open(os.path.join(mountdir, "docker_image")) as di:
- docker_image = di.read().strip()
- except IOError as e:
- pass
-
- if not docker_image:
- logger.error("Collection must contain a file 'docker_image' or must specify --image on the command line.")
-
- if docker_image != prev_docker_image:
- if cid:
- logger.info("Stopping docker container")
- subprocess.check_call(["docker", "stop", cid])
-
- if docker_image:
- logger.info("Starting docker container %s", docker_image)
- cid = subprocess.check_output(["docker", "run",
- "--detach=true",
- "--publish=%i:80" % (port),
- "--volume=%s:/mnt:ro" % mountdir,
- docker_image])
- cid = cid.rstrip()
- prev_docker_image = docker_image
- logger.info("Container id is %s", cid)
- elif cid:
- subprocess.check_call(["docker", "kill", "--signal=HUP", cid])
- elif cid:
- logger.info("Stopping docker container")
- subprocess.call(["docker", "stop", cid])
- except subprocess.CalledProcessError:
- cid = None
-
- if not cid:
- logger.warning("No service running! Will wait for a new collection to appear in the project.")
- else:
- logger.info("Waiting for events")
- running = True
- loop = True
- while running:
- try:
- eq = evqueue.get(True, 1)
- logger.info("%s %s", eq[1], eq[2])
- newcollection = collection
- if eq[1] in ('add', 'update', 'create'):
- newcollection = eq[2]
- elif eq[1] == 'remove':
- collections = api.collections().list(filters=[["owner_uuid", "=", project]],
- limit=1,
- order='modified_at desc').execute()['items']
- newcollection = collections[0]['uuid'] if len(collections) > 0 else None
- running = False
- except Queue.Empty:
- pass
-
- except (KeyboardInterrupt):
- logger.info("Got keyboard interrupt")
- ws.close()
- loop = False
- except Exception as e:
- logger.exception(e)
- ws.close()
- loop = False
- finally:
- if cid:
- logger.info("Stopping docker container")
- cid = subprocess.call(["docker", "stop", cid])
-
- if mountdir:
- logger.info("Unmounting")
- subprocess.call(["fusermount", "-u", "-z", mountdir])
- os.rmdir(mountdir)
+ self.run_docker()
+ self.wait_for_events()
+ except (KeyboardInterrupt):
+ logger.info("Got keyboard interrupt")
+ self.ws.close()
+ self.loop = False
+ except Exception as e:
+ logger.exception("Caught fatal exception, shutting down")
+ self.ws.close()
+ self.loop = False
+ finally:
+ self.stop_docker()
+
+ if self.mountdir:
+ logger.info("Unmounting")
+ subprocess.call(["fusermount", "-u", self.mountdir])
+ os.rmdir(self.mountdir)
+
+
+def main(argv):
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--project-uuid', type=str, required=True, help="Project uuid to watch")
+ parser.add_argument('--port', type=int, default=8080, help="Host port to listen on (default 8080)")
+ parser.add_argument('--image', type=str, help="Docker image to run")
+
+ args = parser.parse_args(argv)
+
+ signal.signal(signal.SIGTERM, lambda signal, frame: sys.exit(0))
+
+ try:
+ arvweb = ArvWeb(args.project_uuid, args.image, args.port)
+ arvweb.run()
+ except arvados.errors.ArgumentError as e:
+ logger.error(e)
+ return 1
+
+ return 0