Skip to content

Commit

Permalink
Run modes in instance state
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed Jan 26, 2023
1 parent f3033df commit fd0cd41
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
14 changes: 14 additions & 0 deletions pkg/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ 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 {
return InstanceState{
ID: i.id,
URL: i.http.BaseURL(),
Attributes: i.Attributes(),
AemVersion: i.AemVersion(),
RunModes: i.RunModes(),
}
}

Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions pkg/local_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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(),
}
}
Expand Down
58 changes: 53 additions & 5 deletions pkg/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
)
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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 {
Expand Down

0 comments on commit fd0cd41

Please sign in to comment.