Skip to content

Commit

Permalink
Bin script probably not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed Nov 10, 2023
1 parent e6eac8c commit 0ffc523
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 51 deletions.
8 changes: 8 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ func (c Client) FileMove(oldPath string, newPath string) error {
return nil
}

func (c Client) FileMakeExecutable(path string) error {
_, err := c.RunShellPurely(fmt.Sprintf("chmod +x %s", path))
if err != nil {
return fmt.Errorf("cannot make file executable '%s': %w", path, err)
}
return nil
}

func (c Client) DirExists(path string) (bool, error) {
out, err := c.RunShellPurely(fmt.Sprintf("test -d %s && echo '0' || echo '1'", path))
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/provider/instance/bin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

. /etc/profile && cd [[.DATA_DIR]] && sh aemw "$@"
3 changes: 3 additions & 0 deletions internal/provider/instance/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ var ConfigYML string
//go:embed systemd.conf
var ServiceConf string

//go:embed bin.sh
var BinScript string

//go:embed create.sh
var CreateScript string

Expand Down
3 changes: 1 addition & 2 deletions internal/provider/instance/launch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ 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"
" | sh aemw repl agent setup -A --location "author" --name "publish"
7 changes: 4 additions & 3 deletions internal/provider/instance/systemd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ After=cloud-final.service

[Service]
Type=forking
User=[[.USER]]

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"
ExecStart=sh -c ". /etc/profile && cd [[.DATA_DIR]] && sh aemw instance start"
ExecStop=sh -c ". /etc/profile && cd [[.DATA_DIR]] && sh aemw instance stop"
ExecReload=sh -c ". /etc/profile && cd [[.DATA_DIR]] && sh aemw instance restart"
KillMode=process
RemainAfterExit=yes
TimeoutStartSec=1810
Expand Down
61 changes: 36 additions & 25 deletions internal/provider/instance_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,17 @@ func (ic *InstanceClient) create() error {
if err := ic.configureService(); err != nil {
return err
}
if err := ic.saveEnv(); err != nil {
if err := ic.saveProfileScript(); err != nil {
return err
}
if err := ic.runHook("create", ic.data.Compose.Create.ValueString(), ic.dataDir()); err != nil {
if err := ic.runScript("create", ic.data.Compose.CreateScript.ValueString(), ic.dataDir()); err != nil {
return err
}
tflog.Info(ic.ctx, "Created AEM instance(s)")
return nil
}

func (ic *InstanceClient) saveEnv() error {
func (ic *InstanceClient) saveProfileScript() error {
envFile := fmt.Sprintf("/etc/profile.d/%s.sh", ServiceName)
var envMap map[string]string
ic.data.System.Env.ElementsAs(ic.ctx, &envMap, true)
Expand All @@ -99,21 +99,40 @@ func (ic *InstanceClient) saveEnv() error {
}

func (ic *InstanceClient) configureService() error {
serviceFile := fmt.Sprintf("/etc/systemd/system/%s.service", ServiceName)
serviceTemplated, err := utils.TemplateString(ic.data.System.Service.ValueString(), map[string]string{
"DATA_DIR": ic.dataDir(),
"SERVICE_USER": ic.serviceUser(),
})
if err != nil {
return fmt.Errorf("unable to template AEM system service definition: %w", err)
binFile := fmt.Sprintf("/usr/sbin/%s", ServiceName)
user := ic.data.System.User.ValueString()
if user == "" {
user = ic.cl.Connection().User()
}
vars := map[string]string{
"DATA_DIR": ic.dataDir(),
"USER": user,
"BIN_FILE": binFile,
}

ic.cl.Sudo = true
defer func() { ic.cl.Sudo = false }()

binScriptTemplated, err := utils.TemplateString(ic.data.System.BinScript.ValueString(), vars)
if err != nil {
return fmt.Errorf("unable to template AEM system script: %w", err)
}
if err := ic.cl.FileWrite(binFile, binScriptTemplated); err != nil {
return fmt.Errorf("unable to write AEM system script '%s': %w", binFile, err)
}
if err := ic.cl.FileMakeExecutable(binFile); err != nil {
return fmt.Errorf("unable to make AEM system script '%s' executable: %w", binFile, err)
}

serviceTemplated, err := utils.TemplateString(ic.data.System.ServiceConfig.ValueString(), vars)
if err != nil {
return fmt.Errorf("unable to template AEM system service definition: %w", err)
}
serviceFile := fmt.Sprintf("/etc/systemd/system/%s.service", ServiceName)
if err := ic.cl.FileWrite(serviceFile, serviceTemplated); err != nil {
return fmt.Errorf("unable to write AEM system service definition '%s': %w", serviceFile, err)
}

if err := ic.runServiceAction("enable"); err != nil {
return err
}
Expand All @@ -133,20 +152,12 @@ func (ic *InstanceClient) runServiceAction(action string) error {
return nil
}

func (ic *InstanceClient) serviceUser() string {
user := ic.data.System.User.ValueString()
if user == "" {
user = ic.cl.Connection().User()
}
return user
}

func (ic *InstanceClient) launch() error {
tflog.Info(ic.ctx, "Launching AEM instance(s)")
if err := ic.runServiceAction("start"); err != nil {
return err
}
if err := ic.runHook("launch", ic.data.Compose.Launch.ValueString(), ic.dataDir()); err != nil {
if err := ic.runScript("launch", ic.data.Compose.LaunchScript.ValueString(), ic.dataDir()); err != nil {
return err
}
tflog.Info(ic.ctx, "Launched AEM instance(s)")
Expand All @@ -159,7 +170,7 @@ func (ic *InstanceClient) terminate() error {
if err := ic.runServiceAction("stop"); err != nil {
return err
}
if err := ic.runHook("delete", ic.data.Compose.Delete.ValueString(), ic.dataDir()); err != nil {
if err := ic.runScript("delete", ic.data.Compose.DeleteScript.ValueString(), ic.dataDir()); err != nil {
return err
}
tflog.Info(ic.ctx, "Terminated AEM instance(s)")
Expand Down Expand Up @@ -200,23 +211,23 @@ func (ic *InstanceClient) ReadStatus() (InstanceStatus, error) {
}

func (ic *InstanceClient) bootstrap() error {
return ic.runHook("bootstrap", ic.data.System.Bootstrap.ValueString(), ".")
return ic.runScript("bootstrap", ic.data.System.Bootstrap.ValueString(), ".")
}

func (ic *InstanceClient) runHook(name, cmdScript, dir string) error {
func (ic *InstanceClient) runScript(name, cmdScript, dir string) error {
if cmdScript == "" {
return nil
}

tflog.Info(ic.ctx, fmt.Sprintf("Executing instance hook '%s'", name))
tflog.Info(ic.ctx, fmt.Sprintf("Executing instance script '%s'", name))

textOut, err := ic.cl.RunShellScript(name, cmdScript, dir)
if err != nil {
return fmt.Errorf("unable to execute hook '%s' properly: %w", name, err)
return fmt.Errorf("unable to execute script '%s' properly: %w", name, err)
}
textStr := string(textOut) // TODO how about streaming it line by line to tflog ;)

tflog.Info(ic.ctx, fmt.Sprintf("Executed instance hook '%s'", name))
tflog.Info(ic.ctx, fmt.Sprintf("Executed instance script '%s'", name))
tflog.Info(ic.ctx, textStr)

return nil
Expand Down
49 changes: 28 additions & 21 deletions internal/provider/instance_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,21 @@ type InstanceResourceModel struct {
} `tfsdk:"client"`
Files types.Map `tfsdk:"files"`
System struct {
DataDir types.String `tfsdk:"data_dir"`
WorkDir types.String `tfsdk:"work_dir"`
Env types.Map `tfsdk:"env"`
Service types.String `tfsdk:"service"`
User types.String `tfsdk:"user"`
Bootstrap types.String `tfsdk:"bootstrap"`
DataDir types.String `tfsdk:"data_dir"`
WorkDir types.String `tfsdk:"work_dir"`
Env types.Map `tfsdk:"env"`
BinScript types.String `tfsdk:"bin_script"`
ServiceConfig types.String `tfsdk:"service_config"`
User types.String `tfsdk:"user"`
Bootstrap types.String `tfsdk:"bootstrap"`
} `tfsdk:"system"`
Compose struct {
Download types.Bool `tfsdk:"download"`
Version types.String `tfsdk:"version"`
Config types.String `tfsdk:"config"`
Create types.String `tfsdk:"create"`
Launch types.String `tfsdk:"launch"`
Delete types.String `tfsdk:"delete"`
Download types.Bool `tfsdk:"download"`
Version types.String `tfsdk:"version"`
Config types.String `tfsdk:"config"`
CreateScript types.String `tfsdk:"create_script"`
LaunchScript types.String `tfsdk:"launch_script"`
DeleteScript types.String `tfsdk:"delete_script"`
} `tfsdk:"compose"`
Instances types.List `tfsdk:"instances"`
}
Expand Down Expand Up @@ -116,8 +117,14 @@ func (r *InstanceResource) Schema(ctx context.Context, req resource.SchemaReques
Optional: true,
Default: stringdefault.StaticString("/tmp/aemc"),
},
"service": schema.StringAttribute{
MarkdownDescription: "Contents of the 'systemd' service configuration file",
"bin_script": schema.StringAttribute{
MarkdownDescription: "Contents of the system-wide binary script for controlling AEM instance.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(instance.BinScript),
},
"service_config": schema.StringAttribute{
MarkdownDescription: "Contents of the AEM 'systemd' service definition file",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(instance.ServiceConf),
Expand Down Expand Up @@ -154,26 +161,26 @@ func (r *InstanceResource) Schema(ctx context.Context, req resource.SchemaReques
Default: stringdefault.StaticString("1.5.8"),
},
"config": schema.StringAttribute{
MarkdownDescription: "Contents of the AEM Compose YML configuration file",
MarkdownDescription: "Contents of the AEM Compose YML configuration file.",
Computed: true,
Optional: true,
Default: stringdefault.StaticString(instance.ConfigYML),
},
"create": schema.StringAttribute{
MarkdownDescription: "Script for creating the instance or restoring from backup. Forces instance recreation if changed.",
"create_script": schema.StringAttribute{
MarkdownDescription: "Creates the instance or restores from backup. Forces instance recreation if changed.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(instance.CreateScript),
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
"launch": schema.StringAttribute{
MarkdownDescription: "Script for configuring launched instance. Must be idempotent as it is executed always when changed. Typically used for setting up replication agents, installing service packs, etc.",
"launch_script": schema.StringAttribute{
MarkdownDescription: "Configures launched instance. Must be idempotent as it is executed always when changed. Typically used for setting up replication agents, installing service packs, etc.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(instance.LaunchScript),
},
"delete": schema.StringAttribute{
MarkdownDescription: "Script for deleting the instance.",
"delete_script": schema.StringAttribute{
MarkdownDescription: "Deletes the instance.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(instance.DeleteScript),
Expand Down

0 comments on commit 0ffc523

Please sign in to comment.