From fd0cd417eb33719b1ae980b92d60fa1e11e840d0 Mon Sep 17 00:00:00 2001 From: Krystian Panek Date: Thu, 26 Jan 2023 13:45:56 +0100 Subject: [PATCH] Run modes in instance state --- pkg/instance.go | 14 +++++++++++ pkg/local_instance.go | 4 +++ pkg/status.go | 58 +++++++++++++++++++++++++++++++++++++++---- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/pkg/instance.go b/pkg/instance.go index 75ff9586..b8a58257 100644 --- a/pkg/instance.go +++ b/pkg/instance.go @@ -35,6 +35,8 @@ type InstanceState struct { ID string `yaml:"id" json:"id"` URL string `json:"url" json:"url"` Attributes []string `yaml:"attributes" json:"attributes"` + RunModes []string `yaml:"run_modes" json:"runModes"` + AemVersion string `yaml:"aem_version" json:"aemVersion"` } func (i Instance) State() InstanceState { @@ -42,6 +44,8 @@ func (i Instance) State() InstanceState { ID: i.id, URL: i.http.BaseURL(), Attributes: i.Attributes(), + AemVersion: i.AemVersion(), + RunModes: i.RunModes(), } } @@ -185,6 +189,15 @@ func (i Instance) AemVersion() string { 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()) } @@ -238,6 +251,7 @@ func (i Instance) MarshalText() string { "http url": state.URL, "attributes": state.Attributes, "aem version": i.AemVersion(), + "run modes": i.RunModes(), } if i.IsLocal() { l := i.Local() diff --git a/pkg/local_instance.go b/pkg/local_instance.go index eecde372..67894174 100644 --- a/pkg/local_instance.go +++ b/pkg/local_instance.go @@ -35,6 +35,8 @@ type LocalInstanceState struct { ID string `yaml:"id" json:"id"` URL string `json:"url" json:"url"` Attributes []string `yaml:"attributes" json:"attributes"` + RunModes []string `yaml:"run_modes" json:"runModes"` + AemVersion string `yaml:"aem_version" json:"aemVersion"` Dir string `yaml:"dir" json:"dir"` } @@ -68,6 +70,8 @@ func (li LocalInstance) State() LocalInstanceState { ID: li.instance.id, URL: li.instance.http.BaseURL(), Attributes: li.instance.Attributes(), + AemVersion: li.instance.AemVersion(), + RunModes: li.instance.RunModes(), Dir: li.Dir(), } } diff --git a/pkg/status.go b/pkg/status.go index 0dedf2bc..8e39bb86 100644 --- a/pkg/status.go +++ b/pkg/status.go @@ -5,6 +5,7 @@ import ( "github.com/samber/lo" log "github.com/sirupsen/logrus" "github.com/wttech/aemc/pkg/common/fmtx" + "github.com/wttech/aemc/pkg/common/stringsx" "github.com/wttech/aemc/pkg/instance" "io" "regexp" @@ -15,6 +16,9 @@ import ( const ( SystemPropPath = "/system/console/status-System%20Properties.json" SystemPropTimezone = "user.timezone" + SlingPropPath = "/system/console/status-slingprops.json" + SlingSettingsPath = "/system/console/status-slingsettings.json" + SlingSettingRunModes = "Run Modes" SystemProductInfoPath = "/system/console/status-productinfo.txt" SystemProductInfoRegex ) @@ -31,7 +35,7 @@ func NewStatus(res *Instance) *Status { return &Status{instance: res} } -func (sm Status) SystemProperties() (map[string]string, error) { +func (sm Status) SystemProps() (map[string]string, error) { response, err := sm.instance.http.Request().Get(SystemPropPath) if err != nil { return nil, fmt.Errorf("cannot read system properties on instance '%s'", sm.instance.id) @@ -40,21 +44,53 @@ func (sm Status) SystemProperties() (map[string]string, error) { if err = fmtx.UnmarshalJSON(response.RawBody(), &results); err != nil { return nil, fmt.Errorf("cannot parse system properties response from instance '%s': %w", sm.instance.id, err) } + props := parseProperties(results) + return props, nil +} + +func (sm Status) SlingProps() (map[string]string, error) { + response, err := sm.instance.http.Request().Get(SlingPropPath) + if err != nil { + return nil, fmt.Errorf("cannot read Sling properties on instance '%s'", sm.instance.id) + } + var results []string + if err = fmtx.UnmarshalJSON(response.RawBody(), &results); err != nil { + return nil, fmt.Errorf("cannot parse Sling properties response from instance '%s': %w", sm.instance.id, err) + } + props := parseProperties(results) + return props, nil +} + +func (sm Status) SlingSettings() (map[string]string, error) { + response, err := sm.instance.http.Request().Get(SlingSettingsPath) + if err != nil { + return nil, fmt.Errorf("cannot read Sling settings on instance '%s'", sm.instance.id) + } + var results []string + if err = fmtx.UnmarshalJSON(response.RawBody(), &results); err != nil { + return nil, fmt.Errorf("cannot parse Sling settings response from instance '%s': %w", sm.instance.id, err) + } + props := parseProperties(results) + return props, nil +} + +func parseProperties(results []string) map[string]string { results = lo.Filter(results, func(r string, _ int) bool { return strings.Count(strings.TrimSpace(r), " = ") == 1 }) - return lo.Associate(results, func(r string) (string, string) { + resultMap := lo.Associate(results, func(r string) (string, string) { parts := strings.Split(strings.TrimSpace(r), " = ") return parts[0], parts[1] - }), nil + }) + return resultMap } func (sm Status) TimeLocation() (*time.Location, error) { - systemProperties, err := sm.SystemProperties() + systemProps, err := sm.SystemProps() if err != nil { return nil, err } - locName, ok := systemProperties[SystemPropTimezone] + locName, ok := systemProps[SystemPropTimezone] if !ok { return nil, fmt.Errorf("system property '%s' does not exist on instance ''%s", SystemPropTimezone, sm.instance.id) } @@ -65,6 +101,18 @@ func (sm Status) TimeLocation() (*time.Location, error) { return timeLocation, nil } +func (sm Status) RunModes() ([]string, error) { + slingSettings, err := sm.SlingSettings() + if err != nil { + return nil, err + } + values, ok := slingSettings[SlingSettingRunModes] + if !ok { + return []string{}, fmt.Errorf(" Sling setting '%s' does not exist on instance ''%s", SlingSettingRunModes, sm.instance.id) + } + return lo.Map(strings.Split(stringsx.Between(values, "[", "]"), ","), func(rm string, _ int) string { return strings.TrimSpace(rm) }), nil +} + func (sm Status) AemVersion() (string, error) { response, err := sm.instance.http.Request().Get(SystemProductInfoPath) if err != nil {