/
main.go
82 lines (67 loc) · 2.01 KB
/
main.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
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"code.cloudfoundry.org/buildpackapplifecycle/buildpackrunner"
"code.cloudfoundry.org/buildpackapplifecycle/credhub_flags"
"code.cloudfoundry.org/buildpackapplifecycle/env"
"code.cloudfoundry.org/goshims/osshim"
yaml "gopkg.in/yaml.v2"
)
var preStartMessage = "Invoking pre-start scripts."
var startMessage = "Invoking start command."
func main() {
if len(os.Args) < 4 {
fmt.Fprintf(os.Stderr, "%s: received only %d arguments\n", os.Args[0], len(os.Args)-1)
fmt.Fprintf(os.Stderr, "Usage: %s <app-directory> <start-command> <metadata>", os.Args[0])
os.Exit(1)
}
dir := os.Args[1]
startCommand := os.Args[2]
absDir, err := filepath.Abs(dir)
if err == nil {
dir = absDir
}
stagingInfo, err := unmarhsalStagingInfo()
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid staging info - %s", err)
os.Exit(1)
}
var command string
if startCommand != "" {
command = startCommand
} else {
command = stagingInfo.StartCommand
}
if command == "" {
fmt.Fprintf(os.Stderr, "%s: no start command specified or detected in droplet", os.Args[0])
os.Exit(1)
}
credhubFlags := credhub_flags.NewCredhubFlags("launcher")
credhubFlags.Parse(os.Args[3:len(os.Args)])
attempts := credhubFlags.ConnectAttempts()
delay := credhubFlags.RetryDelay()
if err := env.CalcEnv(&osshim.OsShim{}, dir, attempts, delay); err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(3)
}
runtime.GOMAXPROCS(1)
runProcess(dir, command, stagingInfo.GetEntrypointPrefix())
}
func unmarhsalStagingInfo() (buildpackrunner.DeaStagingInfo, error) {
stagingInfo := buildpackrunner.DeaStagingInfo{}
stagingInfoData, err := os.ReadFile(buildpackrunner.DeaStagingInfoFilename)
if err != nil {
if os.IsNotExist(err) {
return stagingInfo, nil
}
return stagingInfo, err
}
err = yaml.Unmarshal(stagingInfoData, &stagingInfo)
if err != nil {
return stagingInfo, fmt.Errorf("failed to unmarshal %s: %w", buildpackrunner.DeaStagingInfoFilename, err)
}
return stagingInfo, nil
}