github.com/cloudcredo/cloudrocker@v0.0.0-20160108110610-1320f8cc2dfd/stager/stager.go (about)

     1  package stager
     2  
     3  import (
     4  	"crypto/md5"
     5  	"fmt"
     6  	"io"
     7  	"log"
     8  	"os"
     9  
    10  	"github.com/cloudcredo/cloudrocker/config"
    11  	"github.com/cloudcredo/cloudrocker/utils"
    12  
    13  	"github.com/cloudcredo/cloudrocker/Godeps/_workspace/src/github.com/cloudfoundry-incubator/buildpack_app_lifecycle"
    14  	"github.com/cloudcredo/cloudrocker/Godeps/_workspace/src/github.com/cloudfoundry-incubator/buildpack_app_lifecycle/buildpackrunner"
    15  )
    16  
    17  type BuildpackRunner interface {
    18  	Run() error
    19  }
    20  
    21  func RunBuildpack(writer io.Writer, runner BuildpackRunner) error {
    22  	fmt.Fprintln(writer, "Running Buildpacks...")
    23  	return runner.Run()
    24  }
    25  
    26  func NewBuildpackRunner(buildpackDir string) *buildpackrunner.Runner {
    27  	prepareMd5BuildpacksDir(buildpackDir, "/tmp/buildpacks")
    28  	var err error
    29  	dirs := []string{}
    30  	if dirs, err = utils.SubDirs(buildpackDir); err != nil {
    31  		log.Fatalf(" %s", err)
    32  	}
    33  	config := buildpack_app_lifecycle.NewLifecycleBuilderConfig(dirs, false, false)
    34  	return buildpackrunner.New(&config)
    35  }
    36  
    37  func ValidateStagedApp(directories *config.Directories) error {
    38  	if _, err := os.Stat(directories.Tmp() + "/droplet"); err != nil {
    39  		return fmt.Errorf("Staging failed - have you added a buildpack for this type of application?")
    40  	}
    41  	if _, err := os.Stat(directories.Tmp() + "/result.json"); err != nil {
    42  		return fmt.Errorf("Staging failed - no result json was produced by the matching buildpack!")
    43  	}
    44  	return nil
    45  }
    46  
    47  func prepareMd5BuildpacksDir(src string, dst string) {
    48  	os.MkdirAll(src, 0755)
    49  	os.MkdirAll(dst, 0755)
    50  	var err error
    51  	dirs := []string{}
    52  	if dirs, err = utils.SubDirs(src); err != nil {
    53  		log.Fatalf(" %s", err)
    54  	}
    55  	for _, dir := range dirs {
    56  		if err := os.Symlink(src+"/"+dir, dst+"/"+md5sum(dir)); err != nil {
    57  			log.Fatalf(" %s", err)
    58  		}
    59  	}
    60  }
    61  
    62  func md5sum(src string) string {
    63  	return fmt.Sprintf("%x", md5.Sum([]byte(src)))
    64  }