forked from elastic/elastic-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_namespace.go
130 lines (106 loc) · 4.35 KB
/
common_namespace.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
// This file encapsulates the common paths that need to account for installation namepsaces.
// Installation namespaces allow multiple agents on the same machine.
package paths
import (
"fmt"
"path/filepath"
"strings"
)
const (
// installDirNamespaceFmt is the format of the directory agent will be installed to within the base path when using an installation namepsace.
// It is $BasePath/Agent-$namespace.
installDir = "Agent"
installDirNamespaceSep = "-"
installDirNamespacePrefix = installDir + installDirNamespaceSep
installDirNamespaceFmt = installDirNamespacePrefix + "%s"
// DevelopmentNamespace defines the "well known" development namespace.
DevelopmentNamespace = "Development"
// Service display names. Must be different from the ServiceName() on Windows.
serviceDisplayName = "Elastic Agent"
serviceDisplayNameNamespaceFmt = "Elastic Agent - %s"
)
// installNamespace is the name of the agent's current installation namepsace.
var installNamespace string
// SetInstallNamespace sets whether the agent is currently in or is being installed in an installation namespace.
// Removes leading and trailing whitespace
func SetInstallNamespace(namespace string) {
installNamespace = strings.TrimSpace(namespace)
}
// InstallNamespace returns the name of the current installation namespace. Returns the empty string
// for the default namespace. For installed agents, the namespace is parsed from the installation
// directory name, since a unique directory name is required to avoid collisions between installed
// agents in the same base path. Before installation, the installation namespace must be configured
// using SetInstallNamespace().
func InstallNamespace() string {
if installNamespace != "" {
return installNamespace
}
if RunningInstalled() {
// Parse the namespace from the directory once to ensure deterministic behavior from startup.
namespace := parseNamespaceFromDir(filepath.Base(Top()))
installNamespace = namespace
}
return ""
}
func parseNamespaceFromDir(dir string) string {
parts := strings.SplitAfterN(dir, "-", 2)
if len(parts) <= 1 {
return ""
} else if parts[0] != installDirNamespacePrefix {
return ""
}
return parts[1]
}
// InInstallNamespace returns true if the agent is being installed in an installation namespace.
func InInstallNamespace() bool {
return InstallNamespace() != ""
}
// InstallDirNameForNamespace returns the installation directory name for a given namespace.
// The installation directory name with a namespace is $BasePath/InstallDirNameForNamespace().
func InstallDirNameForNamespace(namespace string) string {
if namespace == "" {
return installDir
}
return fmt.Sprintf(installDirNamespaceFmt, namespace)
}
// InstallPath returns the top level directory Agent will be installed into, accounting for any namespace.
func InstallPath(basePath string) string {
namespace := InstallNamespace()
return filepath.Join(basePath, "Elastic", InstallDirNameForNamespace(namespace))
}
// ServiceName returns the service name accounting for any namespace.
func ServiceName() string {
namespace := InstallNamespace()
if namespace == "" {
return serviceName
}
return fmt.Sprintf(serviceNameNamespaceFmt, namespace)
}
// ServiceDisplayName returns the service display name accounting for any namespace.
func ServiceDisplayName() string {
namespace := InstallNamespace()
if namespace == "" {
return serviceDisplayName
}
return fmt.Sprintf(serviceDisplayNameNamespaceFmt, namespace)
}
// ShellWrapperPath returns the shell wrapper path accounting for any namespace.
// The provided namespace is always lowercased for consistency.
func ShellWrapperPath() string {
namespace := InstallNamespace()
if namespace == "" {
return shellWrapperPath
}
return ShellWrapperPathForNamespace(namespace)
}
// ControlSocketRunSymlink returns the shell wrapper path accounting for any namespace.
// Does not auto detect the namespace because it is used outside of agent itself in the testing framework.
func ControlSocketRunSymlink(namespace string) string {
if namespace == "" {
return controlSocketRunSymlink
}
return controlSocketRunSymlinkForNamespace(namespace)
}