Skip to content

Commit

Permalink
Merge pull request #13 from cloudfoundry/launcher-pkg
Browse files Browse the repository at this point in the history
Move launcher command to a package
  • Loading branch information
modulo11 authored Jun 13, 2024
2 parents a647764 + 8236b26 commit 8404d88
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 67 deletions.
82 changes: 82 additions & 0 deletions cmd/launcher/cli/cli.go
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
}
69 changes: 2 additions & 67 deletions cmd/launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,79 +3,14 @@ package main
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"

builderCli "code.cloudfoundry.org/cnbapplifecycle/cmd/builder/cli"
"code.cloudfoundry.org/cnbapplifecycle/cmd/launcher/cli"
"code.cloudfoundry.org/cnbapplifecycle/pkg/errors"
"code.cloudfoundry.org/cnbapplifecycle/pkg/log"
)

const defaultProcessType = "web"

func main() {
err := run()
err := cli.Execute()

if err != nil {
os.Exit(errors.ExitCodeFromError(err))
}
}

func run() 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
}

0 comments on commit 8404d88

Please sign in to comment.