Skip to content

Commit

Permalink
System service API PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed Nov 9, 2023
1 parent 73bee1b commit f0f9f3d
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 336 deletions.
244 changes: 0 additions & 244 deletions examples/ssh/aem.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,250 +33,6 @@ resource "aem_instance" "single" {
)
SHELL
}

compose {
version = "1.5.8"
launch = <<SHELL
#!/bin/sh
sh aemw osgi bundle install --url "https://github.com/neva-dev/felix-search-webconsole-plugin/releases/download/2.0.0/search-webconsole-plugin-2.0.0.jar" && \
sh aemw osgi config save --pid "org.apache.sling.jcr.davex.impl.servlets.SlingDavExServlet" --input-string "alias: /crx/server" && \
echo "
enabled: true
transportUri: http://localhost:4503/bin/receive?sling:authRequestLogin=1
transportUser: admin
transportPassword: admin
userId: admin
" | sh aemw repl agent setup -A --location "author" --name "publish" && \
sh aemw package deploy --file "aem/home/lib/aem-service-pkg-6.5.*.0.zip"
SHELL

config = <<YAML
# AEM instances to work with
instance:
# Full details of local or remote instances
config:
local_author:
active: true
http_url: http://127.0.0.1:4502
user: admin
password: admin
run_modes: [local]
jvm_opts:
- -server
- -Djava.awt.headless=true
- -Djava.io.tmpdir=[[canonicalPath .Path "aem/home/tmp"]]
- -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:14502
- -Duser.language=en
- -Duser.country=US
- -Duser.timezone=UTC
start_opts: []
secret_vars:
- ACME_SECRET=value
env_vars:
- ACME_VAR=value
sling_props: []
local_publish:
active: true
http_url: http://127.0.0.1:4503
user: admin
password: admin
run_modes: [local]
jvm_opts:
- -server
- -Djava.awt.headless=true
- -Djava.io.tmpdir=[[canonicalPath .Path "aem/home/tmp"]]
- -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:14503
- -Duser.language=en
- -Duser.country=US
- -Duser.timezone=UTC
start_opts: []
secret_vars:
- ACME_SECRET=value
env_vars:
- ACME_VAR=value
sling_props: []
# Tuning performance & reliability
# 'auto' - for more than 1 local instances - 'serial', otherwise 'parallel'
# 'parallel' - for working with remote instances
# 'serial' - for working with local instances
processing_mode: auto
# HTTP client settings
http:
timeout: 10m
debug: false
disable_warn: true
# State checking
check:
# Time to wait before first state checking (to avoid false-positives)
warmup: 1s
# Time to wait for next state checking
interval: 6s
# Number of successful check attempts that indicates end of checking
done_threshold: 3
# Wait only for those instances whose state has been changed internally (unaware of external changes)
await_strict: true
# Max time to wait for the instance to be healthy after executing the start script or e.g deploying a package
await_started:
timeout: 30m
# Max time to wait for the instance to be stopped after executing the stop script
await_stopped:
timeout: 10m
# Max time in which socket connection to instance should be established
reachable:
timeout: 3s
# Bundle state tracking
bundle_stable:
symbolic_names_ignored: []
# OSGi events tracking
event_stable:
# Topics indicating that instance is not stable
topics_unstable:
- "org/osgi/framework/ServiceEvent/*"
- "org/osgi/framework/FrameworkEvent/*"
- "org/osgi/framework/BundleEvent/*"
# Ignored service names to handle known issues
details_ignored:
- "*.*MBean"
- "org.osgi.service.component.runtime.ServiceComponentRuntime"
- "java.util.ResourceBundle"
received_max_age: 5s
# Sling Installer tracking
installer:
# JMX state checking
state: true
# Pause Installation nodes checking
pause: true
# Specific endpoints / paths (like login page)
path_ready:
timeout: 10s
# Managed locally (set up automatically)
local:
# Current runtime dir (Sling launchpad, JCR repository)
unpack_dir: "aem/home/var/instance"
# Archived runtime dir (AEM backup files '*.aemb.zst')
backup_dir: "aem/home/var/backup"
# Oak Run tool options (offline instance management)
oak_run:
download_url: "https://repo1.maven.org/maven2/org/apache/jackrabbit/oak-run/1.44.0/oak-run-1.44.0.jar"
store_path: "crx-quickstart/repository/segmentstore"
# Source files
quickstart:
# AEM SDK ZIP or JAR
dist_file: 'aem/home/lib/{aem-sdk,cq-quickstart}-*.{zip,jar}'
# AEM License properties file
license_file: "aem/home/lib/license.properties"
# Status discovery (timezone, AEM version, etc)
status:
timeout: 500ms
# JCR Repository
repo:
property_change_ignored:
# AEM assigns them automatically
- "jcr:created"
- "cq:lastModified"
# AEM encrypts it right after changing by replication agent setup command
- "transportPassword"
# CRX Package Manager
package:
# Force re-uploading/installing of snapshot AEM packages (just built / unreleased)
snapshot_patterns: [ "**/*-SNAPSHOT.zip" ]
# Use checksums to avoid re-deployments when snapshot AEM packages are unchanged
snapshot_deploy_skipping: true
# Disable following workflow launchers for a package deployment time only
toggled_workflows: [/libs/settings/workflow/launcher/config/update_asset_*,/libs/settings/workflow/launcher/config/dam_*]
# Also sub-packages
install_recursive: true
# Use slower HTML endpoint for deployments but with better troubleshooting
install_html:
enabled: false
# Print HTML directly to console instead of writing to file
console: false
# Fail on case 'installed with errors'
strict: true
# OSGi Framework
osgi:
shutdown_delay: 3s
bundle:
install:
start: true
start_level: 20
refresh_packages: true
# Crypto Support
crypto:
key_bundle_symbolic_name: com.adobe.granite.crypto.file
# Workflow Manager
workflow:
launcher:
lib_root: /libs/settings/workflow/launcher
config_root: /conf/global/settings/workflow/launcher
toggle_retry:
timeout: 10m
delay: 10s
java:
# Require following versions before e.g running AEM instances
version_constraints: ">= 11, < 12"
# Pre-installed local JDK dir
# a) keep it empty to download open source Java automatically for current OS and architecture
# b) set it to absolute path or to env var '[[.Env.JAVA_HOME]]' to indicate where closed source Java like Oracle is installed
home_dir: ""
# Auto-installed JDK options
download:
# Source URL with template vars support
url: "https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.18%2B10/OpenJDK11U-jdk_[[.Arch]]_[[.Os]]_hotspot_11.0.18_10.[[.ArchiveExt]]"
# Map source URL template vars to be compatible with Adoptium Java
replacements:
# Var 'Os' (GOOS)
"darwin": "mac"
# Var 'Arch' (GOARCH)
"x86_64": "x64"
"amd64": "x64"
"386": "x86-32"
# enforce non-ARM Java as some AEM features are not working on ARM (e.g Scene7)
"arm64": "x64"
"aarch64": "x64"
base:
# Location of temporary files (downloaded AEM packages, etc)
tmp_dir: aem/home/tmp
# Location of supportive tools (downloaded Java, OakRun, unpacked AEM SDK)
tool_dir: aem/home/opt
log:
level: info
timestamp_format: "2006-01-02 15:04:05"
full_timestamp: true
input:
format: yml
file: STDIN
output:
format: text
log:
# File path of logs written especially when output format is different than 'text'
file: aem/home/var/log/aem.log
# Controls where outputs and logs should be written to when format is 'text' (console|file|both)
mode: console
YAML
}
}

locals {
Expand Down
18 changes: 6 additions & 12 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/melbahja/goph"
"github.com/wttech/terraform-provider-aem/internal/utils"
"os"
"path/filepath"
"strings"
Expand All @@ -15,8 +16,8 @@ type Client struct {
settings map[string]string
connection Connection

Env map[string]string
EnvDir string // TODO this is more like tmp script dir
Env map[string]string
WorkDir string
}

func (c Client) TypeName() string {
Expand Down Expand Up @@ -78,26 +79,19 @@ func (c Client) SetupEnv() error {
}

func (c Client) envScriptPath() string {
return fmt.Sprintf("%s/env.sh", c.EnvDir)
return fmt.Sprintf("%s/env.sh", c.WorkDir)
}

func (c Client) envScriptString() string {
var sb strings.Builder
sb.WriteString("#!/bin/sh\n")
for name, value := range c.Env {
escapedValue := strings.ReplaceAll(value, "\"", "\\\"")
escapedValue = strings.ReplaceAll(escapedValue, "$", "\\$")
sb.WriteString(fmt.Sprintf("export %s=\"%s\"\n", name, escapedValue))
}
return sb.String()
return utils.EnvToScript(c.Env)
}

func (c Client) RunShellCommand(cmd string) ([]byte, error) {
return c.RunShellPurely(fmt.Sprintf("source %s && %s", c.envScriptPath(), cmd))
}

func (c Client) RunShellScript(cmdName string, cmdScript string, dir string) ([]byte, error) {
remotePath := fmt.Sprintf("%s/%s.sh", c.EnvDir, cmdName)
remotePath := fmt.Sprintf("%s/%s.sh", c.WorkDir, cmdName)
if err := c.FileWrite(remotePath, cmdScript); err != nil {
return nil, fmt.Errorf("cannot write temporary script at remote path '%s': %w", remotePath, err)
}
Expand Down
1 change: 1 addition & 0 deletions internal/client/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

type Connection interface {
Info() string
User() string
Connect() error
Disconnect() error
Command(cmdLine []string) (*goph.Cmd, error)
Expand Down
5 changes: 5 additions & 0 deletions internal/client/connection_aws_ssm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ func (A AWSSSMConnection) Info() string {
panic("implement me")
}

func (A AWSSSMConnection) User() string {
//TODO implement me
panic("implement me")
}

func (A AWSSSMConnection) Connect() error {
//TODO implement me
panic("implement me")
Expand Down
4 changes: 4 additions & 0 deletions internal/client/connection_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func (s *SSHConnection) Info() string {
return fmt.Sprintf("ssh: host='%s', user='%s', port='%d'", s.host, s.user, s.port)
}

func (s *SSHConnection) User() string {
return s.user
}

func (s *SSHConnection) Disconnect() error {
if s.client == nil {
return nil
Expand Down
3 changes: 3 additions & 0 deletions internal/provider/instance/create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

sh aemw instance create
3 changes: 3 additions & 0 deletions internal/provider/instance/delete.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

sh aemw instance delete
11 changes: 10 additions & 1 deletion internal/provider/instance/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@ import _ "embed"
var ConfigYML string

//go:embed systemd.conf.go.tpl
var ServiceTemplate string
var ServiceConf string

//go:embed create.sh
var CreateScript string

//go:embed launch.sh
var LaunchScript string

//go:embed delete.sh
var DeleteScript string
11 changes: 11 additions & 0 deletions internal/provider/instance/launch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

sh aemw osgi config save --pid "org.apache.sling.jcr.davex.impl.servlets.SlingDavExServlet" --input-string "alias: /crx/server" && \
echo "
enabled: true
transportUri: http://localhost:4503/bin/receive?sling:authRequestLogin=1
transportUser: admin
transportPassword: admin
userId: admin
" | sh aemw repl agent setup -A --location "author" --name "publish" && \
sh aemw package deploy --file "aem/home/lib/aem-service-pkg-6.5.*.0.zip"
6 changes: 3 additions & 3 deletions internal/provider/instance/systemd.conf.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ After=cloud-final.service
[Service]
Type=forking

ExecStart=su - "{{.USER}}" -c ". /etc/profile && cd {{.DATA_DIR}} && sh aemw instance start"
ExecStop=su - "{{.USER}}" -c ". /etc/profile && cd {{.DATA_DIR}} && sh aemw instance stop"
ExecReload=su - "{{.USER}}" -c ". /etc/profile && cd {{.DATA_DIR}} && sh aemw instance restart"
ExecStart=su - "{{.SERVICE_USER}}" -c ". /etc/profile && cd {{.DATA_DIR}} && sh aemw instance start"
ExecStop=su - "{{.SERVICE_USER}}" -c ". /etc/profile && cd {{.DATA_DIR}} && sh aemw instance stop"
ExecReload=su - "{{.SERVICE_USER}}" -c ". /etc/profile && cd {{.DATA_DIR}} && sh aemw instance restart"
KillMode=process
RemainAfterExit=yes
TimeoutStartSec=1810
Expand Down
Loading

0 comments on commit f0f9f3d

Please sign in to comment.