forked from heroku/cnb-shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
releaser.go
90 lines (71 loc) · 1.98 KB
/
releaser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package cnbshim
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"gopkg.in/yaml.v2"
"github.com/buildpack/libbuildpack/layers"
"github.com/buildpack/libbuildpack/logger"
)
type Release struct {
DefaultProcessTypes map[string]string `yaml:"default_process_types,omitempty"`
}
func WriteLaunchMetadata(appDir, layersDir, targetBuildpackDir string, log logger.Logger) error {
release, err := ExecReleaseScript(appDir, targetBuildpackDir)
if err != nil {
return err
}
procfile, err := ReadProcfile(appDir)
if err != nil {
return err
}
processTypes := make(map[string]string)
for name, command := range release.DefaultProcessTypes {
processTypes[name] = command
}
for name, command := range procfile {
processTypes[name] = command
}
processes := layers.Processes{}
for name, command := range processTypes {
processes = append(processes, layers.Process{
Type: name,
Command: command,
})
}
l := layers.NewLayers(layersDir, log)
return l.WriteApplicationMetadata(layers.Metadata{
Processes: processes,
})
}
func ExecReleaseScript(appDir, targetBuildpackDir string) (Release, error) {
releaseScript := filepath.Join(targetBuildpackDir, "bin", "release")
_, err := os.Stat(releaseScript)
if !os.IsNotExist(err) {
cmd := exec.Command(releaseScript, appDir)
cmd.Env = os.Environ()
out, err := cmd.Output()
if err != nil {
return Release{DefaultProcessTypes: make(map[string]string)}, err
}
release := Release{}
return release, yaml.Unmarshal(out, &release)
} else {
return Release{DefaultProcessTypes: make(map[string]string)}, nil
}
}
func ReadProcfile(appDir string) (map[string]string, error) {
processTypes := make(map[string]string)
procfile := filepath.Join(appDir, "Procfile")
_, err := os.Stat(procfile)
if !os.IsNotExist(err) {
procfileText, err := ioutil.ReadFile(procfile)
if err != nil {
return processTypes, err
}
return processTypes, yaml.Unmarshal(procfileText, &processTypes)
} else {
return processTypes, nil
}
}