services/internal/actions/prepare.go
Dan Anglin ee910722cb
feat: add backup support for Code Flow
Add support for taking on demand backups of Code Flow.
Resolves flow/services#7
2023-12-17 08:51:11 +00:00

258 lines
6.2 KiB
Go

package actions
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"text/template"
"time"
"flow/services/internal"
"flow/services/internal/bundle"
"flow/services/internal/config"
"flow/services/internal/services"
"flow/services/internal/services/forgejo"
"flow/services/internal/services/gotosocial"
"flow/services/internal/services/woodpecker"
)
const templateExtension string = ".gotmpl"
func Prepare(environment, service string) error {
cfg, err := config.NewConfig(environment)
if err != nil {
return fmt.Errorf("unable to load the configuration; %w", err)
}
if err := prepareCompose(cfg, environment); err != nil {
return fmt.Errorf("unable to prepare the compose file; %w", err)
}
if service == "all" {
if err := prepareAllServices(cfg, environment); err != nil {
return fmt.Errorf("error preparing the services; %w", err)
}
} else {
if err := prepareService(cfg, environment, service); err != nil {
return fmt.Errorf("error preparing %q; %w", service, err)
}
}
return nil
}
func prepareCompose(cfg config.Config, environment string) error {
compose := "compose"
buildDir := filepath.Join(internal.RootBuildDir, environment, compose)
if _, err := os.Stat(buildDir); err != nil {
if err := os.MkdirAll(buildDir, 0o700); err != nil {
return fmt.Errorf("unable to make %s; %w", buildDir, err)
}
}
if err := render(cfg, environment, compose); err != nil {
return fmt.Errorf("an error occurred whilst rendering the templates; %w", err)
}
return nil
}
func prepareAllServices(cfg config.Config, environment string) error {
allServices := services.All()
for i := range allServices {
if err := prepareService(cfg, environment, allServices[i]); err != nil {
return fmt.Errorf("error preparing %q; %w", allServices[i], err)
}
}
return nil
}
func prepareService(cfg config.Config, environment, service string) error {
buildDir := filepath.Join(internal.RootBuildDir, environment, service)
if _, err := os.Stat(buildDir); err != nil {
if err := os.MkdirAll(buildDir, 0o700); err != nil {
return fmt.Errorf("unable to make %s; %w", buildDir, err)
}
}
if err := downloadBundle(cfg, environment, service); err != nil {
return fmt.Errorf("error downloading service files for %q; %w", service, err)
}
if err := copyAssets(environment, service); err != nil {
return fmt.Errorf("unable to copy the assets for %s; %w", service, err)
}
if err := render(cfg, environment, service); err != nil {
return fmt.Errorf("an error occurred whilst rendering the templates; %w", err)
}
return nil
}
func downloadBundle(cfg config.Config, environment, service string) error {
var b bundle.Bundle
switch service {
case services.Forgejo:
b = forgejo.Bundle(environment, cfg.Forgejo.Version)
case services.Gotosocial:
b = gotosocial.Bundle(environment, cfg.GoToSocial.Version)
case services.Woodpecker:
b = woodpecker.Bundle(environment, cfg.Woodpecker.Version)
default:
fmt.Printf("There's no files to download for %q.\n", service)
return nil
}
if err := Download(b); err != nil {
return fmt.Errorf("error downloading the files for %q; %w", service, err)
}
return nil
}
func render(cfg config.Config, environment, component string) error {
buildDirName := filepath.Join(internal.RootBuildDir, environment, component)
templateDirName := filepath.Join(internal.RootTemplatesDir, component)
_, err := os.Stat(templateDirName)
if err != nil {
if os.IsNotExist(err) {
fmt.Printf("There's no template directory for %q.\n", component)
return nil
}
return err
}
files, err := os.ReadDir(templateDirName)
if err != nil {
return fmt.Errorf("unable to read files from %s; %w ", templateDirName, err)
}
funcMap := template.FuncMap{
"default": defaultValue,
"timestamp": timestamp(),
}
for _, f := range files {
err := func() error {
templateFilename := f.Name()
if f.IsDir() || !strings.HasSuffix(templateFilename, templateExtension) {
return nil
}
outputFilename := strings.TrimSuffix(templateFilename, templateExtension)
outputPath := filepath.Join(buildDirName, outputFilename)
file, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("unable to create the file '%s'; %w", outputPath, err)
}
defer file.Close()
templatePath := filepath.Join(templateDirName, templateFilename)
tmpl, err := template.New(templateFilename).Funcs(funcMap).ParseFiles(templatePath)
if err != nil {
return fmt.Errorf("unable to create a new template value from '%s'; %w", templateFilename, err)
}
if err = tmpl.Execute(file, cfg); err != nil {
return fmt.Errorf("unable to render the template to '%s'; %w", outputPath, err)
}
return nil
}()
if err != nil {
return fmt.Errorf("an error occurred whilst rendering the templates for '%s'; %w", component, err)
}
}
return nil
}
func copyAssets(environment, service string) error {
assetsDirName := filepath.Join(internal.RootAssetsDir, service)
if _, err := os.Stat(assetsDirName); err != nil {
if os.IsNotExist(err) {
fmt.Printf("There's no assets directory for %q.\n", service)
return nil
}
return err
}
buildDirName := filepath.Join(internal.RootBuildDir, environment, service, "assets")
walkDirFunc := func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if path == assetsDirName {
return nil
}
buildAssetPath := filepath.Join(buildDirName, strings.TrimPrefix(path, assetsDirName))
if d.IsDir() {
if err := os.MkdirAll(buildAssetPath, 0o750); err != nil {
return fmt.Errorf("unable to make %s; %w", path, err)
}
return nil
}
source, err := os.Open(path)
if err != nil {
return err
}
defer source.Close()
dest, err := os.Create(buildAssetPath)
if err != nil {
return err
}
defer dest.Close()
if _, err := io.Copy(dest, source); err != nil {
return err
}
return nil
}
err := filepath.WalkDir(assetsDirName, walkDirFunc)
if err != nil {
return fmt.Errorf("error walking the path '%s'; %w", assetsDirName, err)
}
return nil
}
func defaultValue(value, defaultValue string) string {
if value == "" {
return defaultValue
}
return value
}
func timestamp() func() string {
now := time.Now().Format("20060102-1504")
return func() string {
return now
}
}