@@ -21,6 +21,7 @@ import (
21
21
"io/fs"
22
22
"log/slog"
23
23
"os"
24
+ "os/exec"
24
25
"path/filepath"
25
26
"regexp"
26
27
"strings"
@@ -30,16 +31,20 @@ import (
30
31
)
31
32
32
33
type Package struct {
33
- Name string `yaml:"name,omitempty"`
34
- Version string `yaml:"version,omitempty"`
35
- Description string `yaml:"description,omitempty"`
36
- InstallSteps []PackageInstallStep `yaml:"installSteps,omitempty"`
37
- Dependencies []string `yaml:"dependencies,omitempty"`
38
- Tags []string `yaml:"tags,omitempty"`
39
- PostInstallNotes string `yaml:"postInstallNotes,omitempty"`
40
- Options []PackageOption `yaml:"options,omitempty"`
41
- Outputs []PackageOutput `yaml:"outputs,omitempty"`
42
- filePath string
34
+ Name string `yaml:"name,omitempty"`
35
+ Version string `yaml:"version,omitempty"`
36
+ Description string `yaml:"description,omitempty"`
37
+ InstallSteps []PackageInstallStep `yaml:"installSteps,omitempty"`
38
+ Dependencies []string `yaml:"dependencies,omitempty"`
39
+ Tags []string `yaml:"tags,omitempty"`
40
+ PreInstallScript string `yaml:"preInstallScript,omitempty"`
41
+ PostInstallScript string `yaml:"postInstallScript,omitempty"`
42
+ PreUninstallScript string `yaml:"preUninstallScript,omitempty"`
43
+ PostUninstallScript string `yaml:"postUninstallScript,omitempty"`
44
+ PostInstallNotes string `yaml:"postInstallNotes,omitempty"`
45
+ Options []PackageOption `yaml:"options,omitempty"`
46
+ Outputs []PackageOutput `yaml:"outputs,omitempty"`
47
+ filePath string
43
48
}
44
49
45
50
type PackageOption struct {
@@ -101,7 +106,7 @@ func (p Package) hasTags(tags []string) bool {
101
106
return true
102
107
}
103
108
104
- func (p Package ) install (cfg Config , context string , opts map [string ]bool ) (string , map [string ]string , error ) {
109
+ func (p Package ) install (cfg Config , context string , opts map [string ]bool , runHooks bool ) (string , map [string ]string , error ) {
105
110
// Update template vars
106
111
pkgName := fmt .Sprintf ("%s-%s-%s" , p .Name , p .Version , context )
107
112
pkgCacheDir := filepath .Join (
@@ -154,6 +159,12 @@ func (p Package) install(cfg Config, context string, opts map[string]bool) (stri
154
159
if err := os .MkdirAll (pkgDataDir , fs .ModePerm ); err != nil {
155
160
return "" , nil , err
156
161
}
162
+ // Run pre-install script
163
+ if runHooks && p .PreInstallScript != "" {
164
+ if err := p .runHookScript (cfg , p .PreInstallScript ); err != nil {
165
+ return "" , nil , err
166
+ }
167
+ }
157
168
// Perform install
158
169
for _ , installStep := range p .InstallSteps {
159
170
// Evaluate condition if defined
@@ -235,6 +246,12 @@ func (p Package) install(cfg Config, context string, opts map[string]bool) (stri
235
246
}
236
247
retOutputs [key ] = val
237
248
}
249
+ // Run post-install script
250
+ if runHooks && p .PostInstallScript != "" {
251
+ if err := p .runHookScript (cfg , p .PostInstallScript ); err != nil {
252
+ return "" , nil , err
253
+ }
254
+ }
238
255
// Render notes and return
239
256
var retNotes string
240
257
if p .PostInstallNotes != "" {
@@ -247,8 +264,14 @@ func (p Package) install(cfg Config, context string, opts map[string]bool) (stri
247
264
return retNotes , retOutputs , nil
248
265
}
249
266
250
- func (p Package ) uninstall (cfg Config , context string , keepData bool ) error {
267
+ func (p Package ) uninstall (cfg Config , context string , keepData bool , runHooks bool ) error {
251
268
pkgName := fmt .Sprintf ("%s-%s-%s" , p .Name , p .Version , context )
269
+ // Run pre-uninstall script
270
+ if runHooks && p .PreUninstallScript != "" {
271
+ if err := p .runHookScript (cfg , p .PreUninstallScript ); err != nil {
272
+ return err
273
+ }
274
+ }
252
275
// Iterate over install steps in reverse
253
276
for idx := len (p .InstallSteps ) - 1 ; idx >= 0 ; idx -- {
254
277
installStep := p .InstallSteps [idx ]
@@ -331,6 +354,12 @@ func (p Package) uninstall(cfg Config, context string, keepData bool) error {
331
354
)
332
355
}
333
356
}
357
+ // Run post-uninstall script
358
+ if runHooks && p .PostUninstallScript != "" {
359
+ if err := p .runHookScript (cfg , p .PostUninstallScript ); err != nil {
360
+ return err
361
+ }
362
+ }
334
363
return nil
335
364
}
336
365
@@ -535,6 +564,20 @@ func (p Package) services(cfg Config, context string) ([]*DockerService, error)
535
564
return ret , nil
536
565
}
537
566
567
+ func (p Package ) runHookScript (cfg Config , hookScript string ) error {
568
+ renderedScript , err := cfg .Template .Render (hookScript , nil )
569
+ if err != nil {
570
+ return fmt .Errorf ("failed to render hook script template: %s" , err )
571
+ }
572
+ cmd := exec .Command ("/bin/sh" , "-c" , renderedScript )
573
+ cmd .Stdout = os .Stdout
574
+ cmd .Stderr = os .Stderr
575
+ if err := cmd .Run (); err != nil {
576
+ return fmt .Errorf ("failed to run hook script: %s" , err )
577
+ }
578
+ return nil
579
+ }
580
+
538
581
type PackageInstallStep struct {
539
582
Condition string `yaml:"condition,omitempty"`
540
583
Docker * PackageInstallStepDocker `yaml:"docker,omitempty"`
0 commit comments