Skip to content

Commit

Permalink
atlas templates
Browse files Browse the repository at this point in the history
  • Loading branch information
majodev committed Dec 17, 2024
1 parent e25ddb4 commit adb2fd0
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 74 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ services:
- bash-history:/home/development/commandhistory

environment:
# project root directory, used for relative path resolution (e.g. fixtures)
# project root directory, used for relative path resolution (e.g. templates)
PROJECT_ROOT_DIR: *PROJECT_ROOT_DIR

# Uncomment the next four lines if you will use a ptrace-based debugger like C++, Go, and Rust.
Expand Down
33 changes: 3 additions & 30 deletions internal/lib/mysql.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
package lib

import (
_ "embed"
"fmt"
"log"
"path/filepath"
"text/template"
)

//go:embed templates/mysql_check.sh.tmpl
var mysqlCheckScript string

//go:embed templates/mysql_dump.sh.tmpl
var mysqlDumpScript string

//go:embed templates/mysql_restore.sh.tmpl
var mysqlRestoreScript string

func EnsureMySQLAvailable(namespace string, config MySQLConfig) error {
log.Printf("Checking if MySQL is available in namespace '%s'...", namespace)

tmpl, err := template.New("mysql_check").Parse(mysqlCheckScript)
if err != nil {
return fmt.Errorf("failed to parse MySQL check script template: %w", err)
}

return KubectlExecTemplate(namespace, config.ExecResource, config.ExecContainer, tmpl, config)
return KubectlExecTemplate(namespace, config.ExecResource, config.ExecContainer, GetTemplateAtlas().MySQLCheck, config)
}

func DumpMySQL(namespace string, dryRun bool, config MySQLConfig) error {
Expand All @@ -45,12 +28,7 @@ func DumpMySQL(namespace string, dryRun bool, config MySQLConfig) error {
DumpFileDir: filepath.Dir(config.DumpFile),
}

tmpl, err := template.New("mysql_backup").Parse(mysqlDumpScript)
if err != nil {
return fmt.Errorf("failed to parse MySQL backup script template: %w", err)
}

return KubectlExecTemplate(namespace, config.ExecResource, config.ExecContainer, tmpl, data)
return KubectlExecTemplate(namespace, config.ExecResource, config.ExecContainer, GetTemplateAtlas().MySQLDump, data)
}

func RestoreMySQL(namespace string, dryRun bool, config MySQLConfig) error {
Expand All @@ -60,10 +38,5 @@ func RestoreMySQL(namespace string, dryRun bool, config MySQLConfig) error {
}
log.Printf("Restoring MySQL database '%s' in namespace '%s'...", config.DB, namespace)

tmpl, err := template.New("mysql_backup").Parse(mysqlRestoreScript)
if err != nil {
return fmt.Errorf("failed to parse MySQL restore script template: %w", err)
}

return KubectlExecTemplate(namespace, config.ExecResource, config.ExecContainer, tmpl, config)
return KubectlExecTemplate(namespace, config.ExecResource, config.ExecContainer, GetTemplateAtlas().MySQLRestore, config)
}
33 changes: 3 additions & 30 deletions internal/lib/postgres.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
package lib

import (
_ "embed"
"fmt"
"log"
"path/filepath"
"text/template"
)

//go:embed templates/postgres_check.sh.tmpl
var postgresCheckScript string

//go:embed templates/postgres_dump.sh.tmpl
var postgresDumpScript string

//go:embed templates/postgres_restore.sh.tmpl
var postgresRestoreScript string

func EnsurePostgresAvailable(namespace string, config PostgresConfig) error {
log.Printf("Checking if Postgres is available in namespace '%s'...", namespace)

tmpl, err := template.New("postgres_check").Parse(postgresCheckScript)
if err != nil {
return fmt.Errorf("failed to parse Postgres check script template: %w", err)
}

return KubectlExecTemplate(namespace, config.ExecResource, config.ExecContainer, tmpl, config)
return KubectlExecTemplate(namespace, config.ExecResource, config.ExecContainer, GetTemplateAtlas().PostgresCheck, config)
}

func DumpPostgres(namespace string, dryRun bool, config PostgresConfig) error {
Expand All @@ -45,12 +28,7 @@ func DumpPostgres(namespace string, dryRun bool, config PostgresConfig) error {
DumpFileDir: filepath.Dir(config.DumpFile),
}

tmpl, err := template.New("postgres_backup").Parse(postgresDumpScript)
if err != nil {
return fmt.Errorf("failed to parse Postgres backup script template: %w", err)
}

return KubectlExecTemplate(namespace, config.ExecResource, config.ExecContainer, tmpl, data)
return KubectlExecTemplate(namespace, config.ExecResource, config.ExecContainer, GetTemplateAtlas().PostgresDump, data)
}

func RestorePostgres(namespace string, dryRun bool, config PostgresConfig) error {
Expand All @@ -60,10 +38,5 @@ func RestorePostgres(namespace string, dryRun bool, config PostgresConfig) error
}
log.Printf("Restoring Postgres database '%s' in namespace '%s'...", config.DB, namespace)

tmpl, err := template.New("postgres_restore").Parse(postgresRestoreScript)
if err != nil {
return fmt.Errorf("failed to parse postgres restore script template: %w", err)
}

return KubectlExecTemplate(namespace, config.ExecResource, config.ExecContainer, tmpl, config)
return KubectlExecTemplate(namespace, config.ExecResource, config.ExecContainer, GetTemplateAtlas().PostgresRestore, config)
}
15 changes: 2 additions & 13 deletions internal/lib/script_exec_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package lib_test

import (
_ "embed"
"testing"
"text/template"

"github.com/allaboutapps/backup-ns/internal/lib"
"github.com/stretchr/testify/require"
)

//go:embed templates/test_trap.sh.tmpl
var testTrapScript string

func TestTrap(t *testing.T) {
// Create template data with computed fields
type templateData struct {
Expand All @@ -23,10 +18,7 @@ func TestTrap(t *testing.T) {
Cmd: "sleep 0.1",
}

tmpl, err := template.New("traptest").Parse(testTrapScript)
require.NoError(t, err)

require.NoError(t, lib.KubectlExecTemplate("generic-test", "deployment/writer", "debian", tmpl, data))
require.NoError(t, lib.KubectlExecTemplate("generic-test", "deployment/writer", "debian", lib.GetTemplateAtlas().TestTrap, data))
// succeess testfile exists!
require.NoError(t, lib.KubectlExecCommand("generic-test", "deployment/writer", "debian", "test -f /tmp/trap_test.txt"))
}
Expand All @@ -42,10 +34,7 @@ func TestTrapCommandFail(t *testing.T) {
Cmd: "thiscommanddoesnotexist",
}

tmpl, err := template.New("traptest").Parse(testTrapScript)
require.NoError(t, err)

require.Error(t, lib.KubectlExecTemplate("generic-test", "deployment/writer", "debian", tmpl, data))
require.Error(t, lib.KubectlExecTemplate("generic-test", "deployment/writer", "debian", lib.GetTemplateAtlas().TestTrap, data))
// trap has deleted file
require.Error(t, lib.KubectlExecCommand("generic-test", "deployment/writer", "debian", "test -f /tmp/trap_test.txt"))
}
Expand Down
54 changes: 54 additions & 0 deletions internal/lib/templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package lib

import (
"embed"
"log"
"text/template"
)

//go:embed templates
var templates embed.FS

type TemplateAtlas struct {
MySQLCheck *template.Template
MySQLDump *template.Template
MySQLRestore *template.Template
PostgresCheck *template.Template
PostgresDump *template.Template
PostgresRestore *template.Template
TestTrap *template.Template
}

var templateAtlas TemplateAtlas

func init() {
tmpl, err := template.ParseFS(templates, "templates/*.sh.tmpl")

if err != nil {
log.Fatal("Failed to parse templates/*.sh.tmpl templates.")
}

templateAtlas = TemplateAtlas{
MySQLCheck: ensureChildTemplate(tmpl, "mysql_check.sh.tmpl"),
MySQLDump: ensureChildTemplate(tmpl, "mysql_dump.sh.tmpl"),
MySQLRestore: ensureChildTemplate(tmpl, "mysql_restore.sh.tmpl"),
PostgresCheck: ensureChildTemplate(tmpl, "postgres_check.sh.tmpl"),
PostgresDump: ensureChildTemplate(tmpl, "postgres_dump.sh.tmpl"),
PostgresRestore: ensureChildTemplate(tmpl, "postgres_restore.sh.tmpl"),
TestTrap: ensureChildTemplate(tmpl, "test_trap.sh.tmpl"),
}
}

func ensureChildTemplate(template *template.Template, name string) *template.Template {
tmpl := template.Lookup(name)

if tmpl == nil {
log.Fatalf("Template '%s' not found.", name)
}

return tmpl
}

func GetTemplateAtlas() TemplateAtlas {
return templateAtlas
}
3 changes: 3 additions & 0 deletions internal/lib/templates/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `templates`

These are the scripts templates that the go application runs within the remote resource (the container hosting the db or just having the db utils) during backup and restore.

0 comments on commit adb2fd0

Please sign in to comment.