Skip to content

Commit

Permalink
compose support enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
kcq committed Sep 23, 2021
1 parent 5175e48 commit 85953ab
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 56 deletions.
6 changes: 6 additions & 0 deletions pkg/app/master/commands/build/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ var CLI = cli.Command{
}
}

if targetRef == "" {
xc.Out.Error("param.target", "missing target - make sure to set one of the target params")
cli.ShowCommandHelp(ctx, Name)
return nil
}

gcvalues, err := commands.GlobalFlagValues(ctx)
if err != nil {
xc.Out.Error("param.global", err.Error())
Expand Down
143 changes: 134 additions & 9 deletions pkg/app/master/commands/build/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ import (
)

const appName = commands.AppName
const composeProjectNamePat = "dsbuild_%v_%v"

// Build command exit codes
const (
ecbOther = iota + 1
ecbBadCustomImageTag
ecbImageBuildError
ecbNoEntrypoint
ecbBadTargetComposeSvc
ecbComposeSvcNoImage
)

type ovars = app.OutVars
Expand Down Expand Up @@ -282,20 +285,91 @@ func OnCommand(
//todo: remove the temporary fat image (should have a flag for it in case users want the fat image too)
}

var depServicesExe *compose.Execution
if composeFile != "" {
composeSelectors := compose.NewServiceSelectors(depIncludeComposeSvcDeps,
if targetComposeSvc != "" && depIncludeComposeSvcDeps != targetComposeSvc {
var found bool
for _, svcName := range depExcludeComposeSvcs {
if svcName == targetComposeSvc {
found = true
break
}
}

if !found {
depExcludeComposeSvcs = append(depExcludeComposeSvcs, targetComposeSvc)
}
}

selectors := compose.NewServiceSelectors(depIncludeComposeSvcDeps,
depIncludeComposeSvcs,
depExcludeComposeSvcs)

//todo: move compose flags to options
options := &compose.ExecutionOptions{}

logger.Debugf("compose: file='%s' selectors='%+v'\n",
composeFile, composeSelectors)
/*
cx, err := compose.NewExecution(xc,
logger,
client,
composeFile
)
*/
composeFile, selectors)

composeProjectName := fmt.Sprintf(composeProjectNamePat, os.Getpid(), time.Now().UTC().Format("20060102150405"))

exe, err := compose.NewExecution(xc,
logger,
client,
composeFile,
selectors,
composeProjectName,
"", //workingDir (todo: add a flag)
nil, //environment (todo: add a flag)
false, //buildImages
doPull,
nil, //pullExcludes (todo: add a flag)
true, //ownAllResources
options,
nil, //eventCh
true) //printState

errutil.FailOn(err)

if !exe.SelectedHaveImages() {
xc.Out.Info("compose.file.error",
ovars{
"status": "service.no.image",
"file": composeFile,
})

exitCode := commands.ECTBuild | ecbComposeSvcNoImage
xc.Out.State("exited",
ovars{
"exit.code": exitCode,
"version": v.Current(),
"location": fsutil.ExeDir(),
})

xc.Exit(exitCode)
}

targetSvcInfo := exe.Service(targetComposeSvc)
if targetSvcInfo == nil {
xc.Out.Info("target.compose.svc.error",
ovars{
"status": "unknown.compose.service",
"target": targetComposeSvc,
})

exitCode := commands.ECTBuild | ecbBadTargetComposeSvc
xc.Out.State("exited",
ovars{
"exit.code": exitCode,
"version": v.Current(),
"location": fsutil.ExeDir(),
})

xc.Exit(exitCode)
}

targetRef = targetSvcInfo.Config.Image
depServicesExe = exe
}

logger.Infof("image=%v http-probe=%v remove-file-artifacts=%v image-overrides=%+v entrypoint=%+v (%v) cmd=%+v (%v) workdir='%v' env=%+v expose=%+v",
Expand Down Expand Up @@ -432,6 +506,47 @@ func OnCommand(
}

xc.Out.State("image.inspection.done")

selectedNetNames := map[string]string{}
if depServicesExe != nil {
xc.Out.State("container.dependencies.init.start")
err = depServicesExe.Prepare()
errutil.FailOn(err)
err = depServicesExe.Start()
errutil.FailOn(err)

//todo:
//need a better way to make sure the dependencies are ready
//monitor docker events
//use basic application level checks (probing)
time.Sleep(3 * time.Second)
xc.Out.State("container.dependencies.init.done")

var selectedNetworks bool
//might need more info (including alias info) when targeting compose services
allNetNames := depServicesExe.ActiveNetworkNames()
if len(composeNets) > 0 {
for _, key := range composeNets {
if net, found := allNetNames[key]; found {
selectedNetworks = true
selectedNetNames[key] = net
}
}
}

if targetComposeSvc != "" {
svcNets := depServicesExe.ActiveServiceNetworks(targetComposeSvc)
for key, name := range svcNets {
selectedNetworks = true
selectedNetNames[key] = name
}
}

if !selectedNetworks {
selectedNetNames = allNetNames
}
}

xc.Out.State("container.inspection.start")

containerInspector, err := container.NewInspector(
Expand Down Expand Up @@ -468,6 +583,7 @@ func OnCommand(
doIncludeCertDirs,
doIncludeCertPKAll,
doIncludeCertPKDirs,
selectedNetNames,
gparams.Debug,
gparams.InContainer,
true,
Expand Down Expand Up @@ -683,6 +799,15 @@ func OnCommand(
xc.Exit(exitCode)
}

if depServicesExe != nil {
xc.Out.State("container.dependencies.shutdown.start")
err = depServicesExe.Stop()
errutil.WarnOn(err)
err = depServicesExe.Cleanup()
errutil.WarnOn(err)
xc.Out.State("container.dependencies.shutdown.done")
}

xc.Out.State("container.inspection.artifact.processing")

if !containerInspector.HasCollectedData() {
Expand Down
1 change: 1 addition & 0 deletions pkg/app/master/commands/cliflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const (
//Compose-related flags
FlagComposeFile = "compose-file"
FlagTargetComposeSvc = "target-compose-svc"
FlagDepExcludeComposeSvcAll = "dep-exclude-compose-svc-all" //wip
FlagDepIncludeComposeSvc = "dep-include-compose-svc"
FlagDepExcludeComposeSvc = "dep-exclude-compose-svc"
FlagDepIncludeComposeSvcDeps = "dep-include-compose-svc-deps"
Expand Down
1 change: 1 addition & 0 deletions pkg/app/master/commands/profile/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ func OnCommand(
false, //doIncludeCertDirs
false, //doIncludeCertPKAll
false, //doIncludeCertPKDirs
nil, //selectedNetNames
gparams.Debug,
gparams.InContainer,
true,
Expand Down
Loading

0 comments on commit 85953ab

Please sign in to comment.