Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #326 from snyk/feat/update-dockerfile-base-image
feat: update dockerfile with new base image
- Loading branch information
Showing
4 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { DockerfileParser } from "dockerfile-ast"; | ||
import { EOL } from "os"; | ||
import { getDockerfileBaseImageName } from "./instruction-parser"; | ||
import { | ||
UpdateDockerfileBaseImageNameErrorCode, | ||
UpdateDockerfileBaseImageNameResult, | ||
} from "./types"; | ||
|
||
export { updateDockerfileBaseImageName }; | ||
|
||
/** | ||
* Updates the image name of the last from stage, after resolving all aliases | ||
* @param contents Contents of the Dockerfile to update | ||
* @param newBaseImageName New base image name Dockerfile contents should be updated to | ||
*/ | ||
function updateDockerfileBaseImageName( | ||
contents: string, | ||
newBaseImageName: string, | ||
): UpdateDockerfileBaseImageNameResult { | ||
const dockerfile = DockerfileParser.parse(contents); | ||
|
||
const currentBaseImageName = getDockerfileBaseImageName(dockerfile); | ||
|
||
if (currentBaseImageName === undefined) { | ||
return { | ||
contents, | ||
error: { | ||
code: UpdateDockerfileBaseImageNameErrorCode.BASE_IMAGE_NAME_NOT_FOUND, | ||
}, | ||
}; | ||
} | ||
|
||
const fromRanges = dockerfile | ||
.getFROMs() | ||
.filter((from) => from.getImage() === currentBaseImageName) | ||
.map((from) => from.getImageRange()!); | ||
|
||
const argRanges = dockerfile | ||
.getARGs() | ||
.filter((arg) => arg.getProperty()?.getValue() === currentBaseImageName) | ||
.map((arg) => arg.getProperty()?.getValueRange()!); | ||
|
||
const ranges = fromRanges.concat(argRanges); | ||
|
||
if (ranges.length === 0) { | ||
/** | ||
* This happens when the image is split over multiple FROM and ARG statements | ||
* making it difficult to update Dockerfiles that fall into these edge cases. | ||
* e.g.: | ||
* ARG REPO=repo | ||
* ARG TAG=tag | ||
* FROM ${REPO}:${TAG} | ||
*/ | ||
return { | ||
contents, | ||
error: { | ||
code: UpdateDockerfileBaseImageNameErrorCode.BASE_IMAGE_NAME_FRAGMENTED, | ||
}, | ||
}; | ||
} | ||
|
||
const lines = contents.split(EOL); | ||
|
||
for (const range of ranges) { | ||
const lineNumber = range.start.line; | ||
const start = range.start.character; | ||
const end = range.end.character; | ||
|
||
const content = lines[lineNumber]; | ||
const updated = | ||
content.substring(0, start) + newBaseImageName + content.substring(end); | ||
lines[lineNumber] = updated; | ||
} | ||
|
||
const updatedContents = lines.join(EOL); | ||
const updatedDockerfile = DockerfileParser.parse(updatedContents); | ||
|
||
if ( | ||
dockerfile.getInstructions().length !== | ||
updatedDockerfile.getInstructions().length | ||
) { | ||
return { | ||
contents, | ||
error: { | ||
code: | ||
UpdateDockerfileBaseImageNameErrorCode.DOCKERFILE_GENERATION_FAILED, | ||
}, | ||
}; | ||
} | ||
|
||
return { | ||
contents: updatedContents, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters