def run(_args): utils.run(["docker", "-v"]) utils.run(["docker-compose", "-v"]) resource = pkg_resources.require("d2-docker") if resource: version = pkg_resources.require("d2-docker")[0].version print("d2-docker version {}".format(version)) else: print("Cannot get d2-docker version")
def run(args): image_name = args.image or utils.get_running_image_name() sql_file = args.sql_file status = utils.get_image_status(image_name) is_running = status["state"] == "running" running_containers = utils.noop( status) if is_running else utils.running_containers with running_containers(image_name, "db") as status: db_container = status["containers"]["db"] utils.logger.debug("DB container: {}".format(db_container)) psql_cmd = ["psql", "-U", "dhis", "dhis2"] cmd = [ "docker", "exec", ("-i" if sql_file else "-it"), db_container, *psql_cmd ] if sql_file: utils.logger.info("Run SQL file {} for image {}".format( sql_file, image_name)) result = utils.run(cmd, stdin=(open(sql_file) if sql_file else None), raise_on_error=False) if result.returncode != 0: utils.logger.error("Could not execute SQL") return 1
def create_data(args): image = args.data_image docker_dir = utils.get_docker_directory("data", args) utils.logger.info("Create data image: {}".format(image)) with utils.temporal_build_directory(docker_dir) as build_dir: db_path = os.path.join(build_dir, "db/") utils.mkdir_p(db_path) if args.apps_dir: dest_apps_dir = os.path.join(build_dir, "apps") utils.logger.debug("Copy apps: {} -> {}".format( args.apps_dir, dest_apps_dir)) utils.copytree(args.apps_dir, dest_apps_dir) if args.documents_dir: dest_documents_dir = os.path.join(build_dir, "document") utils.logger.debug("Copy documents: {} -> {}".format( args.documents_dir, dest_documents_dir)) utils.copytree(args.documents_dir, dest_documents_dir) if args.sql: utils.logger.debug("Copy DB file: {} -> {}".format( args.sql, db_path)) shutil.copy(args.sql, db_path) utils.run(["docker", "build", build_dir, "--tag", image])
def get_images_info(running_containers): cmd_image = ["docker", "image", "ls", "--format={{.Repository}} {{.Tag}}"] result_image = utils.run(cmd_image, capture_output=True) lines_parts = [ line.split() for line in result_image.stdout.decode("utf-8").splitlines() ] data_image_names = [] void_tag = "<none>" for entry in lines_parts: if len(entry) != 2: continue repo, tag = entry if void_tag in repo or void_tag in tag: continue image_name = repo + ":" + tag if utils.DHIS2_DATA_IMAGE in repo: info = running_containers.get(image_name, None) or {} port = info.get("port", None) if port: deploy_path = info.get("deploy_path", None) extra_info = ",".join( filter( bool, [ "port={}".format(port), "deploy_path={}".format(deploy_path) if deploy_path else None, ], )) state = "RUNNING[{}]".format(extra_info) else: state = "STOPPED" value = { "port": port, "text": "{} {}".format(image_name, state), } data_image_names.append(value) return data_image_names