def BuildSelection(buildSelection, selectionToBuild):
    cwd = BuildTools.TryChangeToDirectoryAndGetCwd(buildSelection)
    oldEnvironmentVariable = BuildTools.AddEnvironmentVariablesFromSelection(buildSelection)
    BuildTools.HandleTerminalCommandsSelection(buildSelection)
    TerminalTools.LoadDefaultEnvironmentVariablesFile()

    if BuildTools.FILES_KEY in buildSelection:
        composeFiles = buildSelection[BuildTools.FILES_KEY]
        buildComposeFile = BuildTools.GetAvailableComposeFilename('build', selectionToBuild)
        DockerComposeTools.MergeComposeFiles(composeFiles, buildComposeFile)

        try:
            DockerComposeTools.DockerComposeBuild([buildComposeFile])
        except:
            BuildTools.RemoveComposeFileIfNotPreserved(buildComposeFile, buildSelection)
            raise

        if BuildTools.ADDITIONAL_TAG_KEY in buildSelection:
            DockerComposeTools.TagImages(buildComposeFile, buildSelection[BuildTools.ADDITIONAL_TAG_KEY])
        if BuildTools.ADDITIONAL_TAGS_KEY in buildSelection:
            for tag in buildSelection[BuildTools.ADDITIONAL_TAGS_KEY]:
                DockerComposeTools.TagImages(buildComposeFile, tag)
        if SAVE_IMAGES_KEY in buildSelection:
            outputFolder = buildSelection[SAVE_IMAGES_KEY]
            DockerComposeTools.SaveImages(buildComposeFile, outputFolder)

        if BuildTools.COMPOSE_FILE_WITH_DIGESTS_KEY in buildSelection:
            composeFileWithDigests = buildSelection[BuildTools.COMPOSE_FILE_WITH_DIGESTS_KEY]
            BuildTools.GenerateComposeFileWithDigests(composeFiles, composeFileWithDigests)

        BuildTools.RemoveComposeFileIfNotPreserved(buildComposeFile, buildSelection)

    BuildTools.RemoveEnvironmentVariables(oldEnvironmentVariable)
    os.chdir(cwd)
def DeploySwarmSelection(swarmSelection, prefix):
    cwd = BuildTools.TryChangeToDirectoryAndGetCwd(swarmSelection)
    oldEnvironmentVariable = BuildTools.AddEnvironmentVariablesFromSelection(
        swarmSelection)
    BuildTools.HandleTerminalCommandsSelection(swarmSelection)
    TerminalTools.LoadDefaultEnvironmentVariablesFile()
    SwarmManager.HandleManagement(
        [prefix] + BuildSwarmManagementFilesRow(swarmSelection) +
        BuildSwarmManagementPropertiesRow(swarmSelection))
    BuildTools.RemoveEnvironmentVariables(oldEnvironmentVariable)
    os.chdir(cwd)
Ejemplo n.º 3
0
def RunSelection(runSelection, selectionToRun):
    cwd = BuildTools.TryChangeToDirectoryAndGetCwd(runSelection)
    oldEnvironmentVariable = BuildTools.AddEnvironmentVariablesFromSelection(
        runSelection)
    BuildTools.HandleTerminalCommandsSelection(runSelection)
    TerminalTools.LoadDefaultEnvironmentVariablesFile()

    if BuildTools.FILES_KEY in runSelection:
        runComposeFile = BuildTools.GetAvailableComposeFilename(
            'run', selectionToRun)
        composeFiles = runSelection[BuildTools.FILES_KEY]
        if YamlTools.TryGetFromDictionary(runSelection,
                                          VERIFY_CONTAINER_EXIT_CODE, False):
            containerNames = BuildTools.MergeAndPopulateWithContainerNames(
                composeFiles, runComposeFile)
            if BuildTools.CONTAINER_NAMES_KEY in runSelection:
                containerNames = runSelection[BuildTools.CONTAINER_NAMES_KEY]
        else:
            containerNames = []
            DockerComposeTools.MergeComposeFiles(composeFiles, runComposeFile)

        try:
            DockerComposeTools.DockerComposeUp(
                [runComposeFile],
                YamlTools.TryGetFromDictionary(runSelection,
                                               ABORT_ON_CONTAINER_EXIT_KEY,
                                               True),
                YamlTools.TryGetFromDictionary(runSelection, DETACHED_KEY,
                                               False))
        except:
            BuildTools.RemoveComposeFileIfNotPreserved(runComposeFile,
                                                       runSelection)
            raise

        DockerImageTools.VerifyContainerExitCode(containerNames,
                                                 assertExitCodes=True)
        BuildTools.HandleCopyFromContainer(runSelection)

        if YamlTools.TryGetFromDictionary(runSelection,
                                          BuildTools.REMOVE_CONTAINERS_KEY,
                                          False):
            DockerComposeTools.DockerComposeRemove([runComposeFile])

        BuildTools.RemoveComposeFileIfNotPreserved(runComposeFile,
                                                   runSelection)

    BuildTools.RemoveEnvironmentVariables(oldEnvironmentVariable)
    os.chdir(cwd)
Ejemplo n.º 4
0
def PromoteSelection(promoteSelection, selectionToPromote):
    cwd = BuildTools.TryChangeToDirectoryAndGetCwd(promoteSelection)
    oldEnvironmentVariable = BuildTools.AddEnvironmentVariablesFromSelection(
        promoteSelection)
    BuildTools.HandleTerminalCommandsSelection(promoteSelection)
    TerminalTools.LoadDefaultEnvironmentVariablesFile()

    if BuildTools.FILES_KEY in promoteSelection:
        if YamlTools.TryGetFromDictionary(promoteSelection,
                                          CONTAINER_ARTIFACT_KEY, True):
            PromoteImageSelection(promoteSelection, selectionToPromote)
        else:
            print('Only image promotion is supported')

    BuildTools.RemoveEnvironmentVariables(oldEnvironmentVariable)
    os.chdir(cwd)
Ejemplo n.º 5
0
def PublishSelection(publishSelection, selectionToPublish):
    cwd = BuildTools.TryChangeToDirectoryAndGetCwd(publishSelection)
    oldEnvironmentVariable = BuildTools.AddEnvironmentVariablesFromSelection(
        publishSelection)
    BuildTools.HandleTerminalCommandsSelection(publishSelection)
    TerminalTools.LoadDefaultEnvironmentVariablesFile()

    if BuildTools.FILES_KEY in publishSelection:
        if YamlTools.TryGetFromDictionary(publishSelection,
                                          CONTAINER_ARTIFACT_KEY, True):
            PublishContainerSelection(publishSelection, selectionToPublish)
        else:
            PublishArtifactSelection(publishSelection)

        BuildTools.HandleCopyFromContainer(publishSelection)

    BuildTools.RemoveEnvironmentVariables(oldEnvironmentVariable)
    os.chdir(cwd)
def TestSelection(testSelection, selectionToTest):
    cwd = BuildTools.TryChangeToDirectoryAndGetCwd(testSelection)
    oldEnvironmentVariable = BuildTools.AddEnvironmentVariablesFromSelection(
        testSelection)
    BuildTools.HandleTerminalCommandsSelection(testSelection)
    TerminalTools.LoadDefaultEnvironmentVariablesFile()

    if BuildTools.FILES_KEY in testSelection:
        testComposeFile = BuildTools.GetAvailableComposeFilename(
            'test', selectionToTest)
        composeFiles = testSelection[BuildTools.FILES_KEY]
        containerNames = BuildTools.MergeAndPopulateWithContainerNames(
            composeFiles, testComposeFile)
        if BuildTools.CONTAINER_NAMES_KEY in testSelection:
            containerNames = testSelection[BuildTools.CONTAINER_NAMES_KEY]

        try:
            DockerComposeTools.ExecuteComposeTests(
                [testComposeFile],
                testContainerNames=containerNames,
                removeTestContainers=False,
                buildCompose=True,
                downCompose=False)
        except:
            BuildTools.RemoveComposeFileIfNotPreserved(testComposeFile,
                                                       testSelection)
            raise

        BuildTools.HandleCopyFromContainer(testSelection)
        DockerComposeTools.DockerComposeDown([testComposeFile])
        if YamlTools.TryGetFromDictionary(testSelection,
                                          BuildTools.REMOVE_CONTAINERS_KEY,
                                          False):
            DockerComposeTools.DockerComposeRemove([testComposeFile])

        BuildTools.RemoveComposeFileIfNotPreserved(testComposeFile,
                                                   testSelection)

    BuildTools.RemoveEnvironmentVariables(oldEnvironmentVariable)
    os.chdir(cwd)
Ejemplo n.º 7
0
def GenerateComposeFileWithDigests(composeFiles, outputComposeFile):
    TerminalTools.LoadDefaultEnvironmentVariablesFile()
    yamlData = YamlTools.GetYamlData(composeFiles, replaceEnvironmentVariablesMatches=False)
    DockerComposeTools.AddDigestsToImageTags(yamlData)
    YamlTools.DumpYamlDataToFile(yamlData, outputComposeFile)