/
bpm_config.go
165 lines (128 loc) · 3.79 KB
/
bpm_config.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (C) 2017-Present CloudFoundry.org Foundation, Inc. All rights reserved.
//
// This program and the accompanying materials are made available under
// the terms of the under the Apache License, Version 2.0 (the "License”);
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package config
import (
"fmt"
"path/filepath"
"bpm/bosh"
"bpm/jobid"
)
func RuncPath(env *bosh.Env) string {
return env.Root().Join("packages", "bpm", "bin", "runc").External()
}
func BundlesRoot(env *bosh.Env) string {
return env.Root().Join("data", "bpm", "bundles").External()
}
func RuncRoot(env *bosh.Env) string {
return env.Root().Join("data", "bpm", "runc").External()
}
func LocksPath(env *bosh.Env) string {
return env.Root().Join("data", "bpm", "locks").External()
}
type BPMConfig struct {
jobName string
procName string
boshEnv *bosh.Env
}
func NewBPMConfig(boshEnv *bosh.Env, jobName, procName string) *BPMConfig {
return &BPMConfig{
jobName: jobName,
procName: procName,
boshEnv: boshEnv,
}
}
func (c *BPMConfig) JobName() string {
return c.jobName
}
func (c *BPMConfig) ProcName() string {
return c.procName
}
func (c *BPMConfig) DataDir() bosh.Path {
return c.boshEnv.DataDir(c.JobName())
}
func (c *BPMConfig) StoreDir() bosh.Path {
return c.boshEnv.StoreDir(c.JobName())
}
func (c *BPMConfig) SocketDir() bosh.Path {
return c.boshEnv.RunDir(c.JobName())
}
func (c *BPMConfig) TempDir() bosh.Path {
return c.DataDir().Join("tmp")
}
func (c *BPMConfig) LogDir() bosh.Path {
return c.boshEnv.LogDir(c.JobName())
}
func (c *BPMConfig) Stdout() bosh.Path {
return c.LogDir().Join(fmt.Sprintf("%s.stdout.log", c.procName))
}
func (c *BPMConfig) Stderr() bosh.Path {
return c.LogDir().Join(fmt.Sprintf("%s.stderr.log", c.procName))
}
func (c *BPMConfig) PidDir() bosh.Path {
return c.boshEnv.RunDir("bpm").Join(c.JobName())
}
func (c *BPMConfig) PidFile() bosh.Path {
return c.PidDir().Join(fmt.Sprintf("%s.pid", c.procName))
}
func (c *BPMConfig) LockFile() bosh.Path {
return c.PidDir().Join(fmt.Sprintf("%s.lock", c.procName))
}
func (c *BPMConfig) PackageDir() bosh.Path {
return c.boshEnv.PackageDir()
}
func (c *BPMConfig) DataPackageDir() bosh.Path {
return c.boshEnv.DataPackageDir()
}
func (c *BPMConfig) JobDir() bosh.Path {
return c.boshEnv.JobDir(c.JobName())
}
func (c *BPMConfig) JobConfig() string {
return c.JobDir().Join(filepath.Join("config", "bpm.yml")).External()
}
func (c *BPMConfig) TiniPath() bosh.Path {
return c.PackageDir().Join("bpm", "bin", "tini")
}
func (c *BPMConfig) DefaultVolumes() []string {
return []string{c.DataDir().External(), c.StoreDir().External()}
}
func (c *BPMConfig) ParseJobConfig() (*JobConfig, error) {
cfg, err := ParseJobConfig(c.JobConfig())
if err != nil {
return nil, err
}
err = cfg.Validate(c.boshEnv, c.DefaultVolumes())
if err != nil {
return nil, err
}
return cfg, nil
}
func (c *BPMConfig) BPMLog() string {
return c.LogDir().Join("bpm.log").External()
}
func (c *BPMConfig) BundlePath() string {
return filepath.Join(BundlesRoot(c.boshEnv), c.jobName, c.procName)
}
func (c *BPMConfig) RootFSPath() string {
return filepath.Join(c.BundlePath(), "rootfs")
}
func (c *BPMConfig) ContainerID() string {
var containerID string
if c.jobName == c.procName {
containerID = c.jobName
} else {
containerID = fmt.Sprintf("%s.%s", c.jobName, c.procName)
}
return jobid.Encode(containerID)
}