Exemplo n.º 1
0
def test_get_parser():
    from spython.main.parse.parsers import get_parser

    parser = get_parser('docker')
    assert parser == DockerParser

    parser = get_parser('Dockerfile')
    assert parser == DockerParser

    parser = get_parser('Singularity')
    assert parser == SingularityParser
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)