forked from juju/juju
/
paths.go
216 lines (186 loc) · 5.7 KB
/
paths.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2014 Canonical Ltd.
// Copyright 2014 Cloudbase Solutions SRL
// Licensed under the AGPLv3, see LICENCE file for details.
package paths
import (
"os"
"runtime"
"strings"
jujuos "github.com/DavinZhang/juju/core/os"
"github.com/DavinZhang/juju/core/series"
)
type OS int // strongly typed runtime.GOOS value to help with refactoring
const (
OSWindows OS = 1
OSUnixLike OS = 2
)
type osVarType int
const (
tmpDir osVarType = iota
logDir
dataDir
storageDir
confDir
jujuExec
certDir
metricsSpoolDir
uniterStateDir
jujuDumpLogs
jujuIntrospect
instanceCloudInitDir
cloudInitCfgDir
curtinInstallConfig
transientDataDir
)
const (
// NixDataDir is location for agent binaries on *nix operating systems.
NixDataDir = "/var/lib/juju"
// NixTransientDataDir is location for storing transient data on *nix
// operating systems.
NixTransientDataDir = "/var/run/juju"
// NixLogDir is location for Juju logs on *nix operating systems.
NixLogDir = "/var/log"
)
var nixVals = map[osVarType]string{
tmpDir: "/tmp",
logDir: NixLogDir,
dataDir: NixDataDir,
transientDataDir: NixTransientDataDir,
storageDir: "/var/lib/juju/storage",
confDir: "/etc/juju",
jujuExec: "/usr/bin/juju-exec",
jujuDumpLogs: "/usr/bin/juju-dumplogs",
jujuIntrospect: "/usr/bin/juju-introspect",
certDir: "/etc/juju/certs.d",
metricsSpoolDir: "/var/lib/juju/metricspool",
uniterStateDir: "/var/lib/juju/uniter/state",
instanceCloudInitDir: "/var/lib/cloud/instance",
cloudInitCfgDir: "/etc/cloud/cloud.cfg.d",
curtinInstallConfig: "/root/curtin-install-cfg.yaml",
}
var winVals = map[osVarType]string{
tmpDir: "C:/Juju/tmp",
logDir: "C:/Juju/log",
dataDir: "C:/Juju/lib/juju",
transientDataDir: "C:/Juju/lib/juju-transient",
storageDir: "C:/Juju/lib/juju/storage",
confDir: "C:/Juju/etc",
jujuExec: "C:/Juju/bin/juju-exec.exe",
jujuDumpLogs: "C:/Juju/bin/juju-dumplogs.exe",
jujuIntrospect: "C:/Juju/bin/juju-introspect.exe",
certDir: "C:/Juju/certs",
metricsSpoolDir: "C:/Juju/lib/juju/metricspool",
uniterStateDir: "C:/Juju/lib/juju/uniter/state",
}
// Chown is a variable here so it can be mocked out in tests to a no-op.
// Agents run as root, but users don't.
var Chown = os.Chown
// CurrentOS returns the OS value for the currently-running system.
func CurrentOS() OS {
switch runtime.GOOS {
case "windows":
return OSWindows
default:
return OSUnixLike
}
}
// SeriesToOS converts the given series to an OS value.
func SeriesToOS(ser string) OS {
osType, err := series.GetOSFromSeries(ser)
if err != nil {
// We shouldn't get here in normal operation, as the series should be
// valid at this point, but handle in a reasonable way in any case.
if strings.HasPrefix(ser, "win") {
return OSWindows
}
return OSUnixLike
}
switch osType {
case jujuos.Windows:
return OSWindows
default:
return OSUnixLike
}
}
// osVal will lookup the value of the key valname
// in the appropriate map, based on the OS value.
func osVal(os OS, valname osVarType) string {
switch os {
case OSWindows:
return winVals[valname]
default:
return nixVals[valname]
}
}
// TempDir returns the path on disk to the correct tmp directory
// for the series. This value will be the same on virtually
// all linux systems, but will differ on windows
func TempDir(os OS) string {
return osVal(os, tmpDir)
}
// LogDir returns filesystem path the directory where juju may
// save log files.
func LogDir(os OS) string {
return osVal(os, logDir)
}
// DataDir returns a filesystem path to the folder used by juju to
// store tools, charms, locks, etc
func DataDir(os OS) string {
return osVal(os, dataDir)
}
// TransientDataDir returns a filesystem path to the folder used by juju to
// store transient data that will not survive a reboot.
func TransientDataDir(os OS) string {
return osVal(os, transientDataDir)
}
// MetricsSpoolDir returns a filesystem path to the folder used by juju
// to store metrics.
func MetricsSpoolDir(os OS) string {
return osVal(os, metricsSpoolDir)
}
// CertDir returns a filesystem path to the folder used by juju to
// store certificates that are added by default to the Juju client
// api certificate pool.
func CertDir(os OS) string {
return osVal(os, certDir)
}
// StorageDir returns a filesystem path to the folder used by juju to
// mount machine-level storage.
func StorageDir(os OS) string {
return osVal(os, storageDir)
}
// ConfDir returns the path to the directory where Juju may store
// configuration files.
func ConfDir(os OS) string {
return osVal(os, confDir)
}
// JujuExec returns the absolute path to the juju-exec binary for
// a particular series.
func JujuExec(os OS) string {
return osVal(os, jujuExec)
}
// JujuDumpLogs returns the absolute path to the juju-dumplogs binary
// for a particular series.
func JujuDumpLogs(os OS) string {
return osVal(os, jujuDumpLogs)
}
// JujuIntrospect returns the absolute path to the juju-introspect
// binary for a particular series.
func JujuIntrospect(os OS) string {
return osVal(os, jujuIntrospect)
}
// MachineCloudInitDir returns the absolute path to the instance
// cloudinit directory for a particular series.
func MachineCloudInitDir(os OS) string {
return osVal(os, instanceCloudInitDir)
}
// CurtinInstallConfig returns the absolute path the configuration file
// written by Curtin during machine provisioning.
func CurtinInstallConfig(os OS) string {
return osVal(os, curtinInstallConfig)
}
// CloudInitCfgDir returns the absolute path to the instance
// cloud config directory for a particular series.
func CloudInitCfgDir(os OS) string {
return osVal(os, cloudInitCfgDir)
}