This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
env_extractor.go
87 lines (71 loc) · 2.19 KB
/
env_extractor.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
package podprocess
import (
"os"
"github.com/square/p2/pkg/launch"
"github.com/square/p2/pkg/logging"
"github.com/square/p2/pkg/pods"
"github.com/square/p2/pkg/types"
"github.com/square/p2/pkg/util"
"github.com/sirupsen/logrus"
)
type EnvironmentExtractor struct {
DatabasePath string
Logger logging.Logger
}
type RetryableError struct {
Inner error
}
func (r RetryableError) Error() string {
return r.Inner.Error()
}
func IsRetryable(err error) bool {
_, ok := err.(RetryableError)
return ok
}
// Write exit code and exit status along with some information pulled from the current
// environment into the configured sqlite database. The environment extractor is hooked
// into runits ./finish file (http://smarden.org/runit/runsv.8.html)
func (e EnvironmentExtractor) WriteFinish(exitCode int, exitStatus int) error {
finish, err := e.constructFinishFromEnvironment(exitCode, exitStatus)
if err != nil {
return err
}
logger := e.Logger.SubLogger(logrus.Fields{
"component": "finish_environment_extractor",
})
finishService, err := NewSQLiteFinishService(e.DatabasePath, logger)
if err != nil {
return err
}
err = finishService.Insert(finish)
if err != nil {
return RetryableError{
Inner: err,
}
}
return nil
}
func (e EnvironmentExtractor) constructFinishFromEnvironment(exitCode int, exitStatus int) (FinishOutput, error) {
podID := os.Getenv(pods.PodIDEnvVar)
if podID == "" {
return FinishOutput{}, util.Errorf("No %s env var set", pods.PodIDEnvVar)
}
launchableID := os.Getenv(pods.LaunchableIDEnvVar)
if launchableID == "" {
return FinishOutput{}, util.Errorf("No %s env var set", pods.LaunchableIDEnvVar)
}
entryPoint := os.Getenv(launch.EntryPointEnvVar)
if entryPoint == "" {
return FinishOutput{}, util.Errorf("No %s env var set", launch.EntryPointEnvVar)
}
// It's okay if this one is missing, most pods are "legacy" pods that have a blank unique key
podUniqueKey := os.Getenv(pods.PodUniqueKeyEnvVar)
return FinishOutput{
PodID: types.PodID(podID),
LaunchableID: launch.LaunchableID(launchableID),
EntryPoint: entryPoint,
PodUniqueKey: types.PodUniqueKey(podUniqueKey),
ExitCode: exitCode,
ExitStatus: exitStatus,
}, nil
}