|
@ -25,60 +25,56 @@ import ( |
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus" |
|
|
log "github.com/sirupsen/logrus" |
|
|
|
|
|
|
|
|
buildkitDockerfileInstructions "github.com/moby/buildkit/frontend/dockerfile/instructions" |
|
|
|
|
|
buildkitDockerfileParser "github.com/moby/buildkit/frontend/dockerfile/parser" |
|
|
|
|
|
|
|
|
buildkitInstructions "github.com/moby/buildkit/frontend/dockerfile/instructions" |
|
|
|
|
|
buildkitParser "github.com/moby/buildkit/frontend/dockerfile/parser" |
|
|
|
|
|
|
|
|
"sothr.com/warricksothr/pinned-package-updater/internal/parser" |
|
|
"sothr.com/warricksothr/pinned-package-updater/internal/parser" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
// Parse reads a Dockerfile, interprets the AST and returns information about the image(s) and package(s) defined
|
|
|
// Parse reads a Dockerfile, interprets the AST and returns information about the image(s) and package(s) defined
|
|
|
func Parse(dockerfilePath string) (parser.ParseResult, error) { |
|
|
func Parse(dockerfilePath string) (parser.ParseResult, error) { |
|
|
dockerfile, err := os.Open(dockerfilePath) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error reading file %s: %s", dockerfilePath, err) |
|
|
|
|
|
return parser.ParseResult{}, err |
|
|
|
|
|
|
|
|
dockerfile, dockerfileOpenErr := os.Open(dockerfilePath) |
|
|
|
|
|
if dockerfileOpenErr != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error reading file %s: %s", dockerfilePath, dockerfileOpenErr) |
|
|
|
|
|
return parser.ParseResult{}, dockerfileOpenErr |
|
|
} |
|
|
} |
|
|
defer func(dockerfile *os.File) { |
|
|
defer func(dockerfile *os.File) { |
|
|
err := dockerfile.Close() |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Error("Unexpected error closing file %s: %s", dockerfilePath, err) |
|
|
|
|
|
|
|
|
dockerfileCloseErr := dockerfile.Close() |
|
|
|
|
|
if dockerfileCloseErr != nil { |
|
|
|
|
|
log.Error("Unexpected error closing file %s: %s", dockerfilePath, dockerfileCloseErr) |
|
|
} |
|
|
} |
|
|
}(dockerfile) |
|
|
}(dockerfile) |
|
|
|
|
|
|
|
|
dockerfileParseResults, err := buildkitDockerfileParser.Parse(dockerfile) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error parsing Dockerfile %s: %s", dockerfilePath, err) |
|
|
|
|
|
return parser.ParseResult{}, err |
|
|
|
|
|
|
|
|
parsedASTResult, ASTParseErr := buildkitParser.Parse(dockerfile) |
|
|
|
|
|
if ASTParseErr != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error parsing Dockerfile %s: %s", dockerfilePath, ASTParseErr) |
|
|
|
|
|
return parser.ParseResult{}, ASTParseErr |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
dockerfileParseResult, err := parseDockerfileAST(dockerfileParseResults.AST) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error while parsing AST for %s: %s", dockerfilePath, err) |
|
|
|
|
|
return parser.ParseResult{}, err |
|
|
|
|
|
|
|
|
parseResult, parseErr := parseDockerfileAST(parsedASTResult.AST) |
|
|
|
|
|
if parseErr != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error while parsing AST for %s: %s", dockerfilePath, parseErr) |
|
|
|
|
|
return parser.ParseResult{}, parseErr |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return dockerfileParseResult, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type ASTStage struct { |
|
|
|
|
|
children []*buildkitDockerfileParser.Node |
|
|
|
|
|
|
|
|
return parseResult, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// parseDockerfileAST Read the parsed AST and extract the required information from each stage
|
|
|
// parseDockerfileAST Read the parsed AST and extract the required information from each stage
|
|
|
func parseDockerfileAST(dockerfileAST *buildkitDockerfileParser.Node) (parser.ParseResult, error) { |
|
|
|
|
|
|
|
|
func parseDockerfileAST(dockerfileAST *buildkitParser.Node) (parser.ParseResult, error) { |
|
|
|
|
|
|
|
|
dockerfileStages, _, err := buildkitDockerfileInstructions.Parse(dockerfileAST) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Fatal("Unexpected error while parsing Dockerfile Instructions") |
|
|
|
|
|
return parser.ParseResult{}, err |
|
|
|
|
|
|
|
|
dockerfileStages, _, stageParsingErr := buildkitInstructions.Parse(dockerfileAST) |
|
|
|
|
|
if stageParsingErr != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error while parsing Dockerfile Instructions: %s", stageParsingErr) |
|
|
|
|
|
return parser.ParseResult{}, stageParsingErr |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var stages []*parser.Stage |
|
|
var stages []*parser.Stage |
|
|
for stageIndex, stage := range dockerfileStages { |
|
|
for stageIndex, stage := range dockerfileStages { |
|
|
stage, err := parseDockerfileStage(stage) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
|
stage, stageErr := parseDockerfileStage(stage) |
|
|
|
|
|
if stageErr != nil { |
|
|
log.Fatal("Unexpected error while parsing stage %d", stageIndex) |
|
|
log.Fatal("Unexpected error while parsing stage %d", stageIndex) |
|
|
return parser.ParseResult{}, err |
|
|
|
|
|
|
|
|
return parser.ParseResult{}, stageErr |
|
|
} |
|
|
} |
|
|
stages = append(stages, &stage) |
|
|
stages = append(stages, &stage) |
|
|
} |
|
|
} |
|
@ -86,7 +82,7 @@ func parseDockerfileAST(dockerfileAST *buildkitDockerfileParser.Node) (parser.Pa |
|
|
return parser.ParseResult{Stages: stages}, nil |
|
|
return parser.ParseResult{Stages: stages}, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func parseDockerfileStage(dockerfileStage buildkitDockerfileInstructions.Stage) (parser.Stage, error) { |
|
|
|
|
|
|
|
|
func parseDockerfileStage(dockerfileStage buildkitInstructions.Stage) (parser.Stage, error) { |
|
|
|
|
|
|
|
|
imageParts := strings.Split(dockerfileStage.BaseName, ":") |
|
|
imageParts := strings.Split(dockerfileStage.BaseName, ":") |
|
|
|
|
|
|
|
@ -102,22 +98,22 @@ func parseDockerfileStage(dockerfileStage buildkitDockerfileInstructions.Stage) |
|
|
Tag: imageParts[1], |
|
|
Tag: imageParts[1], |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
commandParser, err := parser.GetCommandParser(image) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error while determining command parser for image %s", image) |
|
|
|
|
|
return parser.Stage{}, err |
|
|
|
|
|
|
|
|
commandParser, commandParserRetrievalErr := parser.GetCommandParser(image) |
|
|
|
|
|
if commandParserRetrievalErr != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error while determining command parser for image \"%s\": %s", image, commandParserRetrievalErr) |
|
|
|
|
|
return parser.Stage{}, commandParserRetrievalErr |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
repositories, err := parseRepositoriesFromDockerfileStage(dockerfileStage, commandParser) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
message := fmt.Sprintf("Unexpected error while parsing repositories from stage") |
|
|
|
|
|
|
|
|
repositories, repositoryCommandParseErr := parseRepositoriesFromDockerfileStage(dockerfileStage, commandParser) |
|
|
|
|
|
if repositoryCommandParseErr != nil { |
|
|
|
|
|
message := fmt.Sprintf("Unexpected error while parsing repositories from stage: %s", repositoryCommandParseErr) |
|
|
log.Fatal(message) |
|
|
log.Fatal(message) |
|
|
return parser.Stage{}, errors.New(message) |
|
|
return parser.Stage{}, errors.New(message) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
packages, err := parsePackagesFromDockerfileStage(dockerfileStage, commandParser) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
message := fmt.Sprintf("Unexpected error while parsing packages from stage") |
|
|
|
|
|
|
|
|
packages, packageCommandParseErr := parsePackagesFromDockerfileStage(dockerfileStage, commandParser) |
|
|
|
|
|
if packageCommandParseErr != nil { |
|
|
|
|
|
message := fmt.Sprintf("Unexpected error while parsing packages from stage: %s", packageCommandParseErr) |
|
|
log.Fatal(message) |
|
|
log.Fatal(message) |
|
|
return parser.Stage{}, errors.New(message) |
|
|
return parser.Stage{}, errors.New(message) |
|
|
} |
|
|
} |
|
@ -130,21 +126,21 @@ func parseDockerfileStage(dockerfileStage buildkitDockerfileInstructions.Stage) |
|
|
}, nil |
|
|
}, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func parseRepositoriesFromDockerfileStage(dockerfileStage buildkitDockerfileInstructions.Stage, commandParser *parser.CommandParser) ([]*parser.Repository, error) { |
|
|
|
|
|
|
|
|
func parseRepositoriesFromDockerfileStage(dockerfileStage buildkitInstructions.Stage, commandParser *parser.CommandParser) ([]*parser.Repository, error) { |
|
|
var repositories []*parser.Repository |
|
|
var repositories []*parser.Repository |
|
|
typedCommandParser := *commandParser |
|
|
typedCommandParser := *commandParser |
|
|
|
|
|
|
|
|
for _, command := range dockerfileStage.Commands { |
|
|
for _, command := range dockerfileStage.Commands { |
|
|
switch command.(type) { |
|
|
switch command.(type) { |
|
|
case *buildkitDockerfileInstructions.RunCommand: |
|
|
|
|
|
runCommand := command.(*buildkitDockerfileInstructions.RunCommand) |
|
|
|
|
|
|
|
|
case *buildkitInstructions.RunCommand: |
|
|
|
|
|
runCommand := command.(*buildkitInstructions.RunCommand) |
|
|
|
|
|
|
|
|
log.Tracef("Parsing RunCommand \"%s\" for repositories", runCommand.CmdLine) |
|
|
log.Tracef("Parsing RunCommand \"%s\" for repositories", runCommand.CmdLine) |
|
|
|
|
|
|
|
|
parsedRepositories, err := typedCommandParser.GetRepositories(runCommand.CmdLine[0]) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error while parsing repositories: %s", err) |
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
|
parsedRepositories, repositoryParseErr := typedCommandParser.GetRepositories(runCommand.CmdLine[0]) |
|
|
|
|
|
if repositoryParseErr != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error while parsing repositories: %s", repositoryParseErr) |
|
|
|
|
|
return nil, repositoryParseErr |
|
|
} |
|
|
} |
|
|
for _, parsedRepository := range parsedRepositories { |
|
|
for _, parsedRepository := range parsedRepositories { |
|
|
repositories = append(repositories, parsedRepository) |
|
|
repositories = append(repositories, parsedRepository) |
|
@ -155,21 +151,21 @@ func parseRepositoriesFromDockerfileStage(dockerfileStage buildkitDockerfileInst |
|
|
return repositories, nil |
|
|
return repositories, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func parsePackagesFromDockerfileStage(dockerfileStage buildkitDockerfileInstructions.Stage, commandParser *parser.CommandParser) ([]*parser.Package, error) { |
|
|
|
|
|
|
|
|
func parsePackagesFromDockerfileStage(dockerfileStage buildkitInstructions.Stage, commandParser *parser.CommandParser) ([]*parser.Package, error) { |
|
|
var packages []*parser.Package |
|
|
var packages []*parser.Package |
|
|
typedCommandParser := *commandParser |
|
|
typedCommandParser := *commandParser |
|
|
|
|
|
|
|
|
for _, command := range dockerfileStage.Commands { |
|
|
for _, command := range dockerfileStage.Commands { |
|
|
switch command.(type) { |
|
|
switch command.(type) { |
|
|
case *buildkitDockerfileInstructions.RunCommand: |
|
|
|
|
|
runCommand := command.(*buildkitDockerfileInstructions.RunCommand) |
|
|
|
|
|
|
|
|
case *buildkitInstructions.RunCommand: |
|
|
|
|
|
runCommand := command.(*buildkitInstructions.RunCommand) |
|
|
|
|
|
|
|
|
log.Tracef("Parsing RunCommand \"%s\" for packages", runCommand.CmdLine) |
|
|
log.Tracef("Parsing RunCommand \"%s\" for packages", runCommand.CmdLine) |
|
|
|
|
|
|
|
|
parsedPinnedPackages, err := typedCommandParser.GetPinnedPackages(runCommand.CmdLine[0]) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error while parsing pinned packages: %s", err) |
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
|
parsedPinnedPackages, packageParseErr := typedCommandParser.GetPinnedPackages(runCommand.CmdLine[0]) |
|
|
|
|
|
if packageParseErr != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error while parsing pinned packages: %s", packageParseErr) |
|
|
|
|
|
return nil, packageParseErr |
|
|
} |
|
|
} |
|
|
for _, parsedPinnedPackage := range parsedPinnedPackages { |
|
|
for _, parsedPinnedPackage := range parsedPinnedPackages { |
|
|
packages = append(packages, parsedPinnedPackage) |
|
|
packages = append(packages, parsedPinnedPackage) |
|
|