Skip to content

Commit e4bd715

Browse files
committed
[minor] define jobs using sitectls' new jobs SDK
1 parent 97637ce commit e4bd715

6 files changed

Lines changed: 675 additions & 75 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ INSTALL_DIR ?= $(or $(dir $(shell which $(BINARY_NAME) 2>/dev/null)),/usr/local/
66
deps: work
77
go mod tidy
88

9-
build: deps
9+
build:
1010
go build -o $(BINARY_NAME) .
1111

1212
install: work build

cmd/backup.go

Lines changed: 0 additions & 71 deletions
This file was deleted.

cmd/backup_extension.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"path/filepath"
7+
"strings"
8+
9+
corecron "github.com/libops/sitectl/pkg/cron"
10+
"github.com/spf13/cobra"
11+
yaml "gopkg.in/yaml.v3"
12+
)
13+
14+
var backupRunComponent string
15+
var backupRunOutput string
16+
17+
var cronExtensionCmd = &cobra.Command{
18+
Use: "__cron",
19+
Hidden: true,
20+
}
21+
22+
var cronExtensionComponentsCmd = &cobra.Command{
23+
Use: "components",
24+
Hidden: true,
25+
RunE: func(cmd *cobra.Command, args []string) error {
26+
specs := []corecron.ComponentSpec{
27+
{
28+
Name: "drupal-db-backup",
29+
Plugin: "drupal",
30+
Description: "Drupal SQL dump compressed as gzip",
31+
Filename: "drupal.sql.gz",
32+
},
33+
}
34+
data, err := yaml.Marshal(specs)
35+
if err != nil {
36+
return err
37+
}
38+
_, err = cmd.OutOrStdout().Write(data)
39+
return err
40+
},
41+
}
42+
43+
var cronExtensionRunCmd = &cobra.Command{
44+
Use: "run",
45+
Hidden: true,
46+
RunE: func(cmd *cobra.Command, args []string) error {
47+
if strings.TrimSpace(backupRunComponent) != "drupal-db-backup" {
48+
return fmt.Errorf("unknown cron component %q", backupRunComponent)
49+
}
50+
if strings.TrimSpace(backupRunOutput) == "" {
51+
return fmt.Errorf("--output is required")
52+
}
53+
54+
ctx, _, containerName, err := getDrupalContainerFromFlags(cmd)
55+
if err != nil {
56+
return err
57+
}
58+
59+
if _, err := ctx.RunCommandContext(cmd.Context(), exec.Command("bash", "-lc", buildDrupalComponentBackupScript(containerName, backupRunOutput))); err != nil {
60+
return err
61+
}
62+
fmt.Fprintf(cmd.OutOrStdout(), "Wrote %s\n", filepath.Base(backupRunOutput))
63+
return nil
64+
},
65+
}
66+
67+
func buildDrupalComponentBackupScript(containerName, outputPath string) string {
68+
return strings.Join([]string{
69+
"set -euo pipefail",
70+
"mkdir -p " + shellQuote(filepath.Dir(outputPath)),
71+
fmt.Sprintf("docker exec %s drush sql-dump -y --skip-tables-list=cache,cache_*,watchdog --structure-tables-list=cache,cache_*,watchdog --debug | gzip -c > %s", shellQuote(containerName), shellQuote(outputPath)),
72+
}, "\n")
73+
}
74+
75+
func shellQuote(value string) string {
76+
return "'" + strings.ReplaceAll(value, "'", `'\''`) + "'"
77+
}
78+
79+
func init() {
80+
cronExtensionRunCmd.Flags().StringVar(&backupRunComponent, "component", "", "Cron component name")
81+
cronExtensionRunCmd.Flags().StringVar(&backupRunOutput, "output", "", "Absolute output path on the target host")
82+
83+
cronExtensionCmd.AddCommand(cronExtensionComponentsCmd)
84+
cronExtensionCmd.AddCommand(cronExtensionRunCmd)
85+
}

cmd/root.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
pluginjobs "github.com/libops/sitectl-drupal/pkg/jobs"
45
"github.com/libops/sitectl/pkg/plugin"
56
)
67

@@ -11,16 +12,17 @@ var (
1112

1213
func init() {
1314
loginCmd.Flags().Uint("uid", 1, "Drupal user ID to provide a direct login link for")
14-
15-
backupCmd.Flags().StringVarP(drupalServiceName, "drupal-service", "d", "drupal", "The name of the drupal service in docker compose")
1615
}
1716

1817
// RegisterCommands registers all drupal commands with the plugin SDK
1918
func RegisterCommands(s *plugin.SDK) {
2019
sdk = s
21-
sdk.AddCommand(backupCmd)
20+
pluginjobs.Register(s)
21+
sdk.AddCommand(sdk.GetMetadataCommand())
22+
sdk.AddCommand(cronExtensionCmd)
2223
sdk.AddCommand(componentExtensionCmd)
2324
sdk.AddCommand(debugExtensionCmd)
2425
sdk.AddCommand(drushCmd)
2526
sdk.AddCommand(loginCmd)
27+
sdk.AddCommand(syncCmd)
2628
}

0 commit comments

Comments
 (0)