Ejemplo n.º 1
0
def test_writers():
    from spython.main.parse.writers import get_writer

    writer = get_writer("docker")
    assert writer == DockerWriter

    writer = get_writer("Dockerfile")
    assert writer == DockerWriter

    writer = get_writer("Singularity")
    assert writer == SingularityWriter
Ejemplo n.º 2
0
def test_writers():
    from spython.main.parse.writers import get_writer

    writer = get_writer('docker')
    assert writer == DockerWriter

    writer = get_writer('Dockerfile')
    assert writer == DockerWriter

    writer = get_writer('Singularity')
    assert writer == SingularityWriter
Ejemplo n.º 3
0
def main(args, options, parser):
    """This function serves as a wrapper around the DockerParser,
    SingularityParser, DockerWriter, and SingularityParser converters.
    We can either save to file if args.outfile is defined, or print
    to the console if not.
    """
    # We need something to work with
    if not args.files:
        parser.print_help()
        sys.exit(1)

    # Get the user specified input and output files
    outfile = None
    if len(args.files) > 1:
        outfile = args.files[1]

    # First try to get writer and parser, if not defined will return None
    writer = get_writer(args.writer)
    parser = get_parser(args.parser)

    # If the user wants to auto-detect the type
    if args.parser == "auto":
        if "dockerfile" in args.files[0].lower():
            parser = get_parser("docker")
        elif "singularity" in args.files[0].lower():
            parser = get_parser("singularity")

    # If the parser still isn't defined, no go.
    if parser is None:
        bot.exit(
            "Please provide a Dockerfile or Singularity recipe, or define the --parser type."
        )

    # If the writer needs auto-detect
    if args.writer == "auto":
        if parser.name == "docker":
            writer = get_writer("singularity")
        else:
            writer = get_writer("docker")

    # If the writer still isn't defined, no go
    if writer is None:
        bot.exit("Please define the --writer type.")

    # Initialize the chosen parser
    recipeParser = parser(args.files[0])

    # By default, discover entrypoint / cmd from Dockerfile
    entrypoint = "/bin/bash"
    force = False

    if args.entrypoint is not None:
        entrypoint = args.entrypoint

        # This is only done if the user intended to print json here
        recipeParser.entrypoint = args.entrypoint
        recipeParser.cmd = None
        force = True

    if args.json:

        if outfile is not None:
            if not os.path.exists(outfile):
                if force:
                    write_json(outfile, recipeParser.recipe.json())
                else:
                    bot.exit("%s exists, set --force to overwrite." % outfile)
        else:
            print(json.dumps(recipeParser.recipe.json(), indent=4))

    else:

        # Do the conversion
        recipeWriter = writer(recipeParser.recipe)
        result = recipeWriter.convert(runscript=entrypoint, force=force)

        # If the user specifies an output file, save to it
        if outfile is not None:
            write_file(outfile, result)

        # Otherwise, convert and print to screen
        else:
            print(result)
def convert_definition_file(definition_entry, singularity_def_name=None):
    """Converts a Dockerfile0 to a Singularity definition file or vice versa.

    Parameters:
    definition_id (str): ID of definition db entry to convert.
    singularity_def_name (str): Name to give to converted .def file if converting
    from Dockerfile0 to Singularity definition file.
    """
    try:
        definition_id = definition_entry["definition_id"]

        new_definition_id = str(uuid.uuid4())
        new_path = PROJECT_ROOT + str(new_definition_id)
        os.mkdir(new_path)
        subprocess.call(
            f"aws s3 cp --recursive s3://xtract-container-service/{definition_id} {new_path}",
            shell=True)

        for file in os.listdir(new_path):
            if file == "Dockerfile" or file.endswith(".def"):
                input_file = os.path.join(new_path, file)
                break
            else:
                input_file = None

        assert input_file, "Definition file not found"

        if input_file.endswith(".def"):
            from_format = "Singularity"
            to_format = "docker"
        else:
            from_format = "docker"
            to_format = "Singularity"

        file_parser = get_parser(from_format)
        file_writer = get_writer(to_format)
        parser = file_parser(os.path.join(new_path, input_file))
        writer = file_writer(parser.recipe)
        result = writer.convert()

        if to_format == "Singularity":
            if singularity_def_name is None:
                singularity_def_name = namegenerator.gen() + ".def"

            file_path = os.path.join(new_path, singularity_def_name)
        else:
            file_path = os.path.join(new_path, "Dockerfile")

        with open(file_path, 'w') as f:
            f.write(result)

        os.remove(input_file)

        db_entry = definition_schema
        db_entry["definition_id"] = new_definition_id
        db_entry["definition_type"] = to_format.lower()
        # Might want to change definition name at some point
        db_entry[
            "definition_name"] = singularity_def_name if to_format == "Singularity" else "Dockerfile"
        db_entry["pre_containers"] = definition_entry["pre_containers"]
        db_entry["post_containers"] = definition_entry["post_containers"]
        db_entry["replaces_container"] = definition_entry["replaces_container"]
        db_entry["definition_owner"] = definition_entry["definition_owner"]
        db_entry["location"] = "s3"
        create_table_entry("definition", **db_entry)

        logging.info(
            "Successfully converted %s %s definition file to %s %s definition file",
            os.path.basename(input_file), from_format,
            os.path.basename(file_path), to_format)

        s3 = boto3.client('s3')
        s3.upload_fileobj(
            open(file_path, "rb"), "xtract-container-service",
            f'{new_definition_id}/{singularity_def_name if to_format == "Singularity" else "Dockerfile"}'
        )

        return new_definition_id
    except Exception as e:
        print(e)
        logging.error("Exception", exc_info=True)
        return "Failed"
    finally:
        pass
        shutil.rmtree(new_path)
from spython.main.parse.writers import get_writer
from spython.main.parse.parsers import get_parser

DockerParser = get_parser('docker')
SingularityWriter = get_writer('singularity')
# from spython.main.parse.writers import SingularityWriter

parser = DockerParser(
    '/Users/benjaminglickenhaus/PycharmProjects/dotaservice/dockerfiles/Dockerfile-dota'
)
writer = SingularityWriter(parser.recipe)
print(writer.convert())
result = writer.convert()
with open(
        '/Users/benjaminglickenhaus/PycharmProjects/dotaservice/dockerfiles/Singularity-dota.simg',
        'w') as f:
    f.write(result)

parser = DockerParser(
    '/Users/benjaminglickenhaus/PycharmProjects/dotaservice/dockerfiles/Dockerfile-dotaservice'
)
writer = SingularityWriter(parser.recipe)
print(writer.convert())
result = writer.convert()
with open(
        '/Users/benjaminglickenhaus/PycharmProjects/dotaservice/dockerfiles/Singularity-dotaservice.simg',
        'w') as f:
    f.write(result)