Skip to content

Commit

Permalink
System service PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed Nov 7, 2023
1 parent 917dc09 commit a707d09
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 98 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Built on top of [AEM Compose](https://github.com/wttech/aemc).

```hcl
resource "aem_instance" "single" {
depends_on = [aws_instance.aem_single, aws_volume_attachment.aem_single_data]
depends_on = [aws_instance.aem_single, aws_volume_attachment.aem_single_data]§
client {
type = "ssh"
Expand Down
21 changes: 11 additions & 10 deletions examples/ssh/aem.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ resource "aem_instance" "single" {
private_key_file = local.ssh_private_key # cannot be put into state as this is OS-dependent
}
}
compose {
version = "1.5.8"
data_dir = local.aem_single_compose_dir
/* TODO support this as well
files = {
"lib" = "${local.aem_single_compose_dir}/aem/home/lib"
}
hook {
bootstrap = <<EOF
*/
system {
data_dir = local.aem_single_compose_dir
bootstrap = <<EOF
#!/bin/sh
(
echo "Mounting EBS volume into data directory"
Expand All @@ -34,11 +36,10 @@ resource "aem_instance" "single" {
aws s3 cp --recursive --no-progress "s3://aemc/instance/classic/" "${local.aem_single_compose_dir}/aem/home/lib"
)
EOF
initialize = <<EOF
#!/bin/sh
# sh aemw instance backup restore
EOF
provision = <<EOF
}
compose {
version = "1.5.8"
launch = <<EOF
#!/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" && \
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,5 @@ require (
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.56.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
File renamed without changes.
9 changes: 9 additions & 0 deletions internal/provider/instance/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package instance

import _ "embed"

//go:embed config.yml
var ConfigYML string

//go:embed systemd.conf.go.tpl
var ServiceTemplate string
42 changes: 0 additions & 42 deletions internal/provider/instance/plan_modifiers.go

This file was deleted.

19 changes: 19 additions & 0 deletions internal/provider/instance/systemd.conf.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[Unit]
Description=AEM Instances
Requires=network.target
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"
KillMode=process
RemainAfterExit=yes
TimeoutStartSec=1810
TimeoutStopSec=190
LimitNOFILE=20000

[Install]
WantedBy=cloud-init.target
77 changes: 33 additions & 44 deletions internal/provider/instance_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,18 @@ type InstanceResourceModel struct {
Type types.String `tfsdk:"type"`
Settings types.Map `tfsdk:"settings"`
} `tfsdk:"client"`
Files types.Map `tfsdk:"files"`
System struct {
DataDir types.String `tfsdk:"data_dir"`
Service types.String `tfsdk:"service"`
Bootstrap types.String `tfsdk:"bootstrap"`
} `tfsdk:"system"`
Compose struct {
DataDir types.String `tfsdk:"data_dir"`
Version types.String `tfsdk:"version"`
ConfigFile types.String `tfsdk:"config_file"`
ConfigFileChecksum types.String `tfsdk:"config_file_checksum"`
LibDir types.String `tfsdk:"lib_dir"`
InstanceId types.String `tfsdk:"instance_id"`
Version types.String `tfsdk:"version"`
Config types.String `tfsdk:"config"`
Init types.String `tfsdk:"initialize"`
Launch types.String `tfsdk:"provision"`
} `tfsdk:"compose"`
Hook struct {
Bootstrap types.String `tfsdk:"bootstrap"`
Initialize types.String `tfsdk:"initialize"`
Provision types.String `tfsdk:"provision"`
} `tfsdk:"hook"`
Instances types.List `tfsdk:"instances"`
}

Expand Down Expand Up @@ -90,58 +89,48 @@ func (r *InstanceResource) Schema(ctx context.Context, req resource.SchemaReques
},
},
},
"compose": schema.SingleNestedBlock{
"system": schema.SingleNestedBlock{
Attributes: map[string]schema.Attribute{
"bootstrap": schema.StringAttribute{
MarkdownDescription: "Script executed once after connecting to the instance. Typically used for: providing AEM library files (quickstart.jar, license.properties, service packs), mounting data volume, etc. Forces instance recreation if changed.",
Optional: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
"data_dir": schema.StringAttribute{
MarkdownDescription: "Remote path in which AEM Compose data will be stored",
MarkdownDescription: "Remote root path in which AEM Compose files and unpacked instances will be stored",
Computed: true,
Optional: true,
Default: stringdefault.StaticString("/mnt/aemc"),
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
"service": schema.StringAttribute{
MarkdownDescription: "Contents of the 'systemd' service configuration file",
Optional: true,
Default: stringdefault.StaticString(instance.ServiceTemplate),
},
},
},
"compose": schema.SingleNestedBlock{
Attributes: map[string]schema.Attribute{
"version": schema.StringAttribute{
MarkdownDescription: "Version of AEM Compose tool to use on remote AEM machine",
Computed: true,
Optional: true,
Default: stringdefault.StaticString("1.4.1"),
},
"config_file": schema.StringAttribute{
MarkdownDescription: "Local path to the AEM configuration file",
Computed: true,
Optional: true,
Default: stringdefault.StaticString("aem/default/etc/aem.yml"),
},
"config_file_checksum": schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{instance.ConfigFileChecksumPlanModifier()},
},
"lib_dir": schema.StringAttribute{
MarkdownDescription: "Local path to directory from which AEM library files will be copied to the remote AEM machine",
"config": schema.StringAttribute{
MarkdownDescription: "Contents of the AEM Compose YML configuration file",
Computed: true,
Optional: true,
Default: stringdefault.StaticString("aem/home/lib"),
},
"instance_id": schema.StringAttribute{
MarkdownDescription: "ID of the AEM instance to use (one of the instances defined in the configuration file)",
Optional: true,
},
},
},
"hook": schema.SingleNestedBlock{
MarkdownDescription: "Scripts executed on the remote AEM machine at key stages of the AEM instance lifecycle",
Attributes: map[string]schema.Attribute{
"bootstrap": schema.StringAttribute{
MarkdownDescription: "Executed once after connecting to the instance. Forces instance recreation if changed. Typically used for: providing AEM library files (quickstart.jar, license.properties, service packs), mounting data volume, etc.",
Optional: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
Default: stringdefault.StaticString(instance.ConfigYML),
},
"initialize": schema.StringAttribute{
MarkdownDescription: "Executed once after initializing AEM Compose but before launching the instance. Forces instance recreation if changed. Can be used for restoring instances from backup.",
"init": schema.StringAttribute{
MarkdownDescription: "Script executed once after initializing AEM Compose but before launching the instance. Forces instance recreation if changed. Can be used for restoring instances from backup.",
Optional: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
"provision": schema.StringAttribute{
MarkdownDescription: "Executed when the instance is launched. Must be idempotent as it is executed always when changed. Typically used for setting up replication agents, installing service packs, etc.",
"launch": schema.StringAttribute{
MarkdownDescription: "Script executed when the instance is launched. Must be idempotent as it is executed always when changed. Typically used for setting up replication agents, installing service packs, etc.",
Optional: true,
},
},
Expand Down
18 changes: 18 additions & 0 deletions internal/utils/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package utils

import (
"bytes"
"text/template"
)

func TemplateString(tplContent string, data any) (string, error) {
tplParsed, err := template.New("string-template").Delims("[[", "]]").Parse(tplContent)
if err != nil {
return "", err
}
var tplOutput bytes.Buffer
if err := tplParsed.Execute(&tplOutput, data); err != nil {
return "", err
}
return tplOutput.String(), nil
}

0 comments on commit a707d09

Please sign in to comment.