/
paths.go
217 lines (184 loc) · 6.63 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
217
// Copyright 2012-2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package uniter
import (
"crypto/tls"
"fmt"
"path/filepath"
"github.com/juju/names/v5"
"github.com/juju/juju/agent"
"github.com/juju/juju/agent/tools"
caasconstants "github.com/juju/juju/caas/kubernetes/provider/constants"
"github.com/juju/juju/juju/sockets"
)
// Paths represents the set of filesystem paths a uniter worker has reason to
// care about.
type Paths struct {
// ToolsDir is the directory containing the jujud executable running this
// process; and also containing jujuc tool symlinks to that executable. It's
// the only path in this struct that is not typically pointing inside the
// directory reserved for the exclusive use of this worker (typically
// /var/lib/juju/agents/$UNIT_TAG/ )
ToolsDir string
// Runtime represents the set of paths that are relevant at runtime.
Runtime RuntimePaths
// State represents the set of paths that hold persistent local state for
// the uniter.
State StatePaths
}
// GetToolsDir exists to satisfy the context.Paths interface.
func (paths Paths) GetToolsDir() string {
return paths.ToolsDir
}
// GetBaseDir exists to satisfy the context.Paths interface.
func (paths Paths) GetBaseDir() string {
return paths.State.BaseDir
}
// GetCharmDir exists to satisfy the context.Paths interface.
func (paths Paths) GetCharmDir() string {
return paths.State.CharmDir
}
// GetResourcesDir exists to satisfy the context.Paths interface.
func (paths Paths) GetResourcesDir() string {
return paths.State.ResourcesDir
}
// GetJujucClientSocket exists to satisfy the context.Paths interface.
func (paths Paths) GetJujucClientSocket(remote bool) sockets.Socket {
if remote {
return paths.Runtime.RemoteJujucServerSocket.Client
}
return paths.Runtime.LocalJujucServerSocket.Client
}
// GetJujucServerSocket exists to satisfy the context.Paths interface.
func (paths Paths) GetJujucServerSocket(remote bool) sockets.Socket {
if remote {
return paths.Runtime.RemoteJujucServerSocket.Server
}
return paths.Runtime.LocalJujucServerSocket.Server
}
// GetMetricsSpoolDir exists to satisfy the runner.Paths interface.
func (paths Paths) GetMetricsSpoolDir() string {
return paths.State.MetricsSpoolDir
}
const jujucServerSocketPort = 30000
// SocketPair is a server+client pair of socket descriptors.
type SocketPair struct {
Server sockets.Socket
Client sockets.Socket
}
// RuntimePaths represents the set of paths that are relevant at runtime.
type RuntimePaths struct {
// LocalJujuExecSocket listens for juju-exec invocations, and is always
// active.
LocalJujuExecSocket SocketPair
// RemoteJujuExecSocket listens for remote juju-exec invocations.
RemoteJujuExecSocket SocketPair
// JujucServerSocket listens for jujuc invocations, and is only
// active when supporting a jujuc execution context.
LocalJujucServerSocket SocketPair
// RemoteJujucServerSocket listens for remote jujuc invocations, and is only
// active when supporting a jujuc execution context.
RemoteJujucServerSocket SocketPair
}
// StatePaths represents the set of paths that hold persistent local state for
// the uniter.
type StatePaths struct {
// BaseDir is the unit agent's base directory.
BaseDir string
// CharmDir is the directory to which the charm the uniter runs is deployed.
CharmDir string
// ResourcesDir is the directory to which the charm the uniter runs is deployed.
ResourcesDir string
// BundlesDir holds downloaded charms.
BundlesDir string
// DeployerDir holds metadata about charms that are installing or have
// been installed.
DeployerDir string
// MetricsSpoolDir acts as temporary storage for metrics being sent from
// the uniter to state.
MetricsSpoolDir string
}
// SocketConfig specifies information for remote sockets.
type SocketConfig struct {
ServiceAddress string
OperatorAddress string
TLSConfig *tls.Config
}
// NewPaths returns the set of filesystem paths that the supplied unit should
// use, given the supplied root juju data directory path.
// If socketConfig is specified, all sockets will be TLS over TCP.
func NewPaths(dataDir string, unitTag names.UnitTag, socketConfig *SocketConfig) Paths {
return NewWorkerPaths(dataDir, unitTag, "", socketConfig)
}
// NewWorkerPaths returns the set of filesystem paths that the supplied unit worker should
// use, given the supplied root juju data directory path and worker identifier.
// Distinct worker identifiers ensure that runtime paths of different worker do not interfere.
// If socketConfig is specified, all sockets will be TLS over TCP.
func NewWorkerPaths(dataDir string, unitTag names.UnitTag, worker string, socketConfig *SocketConfig) Paths {
baseDir := agent.Dir(dataDir, unitTag)
join := filepath.Join
stateDir := join(baseDir, "state")
var newSocket func(name string) SocketPair
if socketConfig != nil {
newSocket = func(name string) SocketPair {
var port int
var address string
switch name {
case "agent":
port = jujucServerSocketPort + unitTag.Number()
address = socketConfig.OperatorAddress
case "run":
port = caasconstants.JujuExecServerSocketPort
address = socketConfig.ServiceAddress
default:
return SocketPair{}
}
return SocketPair{
Client: sockets.Socket{
Network: "tcp",
Address: fmt.Sprintf("%s:%d", address, port),
TLSConfig: socketConfig.TLSConfig,
},
Server: sockets.Socket{
Network: "tcp",
Address: fmt.Sprintf(":%d", port),
TLSConfig: socketConfig.TLSConfig,
},
}
}
} else {
newSocket = func(name string) SocketPair {
return SocketPair{}
}
}
toolsDir := tools.ToolsDir(dataDir, unitTag.String())
return Paths{
ToolsDir: filepath.FromSlash(toolsDir),
Runtime: RuntimePaths{
RemoteJujuExecSocket: newSocket("run"),
RemoteJujucServerSocket: newSocket("agent"),
LocalJujuExecSocket: newUnixSocket(baseDir, unitTag, worker, "run", false),
LocalJujucServerSocket: newUnixSocket(baseDir, unitTag, worker, "agent", true),
},
State: StatePaths{
BaseDir: baseDir,
CharmDir: join(baseDir, "charm"),
ResourcesDir: join(baseDir, "resources"),
BundlesDir: join(stateDir, "bundles"),
DeployerDir: join(stateDir, "deployer"),
MetricsSpoolDir: join(stateDir, "spool", "metrics"),
},
}
}
func newUnixSocket(baseDir string, unitTag names.UnitTag, worker string, name string, abstract bool) SocketPair {
socket := sockets.Socket{Network: "unix"}
path := filepath.Join(baseDir, name+".socket")
if worker != "" {
path = filepath.Join(baseDir, fmt.Sprintf("%s-%s.socket", worker, name))
}
if abstract {
path = "@" + path
}
socket.Address = path
return SocketPair{socket, socket}
}