def stop_containers(imagename): containernames = get_running_containers(imagename) if len(containernames) > 0: code, sout, serr = run.call( ["docker", "stop"] + containernames ) if code > 0: common.fail("Error stopping container(s) !\n%s"%(sout)) code, sout, serr = run.call( ["docker", "update", "--restart=no"] + containernames )
def test_call_edai(self, edai_call): # Set up options = Namespace(edai_example=True) # Execute call(self.session, options) # Verify assert edai_call.call_count == 2
def test_call_api(self, api_call): # Set up options = Namespace(edai_example=False) # Execute call(self.session, options) # Verify assert api_call.call_count == 1
def image_exists(imagename): # Searching by ID res, sout, serr = run.call(["docker", "images", "--format", "{{.ID}}"], silent=True) images = sout.strip().split(os.linesep) if imagename in images: return True # Search by name res, sout, serr = run.call(["docker", "images", "--format", "{{.ID}}", imagename], silent=True) images = sout.strip().split(os.linesep) common.remove_empty_strings(images) if len(images) > 0: return True return False
def list_on_image(imagename, category): psfilter = ps_filter(imagename) if category == "containers": print_call(["docker", "ps", "-a"]+psfilter) elif category == "running": print_call(["docker", "ps"]+psfilter) elif category == "images": # list of images associated with image name imagelist = get_image_list_for(imagename) # list of all images res,sout,serr = run.call(["docker", "images"], silent=True) stringlines = sout.strip().split(os.linesep) # header print("Relevant images: %s" % ', '.join(imagelist) ) print(stringlines[0]) # full image line, if image is in imagelist for line in filter_string_lines(stringlines, imagelist): print(line) elif category == "last" or category == "stable": print(store.read_data(category, imagename)) elif category == "jcl": print(store.read_data("jockler-%s"%imagename, imagename)) else: unknown_category(category)
def get_tagged_image_id(imagename): # This should perform an exact match res, sout, serr = run.call( ["docker", "images", "--format", "{{.ID}}", imagename], silent=True) image_id = sout.strip() return image_id
def do_build(imagename, dockerfile, build_path, build_args=[]): print("Building [%s] at [%s] taking files from [%s]" % (imagename, os.path.realpath(dockerfile), build_path)) return run.call(["docker", "build", "-t", imagename, "-f", dockerfile] + build_args + [build_path], stdout=sys.stdout, stderr=sys.stderr)
def do_linux_clear_data(volumes_array, volume_mountpoints): return run.call([ "docker", "run", "--rm", ] + volumes_restore + [lightos["linux"], "rm", "-rf"] + volume_mountpoints, stdout=sys.stdout, stderr=sys.stderr)
def start_container(imagename, containername, doattach=False): stop_containers(imagename) store.write_data("last", imagename, containername) runmode = [] useexec = False if doattach: runmode.append("-i") useexec = True print("Starting %s"%containername) code, sout, serr = run.call( ["docker", "start", containername]+runmode, stdout=sys.stdout, stderr=sys.stderr, useexec=useexec ) if code > 0 or not found_running_container(containername): common.fail("Could not start container %s - try starting with 'attach' mode'\n%s"%(containername,sout)) code, sout, serr = run.call( ["docker", "update", "--restart=unless-stopped", containername])
def do_linux_volume_backup(containername, destination, archivename, mountpoints): # Load into a basic container return run.call([ "docker", "run", "--rm", "--volumes-from", containername, "-v", "%s:/backup" % destination, lightos["linux"], "tar", "cvzf", "/backup/%s" % archivename, "-C", "/" ] + mountpoints, stdout=sys.stdout, stderr=sys.stderr)
def get_image_of(containername): res,sout,serr = run.call(["docker", "ps", "-a", "--format", "{{.Image}}"] + ps_filter(containername), silent=True) images = sout.strip().split("\n") common.remove_empty_strings(images) if len(images) != 1: common.fail("Could not find single image for %s"%containername) return images[0]
def mountdata_of(containername, infolabel): res, sout, serr = run.call( ["docker", "container", "inspect", containername], silent=True) inspect_data = json.loads(sout) mounts_data = inspect_data[0]["Mounts"] infodata = [] for mountdef in mounts_data: infodata.append(mountdef[infolabel]) return infodata
def remove(args, keep_images=[], keep_containers=[]): if not common.args_check(args, 1): common.fail( "Remove all containers and images associated with an image.\n\nUsage:\n\n jockler remove IMAGENAME" ) imagename = args[0] allimages = listing.get_image_list_for(imagename) allcontainers = listing.get_container_list_for(imagename) for k in set(keep_containers): try_remove(k, allcontainers) for k in set(keep_images): try_remove(k, allimages) if not confirm_removal(allcontainers, allimages, keep_containers, keep_images): common.fail("ABORTED") # Remove the image tag run.call(["docker", "rmi", imagename]) run.call(["docker", "rm"] + allcontainers, stdout=sys.stdout, silent=True) run.call(["docker", "image", "rm"] + allimages, stdout=sys.stdout, silent=True) store.cleanup_images(imagename)
def do_linux_volume_restore(volumes_restore, destination, archivename): # Load into a basic container return run.call([ "docker", "run", "--rm", ] + volumes_restore + [ "-v", "%s:/backup" % destination, lightos["linux"], "tar", "xzvf", "/backup/%s" % archivename, "-C", "/" ], stdout=sys.stdout, stderr=sys.stderr)
def start_new_container(imagename, doattach=False): stop_containers(imagename) containername = generate_container_name(imagename) options = load_container_options(imagename) runmode = "-d" useexec = False if doattach: runmode = "-it" useexec = True code, sout, serr = run.call(["docker", "run", runmode, "--name=%s"%containername]+options+[imagename], useexec=useexec) if code > 0 or not found_running_container(containername): common.fail("Could not create new container for %s, or could not start created container:\n%s"%(imagename, sout)) # Do not do this on initial docker-run - otherwise, if the entrypoint is faulty # you get continually restarting containers that are hard to stop code, sout, serr = run.call( ["docker", "update", "--restart=unless-stopped", containername]) store.write_data("last", imagename, containername) return containername
def get_container_list_for(imagename): res,sout,serr = run.call(["docker", "ps", "-a", "--format", "{{.Names}}"] + ps_filter(imagename), silent=True) containers = sout.strip().split(os.linesep) common.remove_empty_strings(containers) return containers
def get_running_containers(imagename): code, sout,serr = run.call(["docker","ps", "--format", "{{.Names}}", "--filter", "name=jcl_%s_"%imagename], silent=True) containernames = sout.strip().split("\n") common.remove_empty_strings(containernames) return containernames
def force_stop(containername): res,sout,serr = run.call(["docker", "update", "--restart=no", containername]) if res > 0: common.fail("Could not update restart policy on container [%s]"%containername) res,sout,serr = run.call(["docker", "stop", containername])
def found_running_container(containername): time.sleep(1) code, sout, serr = run.call(["docker", "ps", "--format", "{{.Names}}", "--filter", "name=%s"%containername], silent=True) containers = sout.strip().split(os.linesep) return containername in containers
def print_call(command_array): run.call(command_array, stdout=stdout, stderr=stderr)