github.com/buildpack/pack@v0.5.0/build/phases.go (about)

     1  package build
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/Masterminds/semver"
     8  )
     9  
    10  const (
    11  	layersDir      = "/layers"
    12  	appDir         = "/workspace"
    13  	cacheDir       = "/cache"
    14  	launchCacheDir = "/launch-cache"
    15  	platformDir    = "/platform"
    16  )
    17  
    18  func (l *Lifecycle) Detect(ctx context.Context, networkMode string) error {
    19  	detect, err := l.NewPhase(
    20  		"detector",
    21  		WithArgs(
    22  			l.withLogLevel(
    23  				"-app", appDir,
    24  				"-platform", platformDir,
    25  			)...,
    26  		),
    27  		WithNetwork(networkMode),
    28  	)
    29  	if err != nil {
    30  		return err
    31  	}
    32  	defer detect.Cleanup()
    33  	return detect.Run(ctx)
    34  }
    35  
    36  func (l *Lifecycle) Restore(ctx context.Context, cacheName string) error {
    37  	restore, err := l.NewPhase(
    38  		"restorer",
    39  		WithDaemonAccess(),
    40  		WithArgs(
    41  			l.withLogLevel(
    42  				"-path", cacheDir,
    43  				"-layers", layersDir,
    44  			)...,
    45  		),
    46  		WithBinds(fmt.Sprintf("%s:%s", cacheName, cacheDir)),
    47  	)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	defer restore.Cleanup()
    52  	return restore.Run(ctx)
    53  }
    54  
    55  func (l *Lifecycle) Analyze(ctx context.Context, repoName string, publish, clearCache bool) error {
    56  	analyze, err := l.newAnalyze(repoName, publish, clearCache)
    57  	if err != nil {
    58  		return err
    59  	}
    60  	defer analyze.Cleanup()
    61  	return analyze.Run(ctx)
    62  }
    63  
    64  func (l *Lifecycle) newAnalyze(repoName string, publish, clearCache bool) (*Phase, error) {
    65  	args := []string{
    66  		"-layers", layersDir,
    67  		repoName,
    68  	}
    69  	if clearCache {
    70  		args = prependArg("-skip-layers", args)
    71  	}
    72  
    73  	if publish {
    74  		return l.NewPhase(
    75  			"analyzer",
    76  			WithRegistryAccess(repoName),
    77  			WithArgs(args...),
    78  		)
    79  	}
    80  	return l.NewPhase(
    81  		"analyzer",
    82  		WithDaemonAccess(),
    83  		WithArgs(
    84  			l.withLogLevel(
    85  				prependArg(
    86  					"-daemon",
    87  					args,
    88  				)...,
    89  			)...,
    90  		),
    91  	)
    92  }
    93  
    94  func prependArg(arg string, args []string) []string {
    95  	return append([]string{arg}, args...)
    96  }
    97  
    98  func (l *Lifecycle) Build(ctx context.Context, networkMode string) error {
    99  	build, err := l.NewPhase(
   100  		"builder",
   101  		WithArgs(
   102  			"-layers", layersDir,
   103  			"-app", appDir,
   104  			"-platform", platformDir,
   105  		),
   106  		WithNetwork(networkMode),
   107  	)
   108  	if err != nil {
   109  		return err
   110  	}
   111  	defer build.Cleanup()
   112  	return build.Run(ctx)
   113  }
   114  
   115  func (l *Lifecycle) Export(ctx context.Context, repoName string, runImage string, publish bool, launchCacheName string) error {
   116  	export, err := l.newExport(repoName, runImage, publish, launchCacheName)
   117  	if err != nil {
   118  		return err
   119  	}
   120  	defer export.Cleanup()
   121  	return export.Run(ctx)
   122  }
   123  
   124  func (l *Lifecycle) newExport(repoName, runImage string, publish bool, launchCacheName string) (*Phase, error) {
   125  	if publish {
   126  		return l.NewPhase(
   127  			"exporter",
   128  			WithRegistryAccess(repoName, runImage),
   129  			WithArgs(
   130  				l.withLogLevel(
   131  					"-image", runImage,
   132  					"-layers", layersDir,
   133  					"-app", appDir,
   134  					repoName,
   135  				)...,
   136  			),
   137  		)
   138  	}
   139  
   140  	return l.NewPhase(
   141  		"exporter",
   142  		WithDaemonAccess(),
   143  		WithArgs(
   144  			l.withLogLevel(
   145  				"-image", runImage,
   146  				"-layers", layersDir,
   147  				"-app", appDir,
   148  				"-daemon",
   149  				"-launch-cache", launchCacheDir,
   150  				repoName,
   151  			)...,
   152  		),
   153  		WithBinds(fmt.Sprintf("%s:%s", launchCacheName, launchCacheDir)),
   154  	)
   155  }
   156  
   157  func (l *Lifecycle) Cache(ctx context.Context, cacheName string) error {
   158  	cache, err := l.NewPhase(
   159  		"cacher",
   160  		WithDaemonAccess(),
   161  		WithArgs(
   162  			l.withLogLevel(
   163  				"-path", cacheDir,
   164  				"-layers", layersDir,
   165  			)...,
   166  		),
   167  		WithBinds(fmt.Sprintf("%s:%s", cacheName, cacheDir)),
   168  	)
   169  	if err != nil {
   170  		return err
   171  	}
   172  	defer cache.Cleanup()
   173  	return cache.Run(ctx)
   174  }
   175  
   176  func (l *Lifecycle) withLogLevel(args ...string) []string {
   177  	version := semver.MustParse(l.version)
   178  	if semver.MustParse("0.4.0").LessThan(version) {
   179  		if l.logger.IsVerbose() {
   180  			return append([]string{"-log-level", "debug"}, args...)
   181  		}
   182  	}
   183  	return args
   184  }