Skip to content

Commit

Permalink
Sling properties via aem.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed Jan 31, 2023
1 parent 35d1073 commit b94c10b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 34 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ require (
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/klauspost/compress v1.15.5 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/nwaples/rardecode v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mholt/archiver/v3 v3.5.1 h1:rDjOBX9JSF5BvoJGvjqK479aL70qh9DIpZCl+k7Clwo=
Expand Down
1 change: 1 addition & 0 deletions pkg/cfg/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type ConfigValues struct {
RunModes []string `mapstructure:"run_modes" yaml:"run_modes"`
EnvVars []string `mapstructure:"env_vars" yaml:"env_vars"`
SecretVars []string `mapstructure:"secret_vars" yaml:"secret_vars"`
SlingProps []string `mapstructure:"sling_props" yaml:"sling_props"`
Version string `mapstructure:"version" yaml:"version"`
} `mapstructure:"config" yaml:"config"`

Expand Down
1 change: 1 addition & 0 deletions pkg/instance_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func (im *InstanceManager) configureInstances(config *cfg.Config) {
i.local.RunModes = iCfg.RunModes
i.local.EnvVars = iCfg.EnvVars
i.local.SecretVars = iCfg.SecretVars
i.local.SlingProps = iCfg.SlingProps

if len(iCfg.Version) > 0 {
i.local.Version = iCfg.Version
Expand Down
61 changes: 35 additions & 26 deletions pkg/local_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
"fmt"
"github.com/magiconair/properties"
"github.com/wttech/aemc/pkg/common"
"github.com/wttech/aemc/pkg/common/cryptox"
"github.com/wttech/aemc/pkg/common/execx"
Expand Down Expand Up @@ -32,6 +33,7 @@ type LocalInstance struct {
RunModes []string
EnvVars []string
SecretVars []string
SlingProps []string
}

type LocalInstanceState struct {
Expand All @@ -45,16 +47,14 @@ type LocalInstanceState struct {
}

const (
LocalInstanceScriptStart = "start"
LocalInstanceScriptStop = "stop"
LocalInstanceScriptStatus = "status"
LocalInstanceBackupExtension = "aemb.tar.zst"
LocalInstanceUser = "admin"
LocalInstanceWorkDirName = common.AppId
LocalInstanceNameCommon = "common"
LocalInstanceSecretsDir = "conf/secret"
LocalInstanceSecretsSlingProp = "org.apache.felix.configadmin.plugin.interpolation.secretsdir"
LocalInstanceSecretsSlingPropValue = "${sling.home}/" + LocalInstanceSecretsDir
LocalInstanceScriptStart = "start"
LocalInstanceScriptStop = "stop"
LocalInstanceScriptStatus = "status"
LocalInstanceBackupExtension = "aemb.tar.zst"
LocalInstanceUser = "admin"
LocalInstanceWorkDirName = common.AppId
LocalInstanceNameCommon = "common"
LocalInstanceSecretsDir = "conf/secret"
)

func (li LocalInstance) Instance() *Instance {
Expand All @@ -72,6 +72,7 @@ func NewLocal(i *Instance) *LocalInstance {
}
li.EnvVars = []string{}
li.SecretVars = []string{}
li.SlingProps = []string{}
return li
}

Expand Down Expand Up @@ -291,6 +292,9 @@ func (li LocalInstance) update() error {
if err := li.copyOverrideDirs(); err != nil {
return err
}
if err := li.recreateSlingPropsFile(); err != nil {
return err
}
if err := li.recreateSecretsDir(); err != nil {
return err
}
Expand All @@ -309,6 +313,11 @@ func (li LocalInstance) update() error {
return err
}
}
if state.Locked.SlingProps != state.Current.SlingProps {
if err := li.recreateSlingPropsFile(); err != nil {
return err
}
}
if state.Locked.SecretVars != state.Current.SecretVars {
if err := li.recreateSecretsDir(); err != nil {
return err
Expand Down Expand Up @@ -337,8 +346,20 @@ func (li LocalInstance) secretsDir() string {
return fmt.Sprintf("%s/%s", li.QuickstartDir(), LocalInstanceSecretsDir)
}

func (li LocalInstance) slingPropertiesFile() string {
return fmt.Sprintf("%s/conf/sling.properties", li.QuickstartDir())
func (li LocalInstance) recreateSlingPropsFile() error {
propsCombined := append(li.SlingProps, "org.apache.felix.configadmin.plugin.interpolation.secretsdir=${sling.home}/"+LocalInstanceSecretsDir)
propsLoaded, err := properties.LoadString(strings.Join(propsCombined, "\n"))
if err != nil {
return fmt.Errorf("cannot parse Sling properties of instance '%s'", li.instance.ID())
}
filePath := fmt.Sprintf("%s/conf/sling.properties", li.QuickstartDir())
file, err := os.Create(filePath)
defer file.Close()
_, err = propsLoaded.Write(file, properties.ISO_8859_1)
if err != nil {
return fmt.Errorf("cannot save Sling properties file '%s'", filePath)
}
return nil
}

func (li LocalInstance) recreateSecretsDir() error {
Expand All @@ -348,20 +369,6 @@ func (li LocalInstance) recreateSecretsDir() error {
}
if len(li.SecretVars) > 0 {
log.Infof("configuring instance secret vars in dir '%s'", dir)
file := li.slingPropertiesFile()
if pathx.Exists(file) {
contentActual, err := filex.ReadString(file)
if err != nil {
return err
}
if !strings.Contains(contentActual, LocalInstanceSecretsSlingProp+"=") {
return fmt.Errorf("existing Sling properties file '%s' needs to reference dir '%s' by prop '%s' to support secret vars", file, dir, LocalInstanceSecretsSlingProp)
}
} else {
if err := filex.WriteString(li.slingPropertiesFile(), fmt.Sprintf("%s=%s", LocalInstanceSecretsSlingProp, LocalInstanceSecretsSlingPropValue)); err != nil {
return err
}
}
for _, secretVar := range li.SecretVars {
k := stringsx.Before(secretVar, "=")
v := stringsx.After(secretVar, "=")
Expand Down Expand Up @@ -401,6 +408,7 @@ func (li LocalInstance) startLock() osx.Lock[localInstanceStartLock] {
Password: cryptox.HashString(li.instance.password),
EnvVars: strings.Join(li.EnvVars, ","),
SecretVars: cryptox.HashString(strings.Join(li.SecretVars, ",")),
SlingProps: strings.Join(li.SlingProps, ","),
Overrides: overrides,
}, nil
})
Expand All @@ -415,6 +423,7 @@ type localInstanceStartLock struct {
Overrides string `yaml:"overrides"`
EnvVars string `yaml:"env_vars"`
SecretVars string `yaml:"secret_vars"`
SlingProps string `yaml:"sling_props"`
}

func (li LocalInstance) Stop() error {
Expand Down

This file was deleted.

8 changes: 2 additions & 6 deletions project/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ LIB_DIR="${HOME_DIR}/lib"

DEFAULT_DIR="${AEM_DIR}/default"
DEFAULT_CONFIG_DIR="${DEFAULT_DIR}/etc"
DEFAULT_INSTANCE_CONFIG_DIR="${DEFAULT_DIR}/var/instance/common/crx-quickstart/conf"

echo "Downloading AEM Compose Files"
echo ""
Expand All @@ -33,18 +32,15 @@ curl -s "${SOURCE_URL}/${SCRIPT_DIR}/restart.sh" -o "${SCRIPT_DIR}/restart.sh"
curl -s "${SOURCE_URL}/${SCRIPT_DIR}/setup.sh" -o "${SCRIPT_DIR}/setup.sh"
curl -s "${SOURCE_URL}/${SCRIPT_DIR}/up.sh" -o "${SCRIPT_DIR}/up.sh"

mkdir -p "${LIB_DIR}"

mkdir -p "${DEFAULT_CONFIG_DIR}"
curl -s "${SOURCE_URL}/${DEFAULT_CONFIG_DIR}/aem.yml" -o "${DEFAULT_CONFIG_DIR}/aem.yml"

mkdir -p "${DEFAULT_INSTANCE_CONFIG_DIR}"
curl -s "${SOURCE_URL}/${DEFAULT_INSTANCE_CONFIG_DIR}/sling.properties" -o "${DEFAULT_INSTANCE_CONFIG_DIR}/sling.properties"

mkdir -p "${AEM_DIR}"
curl -s "${SOURCE_URL}/${AEM_DIR}/api.sh" -o "${AEM_DIR}/api.sh"
curl -s "${SOURCE_URL}/${AEM_WRAPPER}" -o "${AEM_WRAPPER}"

mkdir -p "${LIB_DIR}"

echo "Downloading & Testing AEM Compose CLI"
echo ""

Expand Down

0 comments on commit b94c10b

Please sign in to comment.