Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
(GH-85) Add alwaysBuild flag and functionality
Browse files Browse the repository at this point in the history
In this commit the `alwaysBuild` flag and its functionality were added
to the `prm exec` command. `alwaysBuild` will cause the docker
image pertaining to the executed tool and puppet version to
rebuild on tool execution.
  • Loading branch information
petergmurphy committed Jan 18, 2022
1 parent 31e675f commit 26e036f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
16 changes: 11 additions & 5 deletions cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ var (
format string
selectedTool string
// selectedToolInfo string
listTools bool
prmApi *prm.Prm
toolArgs string
listTools bool
prmApi *prm.Prm
toolArgs string
alwaysBuild bool
)

func CreateCommand(parent *prm.Prm) *cobra.Command {
Expand Down Expand Up @@ -72,6 +73,10 @@ func CreateCommand(parent *prm.Prm) *cobra.Command {
err = viper.BindPFlag("toolArgs", tmp.Flags().Lookup("toolArgs"))
cobra.CheckErr(err)

tmp.Flags().BoolVarP(&alwaysBuild, "alwaysBuild", "a", false, "Rebuild the docker image for each tool execution, even if it already exists")
err = viper.BindPFlag("alwaysBuild", tmp.Flags().Lookup("alwaysBuild"))
cobra.CheckErr(err)

return tmp
}

Expand All @@ -82,9 +87,9 @@ func preExecute(cmd *cobra.Command, args []string) error {

switch prmApi.RunningConfig.Backend {
case prm.DOCKER:
prmApi.Backend = &prm.Docker{AFS: prmApi.AFS, IOFS: prmApi.IOFS}
prmApi.Backend = &prm.Docker{AFS: prmApi.AFS, IOFS: prmApi.IOFS, AlwaysBuild: alwaysBuild}
default:
prmApi.Backend = &prm.Docker{AFS: prmApi.AFS, IOFS: prmApi.IOFS}
prmApi.Backend = &prm.Docker{AFS: prmApi.AFS, IOFS: prmApi.IOFS, AlwaysBuild: alwaysBuild}
}

// handle the default cachepath
Expand Down Expand Up @@ -188,6 +193,7 @@ func execute(cmd *cobra.Command, args []string) error {
if !ok {
return fmt.Errorf("Tool %s not found in cache", tool)
}

err := prmApi.Exec(cachedTool, tool.Args)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions internal/pkg/mock/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ func (m *DockerClient) ImageBuild(ctx context.Context, buildContext io.Reader, o
func (m *DockerClient) ImageList(ctx context.Context, options types.ImageListOptions) ([]types.ImageSummary, error) {
return []types.ImageSummary{}, nil
}

func (m *DockerClient) ImageRemove(ctx context.Context, imageID string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error) {
return []types.ImageDeleteResponseItem{{Deleted: "test_id"}}, nil
}
23 changes: 21 additions & 2 deletions pkg/prm/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Docker struct {
ContextCancel func()
AFS *afero.Afero
IOFS *afero.IOFS
AlwaysBuild bool
}

type DockerClientI interface {
Expand All @@ -42,6 +43,7 @@ type DockerClientI interface {
ContainerWait(ctx context.Context, containerID string, condition container.WaitCondition) (<-chan container.ContainerWaitOKBody, <-chan error)
ImageBuild(ctx context.Context, buildContext io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error)
ImageList(ctx context.Context, options types.ImageListOptions) ([]types.ImageSummary, error)
ImageRemove(ctx context.Context, imageID string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error)
ServerVersion(context.Context) (types.Version, error)
}

Expand All @@ -64,16 +66,33 @@ func (d *Docker) GetTool(tool *Tool, prmConfig Config) error {
return err
}

foundImage := ""
for _, image := range list {
for _, tag := range image.RepoTags {
if tag == toolImageName {
log.Info().Msgf("Found image: %s", image.ID)
return nil
if !d.AlwaysBuild {
return nil
}
foundImage = image.ID
break
}
}
if foundImage != "" {
break
}
}

log.Info().Msg("Creating new image. Please wait...")
if d.AlwaysBuild && foundImage != "" {
log.Info().Msg("Rebuilding image. Please wait...")
_, err = d.Client.ImageRemove(d.Context, foundImage, types.ImageRemoveOptions{Force: true})
if err != nil {
log.Error().Msgf("Error removing docker image: %v", err)
return err
}
} else {
log.Info().Msg("Creating new image. Please wait...")
}

// No image found with that configuration
// we must create it
Expand Down

0 comments on commit 26e036f

Please sign in to comment.