Exemplo n.º 1
0
def evaluate_generate():
    """ Evaluate the correctness of Generated Dockerfiles """
    incorrect = 0
    correct = 0
    # Directories to test with
    directories = (
        "gentest1", "gentest2", "gentest3", "gentest4", "gentest5", "gentest6",
        "gentest7", "gentest8", "gentest9", "gentest10", "gentest11",
        "gentest12"
    )
    # Run evaluation for each directory
    for file in directories:
        data.GENERATE_DATA["output"].append("Evaluating " + file)
        # Generate Dockerfile
        generate.generate_dockerfile(
            "Docker-Automation/samples/" + file,
            add="&& chmod +x test.sh && ./test.sh"
        )
        # Build image
        images.build_image(
            "Docker-Automation/samples/" + file, file, threaded=False
        )
        # Run image
        if images.run_image(file + ":latest", args="--rm", sep=False) == 1:
            data.GENERATE_DATA["output"].append(
                "\tImage: " + file + " is incorrect"
            )
            incorrect = incorrect + 1
        else:
            data.GENERATE_DATA["output"].append(
                "\tImage: " + file + " is correct"
            )
            correct = correct + 1
        # Delete generated image
        os.system("docker rmi " + file + ":latest")
        data.GENERATE_DATA["output"].append("\n")
    # Add test information to data file
    data.GENERATE_DATA["num_tests"] = len(directories)
    data.GENERATE_DATA["num_correct"] = correct
    data.GENERATE_DATA["num_incorrect"] = incorrect
Exemplo n.º 2
0
def repl():
    """ Interactive command system """
    # Get command entered in terminal
    command = str(input(">> ")).split(' ')
    # Ensure command is valid
    while command[0] not in COMMAND_LIST:
        print("That is not a valid command")
        command = str(input(">> "))
    # Check if command was to exit or quit
    if command[0] != "exit" and command[0] != "quit":
        # Generate command entered
        if command[0] == "generate":
            directory = str(input("Input path to directory:\n"))
            to_dir = str(
                input("Input location to mount files (default:project/):")
            ).strip()
            if to_dir != "":
                generate.generate_dockerfile(directory, to_dir=to_dir)
            else:
                generate.generate_dockerfile(directory)
        # Build command entered
        elif command[0] == "build":
            # Check if image or container entered with command
            if len(command) < 2 or command[1] not in ("image", "container"):
                build_type = str(input("Build image or container: "))
                while build_type not in ("image", "container"):
                    build_type = str(input("Build image or container: "))
            else:
                build_type = command[1]
            # Build image
            if build_type == "image":
                images.build_image(str(input("Input path to directory:\n")),
                                   str(input("Input image name: ")))
            # Build container
            elif build_type == "container":
                containers.build_container(
                    str(input("Image to build container: ")),
                    str(input("Arguments: ")))
        # Run command entered
        elif command[0] == "run":
            # Check if image or container entered with command
            if len(command) < 2 or command[1] not in ("image", "container"):
                run_type = str(input("Run image or container: "))
                while run_type not in ("image", "container"):
                    run_type = str(input("Run image or container: "))
            else:
                run_type = command[1]
            # Run image
            if run_type == "image":
                image_name = str(input("Image Name: "))
                args = str(input("Arguments: "))
                if args != "":
                    images.run_image(image_name, args=args)
                else:
                    images.run_image(image_name)
            # Run container
            elif run_type == "container":
                container_name = str(input("Container Name: "))
                args = str(input("Arguments: "))
                if args != "":
                    containers.run_container(container_name, args=args)
                else:
                    containers.run_container(container_name)
        # Connect command entered
        elif command[0] == "connect":
            container_name = str(input("Container to connect to: "))
            arguments = str(input("Arguments to pass: "******"Command to run: "))
            containers.connect_container(container_name,
                                         args=arguments,
                                         command=command)
        # Delete command entered
        elif command[0] == "delete":
            # Check if image or container entered with command
            if len(command) < 2 or command[1] not in ("image", "container"):
                remove_type = str(input("Delete image or container: "))
                while remove_type not in ("image", "container"):
                    remove_type = str(input("Remove image or container: "))
            else:
                remove_type = command[1]
            # Delete image
            if remove_type == "image":
                image_name = str(input("Image to remove: ")).strip().split(',')
                images.delete_image(image_name)
            # Delete container
            elif remove_type == "container":
                container_name = str(
                    input("Container to remove: ")).strip().split(',')
                containers.delete_container(container_name)
        # List command entered
        elif command[0] == "list":
            # Check if image or container entered with command
            if len(command) < 2 or command[1] not in ("images", "containers"):
                list_type = str(input("List images or containers: "))
                while list_type not in ("images", "containers"):
                    list_type = str(input("List images or containers: "))
            else:
                list_type = command[1]
            # List images
            if list_type == "images":
                images = images.list_images()
                if images is not None:
                    for image in images:
                        print(image)
                else:
                    print("None")
            # List containers
            elif list_type == "containers":
                containers = containers.list_containers()
                if containers is not None:
                    for container in containers:
                        print(container)
                else:
                    print("None")

        repl()
    return 0
Exemplo n.º 3
0
def build_image():
    """ Path to call image builder """
    directory = request.form['build_path']
    name = request.form['build_name']
    images.build_image(directory, name)
    return redirect(url_for("images_app.image"))
def test_build_image(path, name, expected):
    """ Checks if building an image works properly """
    assert images.build_image(path, name) == expected
Exemplo n.º 5
0
def command_line(args):
    """ Run functions via command line arguments """
    # Build command entered
    if args.build:
        # Checks to ensure required information given before calling function
        if args.image:
            # Building image
            if args.path and args.name:
                images.build_image(args.path,
                                   args.name,
                                   threaded=args.threaded)
            else:
                return "Additional flags required"
        elif args.container:
            # Building container
            if args.name and args.args:
                containers.build_container(args.name, args.args)
            else:
                return "Additional flags required"
        else:
            return "Additional flags required"
    # Generate command entered
    elif args.generate:
        # Checks to ensure required information given before calling function
        if args.path and args.dest:
            return generate.generate_dockerfile(args.path, args.dest)
        return "Additional flags required"
    # List command entered
    elif args.list:
        # Checks to ensure required information given before calling function
        if args.image:
            # List images
            return images.list_images()
        if args.container:
            # List containers
            return containers.list_containers()
        return "Additional flags required"
    # Run command entered
    elif args.run:
        # Checks to ensure required information given before calling function
        if args.image:
            # Run image
            if args.args and args.name:
                images.run_image(args.name, args=args.args, sep=args.sep)
            elif args.name:
                images.run_image(args.name, sep=args.sep)
            else:
                return "Additional flags required"
        elif args.container:
            # Run container
            if args.args and args.name:
                containers.run_container(args.name,
                                         args=args.args,
                                         sep=args.sep)
            elif args.name:
                containers.run_container(args.name, sep=args.sep)
            else:
                return "Additional flags required"
        else:
            return "Additional flags required"
    # Delete command entered
    elif args.delete:
        # Checks to ensure required information given before calling function
        if args.image:
            # Delete image
            if args.name:
                images.delete_image(args.name)
            elif args.names:
                images.delete_image(args.names[0])
            else:
                return "Additional flags required"
        elif args.container:
            # Delete container
            if args.name:
                containers.delete_container(args.name)
            elif args.names:
                containers.delete_container(args.names[0])
            else:
                return "Additional flags required"
        else:
            return "Additional flags required"
    # Login command entered
    elif args.login:
        # Checks to ensure required information given before calling function
        if args.username and args.password:
            images.login(args.username, args.password)
        else:
            return "Additional flags required"
    # Pull command entered
    elif args.pull:
        # Checks to ensure required information given before calling function
        if args.name and args.tag:
            images.pull_image(args.name, tag=args.tag)
        else:
            return "Additional flags required"
    # Push command entered
    elif args.push:
        # Checks to ensure required information given before calling function
        if args.name and args.tag:
            images.push_image(args.name, args.tag)
        else:
            return "Additional flags required"
    # Kill command entered
    elif args.kill:
        # Checks to ensure required information given before calling function
        if args.name:
            containers.kill_container(args.name)
        else:
            return "Additional flags required"
    # Stop command entered
    elif args.stop:
        # Checks to ensure required information given before calling function
        if args.name:
            containers.stop_container(args.name)
        else:
            return "Additional flags required"
    # Restart command entered
    elif args.restart:
        # Checks to ensure required information given before calling function
        if args.name:
            containers.restart_container(args.name)
        else:
            return "Additional flags required"