-
Notifications
You must be signed in to change notification settings - Fork 2
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 #13 from cloudfoundry/launcher-pkg
Move launcher command to a package
- Loading branch information
Showing
2 changed files
with
84 additions
and
67 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package cli | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/buildpacks/lifecycle/api" | ||
"github.com/buildpacks/lifecycle/cmd" | ||
cli "github.com/buildpacks/lifecycle/cmd/launcher/cli" | ||
"github.com/buildpacks/lifecycle/env" | ||
"github.com/buildpacks/lifecycle/launch" | ||
platform "github.com/buildpacks/lifecycle/platform/launch" | ||
"github.com/spf13/cobra" | ||
|
||
builderCli "code.cloudfoundry.org/cnbapplifecycle/cmd/builder/cli" | ||
"code.cloudfoundry.org/cnbapplifecycle/pkg/errors" | ||
"code.cloudfoundry.org/cnbapplifecycle/pkg/log" | ||
) | ||
|
||
const defaultProcessType = "web" | ||
|
||
func Execute() error { | ||
return launcherCmd.Execute() | ||
} | ||
|
||
var launcherCmd = &cobra.Command{ | ||
Use: "launcher", | ||
SilenceUsage: true, | ||
RunE: func(cobraCmd *cobra.Command, cmdArgs []string) error { | ||
var md launch.Metadata | ||
var args []string | ||
logger := log.NewLogger() | ||
self := defaultProcessType | ||
defaultProc := defaultProcessType | ||
|
||
if _, err := toml.DecodeFile(launch.GetMetadataFilePath(cmd.EnvOrDefault(platform.EnvLayersDir, builderCli.DefaultLayersPath)), &md); err != nil { | ||
logger.Errorf("failed decoding, error: %s\n", err.Error()) | ||
return errors.ErrLaunching | ||
} | ||
|
||
if err := verifyBuildpackAPIs(md.Buildpacks); err != nil { | ||
logger.Errorf("failed verifying buildpack API, error: %s\n", err.Error()) | ||
return errors.ErrLaunching | ||
} | ||
|
||
if len(os.Args) > 1 && os.Args[1] == "--" { | ||
self = "launcher" | ||
args = os.Args[2:] | ||
defaultProc = "" | ||
} | ||
|
||
launcher := &launch.Launcher{ | ||
DefaultProcessType: defaultProc, | ||
LayersDir: cmd.EnvOrDefault(platform.EnvLayersDir, builderCli.DefaultLayersPath), | ||
AppDir: cmd.EnvOrDefault(platform.EnvAppDir, builderCli.DefaultWorkspacePath), | ||
PlatformAPI: api.MustParse(builderCli.PlatformAPI), | ||
Processes: md.Processes, | ||
Buildpacks: md.Buildpacks, | ||
Env: env.NewLaunchEnv(os.Environ(), launch.ProcessDir, "/tmp/lifecycle"), | ||
Exec: launch.OSExecFunc, | ||
ExecD: launch.NewExecDRunner(), | ||
Shell: launch.DefaultShell, | ||
Setenv: os.Setenv, | ||
} | ||
|
||
if err := launcher.Launch(self, args); err != nil { | ||
logger.Errorf("failed launching with self: %q, defaultProc: %q, args: %#v, error: %s\n", self, defaultProc, args, err.Error()) | ||
return errors.ErrLaunching | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func verifyBuildpackAPIs(bps []launch.Buildpack) error { | ||
for _, bp := range bps { | ||
if err := cmd.VerifyBuildpackAPI(cli.KindBuildpack, bp.ID, bp.API, cmd.DefaultLogger); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
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