-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinstance.go
288 lines (246 loc) · 6.17 KB
/
instance.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package pkg
import (
"bytes"
"encoding/json"
"fmt"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
"github.com/wttech/aemc/pkg/common/fmtx"
"github.com/wttech/aemc/pkg/instance"
"golang.org/x/exp/maps"
nurl "net/url"
"strings"
"time"
)
// Instance represents AEM instance
type Instance struct {
manager *InstanceManager
local *LocalInstance
http *HTTP
status *Status
repository *Repo
osgi *OSGi
sling *Sling
packageManager *PackageManager
id string
user string
password string
}
type InstanceState struct {
ID string `yaml:"id" json:"id"`
URL string `json:"url" json:"url"`
AemVersion string `yaml:"aem_version" json:"aemVersion"`
Attributes []string `yaml:"attributes" json:"attributes"`
RunModes []string `yaml:"run_modes" json:"runModes"`
HealthChecks []string `yaml:"health_checks" json:"healthChecks"`
}
func (i Instance) State() InstanceState {
return InstanceState{
ID: i.id,
URL: i.http.BaseURL(),
AemVersion: i.AemVersion(),
Attributes: i.Attributes(),
RunModes: i.RunModes(),
HealthChecks: i.HealthChecks(),
}
}
func (i Instance) ID() string {
return i.id
}
func (i Instance) User() string {
return i.user
}
func (i Instance) Password() string {
return i.password
}
func (i Instance) Manager() *InstanceManager {
return i.manager
}
func (i Instance) Local() *LocalInstance {
return i.local
}
func (i Instance) HTTP() *HTTP {
return i.http
}
func (i Instance) Status() *Status {
return i.status
}
func (i Instance) Repo() *Repo {
return i.repository
}
func (i Instance) OSGI() *OSGi {
return i.osgi
}
func (i Instance) Sling() *Sling {
return i.sling
}
func (i Instance) PackageManager() *PackageManager {
return i.packageManager
}
func (i Instance) IDInfo() IDInfo {
parts := strings.Split(i.id, instance.IDDelimiter)
if len(parts) == 2 {
return IDInfo{
Location: parts[0],
Role: instance.Role(parts[1]),
}
}
return IDInfo{
Location: parts[0],
Role: instance.Role(parts[1]),
Classifier: parts[2],
}
}
type IDInfo struct {
Location string
Role instance.Role
Classifier string
}
func (i Instance) IsLocal() bool {
return i.IDInfo().Location == instance.LocationLocal
}
func (i Instance) IsRemote() bool {
return !i.IsLocal()
}
func (i Instance) IsAuthor() bool {
return i.IDInfo().Role == instance.RoleAuthor
}
func (i Instance) IsPublish() bool {
return i.IDInfo().Role == instance.RolePublish
}
func locationByURL(config *nurl.URL) string {
if lo.Contains(localHosts(), config.Hostname()) {
return instance.LocationLocal
}
return instance.LocationRemote
}
func roleByURL(config *nurl.URL) instance.Role {
if strings.HasSuffix(config.Port(), instance.RoleAuthorPortSuffix) {
return instance.RoleAuthor
}
return instance.RolePublish
}
// TODO local-publish-preview etc
func classifierByURL(_ *nurl.URL) string {
return instance.ClassifierDefault
}
func credentialsByURL(config *nurl.URL) (string, string) {
user := instance.UserDefault
pwd := instance.PasswordDefault
urlUser := config.User.Username()
if urlUser != "" {
user = urlUser
}
urlPwd, hasPwd := config.User.Password()
if hasPwd && urlPwd != "" {
pwd = urlPwd
}
return user, pwd
}
func localHosts() []string {
return []string{"127.0.0.1", "localhost"}
}
func (i Instance) TimeLocation() *time.Location {
loc, err := i.status.TimeLocation()
if err != nil {
log.Debugf("cannot determine time location of instance '%s': %s", i.id, err)
return time.Now().Location()
}
return loc
}
func (i Instance) AemVersion() string {
// TODO try to retrieve version from filename 'aem/home/instance/[author|publish]/crx-quickstart/app/cq-quickstart-6.5.0-standalone-quickstart.jar'
version, err := i.status.AemVersion()
if err != nil {
log.Debugf("cannot determine AEM version of instance '%s': %s", i.id, err)
return instance.AemVersionUnknown
}
return version
}
func (i Instance) RunModes() []string {
runModes, err := i.status.RunModes()
if err != nil {
log.Debugf("cannot determine run modes of instance '%s': %s", i.id, err)
return []string{}
}
return runModes
}
func (i Instance) Now() time.Time {
return time.Now().In(i.TimeLocation())
}
func (i Instance) Time(unixMilli int64) time.Time {
return time.UnixMilli(unixMilli).In(i.TimeLocation())
}
func (i Instance) Attributes() []string {
var result []string
if i.IsLocal() {
result = append(result, "local")
if i.Local().IsCreated() {
result = append(result, "created")
status, err := i.Local().Status()
if err == nil {
result = append(result, status.String())
}
} else {
result = append(result, "uncreated")
}
} else {
result = append(result, "remote")
}
return result
}
func (i Instance) HealthChecks() []string {
messages := []string{}
if !i.IsLocal() || i.Local().IsRunning() {
checks := []Checker{
i.manager.CheckOpts.Reachable,
i.manager.CheckOpts.BundleStable,
i.manager.CheckOpts.EventStable,
i.manager.CheckOpts.Installer,
}
for _, check := range checks {
result := check.Check(i)
if result.message != "" {
messages = append(messages, result.message)
} else if result.err != nil {
messages = append(messages, fmt.Sprintf("%s", result.err))
}
}
}
return messages
}
func (i Instance) String() string {
return fmt.Sprintf("instance '%s' (url: %s, attrs: %s)", i.id, i.HTTP().BaseURL(), strings.Join(i.Attributes(), ", "))
}
func (i Instance) MarshalJSON() ([]byte, error) {
if i.IsLocal() {
return json.Marshal(i.Local().State())
}
return json.Marshal(i.State())
}
func (i Instance) MarshalYAML() (interface{}, error) {
if i.IsLocal() {
return i.Local().State(), nil
}
return i.State(), nil
}
func (i Instance) MarshalText() string {
state := i.State()
sb := bytes.NewBufferString("")
sb.WriteString(fmt.Sprintf("ID '%s'\n", state.ID))
props := map[string]any{
"http url": state.URL,
"attributes": state.Attributes,
"aem version": i.AemVersion(),
"health checks": i.HealthChecks(),
"run modes": i.RunModes(),
}
if i.IsLocal() {
l := i.Local()
maps.Copy(props, map[string]any{
"dir": l.Dir(),
})
}
sb.WriteString(fmtx.TblProps(props))
return sb.String()
}